Skip to content

Fixes #7673: document symbol responses by language server #7674

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
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ class Typer extends Namer
case _ =>
}
val x = tpnme.ANON_CLASS
val clsDef = TypeDef(x, templ1).withFlags(Final)
val clsDef = TypeDef(x, templ1).withFlags(Final | Synthetic)
typed(cpy.Block(tree)(clsDef :: Nil, New(Ident(x), Nil)), pt)
case _ =>
var tpt1 = typedType(tree.tpt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,19 @@ class DottyLanguageServer extends LanguageServer

val uriTrees = driver.openedTrees(uri)

val defs = Interactive.namedTrees(uriTrees, Include.empty)
val excludeLocalsFromSyntheticSymbols = (n: NameTree) => {
val owner = n.symbol.owner
n.symbol.is(Case) || {
Copy link
Member

@smarter smarter Dec 4, 2019

Choose a reason for hiding this comment

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

I think this makes too many assumptions about which trees to include or not, there might be non-synthetic definitions inside synthetic trees in other places than enums. It might not be enough but as a first step maybe we can just exclude type and term parameters of synthetic definitions:

Suggested change
n.symbol.is(Case) || {
!n.symbol.is(Param) || {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. I'm going to commit this soon with the change below.

!owner.is(Synthetic) &&
!owner.isPrimaryConstructor
}
}

val defs = Interactive.namedTrees(uriTrees, Include.local, excludeLocalsFromSyntheticSymbols)

(for {
d <- defs if !isWorksheetWrapper(d)
info <- symbolInfo(d.tree.symbol, d.namePos)
d <- defs if (!isWorksheetWrapper(d) && !isTopLevelWrapper(d))
info <- symbolInfo(d.tree.symbol, d.pos)
} yield JEither.forLeft(info)).asJava
}

Expand Down Expand Up @@ -808,6 +817,14 @@ object DottyLanguageServer {
symbol.owner == ctx.definitions.EmptyPackageClass
}

/**
* Is this symbol the wrapper object for top level definitions?
*/
def isTopLevelWrapper(sourceTree: SourceTree)(implicit ctx: Context): Boolean = {
val symbol = sourceTree.tree.symbol
symbol.isPackageObject
}

/** Create an lsp4j.CompletionItem from a completion result */
def completionItem(completion: Completion)(implicit ctx: Context): lsp4j.CompletionItem = {
def completionItemKind(sym: Symbol)(implicit ctx: Context): lsp4j.CompletionItemKind = {
Expand Down Expand Up @@ -883,24 +900,36 @@ object DottyLanguageServer {
SK.Constructor
else if (sym.is(Module))
SK.Module
else if (sym.isAllOf(EnumCase) || sym.isAllOf(EnumValue))
SK.EnumMember
else if (sym.is(Enum))
SK.Enum
else if (sym.is(Trait))
SK.Interface
else if (sym.isClass)
SK.Class
else if (sym.is(Mutable))
SK.Variable
else if (sym.is(Method))
SK.Method
else if (sym.is(TypeParam) || sym.isAbstractOrAliasType)
SK.TypeParameter
else
SK.Field
}
def containerName(sym: Symbol): String = {
if (sym.owner.exists && !sym.owner.isEmptyPackage) {
if (sym.owner.isPackageObject && sym.owner.owner.exists) {
sym.owner.owner.name.stripModuleClassSuffix.show
} else
sym.owner.name.stripModuleClassSuffix.show
} else
null
}

val name = sym.name.stripModuleClassSuffix.show
val containerName =
if (sym.owner.exists && !sym.owner.isEmptyPackage)
sym.owner.name.show
else
null

location(pos).map(l => new lsp4j.SymbolInformation(name, symbolKind(sym), l, containerName))
location(pos).map(l => new lsp4j.SymbolInformation(name, symbolKind(sym), l, containerName(sym)))
}

/** Convert `signature` to a `SignatureInformation` */
Expand Down