Skip to content
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

Small improvements to workspace/symbol #5443

Merged
merged 7 commits into from
Nov 30, 2018
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
53 changes: 31 additions & 22 deletions compiler/src/dotty/tools/dotc/interactive/Interactive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import scala.collection._
import ast.{NavigateAST, Trees, tpd, untpd}
import core._, core.Decorators.{sourcePos => _, _}
import Contexts._, Flags._, Names._, NameOps._, Symbols._, Trees._, Types._
import transform.SymUtils.decorateSymbol
import util.Positions._, util.SourceFile, util.SourcePosition
import core.Denotations.SingleDenotation
import NameKinds.SimpleNameKind
Expand All @@ -33,6 +34,7 @@ object Interactive {
def isDefinitions: Boolean = (bits & definitions.bits) != 0
def isLinkedClass: Boolean = (bits & linkedClass.bits) != 0
def isImports: Boolean = (bits & imports.bits) != 0
def isLocal: Boolean = (bits & local.bits) != 0
}

/** The empty set */
Expand All @@ -59,6 +61,9 @@ object Interactive {
/** Include imports in the results */
val imports: Set = Set(1 << 5)

/** Include local symbols, inspect local trees */
val local: Set = Set(1 << 6)

/** All the flags */
val all: Set = Set(~0)
}
Expand Down Expand Up @@ -317,40 +322,44 @@ object Interactive {
else
namedTrees(trees, include, matchSymbol(_, sym, include))

/** Find named trees with a non-empty position whose name contains `nameSubstring` in `trees`.
*/
def namedTrees(trees: List[SourceTree], nameSubstring: String)
(implicit ctx: Context): List[SourceTree] = {
val predicate: NameTree => Boolean = _.name.toString.contains(nameSubstring)
namedTrees(trees, Include.empty, predicate)
}

/** Find named trees with a non-empty position satisfying `treePredicate` in `trees`.
*
* @param includeReferences If true, include references and not just definitions
* @param trees The trees to inspect.
* @param include Whether to include references, definitions, etc.
* @param treePredicate An additional predicate that the trees must match.
* @return The trees with a non-empty position satisfying `treePredicate`.
*/
def namedTrees(trees: List[SourceTree], include: Include.Set, treePredicate: NameTree => Boolean)
(implicit ctx: Context): List[SourceTree] = safely {
def namedTrees(trees: List[SourceTree],
include: Include.Set,
treePredicate: NameTree => Boolean = util.common.alwaysTrue
)(implicit ctx: Context): List[SourceTree] = safely {
val buf = new mutable.ListBuffer[SourceTree]

def traverser(source: SourceFile) = {
new untpd.TreeTraverser {
private def handle(utree: untpd.NameTree): Unit = {
val tree = utree.asInstanceOf[tpd.NameTree]
if (tree.symbol.exists
&& !tree.symbol.is(Synthetic)
&& !tree.symbol.isPrimaryConstructor
&& tree.pos.exists
&& !tree.pos.isZeroExtent
&& (include.isReferences || isDefinition(tree))
&& treePredicate(tree))
buf += SourceTree(tree, source)
}
override def traverse(tree: untpd.Tree)(implicit ctx: Context) = {
tree match {
case imp: untpd.Import if include.isImports && tree.hasType =>
val tree = imp.asInstanceOf[tpd.Import]
val selections = tpd.importSelections(tree)
traverse(imp.expr)
selections.foreach(traverse)
case utree: untpd.ValOrDefDef if tree.hasType =>
handle(utree)
if (include.isLocal) traverseChildren(tree)
case utree: untpd.NameTree if tree.hasType =>
val tree = utree.asInstanceOf[tpd.NameTree]
if (tree.symbol.exists
&& !tree.symbol.is(Synthetic)
&& tree.pos.exists
&& !tree.pos.isZeroExtent
&& (include.isReferences || isDefinition(tree))
&& treePredicate(tree))
buf += SourceTree(tree, source)
handle(utree)
traverseChildren(tree)
case tree: untpd.Inlined =>
traverse(tree.call)
Expand Down Expand Up @@ -381,8 +390,7 @@ object Interactive {
)(implicit ctx: Context): List[SourceTree] = {
val linkedSym = symbol.linkedClass
val fullPredicate: NameTree => Boolean = tree =>
( !tree.symbol.isPrimaryConstructor
&& (includes.isDefinitions || !Interactive.isDefinition(tree))
( (includes.isDefinitions || !Interactive.isDefinition(tree))
&& ( Interactive.matchSymbol(tree, symbol, includes)
|| ( includes.isLinkedClass
&& linkedSym.exists
Expand Down Expand Up @@ -482,6 +490,7 @@ object Interactive {
def findDefinitions(path: List[Tree], pos: SourcePosition, driver: InteractiveDriver)(implicit ctx: Context): List[SourceTree] = {
enclosingSourceSymbols(path, pos).flatMap { sym =>
val enclTree = enclosingTree(path)
val includeLocal = if (sym.exists && sym.isLocal) Include.local else Include.empty

val (trees, include) =
if (enclTree.isInstanceOf[MemberDef])
Expand All @@ -500,7 +509,7 @@ object Interactive {
(Nil, Include.empty)
}

findTreesMatching(trees, include, sym)
findTreesMatching(trees, include | includeLocal, sym)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class DottyLanguageServer extends LanguageServer

val includes = {
val includeDeclaration = params.getContext.isIncludeDeclaration
Include.references | Include.overriding | Include.imports |
Include.references | Include.overriding | Include.imports | Include.local |
(if (includeDeclaration) Include.definitions else Include.empty)
}

Expand Down Expand Up @@ -457,7 +457,7 @@ class DottyLanguageServer extends LanguageServer

val uriTrees = driver.openedTrees(uri)

val defs = Interactive.namedTrees(uriTrees, Include.empty, _ => true)
val defs = Interactive.namedTrees(uriTrees, Include.empty)
(for {
d <- defs if !isWorksheetWrapper(d)
info <- symbolInfo(d.tree.symbol, d.namePos, positionMapperFor(d.source))
Expand All @@ -470,8 +470,8 @@ class DottyLanguageServer extends LanguageServer
drivers.values.toList.flatMap { driver =>
implicit val ctx = driver.currentCtx

val trees = driver.allTrees
val defs = Interactive.namedTrees(trees, nameSubstring = query)
val trees = driver.sourceTreesContaining(query)
val defs = Interactive.namedTrees(trees, Include.empty, _.name.toString.contains(query))
defs.flatMap(d => symbolInfo(d.tree.symbol, d.namePos, positionMapperFor(d.source)))
}.asJava
}
Expand Down Expand Up @@ -499,7 +499,7 @@ class DottyLanguageServer extends LanguageServer
val predicates = definitions.map(Interactive.implementationFilter(_)(ctx))
tree => predicates.exists(_(tree))
}
val matches = Interactive.namedTrees(trees, Include.empty, predicate)(ctx)
val matches = Interactive.namedTrees(trees, Include.local, predicate)(ctx)
matches.map(tree => location(tree.namePos(ctx), positionMapperFor(tree.source)))
}
}.toList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ class DocumentSymbolTest {
.documentSymbol(m1, (m1 to m2).symInfo("Foo", SymbolKind.Module),
(m3 to m4).symInfo("Foo", SymbolKind.Class))
}

@Test def documentSymbolSynthetic: Unit = {
code"""case class ${m1}Foo${m2}(${m3}x${m4}: Int)""".withSource
.documentSymbol(m1, (m1 to m2).symInfo("Foo", SymbolKind.Class),
(m3 to m4).symInfo("x", SymbolKind.Field, "Foo"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,14 @@ class ReferencesTest {
.references(m11 to m12, List(m9 to m10, m11 to m12), withDecl = false)
}

@Test def referenceInsideLocalMember: Unit = {
withSources(
code"""object A {
| val ${m1}foo${m2} = 0
| def fizz = println(${m3}foo${m4})
|}"""
).references(m1 to m2, List(m1 to m2, m3 to m4), withDecl = true)
.references(m1 to m2, List(m3 to m4), withDecl = false)
}

}
22 changes: 22 additions & 0 deletions language-server/test/dotty/tools/languageserver/SymbolTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,26 @@ class SymbolTest {
.symbol("Foo", (m1 to m2).symInfo("Foo", SymbolKind.Module),
(m3 to m4).symInfo("Foo", SymbolKind.Class))
}

@Test def multipleProjects0: Unit = {
val p0 = Project.withSources(
code"""class ${m1}Foo${m2}"""
)

val p1 = Project.dependingOn(p0).withSources(
code"""class ${m3}Bar${m4} extends Foo"""
)

withProjects(p0, p1)
.symbol("Foo", (m1 to m2).symInfo("Foo", SymbolKind.Class))
}

@Test def noLocalSymbols: Unit = {
code"""object O {
def foo = {
val hello = 0
}
}""".withSource
.symbol("hello")
}
}