Skip to content

Commit f14c9dd

Browse files
rochalaWojciechMazur
authored andcommitted
Remove redundant path calculation from Completions in PC (#18889)
[Cherry-picked 681f182]
1 parent a406088 commit f14c9dd

File tree

4 files changed

+11
-16
lines changed

4 files changed

+11
-16
lines changed

presentation-compiler/src/main/dotty/tools/pc/completions/CompletionProvider.scala

+5-9
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,15 @@ class CompletionProvider(
5252
val pos = driver.sourcePosition(params)
5353
val (items, isIncomplete) = driver.compilationUnits.get(uri) match
5454
case Some(unit) =>
55-
val path =
56-
Interactive.pathTo(driver.openedTrees(uri), pos)(using ctx)
5755

5856
val newctx = ctx.fresh.setCompilationUnit(unit)
59-
val tpdPath =
60-
Interactive.pathTo(newctx.compilationUnit.tpdTree, pos.span)(
61-
using newctx
62-
)
57+
val tpdPath = Interactive.pathTo(newctx.compilationUnit.tpdTree, pos.span)(using newctx)
58+
6359
val locatedCtx =
6460
Interactive.contextOfPath(tpdPath)(using newctx)
6561
val indexedCtx = IndexedContext(locatedCtx)
6662
val completionPos =
67-
CompletionPos.infer(pos, params, path)(using newctx)
63+
CompletionPos.infer(pos, params, tpdPath)(using newctx)
6864
val autoImportsGen = AutoImports.generator(
6965
completionPos.sourcePos,
7066
text,
@@ -82,7 +78,7 @@ class CompletionProvider(
8278
buildTargetIdentifier,
8379
completionPos,
8480
indexedCtx,
85-
path,
81+
tpdPath,
8682
config,
8783
folderPath,
8884
autoImportsGen,
@@ -96,7 +92,7 @@ class CompletionProvider(
9692
idx,
9793
autoImportsGen,
9894
completionPos,
99-
path,
95+
tpdPath,
10096
indexedCtx
10197
)(using newctx)
10298
}

presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class Completions(
117117
val allAdvanced = advanced ++ keywords
118118
path match
119119
// should not show completions for toplevel
120-
case Nil if pos.source.file.extension != "sc" =>
120+
case Nil | (_: PackageDef) :: _ if pos.source.file.extension != "sc" =>
121121
(allAdvanced, SymbolSearch.Result.COMPLETE)
122122
case Select(qual, _) :: _ if qual.typeOpt.isErroneous =>
123123
(allAdvanced, SymbolSearch.Result.COMPLETE)

presentation-compiler/src/main/dotty/tools/pc/completions/KeywordsCompletions.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ object KeywordsCompletions:
2424
checkIfNotInComment(completionPos.cursorPos, comments)
2525

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

7777
private def isPackage(enclosing: List[Tree]): Boolean =
7878
enclosing match
79-
case Nil => true
79+
case Nil | (_: PackageDef) :: _ => true
8080
case _ => false
8181

8282
private def isParam(enclosing: List[Tree]): Boolean =

presentation-compiler/src/main/dotty/tools/pc/completions/ScalaCliCompletions.scala

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ class ScalaCliCompletions(
1616
pos.lineContent.take(pos.column).stripPrefix("/*<script>*/")
1717
)
1818
path match
19-
case Nil => scalaCliDep
19+
case Nil | (_: PackageDef) :: _ => scalaCliDep
2020
// generated script file will end with .sc.scala
21-
case (_: TypeDef) :: Nil if pos.source.file.path.endsWith(".sc.scala") =>
21+
case (_: TypeDef) :: (_: PackageDef) :: Nil if pos.source.file.path.endsWith(".sc.scala") =>
2222
scalaCliDep
23-
case (_: Template) :: (_: TypeDef) :: Nil
24-
if pos.source.file.path.endsWith(".sc.scala") =>
23+
case (_: Template) :: (_: TypeDef) :: Nil if pos.source.file.path.endsWith(".sc.scala") =>
2524
scalaCliDep
2625
case head :: next => None
2726

0 commit comments

Comments
 (0)