Skip to content

Commit 6fe1132

Browse files
Backport "chore: Backport changes from Metals" to LTS (#20823)
Backports #19410 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents b2ea4bc + 72a5416 commit 6fe1132

16 files changed

+671
-65
lines changed

compiler/src/dotty/tools/dotc/interactive/Interactive.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,10 @@ object Interactive {
282282
case nested :: encl :: rest =>
283283
val outer = contextOfPath(encl :: rest)
284284
try encl match {
285-
case tree @ PackageDef(pkg, stats) =>
286-
assert(tree.symbol.exists)
285+
case tree @ PackageDef(pkg, stats) if tree.symbol.exists =>
287286
if (nested `eq` pkg) outer
288287
else contextOfStat(stats, nested, pkg.symbol.moduleClass, outer.packageContext(tree, tree.symbol))
289-
case tree: DefDef =>
290-
assert(tree.symbol.exists)
288+
case tree: DefDef if tree.symbol.exists =>
291289
val localCtx = outer.localContext(tree, tree.symbol).setNewScope
292290
for params <- tree.paramss; param <- params do localCtx.enter(param.symbol)
293291
// Note: this overapproximates visibility a bit, since value parameters are only visible

presentation-compiler/src/main/dotty/tools/pc/CompilerSearchVisitor.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class CompilerSearchVisitor(
5858
.filter(denot => denot.exists)
5959
.map(_.symbol)
6060
.filter(isAccessible)
61+
.filter(!_.is(Flags.Given))
6162
}
6263
loop(next, tl)
6364
case Nil => owners

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import dotty.tools.dotc.util.Spans
1515
import dotty.tools.pc.utils.MtagsEnrichments.*
1616

1717
import org.eclipse.lsp4j as l
18+
import scala.annotation.tailrec
1819

1920
enum CompletionKind:
2021
case Empty, Scope, Members
@@ -55,8 +56,10 @@ object CompletionPos:
5556
val prevIsDot =
5657
if start - 1 >= 0 then text.charAt(start - 1) == '.' else false
5758
val kind =
58-
if query.nn.isEmpty() && !prevIsDot then CompletionKind.Empty
59-
else if prevIsDot then CompletionKind.Members
59+
if prevIsDot then CompletionKind.Members
60+
else if isImportOrExportSelect(cursorPos, treePath) then
61+
CompletionKind.Members
62+
else if query.nn.isEmpty then CompletionKind.Empty
6063
else CompletionKind.Scope
6164

6265
CompletionPos(kind, start, end, query.nn, cursorPos, uri)
@@ -84,6 +87,21 @@ object CompletionPos:
8487
(i, tabIndented)
8588
end inferIndent
8689

90+
private def isImportOrExportSelect(
91+
pos: SourcePosition,
92+
path: List[Tree],
93+
)(using Context): Boolean =
94+
@tailrec
95+
def loop(enclosing: List[Tree]): Boolean =
96+
enclosing match
97+
case head :: tl if !head.sourcePos.contains(pos) => loop(tl)
98+
case (tree: (Import | Export)) :: _ =>
99+
tree.selectors.exists(_.imported.sourcePos.contains(pos))
100+
case _ => false
101+
102+
loop(path)
103+
104+
87105
/**
88106
* Returns the start offset of the identifier starting as the given offset position.
89107
*/

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ class Completions(
7272
case _ :: (withcursor @ Select(fun, name)) :: (appl: GenericApply) :: _
7373
if appl.fun == withcursor && name.decoded == Cursor.value =>
7474
false
75-
case (_: Import) :: _ => false
76-
case _ :: (_: Import) :: _ => false
75+
case (_: (Import | Export)) :: _ => false
76+
case _ :: (_: (Import | Export)) :: _ => false
7777
case (_: Ident) :: (_: SeqLiteral) :: _ => false
7878
case _ => true
7979

@@ -140,7 +140,8 @@ class Completions(
140140

141141
val application = CompletionApplication.fromPath(path)
142142
val ordering = completionOrdering(application)
143-
val values = application.postProcess(all.sorted(ordering))
143+
val sorted = all.sorted(ordering)
144+
val values = application.postProcess(sorted)
144145
(values, result)
145146
end completions
146147

@@ -441,6 +442,10 @@ class Completions(
441442
true,
442443
)
443444

445+
case (tree: (Import | Export)) :: _
446+
if tree.selectors.exists(_.renamed.sourcePos.contains(pos)) =>
447+
(List.empty, true)
448+
444449
// From Scala 3.1.3-RC3 (as far as I know), path contains
445450
// `Literal(Constant(null))` on head for an incomplete program, in this case, just ignore the head.
446451
case Literal(Constant(null)) :: tl =>
@@ -791,7 +796,8 @@ class Completions(
791796
val fuzzyCache = mutable.Map.empty[CompletionValue, Int]
792797

793798
def compareLocalSymbols(s1: Symbol, s2: Symbol): Int =
794-
if s1.isLocal && s2.isLocal then
799+
if s1.isLocal && s2.isLocal && s1.sourcePos.exists && s2.sourcePos.exists
800+
then
795801
val firstIsAfter = s1.srcPos.isAfter(s2.srcPos)
796802
if firstIsAfter then -1 else 1
797803
else 0
@@ -833,6 +839,16 @@ class Completions(
833839
priority(o1) - priority(o2)
834840
end compareInApplyParams
835841

842+
def prioritizeKeywords(o1: CompletionValue, o2: CompletionValue): Int =
843+
def priority(v: CompletionValue): Int =
844+
v match
845+
case _: CompletionValue.CaseKeyword => 0
846+
case _: CompletionValue.NamedArg => 1
847+
case _: CompletionValue.Keyword => 2
848+
case _ => 3
849+
850+
priority(o1) - priority(o2)
851+
end prioritizeKeywords
836852
/**
837853
* Some completion values should be shown first such as CaseKeyword and
838854
* NamedArg
@@ -909,7 +925,10 @@ class Completions(
909925
case _ =>
910926
val byApplyParams = compareInApplyParams(o1, o2)
911927
if byApplyParams != 0 then byApplyParams
912-
else compareByRelevance(o1, o2)
928+
else
929+
val keywords = prioritizeKeywords(o1, o2)
930+
if keywords != 0 then keywords
931+
else compareByRelevance(o1, o2)
913932
end compare
914933

915934
end Completions

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,13 @@ object InterpolatorCompletions:
8383
case _: Select => true
8484
case _ => false
8585
} =>
86-
val allLiterals = parent match
87-
case SeqLiteral(elems, _) =>
88-
elems
89-
case _ => Nil
90-
expr.elems.zip(allLiterals.tail).collectFirst {
91-
case (i: (Ident | Select), literal) if literal == lit =>
92-
i
93-
}
86+
parent match
87+
case SeqLiteral(elems, _) if elems.size > 0 =>
88+
expr.elems.zip(elems.tail).collectFirst {
89+
case (i: (Ident | Select), literal) if literal == lit =>
90+
i
91+
}
92+
case _ => None
9493
end interpolatorMemberArg
9594

9695
/**

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import dotty.tools.dotc.core.Names.Name
1717
import dotty.tools.dotc.core.StdNames.*
1818
import dotty.tools.dotc.core.SymDenotations.NoDenotation
1919
import dotty.tools.dotc.core.Symbols
20+
import dotty.tools.dotc.core.Symbols.defn
2021
import dotty.tools.dotc.core.Symbols.NoSymbol
2122
import dotty.tools.dotc.core.Symbols.Symbol
2223
import dotty.tools.dotc.core.Types.AndType
@@ -309,15 +310,17 @@ object NamedArgCompletions:
309310

310311
val completionSymbols = indexedContext.scopeSymbols
311312
def matchingTypesInScope(paramType: Type): List[String] =
312-
completionSymbols
313-
.collect {
314-
case sym
315-
if sym.info <:< paramType && sym.isTerm && !sym.info.isErroneous && !sym.info.isNullType && !sym.info.isNothingType && !sym
316-
.is(Flags.Method) && !sym.is(Flags.Synthetic) =>
317-
sym.decodedName
318-
}
319-
.filter(name => name != "Nil" && name != "None")
320-
.sorted
313+
if paramType != defn.AnyType then
314+
completionSymbols
315+
.collect {
316+
case sym
317+
if sym.info <:< paramType && sym.isTerm && !sym.info.isErroneous && !sym.info.isNullType && !sym.info.isNothingType && !sym
318+
.is(Flags.Method) && !sym.is(Flags.Synthetic) =>
319+
sym.decodedName
320+
}
321+
.filter(name => name != "Nil" && name != "None")
322+
.sorted
323+
else Nil
321324

322325
def findDefaultValue(param: ParamSymbol): String =
323326
val matchingType = matchingTypesInScope(param.info)

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionArgSuite.scala

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ class CompletionArgSuite extends BaseCompletionSuite:
10471047
| bbb = 123,
10481048
| aa@@
10491049
| )
1050-
|}
1050+
|}
10511051
|""".stripMargin,
10521052
"""|aaa = : Int
10531053
|""".stripMargin,
@@ -1063,7 +1063,7 @@ class CompletionArgSuite extends BaseCompletionSuite:
10631063
| ccc = 123,
10641064
| aa@@
10651065
| )
1066-
|}
1066+
|}
10671067
|""".stripMargin,
10681068
"""|aaa = : Int
10691069
|""".stripMargin,
@@ -1079,7 +1079,7 @@ class CompletionArgSuite extends BaseCompletionSuite:
10791079
| ccc = 123,
10801080
| aa@@
10811081
| )
1082-
|}
1082+
|}
10831083
|""".stripMargin,
10841084
"""|aaa = : Int
10851085
|""".stripMargin,
@@ -1105,4 +1105,18 @@ class CompletionArgSuite extends BaseCompletionSuite:
11051105
"""|str = : String
11061106
| """.stripMargin,
11071107
topLines = Some(1),
1108+
)
1109+
1110+
@Test def `comparison` =
1111+
check(
1112+
"""package a
1113+
|object w {
1114+
| abstract class T(x: Int) {
1115+
| def met(x: Int): Unit = {
1116+
| println(x@@)
1117+
| }
1118+
| }}
1119+
|""".stripMargin,
1120+
"""x: Int
1121+
|x = : Any""".stripMargin,
11081122
)

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionKeywordSuite.scala

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class CompletionKeywordSuite extends BaseCompletionSuite:
3838
| // tr@@
3939
|}
4040
|""".stripMargin,
41-
"",
41+
"""|transient scala (commit: '')
42+
|transparentTrait - scala.annotation (commit: '')""".stripMargin,
4243
includeCommitCharacter = true
4344
)
4445

@@ -57,7 +58,8 @@ class CompletionKeywordSuite extends BaseCompletionSuite:
5758
| **/
5859
|}
5960
|""".stripMargin,
60-
"",
61+
"""|transient scala (commit: '')
62+
|transparentTrait - scala.annotation (commit: '')""".stripMargin,
6163
includeCommitCharacter = true
6264
)
6365

@@ -151,6 +153,8 @@ class CompletionKeywordSuite extends BaseCompletionSuite:
151153
"""|value: Int
152154
|val
153155
|var
156+
|varargs(): varargs
157+
|varargs - scala.annotation
154158
|""".stripMargin
155159
)
156160

@@ -167,6 +171,8 @@ class CompletionKeywordSuite extends BaseCompletionSuite:
167171
|""".stripMargin,
168172
"""|val
169173
|var
174+
|varargs(): varargs
175+
|varargs - scala.annotation
170176
|""".stripMargin
171177
)
172178

@@ -181,9 +187,10 @@ class CompletionKeywordSuite extends BaseCompletionSuite:
181187
| }
182188
|}
183189
|""".stripMargin,
184-
"""|given (commit: '')
185-
|""".stripMargin,
186-
includeCommitCharacter = true
190+
"""given (commit: '')
191+
|""".stripMargin,
192+
includeCommitCharacter = true,
193+
topLines = Some(5)
187194
)
188195

189196
@Test def `val-arg` =
@@ -198,8 +205,8 @@ class CompletionKeywordSuite extends BaseCompletionSuite:
198205
|}
199206
|""".stripMargin,
200207
"""|value: Int
201-
|""".stripMargin,
202-
topLines = Some(1)
208+
|varargs(): varargs
209+
|varargs - scala.annotation""".stripMargin
203210
)
204211

205212
@Test def `val-trailing-space` =
@@ -406,7 +413,13 @@ class CompletionKeywordSuite extends BaseCompletionSuite:
406413
| protected de@@
407414
|}
408415
""".stripMargin,
409-
"def"
416+
"""|def
417+
|deprecated scala
418+
|deprecatedInheritance scala
419+
|deprecatedName scala
420+
|deprecatedOverriding scala
421+
|""".stripMargin,
422+
topLines = Some(5)
410423
)
411424

412425
@Test def `protected-val` =
@@ -418,9 +431,10 @@ class CompletionKeywordSuite extends BaseCompletionSuite:
418431
| protected va@@
419432
|}
420433
""".stripMargin,
421-
"""val
422-
|var
423-
|""".stripMargin
434+
"""|val
435+
|var
436+
|varargs - scala.annotation
437+
|""".stripMargin
424438
)
425439

426440
@Test def `topLevel` =
@@ -458,16 +472,26 @@ class CompletionKeywordSuite extends BaseCompletionSuite:
458472
| def hello(u@@)
459473
|}""".stripMargin,
460474
"""|using (commit: '')
475+
|unsafeExceptions scala (commit: '')
476+
|unchecked scala (commit: '')
477+
|unsafe - scala.caps (commit: '')
478+
|unsafeNulls - scala.runtime.stdLibPatches.language (commit: '')
461479
|""".stripMargin,
462-
includeCommitCharacter = true
480+
includeCommitCharacter = true,
481+
topLines = Some(5)
463482
)
464483

465484
@Test def `not-using` =
466485
check(
467486
"""|object A{
468487
| def hello(a: String, u@@)
469488
|}""".stripMargin,
470-
""
489+
"""|unsafeExceptions scala
490+
|unchecked scala
491+
|unsafe - scala.caps
492+
|unsafeNulls - scala.runtime.stdLibPatches.language
493+
|unused - scala.annotation """.stripMargin,
494+
topLines = Some(5)
471495
)
472496

473497
@Test def `extends-class` =

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionPatternSuite.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ class CompletionPatternSuite extends BaseCompletionSuite:
5454
| case ma@@
5555
| }
5656
|}""".stripMargin,
57-
""
57+
"""|main scala
58+
|macros - scala.languageFeature.experimental
59+
|macroImpl - scala.reflect.macros.internal
60+
|""".stripMargin
5861
)
5962

6063
@Test def `bind2` =

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSnippetSuite.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ class CompletionSnippetSuite extends BaseCompletionSuite:
323323
|Widget($0) - (age: Int): Widget
324324
|Widget($0) - (name: String, age: Int): Widget
325325
|""".stripMargin,
326-
includeDetail = true
326+
includeDetail = true,
327+
topLines = Some(4)
327328
)
328329

329330
@Test def `no-apply` =
@@ -335,8 +336,13 @@ class CompletionSnippetSuite extends BaseCompletionSuite:
335336
| Wi@@
336337
|}
337338
|""".stripMargin,
338-
"Widget - example",
339-
includeDetail = true
339+
"""|Widget - example
340+
|Window - java.awt
341+
|WindowPeer - java.awt.peer
342+
|WithFilter - scala.collection
343+
|""".stripMargin,
344+
includeDetail = true,
345+
topLines = Some(4)
340346
)
341347

342348
// https://github.com/scalameta/metals/issues/4004

0 commit comments

Comments
 (0)