-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the completion logic easier to extend
- Loading branch information
1 parent
a4fd829
commit 45fddb0
Showing
3 changed files
with
297 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
compiler/test/dotty/tools/dotc/interactive/CustomCompletion.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package dotty.tools.dotc.interactive | ||
|
||
import dotty.tools.dotc.ast.tpd._ | ||
import dotty.tools.dotc.ast.untpd | ||
import dotty.tools.dotc.core.Contexts._ | ||
import dotty.tools.dotc.core.Denotations.SingleDenotation | ||
import dotty.tools.dotc.core.Flags._ | ||
import dotty.tools.dotc.core.Names.{Name, termName} | ||
import dotty.tools.dotc.core.Symbols.{Symbol, defn} | ||
import dotty.tools.dotc.core.TypeError | ||
import dotty.tools.dotc.util.SourcePosition | ||
|
||
object CustomCompletion { | ||
|
||
def completions( | ||
pos: SourcePosition, | ||
dependencyCompleteOpt: Option[String => (Int, Seq[String])], | ||
enableDeep: Boolean | ||
)(using Context): (Int, List[Completion]) = { | ||
val path = Interactive.pathTo(ctx.compilationUnit.tpdTree, pos.span) | ||
computeCompletions(pos, path, dependencyCompleteOpt, enableDeep)(using Interactive.contextOfPath(path)) | ||
} | ||
|
||
def computeCompletions( | ||
pos: SourcePosition, | ||
path: List[Tree], | ||
dependencyCompleteOpt: Option[String => (Int, Seq[String])], | ||
enableDeep: Boolean | ||
)(using Context): (Int, List[Completion]) = { | ||
val mode = Completion.completionMode(path, pos) | ||
val prefix = Completion.completionPrefix(path, pos) | ||
val completer = new DeepCompleter(mode, prefix, pos) | ||
|
||
var extra = List.empty[Completion] | ||
|
||
val completions = path match { | ||
case Select(qual, _) :: _ => completer.selectionCompletions(qual) | ||
case Import(Ident(name), _) :: _ if name.decode.toString == "$ivy" && dependencyCompleteOpt.nonEmpty => | ||
val complete = dependencyCompleteOpt.get | ||
val (pos, completions) = complete(prefix) | ||
val input0 = prefix.take(pos) | ||
extra ++= completions.distinct.toList | ||
.map(s => Completion(Completion.label(termName(input0 + s)), "", Nil)) | ||
Map.empty | ||
case Import(expr, _) :: _ => completer.directMemberCompletions(expr) | ||
case (_: untpd.ImportSelector) :: Import(expr, _) :: _ => completer.directMemberCompletions(expr) | ||
case _ => | ||
completer.scopeCompletions ++ { | ||
if (enableDeep) completer.deepCompletions | ||
else Nil | ||
} | ||
} | ||
|
||
val describedCompletions = extra ++ Completion.describeCompletions(completions) | ||
val offset = Completion.completionOffset(path) | ||
|
||
(pos.span.start - prefix.length, describedCompletions) | ||
} | ||
|
||
class DeepCompleter(mode: Completion.Mode, prefix: String, pos: SourcePosition) extends Completion.Completer(mode, prefix, pos): | ||
def deepCompletions(using Context): Map[Name, Seq[SingleDenotation]] = { | ||
|
||
def allMembers(s: Symbol) = | ||
try s.info.allMembers | ||
catch { | ||
case _: dotty.tools.dotc.core.TypeError => Nil | ||
} | ||
def rec(t: Symbol): Seq[Symbol] = { | ||
val children = | ||
if (t.is(Package) || t.is(PackageVal) || t.is(PackageClass)) { | ||
allMembers(t).map(_.symbol).filter(_ != t).flatMap(rec) | ||
} else Nil | ||
|
||
t +: children.toSeq | ||
} | ||
|
||
val syms = for { | ||
member <- allMembers(defn.RootClass).map(_.symbol).toList | ||
sym <- rec(member) | ||
if sym.name.toString.startsWith(prefix) | ||
} yield sym | ||
|
||
syms.map(sym => (sym.fullName, List(sym: SingleDenotation))).toMap | ||
} | ||
|
||
} | ||
|
170 changes: 170 additions & 0 deletions
170
compiler/test/dotty/tools/dotc/interactive/CustomCompletionTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package dotty.tools | ||
package dotc.interactive | ||
|
||
import dotc.ast.tpd | ||
import dotc.{CompilationUnit, Compiler, Run} | ||
import dotc.core.Contexts.Context | ||
import dotc.core.Mode | ||
import dotc.reporting.StoreReporter | ||
import dotc.util.{SourceFile, SourcePosition} | ||
import dotc.util.Spans.Span | ||
|
||
import org.junit.Assert._ | ||
import org.junit.Test | ||
|
||
class CustomCompletionTests extends DottyTest: | ||
|
||
private def completions( | ||
input: String, | ||
dependencyCompleter: Option[String => (Int, Seq[String])] = None, | ||
deep: Boolean = false, | ||
extraDefinitions: String = "" | ||
): (Int, Seq[Completion]) = | ||
val prefix = extraDefinitions + """ | ||
object Wrapper { | ||
val expr = { | ||
""" | ||
val suffix = """ | ||
} | ||
} | ||
""" | ||
|
||
val allCode = prefix + input + suffix | ||
val index = prefix.length + input.length | ||
|
||
val run = new Run( | ||
new Compiler, | ||
initialCtx.fresh | ||
.addMode(Mode.ReadPositions | Mode.Interactive) | ||
// discard errors - comment out this line to print them in the console | ||
.setReporter(new StoreReporter(null)) | ||
.setSetting(initialCtx.settings.YstopAfter, List("typer")) | ||
) | ||
val file = SourceFile.virtual("<completions>", allCode, maybeIncomplete = true) | ||
given ctx: Context = run.runContext.withSource(file) | ||
val unit = CompilationUnit(file) | ||
ctx | ||
.run | ||
.compileUnits(unit :: Nil, ctx) | ||
|
||
// ignoring compilation errors here - the input code | ||
// to complete likely doesn't compile | ||
|
||
unit.tpdTree = { | ||
import tpd._ | ||
unit.tpdTree match { | ||
case PackageDef(_, p) => | ||
p.reverseIterator.collectFirst { | ||
case TypeDef(_, tmpl: Template) => | ||
tmpl.body | ||
.collectFirst { case dd: ValDef if dd.name.show == "expr" => dd } | ||
.getOrElse(sys.error("Unexpected tree shape")) | ||
} | ||
.getOrElse(sys.error("Unexpected tree shape")) | ||
case _ => sys.error("Unexpected tree shape") | ||
} | ||
} | ||
val ctx1 = ctx.fresh.setCompilationUnit(unit) | ||
val srcPos = SourcePosition(file, Span(index)) | ||
val (offset0, completions) = | ||
if (deep || dependencyCompleter.nonEmpty) | ||
CustomCompletion.completions(srcPos, dependencyCompleteOpt = dependencyCompleter, enableDeep = deep)(using ctx1) | ||
else | ||
Completion.completions(srcPos)(using ctx1) | ||
val offset = offset0 - prefix.length | ||
(offset, completions) | ||
|
||
|
||
@Test def simple(): Unit = | ||
val prefix = "scala.collection.immutable." | ||
val input = prefix + "Ma" | ||
|
||
val (offset, completions0) = completions(input) | ||
val labels = completions0.map(_.label) | ||
|
||
assert(offset == prefix.length) | ||
assert(labels.contains("Map")) | ||
|
||
@Test def custom(): Unit = | ||
val prefix = "import $ivy." | ||
val input = prefix + "scala" | ||
|
||
val dependencies = Seq( | ||
"scalaCompiler", | ||
"scalaLibrary", | ||
"other" | ||
) | ||
val (offset, completions0) = completions( | ||
input, | ||
dependencyCompleter = Some { dep => | ||
val matches = dependencies.filter(_.startsWith(dep)) | ||
(0, matches) | ||
} | ||
) | ||
val labels = completions0.map(_.label) | ||
|
||
assert(offset == prefix.length) | ||
assert(labels.contains("scalaCompiler")) | ||
assert(labels.contains("scalaLibrary")) | ||
assert(labels.length == 2) | ||
|
||
@Test def backTicks(): Unit = | ||
val prefix = "Foo." | ||
val input = prefix + "a" | ||
|
||
val extraDefinitions = | ||
"""object Foo { def a1 = 2; def `a-b` = 3 } | ||
|""".stripMargin | ||
val (offset, completions0) = completions( | ||
input, | ||
extraDefinitions = extraDefinitions | ||
) | ||
val labels = completions0.map(_.label) | ||
|
||
assert(offset == prefix.length) | ||
assert(labels.contains("a1")) | ||
assert(labels.contains("`a-b`")) | ||
|
||
@Test def backTicksDependencies(): Unit = | ||
val prefix = "import $ivy." | ||
val input = prefix + "`org.scala-lang:scala-`" | ||
|
||
val dependencies = Seq( | ||
"org.scala-lang:scala-compiler", | ||
"org.scala-lang:scala-library", | ||
"other" | ||
) | ||
val (offset, completions0) = completions( | ||
input, | ||
dependencyCompleter = Some { dep => | ||
val matches = dependencies.filter(_.startsWith(dep)) | ||
(0, matches) | ||
} | ||
) | ||
val labels = completions0.map(_.label) | ||
|
||
// Seems backticks mess with that for now... | ||
// assert(offset == prefix.length) | ||
assert(labels.contains("`org.scala-lang:scala-compiler`")) | ||
assert(labels.contains("`org.scala-lang:scala-library`")) | ||
assert(labels.length == 2) | ||
|
||
@Test def deep(): Unit = | ||
val prefix = "" | ||
val input = prefix + "ListBuf" | ||
|
||
val (offset, completions0) = completions(input, deep = true) | ||
val labels = completions0.map(_.label) | ||
|
||
assert(offset == prefix.length) | ||
assert(labels.contains("scala.collection.mutable.ListBuffer")) | ||
|
||
@Test def deepType(): Unit = | ||
val prefix = "" | ||
val input = prefix + "Function2" | ||
|
||
val (offset, completions0) = completions(input, deep = true) | ||
val labels = completions0.map(_.label) | ||
|
||
assert(offset == prefix.length) | ||
assert(labels.contains("scala.Function2")) |