Skip to content

Add support for completions on export #15795

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
Aug 2, 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
20 changes: 8 additions & 12 deletions compiler/src/dotty/tools/dotc/interactive/Completion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,18 @@ object Completion {
*/
def completionMode(path: List[Tree], pos: SourcePosition): Mode =
path match {
case Ident(_) :: Import(_, _) :: _ =>
Mode.Import
case Ident(_) :: Import(_, _) :: _ => Mode.ImportOrExport
case (ref: RefTree) :: _ =>
if (ref.name.isTermName) Mode.Term
else if (ref.name.isTypeName) Mode.Type
else Mode.None

case (sel: untpd.ImportSelector) :: _ =>
if sel.imported.span.contains(pos.span) then Mode.Import
if sel.imported.span.contains(pos.span) then Mode.ImportOrExport
else Mode.None // Can't help completing the renaming

case Import(_, _) :: _ =>
Mode.Import

case _ =>
Mode.None
case (_: ImportOrExport) :: _ => Mode.ImportOrExport
case _ => Mode.None
}

/** When dealing with <errors> in varios palces we check to see if they are
Expand Down Expand Up @@ -103,8 +99,8 @@ object Completion {
case (sel: untpd.ImportSelector) :: _ =>
completionPrefix(sel.imported :: Nil, pos)

case Import(expr, selectors) :: _ =>
selectors.find(_.span.contains(pos.span)).map { selector =>
case (tree: untpd.ImportOrExport) :: _ =>
tree.selectors.find(_.span.contains(pos.span)).map { selector =>
completionPrefix(selector :: Nil, pos)
}.getOrElse("")

Expand Down Expand Up @@ -146,7 +142,7 @@ object Completion {
case Select(qual @ This(_), _) :: _ if qual.span.isSynthetic => completer.scopeCompletions
case Select(qual, _) :: _ if qual.tpe.hasSimpleKind => completer.selectionCompletions(qual)
case Select(qual, _) :: _ => Map.empty
case Import(expr, _) :: _ => completer.directMemberCompletions(expr)
case (tree: ImportOrExport) :: _ => completer.directMemberCompletions(tree.expr)
case (_: untpd.ImportSelector) :: Import(expr, _) :: _ => completer.directMemberCompletions(expr)
case _ => completer.scopeCompletions
}
Expand Down Expand Up @@ -566,7 +562,7 @@ object Completion {
val Type: Mode = new Mode(2)

/** Both term and type symbols are allowed */
val Import: Mode = new Mode(4) | Term | Type
val ImportOrExport: Mode = new Mode(4) | Term | Type
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,24 @@ class CompletionTest {
| testMet$m2
| Test(1, 2)
"""
.completion(m1, expected)
.completion(m2, expected)
.completion(m1, expected)
.completion(m2, expected)
}

@Test def exportCompletions: Unit = {
code"""object Foo:
| def xDef = 1
| val xVal = 1
| class xClass()
| object xObject {}
|object Test:
| export Foo.x${m1}
"""
.completion(m1, Set(
("xDef", Method, "=> Int"),
("xVal", Field, "Int"),
("xObject", Module, "Foo.xObject"),
("xClass", Module, "Foo.xClass"),
("xClass", Class, "Foo.xClass")))
}
}