Skip to content

Commit 705d30b

Browse files
committed
Try to extract name when backtick is missing
Previously, completion prefix would show up as an empty string if we had an unclosed backtick. Now, we try to extract the prefix from the file contents in that case. An alternative approach would be to tranfer somehow the name string in `nme.Error`, but I didn't see anything like that done before, so not sure how to properly address that. Fixes #12514
1 parent ffd9471 commit 705d30b

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,14 @@ object Completion {
9292
}.getOrElse("")
9393

9494
case (ref: untpd.RefTree) :: _ =>
95-
if (ref.name == nme.ERROR) ""
95+
if (ref.name == nme.ERROR) {
96+
val content = ref.source.content()
97+
// if the error resulted from unclosed back tick
98+
if content(ref.span.start) == '`' then
99+
content.slice(ref.span.start + 1, ref.span.end).mkString
100+
else
101+
""
102+
}
96103
else ref.name.toString.take(pos.span.point - ref.span.point)
97104

98105
case _ =>

compiler/test/dotty/tools/repl/TabcompleteTests.scala

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,50 @@ class TabcompleteTests extends ReplTest {
138138
tabComplete("import quoted.* ; def fooImpl(using Quotes): Expr[Int] = { import quotes.reflect.* ; TypeRepr.of[Int].s"))
139139
}
140140

141+
@Test def wrongAnyMember: Unit = fromInitialState { implicit s =>
142+
assertEquals(List("scalaUtilChainingOps"), tabComplete("import scala.util.chaining.`sc"))
143+
}
144+
145+
@Test def importBackticked: Unit = {
146+
fromInitialState { implicit state =>
147+
val src = """|object O{
148+
| val `extends` = ""
149+
|}
150+
|""".stripMargin
151+
run(src)
152+
}
153+
.andThen { implicit state =>
154+
val expected = List("extends")
155+
assertEquals(expected, tabComplete("import O.`extends`"))
156+
}
157+
}
158+
159+
@Test def importBacktickedUnclosed: Unit = {
160+
fromInitialState { implicit state =>
161+
val src = """|object O{
162+
| val `extends` = ""
163+
|}
164+
|""".stripMargin
165+
run(src)
166+
}
167+
.andThen { implicit state =>
168+
val expected = List("extends")
169+
assertEquals(expected, tabComplete("import O.`extends"))
170+
}
171+
}
172+
173+
174+
@Test def importBacktickedUnclosedSpace: Unit = {
175+
fromInitialState { implicit state =>
176+
val src = """|object O{
177+
| val `extends ` = ""
178+
|}
179+
|""".stripMargin
180+
run(src)
181+
}
182+
.andThen { implicit state =>
183+
val expected = List("extends ")
184+
assertEquals(expected, tabComplete("import O.`extends"))
185+
}
186+
}
141187
}

language-server/test/dotty/tools/languageserver/CompletionTest.scala

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,4 +887,38 @@ class CompletionTest {
887887
)
888888
)
889889
}
890+
891+
@Test def wrongAnyMember: Unit = {
892+
code"""|import scala.util.chaining.`sc${m1}
893+
|""".withSource
894+
.completion(m1, Set(("scalaUtilChainingOps",Method,"[A](a: A): scala.util.ChainingOps[A]")))
895+
}
896+
897+
@Test def importBackticked: Unit = {
898+
code"""|object O{
899+
| val `extends` = ""
900+
|}
901+
|import O.`extends`${m1}
902+
|""".withSource
903+
.completion(m1, Set(("extends",Field,"String")))
904+
}
905+
906+
@Test def importBacktickedUnclosed: Unit = {
907+
code"""|object O{
908+
| val `extends` = ""
909+
|}
910+
|import O.`extends${m1}
911+
|""".withSource
912+
.completion(m1, Set(("extends",Field,"String")))
913+
}
914+
915+
916+
@Test def importBacktickedUnclosedSpace: Unit = {
917+
code"""|object O{
918+
| val `extends ` = ""
919+
|}
920+
|import O.`extends ${m1}
921+
|""".withSource
922+
.completion(m1, Set(("extends ",Field,"String")))
923+
}
890924
}

0 commit comments

Comments
 (0)