/
hover-provider.ts
82 lines (69 loc) · 3.22 KB
/
hover-provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/******************************************************************************
* 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 type { Hover, HoverParams } from 'vscode-languageserver';
import type { CancellationToken } from '../utils/cancellation.js';
import type { GrammarConfig } from '../languages/grammar-config.js';
import type { References } from '../references/references.js';
import type { LangiumServices } from './lsp-services.js';
import type { AstNode } from '../syntax-tree.js';
import type { MaybePromise } from '../utils/promise-utils.js';
import type { LangiumDocument } from '../workspace/documents.js';
import type { DocumentationProvider } from '../documentation/documentation-provider.js';
import { findDeclarationNodeAtOffset } from '../utils/cst-utils.js';
/**
* Language-specific service for handling hover requests.
*/
export interface HoverProvider {
/**
* Handle a hover request.
*
* @throws `OperationCancelled` if cancellation is detected during execution
* @throws `ResponseError` if an error is detected that should be sent as response to the client
*/
getHoverContent(document: LangiumDocument, params: HoverParams, cancelToken?: CancellationToken): MaybePromise<Hover | undefined>;
}
export abstract class AstNodeHoverProvider implements HoverProvider {
protected readonly references: References;
protected readonly grammarConfig: GrammarConfig;
constructor(services: LangiumServices) {
this.references = services.references.References;
this.grammarConfig = services.parser.GrammarConfig;
}
getHoverContent(document: LangiumDocument, params: HoverParams): MaybePromise<Hover | undefined> {
const rootNode = document.parseResult?.value?.$cstNode;
if (rootNode) {
const offset = document.textDocument.offsetAt(params.position);
const cstNode = findDeclarationNodeAtOffset(rootNode, offset, this.grammarConfig.nameRegexp);
if (cstNode && cstNode.offset + cstNode.length > offset) {
const targetNode = this.references.findDeclaration(cstNode);
if (targetNode) {
return this.getAstNodeHoverContent(targetNode);
}
}
}
return undefined;
}
protected abstract getAstNodeHoverContent(node: AstNode): MaybePromise<Hover | undefined>;
}
export class MultilineCommentHoverProvider extends AstNodeHoverProvider {
protected readonly documentationProvider: DocumentationProvider;
constructor(services: LangiumServices) {
super(services);
this.documentationProvider = services.documentation.DocumentationProvider;
}
protected getAstNodeHoverContent(node: AstNode): MaybePromise<Hover | undefined> {
const content = this.documentationProvider.getDocumentation(node);
if (content) {
return {
contents: {
kind: 'markdown',
value: content
}
};
}
return undefined;
}
}