-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added hover provider for crossrefs * Moved HoverProvider to LangiumGrammarModule * Refactored LangiumGrammarHoverProvider * Formatting * Update packages/langium/src/lsp/langium-grammar-hover-provider.ts Co-authored-by: Mark Sujew <mark.sujew@typefox.io> * Update packages/langium/src/lsp/langium-grammar-hover-provider.ts Co-authored-by: Mark Sujew <mark.sujew@typefox.io> * Update packages/langium/src/lsp/langium-grammar-hover-provider.ts Co-authored-by: Mark Sujew <mark.sujew@typefox.io> * Moved langium-grammar-hover-provider to src/grammar/lsp Co-authored-by: Mark Sujew <mark.sujew@typefox.io>
- Loading branch information
Showing
3 changed files
with
87 additions
and
2 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
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
83 changes: 83 additions & 0 deletions
83
packages/langium/src/grammar/lsp/langium-grammar-hover-provider.ts
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,83 @@ | ||
/****************************************************************************** | ||
* Copyright 2021 TypeFox GmbH | ||
* This program and the accompanying materials are made available under the | ||
* terms of the MIT License, which is available in the project root. | ||
******************************************************************************/ | ||
import { Hover, HoverParams } from 'vscode-languageserver'; | ||
import { AstNode } from '../../syntax-tree'; | ||
import { MaybePromise } from '../../utils/promise-util'; | ||
import { MultilineCommentHoverProvider } from '../../lsp/hover-provider'; | ||
import { CrossReference, findLeafNodeAtOffset, findNameAssignment, findRelevantNode, Grammar, isCrossReference, isKeyword, isParserRule, isRuleCall, isType, LangiumDocument, LangiumDocuments, LangiumServices } from '../..'; | ||
|
||
export class LangiumGrammarHoverProvider extends MultilineCommentHoverProvider { | ||
|
||
readonly grammar: Grammar; | ||
readonly documents: LangiumDocuments; | ||
constructor(services: LangiumServices) { | ||
super(services); | ||
this.grammar = services.Grammar; | ||
this.documents = services.shared.workspace.LangiumDocuments; | ||
} | ||
|
||
protected getAstNodeHoverContent(node: AstNode): MaybePromise<Hover | undefined> { | ||
if(isCrossReference(node)) { | ||
return this.getCrossReferenceHoverContent(node); | ||
} | ||
return super.getAstNodeHoverContent(node); | ||
} | ||
|
||
getHoverContent(document: LangiumDocument<AstNode>, params: HoverParams): MaybePromise<Hover | undefined> { | ||
const rootNode = document.parseResult?.value?.$cstNode; | ||
if (rootNode) { | ||
const offset = document.textDocument.offsetAt(params.position); | ||
const cstNode = findLeafNodeAtOffset(rootNode, offset); | ||
if (cstNode && cstNode.offset + cstNode.length > offset) { | ||
const relevantNode = findRelevantNode(cstNode); | ||
const targetNode = this.references.findDeclaration(cstNode); | ||
if (isCrossReference(relevantNode)) { | ||
return this.getAstNodeHoverContent(relevantNode); | ||
} else if (targetNode) { | ||
return this.getAstNodeHoverContent(targetNode?.element); | ||
} | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
getCrossReferenceHoverContent(node: CrossReference): MaybePromise<Hover | undefined> { | ||
const ref = node.type.ref; | ||
if (ref) { | ||
if (isType(ref)) { | ||
return { | ||
contents: { | ||
kind: 'markdown', | ||
value: '```\n' + ref.$cstNode!.text + '\n```' | ||
} | ||
}; | ||
} else if (isParserRule(ref)) { | ||
const typeName = ref.name; | ||
const terminal = node.terminal; | ||
let terminalName = ''; | ||
if (terminal === undefined) { | ||
const terminalRule = findNameAssignment(ref); | ||
if (terminalRule && isRuleCall(terminalRule.terminal)){ | ||
terminalName = terminalRule.terminal.rule.ref!.name; | ||
} | ||
} else if (isKeyword(terminal)) { | ||
terminalName = terminal.value; | ||
} else if (isRuleCall(terminal)) { | ||
terminalName = terminal.rule.ref!.name; | ||
} | ||
|
||
return { | ||
contents: { | ||
kind: 'markdown', | ||
value: '```\n' + `[${typeName}:${terminalName}]` + '\n```' | ||
} | ||
}; | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
} |