Skip to content

Commit

Permalink
Remove some replaced code, start using selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Nov 11, 2024
1 parent e5946c3 commit d790595
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 151 deletions.
136 changes: 0 additions & 136 deletions modules/core/src/main/scala/playground/smithyql/RangeIndex.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,47 +85,6 @@ object RangeIndex {
preludeRanges ++ queryRanges
}

def build(
sf: SourceFile[WithSource]
): RangeIndex = fromRanges {
val path = NodeContext.EmptyPath

val preludeRanges: List[ContextRange] = sf
.prelude
.useClauses
.toNel
.foldMap { useClauses =>
val newBase = path.inPrelude

ContextRange(useClauses.map(_.range).reduceLeft(_.fakeUnion(_)), newBase) ::
sf.prelude
.useClauses
.mapWithIndex {
(
uc,
i,
) =>
findInUseClause(uc, newBase.inUseClause(i))
}
.combineAll
}

val queryRanges = sf.queries(WithSource.unwrap).zipWithIndex.flatMap { case (rq, index) =>
findInQuery(rq.query, path.inQuery(index))
}

preludeRanges ++ queryRanges

// Console
// .err
// .println(
// s"""Found ${allRanges.size} ranges for query ${q.operationName.value.text}:
// |${allRanges
// .map(_.render)
// .mkString("\n")}""".stripMargin
// )
}

def fromRanges(allRanges: List[ContextRange]): RangeIndex =
pos =>
allRanges
Expand All @@ -141,101 +100,6 @@ object RangeIndex {
.maxByOption(_.ctx.length)
.map(_.ctx)

private def findInQuery(
q: WithSource[Query[WithSource]],
path: NodeContext,
) = {
val qv = q.value

List(ContextRange(q.range, path)) ++
findInOperationName(qv.operationName, path.inOperationName) ++
findInNode(qv.input, path.inOperationInput)
}

private def findInUseClause(
useClause: WithSource[UseClause[WithSource]],
path: NodeContext,
): List[ContextRange] = ContextRange(useClause.value.identifier.range, path) :: Nil

private def findInOperationName(
operationName: WithSource[QueryOperationName[WithSource]],
path: NodeContext,
): List[ContextRange] =
ContextRange(
operationName.value.operationName.range,
path,
) :: Nil

private def findInNode(
node: WithSource[InputNode[WithSource]],
ctx: NodeContext,
): List[ContextRange] = {
def entireNode(
ctx: NodeContext
) = ContextRange(node.range, ctx)

val default = Function.const(
// Default case: can be triggered e.g. inside a string literal
// which would affect completions of enum values and timestamps.
entireNode(ctx) :: Nil
)

node
.value
.fold(
listed = l => entireNode(ctx) :: findInList(l, ctx),
struct = s => entireNode(ctx) :: findInStruct(s, ctx.inStructBody),
string = { _ =>
val inQuotes = ContextRange(
node.range.shrink1,
ctx.inQuotes,
)

inQuotes :: entireNode(ctx) :: Nil
},
int = default,
bool = default,
nul = default,
)

}

private def findInList(
list: Listed[WithSource],
ctx: NodeContext,
): List[ContextRange] = {
val inItems = list
.values
.value
.zipWithIndex
.flatMap { case (entry, index) => findInNode(entry, ctx.inCollectionEntry(index.some)) }

val inBody = ContextRange(
list
.values
.range,
ctx.inCollectionEntry(None),
)

inBody :: inItems
}

private def findInStruct(
struct: Struct[WithSource],
ctx: NodeContext,
): List[ContextRange] = {
// Struct fields that allow nesting in them
val inFields = struct
.fields
.value
.value
.flatMap { binding =>
findInNode(binding.value, ctx.inStructValue(binding.identifier.value.text))
}

ContextRange(struct.fields.range, ctx) :: inFields
}

}

case class ContextRange(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ object CompletionProvider {
// there's definitely a test missing for N>1 clauses.
// https://github.com/kubukoz/smithy-playground/issues/161
val presentServiceIds: List[QualifiedIdentifier] = file
.prelude
.toList
.flatMap(_.use_clause)
.flatMap(_.identifier)
.select(_.prelude.use_clause.identifier)
.flatMap(ASTAdapter.decodeQI)

// for operations on root level we show:
Expand Down Expand Up @@ -122,13 +119,10 @@ object CompletionProvider {
case Some(serviceId) =>
// includes the current query's service reference
// as it wouldn't result in ading a use clause
val presentServiceIdentifiers =
q.operation_name.toList.flatMap(_.identifier).flatMap(ASTAdapter.decodeQI) ++
sf.prelude
.toList
.flatMap(_.use_clause)
.flatMap(_.identifier)
.flatMap(ASTAdapter.decodeQI)
val presentServiceIdentifiers = {
q.select(_.operation_name.identifier) ++
sf.select(_.prelude.use_clause.identifier)
}.flatMap(ASTAdapter.decodeQI)

completeOperationName(
serviceId,
Expand All @@ -149,7 +143,7 @@ object CompletionProvider {
.resolveServiceTs(
operationName,
serviceIndex,
sf.prelude.toList.flatMap(_.use_clause),
sf.select(_.prelude.use_clause),
)
.toOption

Expand All @@ -160,9 +154,7 @@ object CompletionProvider {
case NodeContext.PathEntry.AtOperationInput ^^: ctx =>
resolvedServiceId match {
case Some(serviceId) =>
q.operation_name
.toList
.flatMap(_.name)
q.select(_.operation_name.name)
.map(id => OperationName[Id](id.source))
.flatMap {
inputCompletions(serviceId)(_)
Expand Down

0 comments on commit d790595

Please sign in to comment.