/
inlayHints.ts
83 lines (68 loc) · 2.25 KB
/
inlayHints.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
83
import * as shared from '@volar/shared';
import { transformTextEdit } from '@volar/transforms';
import * as vscode from 'vscode-languageserver-protocol';
import type { LanguageServiceRuntimeContext } from '../types';
import { getOverlapRange } from '../utils/common';
import { languageFeatureWorker } from '../utils/featureWorkers';
export function register(context: LanguageServiceRuntimeContext) {
return async (uri: string, range: vscode.Range) => {
const document = context.getTextDocument(uri);
if (!document)
return;
const offsetRange = {
start: document.offsetAt(range.start),
end: document.offsetAt(range.end),
};
return languageFeatureWorker(
context,
uri,
range,
(_arg, map, file) => {
/**
* copy from ./codeActions.ts
*/
if (!file.capabilities.inlayHint)
return [];
let minStart: number | undefined;
let maxEnd: number | undefined;
for (const mapping of map.map.mappings) {
const overlapRange = getOverlapRange(offsetRange.start, offsetRange.end, mapping.sourceRange[0], mapping.sourceRange[1]);
if (overlapRange) {
const start = map.map.toGeneratedOffset(overlapRange.start)?.[0];
const end = map.map.toGeneratedOffset(overlapRange.end)?.[0];
if (start !== undefined && end !== undefined) {
minStart = minStart === undefined ? start : Math.min(start, minStart);
maxEnd = maxEnd === undefined ? end : Math.max(end, maxEnd);
}
}
}
if (minStart !== undefined && maxEnd !== undefined) {
return [vscode.Range.create(
map.virtualFileDocument.positionAt(minStart),
map.virtualFileDocument.positionAt(maxEnd),
)];
}
return [];
},
(plugin, document, arg) => {
return plugin.inlayHints?.on?.(document, arg);
},
(inlayHints, map) => inlayHints.map(_inlayHint => {
if (!map)
return _inlayHint;
const position = map.toSourcePosition(_inlayHint.position);
const edits = _inlayHint.textEdits
?.map(textEdit => transformTextEdit(textEdit, range => map!.toSourceRange(range), map.virtualFileDocument))
.filter(shared.notEmpty);
if (position) {
return {
..._inlayHint,
position,
edits,
};
}
}).filter(shared.notEmpty),
arr => arr.flat(),
);
};
}