Skip to content

fix(completions): support backticked imports in completions #14649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions compiler/src/dotty/tools/dotc/interactive/Completion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ object Completion {
Mode.None
}

/** When dealing with <errors> in varios palces we check to see if they are
* due to incomplete backticks. If so, we ensure we get the full prefix
* including the backtick.
*
* @param content The source content that we'll check the positions for the prefix
* @param start The start position we'll start to look for the prefix at
* @param end The end position we'll look for the prefix at
* @return Either the full prefix including the ` or an empty string
*/
private def checkBacktickPrefix(content: Array[Char], start: Int, end: Int): String =
content.lift(start) match
case Some(char) if char == '`' =>
content.slice(start, end).mkString
case _ =>
""

/**
* Inspect `path` to determine the completion prefix. Only symbols whose name start with the
* returned prefix should be considered.
Expand All @@ -92,15 +108,14 @@ object Completion {
completionPrefix(selector :: Nil, pos)
}.getOrElse("")

// We special case Select here because we want to determine if the name
// is an error due to an unclosed backtick.
case (select: untpd.Select) :: _ if (select.name == nme.ERROR) =>
val content = select.source.content()
content.lift(select.nameSpan.start) match
case Some(char) if char == '`' =>
content.slice(select.nameSpan.start, select.span.end).mkString
case _ =>
""
// Foo.`se<TAB> will result in Select(Ident(Foo), <error>)
case (select: untpd.Select) :: _ if select.name == nme.ERROR =>
checkBacktickPrefix(select.source.content(), select.nameSpan.start, select.span.end)

// import scala.util.chaining.`s<TAB> will result in a Ident(<error>)
case (ident: untpd.Ident) :: _ if ident.name == nme.ERROR =>
checkBacktickPrefix(ident.source.content(), ident.span.start, ident.span.end)

case (ref: untpd.RefTree) :: _ =>
if (ref.name == nme.ERROR) ""
else ref.name.toString.take(pos.span.point - ref.span.point)
Expand Down
9 changes: 9 additions & 0 deletions compiler/test/dotty/tools/repl/TabcompleteTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ class TabcompleteTests extends ReplTest {
|Foo.`bac"""stripMargin))
}

@Test def backtickedImport = initially {
assertEquals(
List(
"`scalaUtilChainingOps`",
"`synchronized`"
),
tabComplete("import scala.util.chaining.`s"))
}

@Test def commands = initially {
assertEquals(
List(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1121,4 +1121,13 @@ class CompletionTest {
| val x = Bar.`fo${m1}"""
.withSource.completion(m1, expected)
}

@Test def backticksImported: Unit = {
val expected = Set(
("`scalaUtilChainingOps`", Method, "[A](a: A): scala.util.ChainingOps[A]"),
("`synchronized`", Method, "[X0](x$0: X0): X0")
)
code"""import scala.util.chaining.`s${m1}"""
.withSource.completion(m1, expected)
}
}