-
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 9441ab6
Showing
3 changed files
with
282 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
152 changes: 152 additions & 0 deletions
152
compiler/test/dotty/tools/dotc/interactive/CompletionTests.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,152 @@ | ||
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 CompletionTests extends DottyTest: | ||
|
||
private def completions( | ||
input: String, | ||
dependencyCompleter: Option[String => (Int, Seq[String])] = None, | ||
deep: Boolean = false | ||
): (Int, Seq[Completion]) = | ||
val prefix = """ | ||
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.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 = "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")) |
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 | ||
} | ||
|
||
} | ||
|