-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhover.ts
More file actions
50 lines (43 loc) · 2.25 KB
/
hover.ts
File metadata and controls
50 lines (43 loc) · 2.25 KB
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
import * as vscode from 'vscode';
import { extension } from '../state';
import type { Range, LJVariable } from '../types/context';
import { getSelectionContextVariables } from './context';
import { getOriginalVariableName, normalizeFilePath, toRange } from '../utils/utils';
/**
* Initializes hover provider for LiquidJava diagnostics
*/
export function registerHover() {
vscode.languages.registerHoverProvider('java', {
provideHover(document, position) {
const hoverContent = new vscode.MarkdownString();
hoverContent.isTrusted = true;
const variable = getHoveredVariable(document, position);
if (variable && variable.mainRefinement && variable.mainRefinement !== 'true')
hoverContent.appendCodeblock(`@Refinement("${variable.mainRefinement}")`, 'java');
const diagnostics = vscode.languages.getDiagnostics(document.uri);
const containsDiagnostic = !!diagnostics.find(d => d.range.contains(position) && d.source === 'liquidjava');
if (containsDiagnostic) {
if (hoverContent.value.length > 0) hoverContent.appendMarkdown(`\n\n`);
hoverContent.appendMarkdown(`[Open LiquidJava view](command:liquidjava.showView) for more details.`);
}
if (hoverContent.value.length === 0) return null;
return new vscode.Hover(hoverContent);
}
});
}
function getHoveredVariable(document: vscode.TextDocument, position: vscode.Position): LJVariable | null {
if (!extension.context) return null;
const wordRange = document.getWordRangeAtPosition(position, /[#]?[A-Za-z_][A-Za-z0-9_#]*/);
if (!wordRange) return null;
const hoveredWord = document.getText(wordRange);
const file = normalizeFilePath(document.uri.fsPath);
// we need to use single point cursor position after variable to get all variables until that point
const positionAfterVariable = {
lineStart: wordRange.end.line,
colStart: wordRange.end.character,
lineEnd: wordRange.end.line,
colEnd: wordRange.end.character
};
const { allVars } = getSelectionContextVariables(file, positionAfterVariable);
return allVars.find(variable => getOriginalVariableName(variable.name) === hoveredWord);
}