Skip to content

Small improvements to workspace/symbol #5443

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 7 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 0 additions & 8 deletions compiler/src/dotty/tools/dotc/interactive/Interactive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,6 @@ 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import core._, core.Decorators.{sourcePos => _, _}
import Comments._, Constants._, Contexts._, Flags._, Names._, NameOps._, Symbols._, SymDenotations._, Trees._, Types._
import classpath.ClassPathEntries
import reporting._, reporting.diagnostic.{Message, MessageContainer, messages}
import transform.SymUtils.decorateSymbol
import typer.Typer
import util.{Set => _, _}
import interactive._, interactive.InteractiveDriver._
Expand Down Expand Up @@ -456,8 +457,12 @@ class DottyLanguageServer extends LanguageServer
implicit val ctx = driver.currentCtx

val uriTrees = driver.openedTrees(uri)
val predicate = (tree: NameTree) => {
val sym = tree.symbol
sym.exists && !sym.isLocal && !sym.isPrimaryConstructor && !sym.is(Synthetic)
}

val defs = Interactive.namedTrees(uriTrees, Include.empty, _ => true)
val defs = Interactive.namedTrees(uriTrees, Include.empty, predicate)
(for {
d <- defs if !isWorksheetWrapper(d)
info <- symbolInfo(d.tree.symbol, d.namePos, positionMapperFor(d.source))
Expand All @@ -466,12 +471,14 @@ class DottyLanguageServer extends LanguageServer

override def symbol(params: WorkspaceSymbolParams) = computeAsync { cancelToken =>
val query = params.getQuery
def predicate(implicit ctx: Context): NameTree => Boolean =
tree => tree.symbol.exists && !tree.symbol.isLocal && tree.name.toString.contains(query)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't symbol and documentSymbol have similar predicates ? I.e., should we also exclude primary constructors and synthetic symbols here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that makes a lot of sense. I've removed checks that were redundant with those happening in namedTrees.

I'm also thinking that namedTrees should be the one excluding primary constructors. I can't think of situations where they should be included, but I may very well be missing something. What do you think?


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, predicate)
defs.flatMap(d => symbolInfo(d.tree.symbol, d.namePos, positionMapperFor(d.source)))
}.asJava
}
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"))
}
}
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")
}
}