Skip to content

Commit

Permalink
Remove redundant path calculation from Completions in PC (#18889)
Browse files Browse the repository at this point in the history
[Cherry-picked 681f182]
  • Loading branch information
rochala authored and WojciechMazur committed Jun 28, 2024
1 parent affe132 commit 26006ce
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,15 @@ class CompletionProvider(
val pos = driver.sourcePosition(params)
val (items, isIncomplete) = driver.compilationUnits.get(uri) match
case Some(unit) =>
val path =
Interactive.pathTo(driver.openedTrees(uri), pos)(using ctx)

val newctx = ctx.fresh.setCompilationUnit(unit)
val tpdPath =
Interactive.pathTo(newctx.compilationUnit.tpdTree, pos.span)(
using newctx
)
val tpdPath = Interactive.pathTo(newctx.compilationUnit.tpdTree, pos.span)(using newctx)

val locatedCtx =
Interactive.contextOfPath(tpdPath)(using newctx)
val indexedCtx = IndexedContext(locatedCtx)
val completionPos =
CompletionPos.infer(pos, params, path)(using newctx)
CompletionPos.infer(pos, params, tpdPath)(using newctx)
val autoImportsGen = AutoImports.generator(
completionPos.sourcePos,
text,
Expand All @@ -82,7 +78,7 @@ class CompletionProvider(
buildTargetIdentifier,
completionPos,
indexedCtx,
path,
tpdPath,
config,
folderPath,
autoImportsGen,
Expand All @@ -96,7 +92,7 @@ class CompletionProvider(
idx,
autoImportsGen,
completionPos,
path,
tpdPath,
indexedCtx
)(using newctx)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Completions(
val allAdvanced = advanced ++ keywords
path match
// should not show completions for toplevel
case Nil if pos.source.file.extension != "sc" =>
case Nil | (_: PackageDef) :: _ if pos.source.file.extension != "sc" =>
(allAdvanced, SymbolSearch.Result.COMPLETE)
case Select(qual, _) :: _ if qual.typeOpt.isErroneous =>
(allAdvanced, SymbolSearch.Result.COMPLETE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object KeywordsCompletions:
checkIfNotInComment(completionPos.cursorPos, comments)

path match
case Nil if completionPos.query.isEmpty() =>
case Nil | (_: PackageDef) :: _ if completionPos.query.isEmpty() =>
Keyword.all.collect {
// topelevel definitions are allowed in Scala 3
case kw if (kw.isPackage || kw.isTemplate) && notInComment =>
Expand Down Expand Up @@ -76,7 +76,7 @@ object KeywordsCompletions:

private def isPackage(enclosing: List[Tree]): Boolean =
enclosing match
case Nil => true
case Nil | (_: PackageDef) :: _ => true
case _ => false

private def isParam(enclosing: List[Tree]): Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ class ScalaCliCompletions(
pos.lineContent.take(pos.column).stripPrefix("/*<script>*/")
)
path match
case Nil => scalaCliDep
case Nil | (_: PackageDef) :: _ => scalaCliDep
// generated script file will end with .sc.scala
case (_: TypeDef) :: Nil if pos.source.file.path.endsWith(".sc.scala") =>
case (_: TypeDef) :: (_: PackageDef) :: Nil if pos.source.file.path.endsWith(".sc.scala") =>
scalaCliDep
case (_: Template) :: (_: TypeDef) :: Nil
if pos.source.file.path.endsWith(".sc.scala") =>
case (_: Template) :: (_: TypeDef) :: Nil if pos.source.file.path.endsWith(".sc.scala") =>
scalaCliDep
case head :: next => None

Expand Down

0 comments on commit 26006ce

Please sign in to comment.