Skip to content

Commit 823782c

Browse files
authored
Completions for requests just before string (#22894)
fixes #22514
1 parent c044420 commit 823782c

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ object Completion:
9797
else if sel.isGiven && sel.bound.span.contains(pos.span) then Mode.ImportOrExport
9898
else Mode.None // import scala.{util => u@@}
9999
case GenericImportOrExport(_) => Mode.ImportOrExport | Mode.Scope // import TrieMa@@
100+
case untpd.InterpolatedString(_, untpd.Literal(Constants.Constant(_: String)) :: _) :: _ =>
101+
Mode.Term | Mode.Scope
100102
case untpd.Literal(Constants.Constant(_: String)) :: _ => Mode.Term | Mode.Scope // literal completions
101103
case (ref: untpd.RefTree) :: _ =>
102104
val maybeSelectMembers = if ref.isInstanceOf[untpd.Select] then Mode.Member else Mode.Scope
@@ -171,6 +173,14 @@ object Completion:
171173
case (importOrExport: untpd.ImportOrExport) :: _ => Some(importOrExport)
172174
case _ => None
173175

176+
private object StringContextApplication:
177+
def unapply(path: List[tpd.Tree]): Option[tpd.Apply] =
178+
path match
179+
case tpd.Select(qual @ tpd.Apply(tpd.Select(tpd.Select(_, StdNames.nme.StringContext), _), _), _) :: _ =>
180+
Some(qual)
181+
case _ => None
182+
183+
174184
/** Inspect `path` to determine the offset where the completion result should be inserted. */
175185
def completionOffset(untpdPath: List[untpd.Tree]): Int =
176186
untpdPath match
@@ -221,7 +231,10 @@ object Completion:
221231
// Ignore synthetic select from `This` because in code it was `Ident`
222232
// See example in dotty.tools.languageserver.CompletionTest.syntheticThis
223233
case tpd.Select(qual @ tpd.This(_), _) :: _ if qual.span.isSynthetic => completer.scopeCompletions
224-
case tpd.Select(qual, _) :: _ if qual.typeOpt.hasSimpleKind => completer.selectionCompletions(qual)
234+
case StringContextApplication(qual) =>
235+
completer.scopeCompletions ++ completer.selectionCompletions(qual)
236+
case tpd.Select(qual, _) :: _ if qual.typeOpt.hasSimpleKind =>
237+
completer.selectionCompletions(qual)
225238
case tpd.Select(qual, _) :: _ => Map.empty
226239
case (tree: tpd.ImportOrExport) :: _ => completer.directMemberCompletions(tree.expr)
227240
case _ => completer.scopeCompletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package dotty.tools.pc.tests.completion
2+
3+
import dotty.tools.pc.base.BaseCompletionSuite
4+
import org.junit.Test
5+
import dotty.tools.dotc.core.Types.ThisType.raw
6+
7+
class CompletionStringContextSuite extends BaseCompletionSuite:
8+
@Test
9+
def inScopeSymbol = check(
10+
"""
11+
|object M:
12+
| val VersionRegex = "".r
13+
| VersionRe@@"1234"
14+
""".stripMargin,
15+
"|VersionRegex: Regex".stripMargin
16+
)
17+
18+
@Test
19+
def workspaceSymbol = check(
20+
"""
21+
|object M:
22+
| ListBuf@@"1234"
23+
""".stripMargin,
24+
"""
25+
|ListBuffer[A](elems: A*): ListBuffer[A] - scala.collection.mutable
26+
|new ListBuffer[A]: ListBuffer[A] - scala.collection.mutable
27+
|ListBuffer - scala.collection.mutable
28+
|""".stripMargin
29+
)
30+
31+
@Test
32+
def providedSymbol = check(
33+
"""
34+
|object M:
35+
| ra@@"1234"
36+
""".stripMargin,
37+
"|raw(args: Any*): String".stripMargin
38+
)
39+
40+
// bellow are tests of edits
41+
@Test
42+
def editTest1 = checkEdit(
43+
"""
44+
|object M:
45+
| ra@@"1234"
46+
""".stripMargin,
47+
"""
48+
|object M:
49+
| raw"1234"
50+
|""".stripMargin
51+
)
52+
53+
@Test
54+
def editTest2 = checkEdit(
55+
"""
56+
|object M:
57+
| printl@@"1234"
58+
""".stripMargin,
59+
"""
60+
|object M:
61+
| println"1234"
62+
|""".stripMargin,
63+
assertSingleItem = false
64+
)
65+
66+
@Test
67+
def editTest3 = checkEdit(
68+
"""
69+
|object M:
70+
| def select(s: String): String = s
71+
| selec@@"1234"
72+
""".stripMargin,
73+
"""
74+
|object M:
75+
| def select(s: String): String = s
76+
| select"1234"
77+
|""".stripMargin,
78+
assertSingleItem = false
79+
)

0 commit comments

Comments
 (0)