|
1 | 1 | import * as vscode from 'vscode'; |
2 | 2 | import { extension } from '../state'; |
| 3 | +import type { Range, LJVariable } from '../types/context'; |
| 4 | +import { getSelectionContextVariables, rangesIntersect } from './context'; |
| 5 | +import { getOriginalVariableName, normalizeFilePath } from '../utils/utils'; |
3 | 6 |
|
4 | 7 | /** |
5 | 8 | * Initializes hover provider for LiquidJava diagnostics |
6 | 9 | */ |
7 | 10 | export function registerHover() { |
8 | 11 | vscode.languages.registerHoverProvider('java', { |
9 | 12 | provideHover(document, position) { |
10 | | - // if webview is visible, do not show hover |
11 | | - if (extension.webview?.isVisible()) return null; |
12 | | - |
13 | | - // get lj diagnostic at the current position |
14 | | - const diagnostics = vscode.languages.getDiagnostics(document.uri); |
15 | | - const diagnostic = diagnostics.find(d => d.range.contains(position) && d.source === 'liquidjava'); |
16 | | - if (!diagnostic) return null; |
17 | | - |
18 | | - // create hover content with link to open webview |
19 | 13 | const hoverContent = new vscode.MarkdownString(); |
20 | 14 | hoverContent.isTrusted = true; |
21 | | - hoverContent.appendMarkdown(`\n\n[Open LiquidJava view](command:liquidjava.showView) for more details.`); |
| 15 | + |
| 16 | + const variable = getHoveredVariable(document, position); |
| 17 | + if (variable && variable.mainRefinement && variable.mainRefinement !== 'true') |
| 18 | + hoverContent.appendCodeblock(`@Refinement(${JSON.stringify(variable.mainRefinement)})`, 'java'); |
| 19 | + |
| 20 | + const diagnostics = vscode.languages.getDiagnostics(document.uri); |
| 21 | + const containsDiagnostic = !!diagnostics.find(d => d.range.contains(position) && d.source === 'liquidjava'); |
| 22 | + if (containsDiagnostic) { |
| 23 | + if (hoverContent.value.length > 0) hoverContent.appendMarkdown(`\n\n`); |
| 24 | + hoverContent.appendMarkdown(`[Open LiquidJava view](command:liquidjava.showView) for more details.`); |
| 25 | + } |
| 26 | + if (hoverContent.value.length === 0) return null; |
22 | 27 | return new vscode.Hover(hoverContent); |
23 | 28 | } |
24 | 29 | }); |
25 | 30 | } |
| 31 | + |
| 32 | +function getHoveredVariable(document: vscode.TextDocument, position: vscode.Position): LJVariable | null { |
| 33 | + if (!extension.context) return null; |
| 34 | + |
| 35 | + const wordRange = document.getWordRangeAtPosition(position, /[#]?[A-Za-z_][A-Za-z0-9_#]*/); |
| 36 | + if (!wordRange) return null; |
| 37 | + |
| 38 | + const hoveredWord = document.getText(wordRange); |
| 39 | + const file = normalizeFilePath(document.uri.fsPath); |
| 40 | + const hoveredRange: Range = { |
| 41 | + lineStart: wordRange.start.line, |
| 42 | + colStart: wordRange.start.character, |
| 43 | + lineEnd: wordRange.end.line, |
| 44 | + colEnd: wordRange.end.character |
| 45 | + }; |
| 46 | + const { allVars } = getSelectionContextVariables(file, hoveredRange); |
| 47 | + return allVars.find(variable => getOriginalVariableName(variable.name) === hoveredWord); |
| 48 | +} |
0 commit comments