Skip to content

Commit 315f7cf

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 315f7cf

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-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 _ =>

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)