-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: document symbol provider (#659)
### Summary of Changes Implement a document symbol provider. Compared to the default provider, it sets proper symbol kinds, adds details for functions and segments to show their signature, and marks symbols as deprecated. --------- Co-authored-by: megalinter-bot <[email protected]>
- Loading branch information
1 parent
9ba9d20
commit fe0c8d5
Showing
84 changed files
with
847 additions
and
399 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,89 @@ | ||
import { AstNode, DefaultDocumentSymbolProvider, LangiumDocument } from 'langium'; | ||
import { DocumentSymbol, SymbolTag } from 'vscode-languageserver'; | ||
import { SafeDsServices } from '../safe-ds-module.js'; | ||
import { SafeDsAnnotations } from '../builtins/safe-ds-annotations.js'; | ||
import { | ||
isSdsAnnotatedObject, | ||
isSdsAnnotation, | ||
isSdsAttribute, | ||
isSdsClass, | ||
isSdsEnumVariant, | ||
isSdsFunction, | ||
isSdsPipeline, | ||
isSdsSegment, | ||
} from '../generated/ast.js'; | ||
import { SafeDsTypeComputer } from '../typing/safe-ds-type-computer.js'; | ||
|
||
export class SafeDsDocumentSymbolProvider extends DefaultDocumentSymbolProvider { | ||
private readonly builtinAnnotations: SafeDsAnnotations; | ||
private readonly typeComputer: SafeDsTypeComputer; | ||
|
||
constructor(services: SafeDsServices) { | ||
super(services); | ||
|
||
this.builtinAnnotations = services.builtins.Annotations; | ||
this.typeComputer = services.types.TypeComputer; | ||
} | ||
|
||
protected override getSymbol(document: LangiumDocument, node: AstNode): DocumentSymbol[] { | ||
const cstNode = node.$cstNode; | ||
const nameNode = this.nameProvider.getNameNode(node); | ||
if (nameNode && cstNode) { | ||
const name = this.nameProvider.getName(node); | ||
return [ | ||
{ | ||
name: name ?? nameNode.text, | ||
kind: this.nodeKindProvider.getSymbolKind(node), | ||
tags: this.getTags(node), | ||
detail: this.getDetails(node), | ||
range: cstNode.range, | ||
selectionRange: nameNode.range, | ||
children: this.getChildSymbols(document, node), | ||
}, | ||
]; | ||
} else { | ||
return this.getChildSymbols(document, node) || []; | ||
} | ||
} | ||
|
||
protected override getChildSymbols(document: LangiumDocument, node: AstNode): DocumentSymbol[] | undefined { | ||
if (this.isLeaf(node)) { | ||
return undefined; | ||
} else if (isSdsClass(node)) { | ||
if (node.body) { | ||
return super.getChildSymbols(document, node.body); | ||
} else { | ||
return undefined; | ||
} | ||
} else { | ||
return super.getChildSymbols(document, node); | ||
} | ||
} | ||
|
||
private getDetails(node: AstNode): string | undefined { | ||
if (isSdsFunction(node) || isSdsSegment(node)) { | ||
const type = this.typeComputer.computeType(node); | ||
return type?.toString(); | ||
} | ||
return undefined; | ||
} | ||
|
||
private getTags(node: AstNode): SymbolTag[] | undefined { | ||
if (isSdsAnnotatedObject(node) && this.builtinAnnotations.isDeprecated(node)) { | ||
return [SymbolTag.Deprecated]; | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
|
||
private isLeaf(node: AstNode): boolean { | ||
return ( | ||
isSdsAnnotation(node) || | ||
isSdsAttribute(node) || | ||
isSdsEnumVariant(node) || | ||
isSdsFunction(node) || | ||
isSdsPipeline(node) || | ||
isSdsSegment(node) | ||
); | ||
} | ||
} |
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,93 @@ | ||
import { AstNode, AstNodeDescription, hasContainerOfType, isAstNode, NodeKindProvider } from 'langium'; | ||
import { CompletionItemKind, SymbolKind } from 'vscode-languageserver'; | ||
import { | ||
isSdsClass, | ||
isSdsFunction, | ||
SdsAnnotation, | ||
SdsAttribute, | ||
SdsBlockLambdaResult, | ||
SdsClass, | ||
SdsEnum, | ||
SdsEnumVariant, | ||
SdsFunction, | ||
SdsModule, | ||
SdsParameter, | ||
SdsPipeline, | ||
SdsPlaceholder, | ||
SdsResult, | ||
SdsSegment, | ||
SdsTypeParameter, | ||
} from '../generated/ast.js'; | ||
|
||
export class SafeDsNodeKindProvider implements NodeKindProvider { | ||
getSymbolKind(nodeOrDescription: AstNode | AstNodeDescription): SymbolKind { | ||
// The WorkspaceSymbolProvider only passes descriptions, where the node might be undefined | ||
const node = this.getNode(nodeOrDescription); | ||
if (isSdsFunction(node) && hasContainerOfType(node, isSdsClass)) { | ||
return SymbolKind.Method; | ||
} | ||
|
||
const type = this.getNodeType(nodeOrDescription); | ||
switch (type) { | ||
case SdsAnnotation: | ||
return SymbolKind.Interface; | ||
case SdsAttribute: | ||
return SymbolKind.Property; | ||
/* c8 ignore next 2 */ | ||
case SdsBlockLambdaResult: | ||
return SymbolKind.Variable; | ||
case SdsClass: | ||
return SymbolKind.Class; | ||
case SdsEnum: | ||
return SymbolKind.Enum; | ||
case SdsEnumVariant: | ||
return SymbolKind.EnumMember; | ||
case SdsFunction: | ||
return SymbolKind.Function; | ||
case SdsModule: | ||
return SymbolKind.Package; | ||
/* c8 ignore next 2 */ | ||
case SdsParameter: | ||
return SymbolKind.Variable; | ||
case SdsPipeline: | ||
return SymbolKind.Function; | ||
/* c8 ignore next 2 */ | ||
case SdsPlaceholder: | ||
return SymbolKind.Variable; | ||
/* c8 ignore next 2 */ | ||
case SdsResult: | ||
return SymbolKind.Variable; | ||
case SdsSegment: | ||
return SymbolKind.Function; | ||
/* c8 ignore next 2 */ | ||
case SdsTypeParameter: | ||
return SymbolKind.TypeParameter; | ||
/* c8 ignore next 2 */ | ||
default: | ||
return SymbolKind.Null; | ||
} | ||
} | ||
|
||
/* c8 ignore start */ | ||
getCompletionItemKind(_nodeOrDescription: AstNode | AstNodeDescription) { | ||
return CompletionItemKind.Reference; | ||
} | ||
|
||
/* c8 ignore stop */ | ||
|
||
private getNode(nodeOrDescription: AstNode | AstNodeDescription): AstNode | undefined { | ||
if (isAstNode(nodeOrDescription)) { | ||
return nodeOrDescription; | ||
} /* c8 ignore start */ else { | ||
return nodeOrDescription.node; | ||
} /* c8 ignore stop */ | ||
} | ||
|
||
private getNodeType(nodeOrDescription: AstNode | AstNodeDescription): string { | ||
if (isAstNode(nodeOrDescription)) { | ||
return nodeOrDescription.$type; | ||
} /* c8 ignore start */ else { | ||
return nodeOrDescription.type; | ||
} /* c8 ignore stop */ | ||
} | ||
} |
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
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
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
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 |
---|---|---|
|
@@ -350,7 +350,7 @@ class UnknownTypeClass extends Type { | |
} | ||
|
||
toString(): string { | ||
return '$Unknown'; | ||
return '?'; | ||
} | ||
} | ||
|
||
|
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
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
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
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
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
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
Oops, something went wrong.