@volar/language-service 2.0.4 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/documents.d.ts +4 -4
- package/lib/documents.js +9 -9
- package/lib/features/provideCallHierarchyItems.js +6 -6
- package/lib/features/provideCodeActions.js +12 -32
- package/lib/features/provideCompletionItems.js +4 -4
- package/lib/features/provideDefinition.js +3 -3
- package/lib/features/provideDiagnostics.js +4 -17
- package/lib/features/provideDocumentFormattingEdits.js +127 -188
- package/lib/features/provideDocumentSemanticTokens.js +4 -25
- package/lib/features/provideFileReferences.js +1 -1
- package/lib/features/provideInlayHints.js +5 -29
- package/lib/features/provideReferences.js +1 -1
- package/lib/features/provideSelectionRanges.js +7 -10
- package/lib/features/provideWorkspaceSymbols.js +1 -1
- package/lib/features/resolveCompletionItem.js +1 -1
- package/lib/types.d.ts +10 -13
- package/lib/utils/common.d.ts +3 -1
- package/lib/utils/common.js +54 -2
- package/lib/utils/featureWorkers.d.ts +2 -2
- package/lib/utils/featureWorkers.js +14 -6
- package/lib/utils/transform.js +10 -10
- package/package.json +3 -3
package/lib/documents.d.ts
CHANGED
|
@@ -4,10 +4,10 @@ import type * as vscode from 'vscode-languageserver-protocol';
|
|
|
4
4
|
import { TextDocument } from 'vscode-languageserver-textdocument';
|
|
5
5
|
export type DocumentProvider = ReturnType<typeof createDocumentProvider>;
|
|
6
6
|
export declare class SourceMapWithDocuments<Data = any> {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
sourceDocument: TextDocument;
|
|
8
|
+
embeddedDocument: TextDocument;
|
|
9
9
|
map: SourceMap<Data>;
|
|
10
|
-
constructor(
|
|
10
|
+
constructor(sourceDocument: TextDocument, embeddedDocument: TextDocument, map: SourceMap<Data>);
|
|
11
11
|
getSourceRange(range: vscode.Range, filter?: (data: Data) => boolean): vscode.Range | undefined;
|
|
12
12
|
getGeneratedRange(range: vscode.Range, filter?: (data: Data) => boolean): vscode.Range | undefined;
|
|
13
13
|
getSourceRanges(range: vscode.Range, filter?: (data: Data) => boolean): Generator<vscode.Range, void, unknown>;
|
|
@@ -33,6 +33,6 @@ export declare function createDocumentProvider(files: FileRegistry): {
|
|
|
33
33
|
get: (uri: string, languageId: string, snapshot: ts.IScriptSnapshot) => TextDocument;
|
|
34
34
|
getMaps(virtualCode: VirtualCode): Generator<SourceMapWithDocuments<CodeInformation>, void, unknown>;
|
|
35
35
|
getLinkedCodeMap(virtualCode: VirtualCode): LinkedCodeMapWithDocument | undefined;
|
|
36
|
-
getVirtualCodeByUri(uri: string): readonly [VirtualCode
|
|
36
|
+
getVirtualCodeByUri(uri: string): readonly [VirtualCode, import("@volar/language-core").SourceFile] | readonly [undefined, undefined];
|
|
37
37
|
getVirtualCodeUri(sourceFileUri: string, virtualCodeId: string): string;
|
|
38
38
|
};
|
package/lib/documents.js
CHANGED
|
@@ -4,9 +4,9 @@ exports.createDocumentProvider = exports.LinkedCodeMapWithDocument = exports.Sou
|
|
|
4
4
|
const language_core_1 = require("@volar/language-core");
|
|
5
5
|
const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");
|
|
6
6
|
class SourceMapWithDocuments {
|
|
7
|
-
constructor(
|
|
8
|
-
this.
|
|
9
|
-
this.
|
|
7
|
+
constructor(sourceDocument, embeddedDocument, map) {
|
|
8
|
+
this.sourceDocument = sourceDocument;
|
|
9
|
+
this.embeddedDocument = embeddedDocument;
|
|
10
10
|
this.map = map;
|
|
11
11
|
}
|
|
12
12
|
// Range APIs
|
|
@@ -69,12 +69,12 @@ class SourceMapWithDocuments {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
*getSourcePositionsBase(position, filter = () => true) {
|
|
72
|
-
for (const mapped of this.findPositions(position, filter, this.
|
|
72
|
+
for (const mapped of this.findPositions(position, filter, this.embeddedDocument, this.sourceDocument, 'generatedOffsets', 'sourceOffsets')) {
|
|
73
73
|
yield mapped;
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
*getGeneratedPositionsBase(position, filter = () => true) {
|
|
77
|
-
for (const mapped of this.findPositions(position, filter, this.
|
|
77
|
+
for (const mapped of this.findPositions(position, filter, this.sourceDocument, this.embeddedDocument, 'sourceOffsets', 'generatedOffsets')) {
|
|
78
78
|
yield mapped;
|
|
79
79
|
}
|
|
80
80
|
}
|
|
@@ -87,15 +87,15 @@ class SourceMapWithDocuments {
|
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
matchSourcePosition(position, mapping) {
|
|
90
|
-
let offset = (0, language_core_1.translateOffset)(this.
|
|
90
|
+
let offset = (0, language_core_1.translateOffset)(this.embeddedDocument.offsetAt(position), mapping.generatedOffsets, mapping.sourceOffsets, mapping.lengths);
|
|
91
91
|
if (offset !== undefined) {
|
|
92
|
-
return this.
|
|
92
|
+
return this.sourceDocument.positionAt(offset);
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
matchGeneratedPosition(position, mapping) {
|
|
96
|
-
let offset = (0, language_core_1.translateOffset)(this.
|
|
96
|
+
let offset = (0, language_core_1.translateOffset)(this.sourceDocument.offsetAt(position), mapping.sourceOffsets, mapping.generatedOffsets, mapping.lengths);
|
|
97
97
|
if (offset !== undefined) {
|
|
98
|
-
return this.
|
|
98
|
+
return this.embeddedDocument.positionAt(offset);
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
}
|
|
@@ -21,7 +21,7 @@ function register(context) {
|
|
|
21
21
|
data: item.data,
|
|
22
22
|
},
|
|
23
23
|
serviceIndex: context.services.indexOf(service),
|
|
24
|
-
virtualDocumentUri: map?.
|
|
24
|
+
virtualDocumentUri: map?.embeddedDocument.uri,
|
|
25
25
|
};
|
|
26
26
|
});
|
|
27
27
|
return items;
|
|
@@ -127,8 +127,8 @@ function register(context) {
|
|
|
127
127
|
if (!range) {
|
|
128
128
|
// TODO: <script> range
|
|
129
129
|
range = {
|
|
130
|
-
start: map.
|
|
131
|
-
end: map.
|
|
130
|
+
start: map.sourceDocument.positionAt(0),
|
|
131
|
+
end: map.sourceDocument.positionAt(map.sourceDocument.getText().length),
|
|
132
132
|
};
|
|
133
133
|
}
|
|
134
134
|
const selectionRange = map.getSourceRange(tsItem.selectionRange);
|
|
@@ -138,10 +138,10 @@ function register(context) {
|
|
|
138
138
|
const vueRanges = tsRanges.map(tsRange => map.getSourceRange(tsRange)).filter(common_1.notEmpty);
|
|
139
139
|
const vueItem = {
|
|
140
140
|
...tsItem,
|
|
141
|
-
name: tsItem.name === map.
|
|
142
|
-
? map.
|
|
141
|
+
name: tsItem.name === map.embeddedDocument.uri.substring(map.embeddedDocument.uri.lastIndexOf('/') + 1)
|
|
142
|
+
? map.sourceDocument.uri.substring(map.sourceDocument.uri.lastIndexOf('/') + 1)
|
|
143
143
|
: tsItem.name,
|
|
144
|
-
uri: map.
|
|
144
|
+
uri: map.sourceDocument.uri,
|
|
145
145
|
// TS Bug: `range: range` not works
|
|
146
146
|
range: {
|
|
147
147
|
start: range.start,
|
|
@@ -13,41 +13,21 @@ function register(context) {
|
|
|
13
13
|
if (!sourceFile) {
|
|
14
14
|
return;
|
|
15
15
|
}
|
|
16
|
-
const document = context.documents.get(uri, sourceFile.languageId, sourceFile.snapshot);
|
|
17
|
-
const offsetRange = {
|
|
18
|
-
start: document.offsetAt(range.start),
|
|
19
|
-
end: document.offsetAt(range.end),
|
|
20
|
-
};
|
|
21
16
|
const transformedCodeActions = new WeakSet();
|
|
22
17
|
return await (0, featureWorkers_1.languageFeatureWorker)(context, uri, () => ({ range, codeActionContext }), function* (map) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
const _codeActionContext = {
|
|
19
|
+
diagnostics: (0, transform_1.transformLocations)(codeActionContext.diagnostics, range => map.getGeneratedRange(range)),
|
|
20
|
+
only: codeActionContext.only,
|
|
21
|
+
};
|
|
22
|
+
const mapped = (0, common_1.findOverlapCodeRange)(map.sourceDocument.offsetAt(range.start), map.sourceDocument.offsetAt(range.end), map.map, language_core_1.isCodeActionsEnabled);
|
|
23
|
+
if (mapped) {
|
|
24
|
+
yield {
|
|
25
|
+
range: {
|
|
26
|
+
start: map.embeddedDocument.positionAt(mapped.start),
|
|
27
|
+
end: map.embeddedDocument.positionAt(mapped.end),
|
|
28
|
+
},
|
|
29
|
+
codeActionContext: _codeActionContext,
|
|
27
30
|
};
|
|
28
|
-
let minStart;
|
|
29
|
-
let maxEnd;
|
|
30
|
-
for (const mapping of map.map.mappings) {
|
|
31
|
-
const overlapRange = (0, common_1.getOverlapRange)(offsetRange.start, offsetRange.end, mapping.sourceOffsets[0], mapping.sourceOffsets[mapping.sourceOffsets.length - 1]
|
|
32
|
-
+ mapping.lengths[mapping.lengths.length - 1]);
|
|
33
|
-
if (overlapRange) {
|
|
34
|
-
const start = map.map.getGeneratedOffset(overlapRange.start)?.[0];
|
|
35
|
-
const end = map.map.getGeneratedOffset(overlapRange.end)?.[0];
|
|
36
|
-
if (start !== undefined && end !== undefined) {
|
|
37
|
-
minStart = minStart === undefined ? start : Math.min(start, minStart);
|
|
38
|
-
maxEnd = maxEnd === undefined ? end : Math.max(end, maxEnd);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if (minStart !== undefined && maxEnd !== undefined) {
|
|
43
|
-
yield {
|
|
44
|
-
range: {
|
|
45
|
-
start: map.virtualFileDocument.positionAt(minStart),
|
|
46
|
-
end: map.virtualFileDocument.positionAt(maxEnd),
|
|
47
|
-
},
|
|
48
|
-
codeActionContext: _codeActionContext,
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
31
|
}
|
|
52
32
|
}, async (service, document, { range, codeActionContext }) => {
|
|
53
33
|
if (token.isCancellationRequested) {
|
|
@@ -32,7 +32,7 @@ function register(context) {
|
|
|
32
32
|
if (!cacheData.service.provideCompletionItems) {
|
|
33
33
|
continue;
|
|
34
34
|
}
|
|
35
|
-
cacheData.list = await cacheData.service.provideCompletionItems(map.
|
|
35
|
+
cacheData.list = await cacheData.service.provideCompletionItems(map.embeddedDocument, mapped, completionContext, token);
|
|
36
36
|
if (!cacheData.list) {
|
|
37
37
|
continue;
|
|
38
38
|
}
|
|
@@ -45,10 +45,10 @@ function register(context) {
|
|
|
45
45
|
data: item.data,
|
|
46
46
|
},
|
|
47
47
|
serviceIndex,
|
|
48
|
-
virtualDocumentUri: map.
|
|
48
|
+
virtualDocumentUri: map.embeddedDocument.uri,
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
|
-
cacheData.list = (0, transform_1.transformCompletionList)(cacheData.list, range => map.getSourceRange(range), map.
|
|
51
|
+
cacheData.list = (0, transform_1.transformCompletionList)(cacheData.list, range => map.getSourceRange(range), map.embeddedDocument, context);
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
}
|
|
@@ -150,7 +150,7 @@ function register(context) {
|
|
|
150
150
|
_data = data;
|
|
151
151
|
return (0, language_core_1.isCompletionEnabled)(data);
|
|
152
152
|
})) {
|
|
153
|
-
await worker(map.
|
|
153
|
+
await worker(map.embeddedDocument, mappedPosition, map, _data);
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
156
|
}
|
|
@@ -70,17 +70,17 @@ function register(context, apiName, isValidPosition) {
|
|
|
70
70
|
}
|
|
71
71
|
foundTargetSelectionRange = true;
|
|
72
72
|
let targetRange = targetSourceMap.getSourceRange(link.targetRange);
|
|
73
|
-
link.targetUri = targetSourceMap.
|
|
73
|
+
link.targetUri = targetSourceMap.sourceDocument.uri;
|
|
74
74
|
// loose range mapping to for template slots, slot properties
|
|
75
75
|
link.targetRange = targetRange ?? targetSelectionRange;
|
|
76
76
|
link.targetSelectionRange = targetSelectionRange;
|
|
77
77
|
}
|
|
78
78
|
if (apiName === 'provideDefinition' && !foundTargetSelectionRange) {
|
|
79
79
|
for (const targetMap of context.documents.getMaps(targetVirtualFile)) {
|
|
80
|
-
if (targetMap && targetMap.
|
|
80
|
+
if (targetMap && targetMap.sourceDocument.uri !== uri) {
|
|
81
81
|
return {
|
|
82
82
|
...link,
|
|
83
|
-
targetUri: targetMap.
|
|
83
|
+
targetUri: targetMap.sourceDocument.uri,
|
|
84
84
|
targetRange: {
|
|
85
85
|
start: { line: 0, character: 0 },
|
|
86
86
|
end: { line: 0, character: 0 },
|
|
@@ -136,20 +136,7 @@ function register(context) {
|
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
138
|
async function collectErrors() {
|
|
139
|
-
|
|
140
|
-
exports.errorMarkups[uri] = [];
|
|
141
|
-
for (const error of errors) {
|
|
142
|
-
for (const service of context.services) {
|
|
143
|
-
if (context.disabledServicePlugins.has(service[1])) {
|
|
144
|
-
continue;
|
|
145
|
-
}
|
|
146
|
-
const markup = await service[1].provideDiagnosticMarkupContent?.(error, token);
|
|
147
|
-
if (markup) {
|
|
148
|
-
exports.errorMarkups[uri].push({ error, markup });
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return errors;
|
|
139
|
+
return Object.values(lastResponse).flatMap(({ errors }) => errors);
|
|
153
140
|
}
|
|
154
141
|
async function worker(api, cacheMap, cache) {
|
|
155
142
|
const result = await (0, featureWorkers_1.documentFeatureWorker)(context, uri, map => map.map.mappings.some(mapping => (0, language_core_1.isDiagnosticsEnabled)(mapping.data)), async (service, document) => {
|
|
@@ -168,8 +155,8 @@ function register(context) {
|
|
|
168
155
|
if (api !== 'provideSemanticDiagnostics' && cache && cache.documentVersion === document.version) {
|
|
169
156
|
return cache.errors;
|
|
170
157
|
}
|
|
171
|
-
const errors = await service[1][api]?.(document, token);
|
|
172
|
-
errors
|
|
158
|
+
const errors = await service[1][api]?.(document, token) || [];
|
|
159
|
+
errors.forEach(error => {
|
|
173
160
|
error.data = {
|
|
174
161
|
uri,
|
|
175
162
|
version: document.version,
|
|
@@ -218,7 +205,7 @@ function register(context) {
|
|
|
218
205
|
if (range) {
|
|
219
206
|
relatedInfos.push({
|
|
220
207
|
location: {
|
|
221
|
-
uri: map.
|
|
208
|
+
uri: map.sourceDocument.uri,
|
|
222
209
|
range,
|
|
223
210
|
},
|
|
224
211
|
message: info.message,
|
|
@@ -21,151 +21,132 @@ function register(context) {
|
|
|
21
21
|
};
|
|
22
22
|
if (!sourceFile.generated) {
|
|
23
23
|
return onTypeParams
|
|
24
|
-
? (await tryFormat(document, onTypeParams.position, onTypeParams.ch))?.edits
|
|
25
|
-
: (await tryFormat(document, range, undefined))?.edits;
|
|
24
|
+
? (await tryFormat(document, document, undefined, 0, onTypeParams.position, onTypeParams.ch))?.edits
|
|
25
|
+
: (await tryFormat(document, document, undefined, 0, range, undefined))?.edits;
|
|
26
26
|
}
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const isCodeBlock = code.mappings.length === 1
|
|
44
|
-
&& code.mappings[0].sourceOffsets.length === 1
|
|
45
|
-
&& code.mappings[0].generatedOffsets[0] === 0
|
|
46
|
-
&& code.mappings[0].lengths[0] === code.snapshot.getLength();
|
|
47
|
-
if (onTypeParams && !isCodeBlock) {
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
const docMap = createDocMap(code, uri, sourceFile.languageId, tempSourceSnapshot);
|
|
51
|
-
if (!docMap) {
|
|
52
|
-
continue;
|
|
53
|
-
}
|
|
54
|
-
let embeddedCodeResult;
|
|
55
|
-
if (onTypeParams) {
|
|
56
|
-
const embeddedPosition = docMap.getGeneratedPosition(onTypeParams.position);
|
|
57
|
-
if (embeddedPosition) {
|
|
58
|
-
embeddedCodeResult = await tryFormat(docMap.virtualFileDocument, embeddedPosition, onTypeParams.ch);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
embeddedCodeResult = await tryFormat(docMap.virtualFileDocument, {
|
|
63
|
-
start: docMap.virtualFileDocument.positionAt(0),
|
|
64
|
-
end: docMap.virtualFileDocument.positionAt(docMap.virtualFileDocument.getText().length),
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
if (!embeddedCodeResult) {
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
toPatchIndent.push({
|
|
71
|
-
virtualCodeId: code.id,
|
|
72
|
-
isCodeBlock,
|
|
73
|
-
service: embeddedCodeResult.service[1],
|
|
74
|
-
});
|
|
75
|
-
for (const textEdit of embeddedCodeResult.edits) {
|
|
76
|
-
const range = docMap.getSourceRange(textEdit.range);
|
|
77
|
-
if (range) {
|
|
78
|
-
edits.push({
|
|
79
|
-
newText: textEdit.newText,
|
|
80
|
-
range,
|
|
81
|
-
});
|
|
27
|
+
const embeddedRanges = new Map();
|
|
28
|
+
const startOffset = document.offsetAt(range.start);
|
|
29
|
+
const endOffset = document.offsetAt(range.end);
|
|
30
|
+
for (const code of (0, language_core_1.forEachEmbeddedCode)(sourceFile.generated.code)) {
|
|
31
|
+
for (const [sourceFileUri, [_snapshot, map]] of context.language.files.getMaps(code)) {
|
|
32
|
+
if (sourceFileUri === uri) {
|
|
33
|
+
const embeddedRange = (0, common_1.findOverlapCodeRange)(startOffset, endOffset, map, language_core_1.isFormattingEnabled);
|
|
34
|
+
if (embeddedRange) {
|
|
35
|
+
if (embeddedRange.start === map.mappings[0].generatedOffsets[0]) {
|
|
36
|
+
embeddedRange.start = 0;
|
|
37
|
+
}
|
|
38
|
+
const lastMapping = map.mappings[map.mappings.length - 1];
|
|
39
|
+
if (embeddedRange.end === lastMapping.generatedOffsets[lastMapping.generatedOffsets.length - 1] + lastMapping.lengths[lastMapping.lengths.length - 1]) {
|
|
40
|
+
embeddedRange.end = code.snapshot.getLength();
|
|
41
|
+
}
|
|
42
|
+
embeddedRanges.set(code.id, embeddedRange);
|
|
82
43
|
}
|
|
44
|
+
break;
|
|
83
45
|
}
|
|
84
46
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
const originalDocument = document;
|
|
50
|
+
let tempSourceSnapshot = sourceFile.snapshot;
|
|
51
|
+
let tempVirtualFile = context.language.files.set(sourceFile.id + '.tmp', sourceFile.languageId, sourceFile.snapshot, [sourceFile.generated.languagePlugin]).generated?.code;
|
|
52
|
+
if (!tempVirtualFile) {
|
|
53
|
+
return;
|
|
91
54
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const
|
|
95
|
-
if (
|
|
96
|
-
|
|
97
|
-
for (let line = edit.range.start.line; line <= edit.range.end.line; line++) {
|
|
98
|
-
editLines.add(line);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
55
|
+
let level = 0;
|
|
56
|
+
while (true) {
|
|
57
|
+
const embeddedCodes = (0, featureWorkers_1.getEmbeddedFilesByLevel)(context, sourceFile.id, tempVirtualFile, level++);
|
|
58
|
+
if (embeddedCodes.length === 0) {
|
|
59
|
+
break;
|
|
101
60
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
virtualCode = file;
|
|
107
|
-
break;
|
|
108
|
-
}
|
|
61
|
+
let edits = [];
|
|
62
|
+
for (const code of embeddedCodes) {
|
|
63
|
+
if (!code.mappings.some(mapping => (0, language_core_1.isFormattingEnabled)(mapping.data))) {
|
|
64
|
+
continue;
|
|
109
65
|
}
|
|
110
|
-
const docMap = createDocMap(
|
|
66
|
+
const docMap = createDocMap(code, uri, sourceFile.languageId, tempSourceSnapshot);
|
|
111
67
|
if (!docMap) {
|
|
112
68
|
continue;
|
|
113
69
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const lines = await service.provideFormattingIndentSensitiveLines(docMap.virtualFileDocument, token);
|
|
121
|
-
if (lines) {
|
|
122
|
-
for (const line of lines) {
|
|
123
|
-
const sourceLine = docMap.getSourcePosition({ line: line, character: 0 })?.line;
|
|
124
|
-
if (sourceLine !== undefined) {
|
|
125
|
-
indentSensitiveLines.add(sourceLine);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
70
|
+
let embeddedCodeResult;
|
|
71
|
+
let embeddedRange = embeddedRanges.get(code.id);
|
|
72
|
+
if (onTypeParams) {
|
|
73
|
+
const embeddedPosition = docMap.getGeneratedPosition(onTypeParams.position);
|
|
74
|
+
if (embeddedPosition) {
|
|
75
|
+
embeddedCodeResult = await tryFormat(docMap.sourceDocument, docMap.embeddedDocument, code, level, embeddedPosition, onTypeParams.ch);
|
|
129
76
|
}
|
|
130
77
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
78
|
+
else if (embeddedRange) {
|
|
79
|
+
embeddedCodeResult = await tryFormat(docMap.sourceDocument, docMap.embeddedDocument, code, level, {
|
|
80
|
+
start: docMap.embeddedDocument.positionAt(embeddedRange.start),
|
|
81
|
+
end: docMap.embeddedDocument.positionAt(embeddedRange.end),
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (!embeddedCodeResult) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
for (const textEdit of embeddedCodeResult.edits) {
|
|
88
|
+
const range = docMap.getSourceRange(textEdit.range);
|
|
89
|
+
if (range) {
|
|
90
|
+
edits.push({
|
|
91
|
+
newText: textEdit.newText,
|
|
92
|
+
range,
|
|
93
|
+
});
|
|
143
94
|
}
|
|
144
|
-
return true;
|
|
145
|
-
});
|
|
146
|
-
if (indentEdits.length > 0) {
|
|
147
|
-
const newText = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, indentEdits);
|
|
148
|
-
document = vscode_languageserver_textdocument_1.TextDocument.create(document.uri, document.languageId, document.version + 1, newText);
|
|
149
|
-
tempSourceSnapshot = (0, common_1.stringToSnapshot)(newText);
|
|
150
|
-
tempVirtualFile = sourceFile.generated.languagePlugin.updateVirtualCode(uri, tempVirtualFile, tempSourceSnapshot, context.language.files);
|
|
151
95
|
}
|
|
152
96
|
}
|
|
97
|
+
if (edits.length > 0) {
|
|
98
|
+
const newText = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, edits);
|
|
99
|
+
document = vscode_languageserver_textdocument_1.TextDocument.create(document.uri, document.languageId, document.version + 1, newText);
|
|
100
|
+
tempSourceSnapshot = (0, common_1.stringToSnapshot)(newText);
|
|
101
|
+
tempVirtualFile = context.language.files.set(sourceFile.id + '.tmp', sourceFile.languageId, tempSourceSnapshot, [sourceFile.generated.languagePlugin]).generated?.code;
|
|
102
|
+
if (!tempVirtualFile) {
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (document.getText() === originalDocument.getText()) {
|
|
108
|
+
return;
|
|
153
109
|
}
|
|
110
|
+
const editRange = {
|
|
111
|
+
start: originalDocument.positionAt(0),
|
|
112
|
+
end: originalDocument.positionAt(originalDocument.getText().length),
|
|
113
|
+
};
|
|
114
|
+
const textEdit = {
|
|
115
|
+
range: editRange,
|
|
116
|
+
newText: document.getText(),
|
|
117
|
+
};
|
|
118
|
+
return [textEdit];
|
|
154
119
|
}
|
|
155
|
-
|
|
156
|
-
|
|
120
|
+
finally {
|
|
121
|
+
context.language.files.delete(sourceFile.id + '.tmp');
|
|
157
122
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
123
|
+
async function tryFormat(sourceDocument, document, code, codeLevel, rangeOrPosition, ch) {
|
|
124
|
+
if (context.disabledVirtualFileUris.has(document.uri)) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
let codeOptions;
|
|
128
|
+
rangeOrPosition ??= {
|
|
129
|
+
start: document.positionAt(0),
|
|
130
|
+
end: document.positionAt(document.getText().length),
|
|
131
|
+
};
|
|
132
|
+
if (code) {
|
|
133
|
+
codeOptions = {
|
|
134
|
+
level: codeLevel - 1,
|
|
135
|
+
initialIndentLevel: 0,
|
|
136
|
+
};
|
|
137
|
+
if (code.mappings.length) {
|
|
138
|
+
const firstMapping = code.mappings[0];
|
|
139
|
+
const startOffset = firstMapping.sourceOffsets[0];
|
|
140
|
+
const startPosition = sourceDocument.positionAt(startOffset);
|
|
141
|
+
codeOptions.initialIndentLevel = computeInitialIndent(sourceDocument.getText(), sourceDocument.offsetAt({ line: startPosition.line, character: 0 }), options);
|
|
142
|
+
}
|
|
143
|
+
for (const service of context.services) {
|
|
144
|
+
if (context.disabledServicePlugins.has(service[1])) {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
codeOptions = await service[1].resolveEmbeddedCodeFormattingOptions?.(code, codeOptions, token) ?? codeOptions;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
169
150
|
for (const service of context.services) {
|
|
170
151
|
if (context.disabledServicePlugins.has(service[1])) {
|
|
171
152
|
continue;
|
|
@@ -175,13 +156,13 @@ function register(context) {
|
|
|
175
156
|
}
|
|
176
157
|
let edits;
|
|
177
158
|
try {
|
|
178
|
-
if (ch !== undefined && 'line' in
|
|
159
|
+
if (ch !== undefined && rangeOrPosition && 'line' in rangeOrPosition && 'character' in rangeOrPosition) {
|
|
179
160
|
if (service[0].autoFormatTriggerCharacters?.includes(ch)) {
|
|
180
|
-
edits = await service[1].provideOnTypeFormattingEdits?.(document,
|
|
161
|
+
edits = await service[1].provideOnTypeFormattingEdits?.(document, rangeOrPosition, ch, options, codeOptions, token);
|
|
181
162
|
}
|
|
182
163
|
}
|
|
183
|
-
else if (ch === undefined && 'start' in
|
|
184
|
-
edits = await service[1].provideDocumentFormattingEdits?.(document,
|
|
164
|
+
else if (ch === undefined && rangeOrPosition && 'start' in rangeOrPosition && 'end' in rangeOrPosition) {
|
|
165
|
+
edits = await service[1].provideDocumentFormattingEdits?.(document, rangeOrPosition, options, codeOptions, token);
|
|
185
166
|
}
|
|
186
167
|
}
|
|
187
168
|
catch (err) {
|
|
@@ -211,64 +192,22 @@ function register(context) {
|
|
|
211
192
|
}
|
|
212
193
|
}
|
|
213
194
|
exports.register = register;
|
|
214
|
-
function
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
for (let j = 0; j < mapping.sourceOffsets.length; j++) {
|
|
222
|
-
const firstLineIndent = getBaseIndent(mapping.sourceOffsets[j]);
|
|
223
|
-
const text = document.getText().substring(mapping.sourceOffsets[j], mapping.sourceOffsets[j] + mapping.lengths[j]);
|
|
224
|
-
const lines = text.split('\n');
|
|
225
|
-
const baseIndent = firstLineIndent + initialIndent;
|
|
226
|
-
let lineOffset = lines[0].length + 1;
|
|
227
|
-
let insertedFinalNewLine = false;
|
|
228
|
-
if (!text.trim()) {
|
|
229
|
-
continue;
|
|
230
|
-
}
|
|
231
|
-
if (isCodeBlock && text.trimStart().length === text.length) {
|
|
232
|
-
indentTextEdits.push({
|
|
233
|
-
newText: '\n' + baseIndent,
|
|
234
|
-
range: {
|
|
235
|
-
start: document.positionAt(mapping.sourceOffsets[j]),
|
|
236
|
-
end: document.positionAt(mapping.sourceOffsets[j]),
|
|
237
|
-
},
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
if (isCodeBlock && text.trimEnd().length === text.length) {
|
|
241
|
-
indentTextEdits.push({
|
|
242
|
-
newText: '\n',
|
|
243
|
-
range: {
|
|
244
|
-
start: document.positionAt(mapping.sourceOffsets[j] + mapping.lengths[j]),
|
|
245
|
-
end: document.positionAt(mapping.sourceOffsets[j] + mapping.lengths[j]),
|
|
246
|
-
},
|
|
247
|
-
});
|
|
248
|
-
insertedFinalNewLine = true;
|
|
249
|
-
}
|
|
250
|
-
if (baseIndent && lines.length > 1) {
|
|
251
|
-
for (let i = 1; i < lines.length; i++) {
|
|
252
|
-
if (lines[i].trim() || i === lines.length - 1) {
|
|
253
|
-
const isLastLine = i === lines.length - 1 && !insertedFinalNewLine;
|
|
254
|
-
indentTextEdits.push({
|
|
255
|
-
newText: isLastLine ? firstLineIndent : baseIndent,
|
|
256
|
-
range: {
|
|
257
|
-
start: document.positionAt(mapping.sourceOffsets[j] + lineOffset),
|
|
258
|
-
end: document.positionAt(mapping.sourceOffsets[j] + lineOffset),
|
|
259
|
-
},
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
lineOffset += lines[i].length + 1;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
195
|
+
function computeInitialIndent(content, i, options) {
|
|
196
|
+
let nChars = 0;
|
|
197
|
+
const tabSize = options.tabSize || 4;
|
|
198
|
+
while (i < content.length) {
|
|
199
|
+
const ch = content.charAt(i);
|
|
200
|
+
if (ch === ' ') {
|
|
201
|
+
nChars++;
|
|
265
202
|
}
|
|
203
|
+
else if (ch === '\t') {
|
|
204
|
+
nChars += tabSize;
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
i++;
|
|
266
210
|
}
|
|
267
|
-
return
|
|
268
|
-
function getBaseIndent(pos) {
|
|
269
|
-
const startPos = document.positionAt(pos);
|
|
270
|
-
const startLineText = document.getText({ start: { line: startPos.line, character: 0 }, end: startPos });
|
|
271
|
-
return startLineText.substring(0, startLineText.length - startLineText.trimStart().length);
|
|
272
|
-
}
|
|
211
|
+
return Math.floor(nChars / tabSize);
|
|
273
212
|
}
|
|
274
213
|
//# sourceMappingURL=provideDocumentFormattingEdits.js.map
|
|
@@ -20,32 +20,11 @@ function register(context) {
|
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
22
|
const tokens = await (0, featureWorkers_1.languageFeatureWorker)(context, uri, () => range, function* (map) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const end = document.offsetAt(range.end);
|
|
26
|
-
for (const mapping of map.map.mappings) {
|
|
27
|
-
if ((0, language_core_1.isSemanticTokensEnabled)(mapping.data)) {
|
|
28
|
-
for (let i = 0; i < mapping.sourceOffsets.length; i++) {
|
|
29
|
-
if (mapping.sourceOffsets[i] + mapping.lengths[i] > start
|
|
30
|
-
&& mapping.sourceOffsets[i] < end) {
|
|
31
|
-
if (!result) {
|
|
32
|
-
result = {
|
|
33
|
-
start: mapping.generatedOffsets[i],
|
|
34
|
-
end: mapping.generatedOffsets[i] + mapping.lengths[i],
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
result.start = Math.min(result.start, mapping.generatedOffsets[i]);
|
|
39
|
-
result.end = Math.max(result.end, mapping.generatedOffsets[i] + mapping.lengths[i]);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
if (result) {
|
|
23
|
+
const mapped = (0, common_1.findOverlapCodeRange)(map.sourceDocument.offsetAt(range.start), map.sourceDocument.offsetAt(range.end), map.map, language_core_1.isSemanticTokensEnabled);
|
|
24
|
+
if (mapped) {
|
|
46
25
|
yield {
|
|
47
|
-
start: map.
|
|
48
|
-
end: map.
|
|
26
|
+
start: map.embeddedDocument.positionAt(mapped.start),
|
|
27
|
+
end: map.embeddedDocument.positionAt(mapped.end),
|
|
49
28
|
};
|
|
50
29
|
}
|
|
51
30
|
}, (service, document, range) => {
|
|
@@ -22,7 +22,7 @@ function register(context) {
|
|
|
22
22
|
for (const map of context.documents.getMaps(virtualCode)) {
|
|
23
23
|
const range = map.getSourceRange(reference.range, language_core_1.isReferencesEnabled);
|
|
24
24
|
if (range) {
|
|
25
|
-
reference.uri = map.
|
|
25
|
+
reference.uri = map.sourceDocument.uri;
|
|
26
26
|
reference.range = range;
|
|
27
27
|
return reference;
|
|
28
28
|
}
|
|
@@ -12,36 +12,12 @@ function register(context) {
|
|
|
12
12
|
if (!sourceFile) {
|
|
13
13
|
return;
|
|
14
14
|
}
|
|
15
|
-
const document = context.documents.get(uri, sourceFile.languageId, sourceFile.snapshot);
|
|
16
|
-
const offsetRange = {
|
|
17
|
-
start: document.offsetAt(range.start),
|
|
18
|
-
end: document.offsetAt(range.end),
|
|
19
|
-
};
|
|
20
15
|
return (0, featureWorkers_1.languageFeatureWorker)(context, uri, () => range, function* (map) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
*/
|
|
24
|
-
if (!map.map.mappings.some(mapping => (0, language_core_1.isInlayHintsEnabled)(mapping.data))) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
let minStart;
|
|
28
|
-
let maxEnd;
|
|
29
|
-
for (const mapping of map.map.mappings) {
|
|
30
|
-
const overlapRange = (0, common_1.getOverlapRange)(offsetRange.start, offsetRange.end, mapping.sourceOffsets[0], mapping.sourceOffsets[mapping.sourceOffsets.length - 1]
|
|
31
|
-
+ mapping.lengths[mapping.lengths.length - 1]);
|
|
32
|
-
if (overlapRange) {
|
|
33
|
-
const start = map.map.getGeneratedOffset(overlapRange.start)?.[0];
|
|
34
|
-
const end = map.map.getGeneratedOffset(overlapRange.end)?.[0];
|
|
35
|
-
if (start !== undefined && end !== undefined) {
|
|
36
|
-
minStart = minStart === undefined ? start : Math.min(start, minStart);
|
|
37
|
-
maxEnd = maxEnd === undefined ? end : Math.max(end, maxEnd);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
if (minStart !== undefined && maxEnd !== undefined) {
|
|
16
|
+
const mapped = (0, common_1.findOverlapCodeRange)(map.sourceDocument.offsetAt(range.start), map.sourceDocument.offsetAt(range.end), map.map, language_core_1.isInlayHintsEnabled);
|
|
17
|
+
if (mapped) {
|
|
42
18
|
yield {
|
|
43
|
-
start: map.
|
|
44
|
-
end: map.
|
|
19
|
+
start: map.embeddedDocument.positionAt(mapped.start),
|
|
20
|
+
end: map.embeddedDocument.positionAt(mapped.end),
|
|
45
21
|
};
|
|
46
22
|
}
|
|
47
23
|
}, async (service, document, arg) => {
|
|
@@ -67,7 +43,7 @@ function register(context) {
|
|
|
67
43
|
.map((_inlayHint) => {
|
|
68
44
|
const position = map.getSourcePosition(_inlayHint.position, language_core_1.isInlayHintsEnabled);
|
|
69
45
|
const edits = _inlayHint.textEdits
|
|
70
|
-
?.map(textEdit => (0, transform_1.transformTextEdit)(textEdit, range => map.getSourceRange(range), map.
|
|
46
|
+
?.map(textEdit => (0, transform_1.transformTextEdit)(textEdit, range => map.getSourceRange(range), map.embeddedDocument))
|
|
71
47
|
.filter(common_1.notEmpty);
|
|
72
48
|
if (position) {
|
|
73
49
|
return {
|
|
@@ -41,17 +41,14 @@ function register(context) {
|
|
|
41
41
|
}
|
|
42
42
|
return 0;
|
|
43
43
|
});
|
|
44
|
-
for (let
|
|
45
|
-
let
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
for (let j = 1; j < pluginResults.length; j++) {
|
|
45
|
+
let top = pluginResults[j - 1];
|
|
46
|
+
const parent = pluginResults[j];
|
|
47
|
+
while (top.parent && (0, common_1.isInsideRange)(parent.range, top.parent.range) && !(0, common_1.isEqualRange)(parent.range, top.parent.range)) {
|
|
48
|
+
top = top.parent;
|
|
48
49
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
parent = parent.parent;
|
|
52
|
-
}
|
|
53
|
-
if (parent) {
|
|
54
|
-
root.parent = parent;
|
|
50
|
+
if (top) {
|
|
51
|
+
top.parent = parent;
|
|
55
52
|
}
|
|
56
53
|
}
|
|
57
54
|
result.push(pluginResults[0]);
|
|
@@ -27,7 +27,7 @@ function register(context) {
|
|
|
27
27
|
for (const map of context.documents.getMaps(virtualCode)) {
|
|
28
28
|
const range = map.getSourceRange(loc.range);
|
|
29
29
|
if (range) {
|
|
30
|
-
return { uri: map.
|
|
30
|
+
return { uri: map.sourceDocument.uri, range };
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
}
|
|
@@ -17,7 +17,7 @@ function register(context) {
|
|
|
17
17
|
if (virtualCode) {
|
|
18
18
|
for (const map of context.documents.getMaps(virtualCode)) {
|
|
19
19
|
item = await service[1].resolveCompletionItem(item, token);
|
|
20
|
-
item = service[1].transformCompletionItem?.(item) ?? (0, transform_1.transformCompletionItem)(item, embeddedRange => map.getSourceRange(embeddedRange), map.
|
|
20
|
+
item = service[1].transformCompletionItem?.(item) ?? (0, transform_1.transformCompletionItem)(item, embeddedRange => map.getSourceRange(embeddedRange), map.embeddedDocument, context);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
}
|
package/lib/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { LanguageContext } from '@volar/language-core';
|
|
1
|
+
import type { LanguageContext, VirtualCode } from '@volar/language-core';
|
|
2
2
|
import type * as vscode from 'vscode-languageserver-protocol';
|
|
3
3
|
import type { TextDocument } from 'vscode-languageserver-textdocument';
|
|
4
4
|
import type { DocumentProvider } from './documents';
|
|
@@ -17,12 +17,6 @@ export interface ServiceEnvironment {
|
|
|
17
17
|
onDidChangeConfiguration?(cb: () => void): vscode.Disposable;
|
|
18
18
|
onDidChangeWatchedFiles?(cb: (params: vscode.DidChangeWatchedFilesParams) => void): vscode.Disposable;
|
|
19
19
|
}
|
|
20
|
-
export interface Console {
|
|
21
|
-
error(message: string): void;
|
|
22
|
-
info(message: string): void;
|
|
23
|
-
log(message: string): void;
|
|
24
|
-
warn(message: string): void;
|
|
25
|
-
}
|
|
26
20
|
export interface FileSystem {
|
|
27
21
|
stat(uri: string): Result<FileStat | undefined>;
|
|
28
22
|
readDirectory(uri: string): Result<[string, FileType][]>;
|
|
@@ -61,13 +55,17 @@ export interface ServiceContext {
|
|
|
61
55
|
export type Result<T> = T | Thenable<T>;
|
|
62
56
|
export type NullableResult<T> = Result<T | undefined | null>;
|
|
63
57
|
export type SemanticToken = [number, number, number, number, number];
|
|
64
|
-
export interface ServicePlugin {
|
|
58
|
+
export interface ServicePlugin<P = any> {
|
|
65
59
|
name?: string;
|
|
66
60
|
triggerCharacters?: string[];
|
|
67
61
|
signatureHelpTriggerCharacters?: string[];
|
|
68
62
|
signatureHelpRetriggerCharacters?: string[];
|
|
69
63
|
autoFormatTriggerCharacters?: string[];
|
|
70
|
-
create(context: ServiceContext): ServicePluginInstance
|
|
64
|
+
create(context: ServiceContext): ServicePluginInstance<P>;
|
|
65
|
+
}
|
|
66
|
+
export interface EmbeddedCodeFormattingOptions {
|
|
67
|
+
level: number;
|
|
68
|
+
initialIndentLevel: number;
|
|
71
69
|
}
|
|
72
70
|
export interface ServicePluginInstance<P = any> {
|
|
73
71
|
provide?: P;
|
|
@@ -81,8 +79,8 @@ export interface ServicePluginInstance<P = any> {
|
|
|
81
79
|
provideImplementation?(document: TextDocument, position: vscode.Position, token: vscode.CancellationToken): NullableResult<vscode.LocationLink[]>;
|
|
82
80
|
provideCodeLenses?(document: TextDocument, token: vscode.CancellationToken): NullableResult<vscode.CodeLens[]>;
|
|
83
81
|
provideCodeActions?(document: TextDocument, range: vscode.Range, context: vscode.CodeActionContext, token: vscode.CancellationToken): NullableResult<vscode.CodeAction[]>;
|
|
84
|
-
provideDocumentFormattingEdits?(document: TextDocument, range: vscode.Range, options: vscode.FormattingOptions, token: vscode.CancellationToken): NullableResult<vscode.TextEdit[]>;
|
|
85
|
-
provideOnTypeFormattingEdits?(document: TextDocument, position: vscode.Position, key: string, options: vscode.FormattingOptions, token: vscode.CancellationToken): NullableResult<vscode.TextEdit[]>;
|
|
82
|
+
provideDocumentFormattingEdits?(document: TextDocument, range: vscode.Range, options: vscode.FormattingOptions, embeddedCodeContext: EmbeddedCodeFormattingOptions | undefined, token: vscode.CancellationToken): NullableResult<vscode.TextEdit[]>;
|
|
83
|
+
provideOnTypeFormattingEdits?(document: TextDocument, position: vscode.Position, key: string, options: vscode.FormattingOptions, embeddedCodeContext: EmbeddedCodeFormattingOptions | undefined, token: vscode.CancellationToken): NullableResult<vscode.TextEdit[]>;
|
|
86
84
|
provideDocumentLinks?(document: TextDocument, token: vscode.CancellationToken): NullableResult<vscode.DocumentLink[]>;
|
|
87
85
|
provideCompletionItems?(document: TextDocument, position: vscode.Position, context: vscode.CompletionContext, token: vscode.CancellationToken): NullableResult<vscode.CompletionList>;
|
|
88
86
|
provideDocumentColors?(document: TextDocument, token: vscode.CancellationToken): NullableResult<vscode.ColorInformation[]>;
|
|
@@ -106,7 +104,6 @@ export interface ServicePluginInstance<P = any> {
|
|
|
106
104
|
provideWorkspaceSymbols?(query: string, token: vscode.CancellationToken): NullableResult<vscode.WorkspaceSymbol[]>;
|
|
107
105
|
provideDiagnostics?(document: TextDocument, token: vscode.CancellationToken): NullableResult<vscode.Diagnostic[]>;
|
|
108
106
|
provideSemanticDiagnostics?(document: TextDocument, token: vscode.CancellationToken): NullableResult<vscode.Diagnostic[]>;
|
|
109
|
-
provideDiagnosticMarkupContent?(diagnostic: vscode.Diagnostic, token: vscode.CancellationToken): NullableResult<vscode.MarkupContent>;
|
|
110
107
|
provideFileReferences?(document: TextDocument, token: vscode.CancellationToken): NullableResult<vscode.Location[]>;
|
|
111
108
|
provideReferencesCodeLensRanges?(document: TextDocument, token: vscode.CancellationToken): NullableResult<vscode.Range[]>;
|
|
112
109
|
provideAutoInsertionEdit?(document: TextDocument, position: vscode.Position, lastChange: {
|
|
@@ -114,13 +111,13 @@ export interface ServicePluginInstance<P = any> {
|
|
|
114
111
|
text: string;
|
|
115
112
|
}, token: vscode.CancellationToken): NullableResult<string | vscode.TextEdit>;
|
|
116
113
|
provideFileRenameEdits?(oldUri: string, newUri: string, token: vscode.CancellationToken): NullableResult<vscode.WorkspaceEdit>;
|
|
117
|
-
provideFormattingIndentSensitiveLines?(document: TextDocument, token: vscode.CancellationToken): NullableResult<number[]>;
|
|
118
114
|
provideDocumentDropEdits?(document: TextDocument, position: vscode.Position, dataTransfer: Map<string, DataTransferItem>, token: vscode.CancellationToken): NullableResult<DocumentDropEdit>;
|
|
119
115
|
resolveCodeLens?(codeLens: vscode.CodeLens, token: vscode.CancellationToken): Result<vscode.CodeLens>;
|
|
120
116
|
resolveCodeAction?(codeAction: vscode.CodeAction, token: vscode.CancellationToken): Result<vscode.CodeAction>;
|
|
121
117
|
resolveCompletionItem?(item: vscode.CompletionItem, token: vscode.CancellationToken): Result<vscode.CompletionItem>;
|
|
122
118
|
resolveDocumentLink?(link: vscode.DocumentLink, token: vscode.CancellationToken): Result<vscode.DocumentLink>;
|
|
123
119
|
resolveInlayHint?(inlayHint: vscode.InlayHint, token: vscode.CancellationToken): Result<vscode.InlayHint>;
|
|
120
|
+
resolveEmbeddedCodeFormattingOptions?(code: VirtualCode, options: EmbeddedCodeFormattingOptions, token: vscode.CancellationToken): NullableResult<EmbeddedCodeFormattingOptions>;
|
|
124
121
|
transformCompletionItem?(item: vscode.CompletionItem): vscode.CompletionItem | undefined;
|
|
125
122
|
transformCodeAction?(item: vscode.CodeAction): vscode.CodeAction | undefined;
|
|
126
123
|
dispose?(): void;
|
package/lib/utils/common.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import type * as ts from 'typescript';
|
|
2
2
|
import type * as vscode from 'vscode-languageserver-protocol';
|
|
3
|
-
|
|
3
|
+
import type { CodeInformation, SourceMap } from '@volar/language-core';
|
|
4
|
+
export declare function findOverlapCodeRange(start: number, end: number, map: SourceMap<CodeInformation>, filter: (data: CodeInformation) => boolean): {
|
|
4
5
|
start: number;
|
|
5
6
|
end: number;
|
|
6
7
|
} | undefined;
|
|
7
8
|
export declare function isInsideRange(parent: vscode.Range, child: vscode.Range): boolean;
|
|
9
|
+
export declare function isEqualRange(a: vscode.Range, b: vscode.Range): boolean;
|
|
8
10
|
export declare function stringToSnapshot(str: string): ts.IScriptSnapshot;
|
|
9
11
|
export declare function sleep(ms: number): Promise<unknown>;
|
|
10
12
|
export declare function notEmpty<T>(value: T | null | undefined): value is T;
|
package/lib/utils/common.js
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.notEmpty = exports.sleep = exports.stringToSnapshot = exports.isInsideRange = exports.
|
|
3
|
+
exports.notEmpty = exports.sleep = exports.stringToSnapshot = exports.isEqualRange = exports.isInsideRange = exports.findOverlapCodeRange = void 0;
|
|
4
|
+
function findOverlapCodeRange(start, end, map, filter) {
|
|
5
|
+
let mappedStart;
|
|
6
|
+
let mappedEnd;
|
|
7
|
+
for (const [mapped, mapping] of map.getGeneratedOffsets(start)) {
|
|
8
|
+
if (filter(mapping.data)) {
|
|
9
|
+
mappedStart = mapped;
|
|
10
|
+
break;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
for (const [mapped, mapping] of map.getGeneratedOffsets(end)) {
|
|
14
|
+
if (filter(mapping.data)) {
|
|
15
|
+
mappedEnd = mapped;
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (mappedStart === undefined || mappedEnd === undefined) {
|
|
20
|
+
for (const mapping of map.mappings) {
|
|
21
|
+
if (filter(mapping.data)) {
|
|
22
|
+
const mappingStart = mapping.sourceOffsets[0];
|
|
23
|
+
const mappingEnd = mapping.sourceOffsets[mapping.sourceOffsets.length - 1] + mapping.lengths[mapping.lengths.length - 1];
|
|
24
|
+
const overlap = getOverlapRange(start, end, mappingStart, mappingEnd);
|
|
25
|
+
if (overlap) {
|
|
26
|
+
if (mappedStart === undefined) {
|
|
27
|
+
mappedStart = overlap.start + mapping.generatedOffsets[0] - mappingStart;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
mappedStart = Math.min(mappedStart, overlap.start + mapping.generatedOffsets[0] - mappingStart);
|
|
31
|
+
}
|
|
32
|
+
if (mappedEnd === undefined) {
|
|
33
|
+
mappedEnd = overlap.end + mapping.generatedOffsets[0] - mappingStart;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
mappedEnd = Math.max(mappedEnd, overlap.end + mapping.generatedOffsets[0] - mappingStart);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (mappedStart !== undefined && mappedEnd !== undefined) {
|
|
43
|
+
return {
|
|
44
|
+
start: mappedStart,
|
|
45
|
+
end: mappedEnd,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.findOverlapCodeRange = findOverlapCodeRange;
|
|
4
50
|
function getOverlapRange(range1Start, range1End, range2Start, range2End) {
|
|
5
51
|
const start = Math.max(range1Start, range2Start);
|
|
6
52
|
const end = Math.min(range1End, range2End);
|
|
@@ -12,7 +58,6 @@ function getOverlapRange(range1Start, range1End, range2Start, range2End) {
|
|
|
12
58
|
end,
|
|
13
59
|
};
|
|
14
60
|
}
|
|
15
|
-
exports.getOverlapRange = getOverlapRange;
|
|
16
61
|
function isInsideRange(parent, child) {
|
|
17
62
|
if (child.start.line < parent.start.line) {
|
|
18
63
|
return false;
|
|
@@ -29,6 +74,13 @@ function isInsideRange(parent, child) {
|
|
|
29
74
|
return true;
|
|
30
75
|
}
|
|
31
76
|
exports.isInsideRange = isInsideRange;
|
|
77
|
+
function isEqualRange(a, b) {
|
|
78
|
+
return a.start.line === b.start.line
|
|
79
|
+
&& a.start.character === b.start.character
|
|
80
|
+
&& a.end.line === b.end.line
|
|
81
|
+
&& a.end.character === b.end.character;
|
|
82
|
+
}
|
|
83
|
+
exports.isEqualRange = isEqualRange;
|
|
32
84
|
function stringToSnapshot(str) {
|
|
33
85
|
return {
|
|
34
86
|
getText: (start, end) => str.substring(start, end),
|
|
@@ -5,5 +5,5 @@ import type { ServiceContext, ServicePluginInstance, ServicePlugin } from '../ty
|
|
|
5
5
|
export declare function documentFeatureWorker<T>(context: ServiceContext, uri: string, valid: (map: SourceMapWithDocuments<CodeInformation>) => boolean, worker: (service: [ServicePlugin, ServicePluginInstance], document: TextDocument) => Thenable<T | null | undefined> | T | null | undefined, transformResult: (result: T, map?: SourceMapWithDocuments<CodeInformation>) => T | undefined, combineResult?: (results: T[]) => T): Promise<T | undefined>;
|
|
6
6
|
export declare function languageFeatureWorker<T, K>(context: ServiceContext, uri: string, getReadDocParams: () => K, eachVirtualDocParams: (map: SourceMapWithDocuments<CodeInformation>) => Generator<K>, worker: (service: [ServicePlugin, ServicePluginInstance], document: TextDocument, params: K, map?: SourceMapWithDocuments<CodeInformation>) => Thenable<T | null | undefined> | T | null | undefined, transformResult: (result: T, map?: SourceMapWithDocuments<CodeInformation>) => T | undefined, combineResult?: (results: T[]) => T): Promise<T | undefined>;
|
|
7
7
|
export declare function safeCall<T>(cb: () => Thenable<T> | T, errorMsg?: string): Promise<T | undefined>;
|
|
8
|
-
export declare function eachEmbeddedDocument(context: ServiceContext, current: VirtualCode, rootCode?: VirtualCode
|
|
9
|
-
export declare function getEmbeddedFilesByLevel(context: ServiceContext, sourceFileUri: string, rootFile: VirtualCode, level: number): VirtualCode
|
|
8
|
+
export declare function eachEmbeddedDocument(context: ServiceContext, current: VirtualCode, rootCode?: VirtualCode): Generator<SourceMapWithDocuments<CodeInformation>>;
|
|
9
|
+
export declare function getEmbeddedFilesByLevel(context: ServiceContext, sourceFileUri: string, rootFile: VirtualCode, level: number): VirtualCode[];
|
|
@@ -22,7 +22,7 @@ async function languageFeatureWorker(context, uri, getReadDocParams, eachVirtual
|
|
|
22
22
|
if (context.disabledServicePlugins.has(service[1])) {
|
|
23
23
|
continue;
|
|
24
24
|
}
|
|
25
|
-
const embeddedResult = await safeCall(() => worker(service, map.
|
|
25
|
+
const embeddedResult = await safeCall(() => worker(service, map.embeddedDocument, mappedArg, map), 'service ' + serviceId + ' crashed on ' + map.embeddedDocument.uri);
|
|
26
26
|
if (!embeddedResult) {
|
|
27
27
|
continue;
|
|
28
28
|
}
|
|
@@ -77,11 +77,13 @@ async function safeCall(cb, errorMsg) {
|
|
|
77
77
|
}
|
|
78
78
|
exports.safeCall = safeCall;
|
|
79
79
|
function* eachEmbeddedDocument(context, current, rootCode = current) {
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
if (current.embeddedCodes) {
|
|
81
|
+
for (const embeddedCode of current.embeddedCodes) {
|
|
82
|
+
yield* eachEmbeddedDocument(context, embeddedCode, rootCode);
|
|
83
|
+
}
|
|
82
84
|
}
|
|
83
85
|
for (const map of context.documents.getMaps(current)) {
|
|
84
|
-
const sourceFile = context.language.files.get(map.
|
|
86
|
+
const sourceFile = context.language.files.get(map.sourceDocument.uri);
|
|
85
87
|
if (sourceFile?.generated?.code === rootCode
|
|
86
88
|
&& !context.disabledVirtualFileUris.has(context.documents.getVirtualCodeUri(context.language.files.getByVirtualCode(current).id, current.id))) {
|
|
87
89
|
yield map;
|
|
@@ -95,9 +97,15 @@ function getEmbeddedFilesByLevel(context, sourceFileUri, rootFile, level) {
|
|
|
95
97
|
if (embeddedFilesByLevel.length > level) {
|
|
96
98
|
return embeddedFilesByLevel[level];
|
|
97
99
|
}
|
|
98
|
-
|
|
100
|
+
const nextLevel = [];
|
|
99
101
|
for (const file of embeddedFilesByLevel[embeddedFilesByLevel.length - 1]) {
|
|
100
|
-
|
|
102
|
+
if (file.embeddedCodes) {
|
|
103
|
+
for (const embedded of file.embeddedCodes) {
|
|
104
|
+
if (!context.disabledVirtualFileUris.has(context.documents.getVirtualCodeUri(sourceFileUri, embedded.id))) {
|
|
105
|
+
nextLevel.push(embedded);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
101
109
|
}
|
|
102
110
|
embeddedFilesByLevel.push(nextLevel);
|
|
103
111
|
}
|
package/lib/utils/transform.js
CHANGED
|
@@ -13,7 +13,7 @@ function transformDocumentLinkTarget(target, context) {
|
|
|
13
13
|
if (!map.map.mappings.some(mapping => (0, language_core_1.isDocumentLinkEnabled)(mapping.data))) {
|
|
14
14
|
continue;
|
|
15
15
|
}
|
|
16
|
-
target = map.
|
|
16
|
+
target = map.sourceDocument.uri;
|
|
17
17
|
const hash = targetUri.fragment;
|
|
18
18
|
const range = hash.match(/^L(\d+)(,(\d+))?(-L(\d+)(,(\d+))?)?$/);
|
|
19
19
|
if (range) {
|
|
@@ -270,7 +270,7 @@ function transformWorkspaceEdit(edit, { documents }, mode, versions = {}) {
|
|
|
270
270
|
if (virtualCode) {
|
|
271
271
|
for (const map of documents.getMaps(virtualCode)) {
|
|
272
272
|
// TODO: check capability?
|
|
273
|
-
const uri = map.
|
|
273
|
+
const uri = map.sourceDocument.uri;
|
|
274
274
|
sourceResult.changeAnnotations[uri] = tsAnno;
|
|
275
275
|
}
|
|
276
276
|
}
|
|
@@ -292,8 +292,8 @@ function transformWorkspaceEdit(edit, { documents }, mode, versions = {}) {
|
|
|
292
292
|
return (0, language_core_1.isRenameEnabled)(data);
|
|
293
293
|
});
|
|
294
294
|
if (range) {
|
|
295
|
-
sourceResult.changes[map.
|
|
296
|
-
sourceResult.changes[map.
|
|
295
|
+
sourceResult.changes[map.sourceDocument.uri] ??= [];
|
|
296
|
+
sourceResult.changes[map.sourceDocument.uri].push({
|
|
297
297
|
newText: (0, language_core_1.resolveRenameEditText)(tsEdit.newText, _data),
|
|
298
298
|
range,
|
|
299
299
|
});
|
|
@@ -303,8 +303,8 @@ function transformWorkspaceEdit(edit, { documents }, mode, versions = {}) {
|
|
|
303
303
|
else {
|
|
304
304
|
const range = map.getSourceRange(tsEdit.range);
|
|
305
305
|
if (range) {
|
|
306
|
-
sourceResult.changes[map.
|
|
307
|
-
sourceResult.changes[map.
|
|
306
|
+
sourceResult.changes[map.sourceDocument.uri] ??= [];
|
|
307
|
+
sourceResult.changes[map.sourceDocument.uri].push({ newText: tsEdit.newText, range });
|
|
308
308
|
hasResult = true;
|
|
309
309
|
}
|
|
310
310
|
}
|
|
@@ -326,8 +326,8 @@ function transformWorkspaceEdit(edit, { documents }, mode, versions = {}) {
|
|
|
326
326
|
for (const map of documents.getMaps(virtualCode)) {
|
|
327
327
|
sourceEdit = {
|
|
328
328
|
textDocument: {
|
|
329
|
-
uri: map.
|
|
330
|
-
version: versions[map.
|
|
329
|
+
uri: map.sourceDocument.uri,
|
|
330
|
+
version: versions[map.sourceDocument.uri] ?? null,
|
|
331
331
|
},
|
|
332
332
|
edits: [],
|
|
333
333
|
};
|
|
@@ -377,7 +377,7 @@ function transformWorkspaceEdit(edit, { documents }, mode, versions = {}) {
|
|
|
377
377
|
// TODO: check capability?
|
|
378
378
|
sourceEdit = {
|
|
379
379
|
kind: 'rename',
|
|
380
|
-
oldUri: map.
|
|
380
|
+
oldUri: map.sourceDocument.uri,
|
|
381
381
|
newUri: tsDocEdit.newUri /* TODO: remove .ts? */,
|
|
382
382
|
options: tsDocEdit.options,
|
|
383
383
|
annotationId: tsDocEdit.annotationId,
|
|
@@ -395,7 +395,7 @@ function transformWorkspaceEdit(edit, { documents }, mode, versions = {}) {
|
|
|
395
395
|
// TODO: check capability?
|
|
396
396
|
sourceEdit = {
|
|
397
397
|
kind: 'delete',
|
|
398
|
-
uri: map.
|
|
398
|
+
uri: map.sourceDocument.uri,
|
|
399
399
|
options: tsDocEdit.options,
|
|
400
400
|
annotationId: tsDocEdit.annotationId,
|
|
401
401
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@volar/language-service",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"**/*.js",
|
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
"directory": "packages/language-service"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@volar/language-core": "2.
|
|
15
|
+
"@volar/language-core": "2.1.1",
|
|
16
16
|
"vscode-languageserver-protocol": "^3.17.5",
|
|
17
17
|
"vscode-languageserver-textdocument": "^1.0.11",
|
|
18
18
|
"vscode-uri": "^3.0.8"
|
|
19
19
|
},
|
|
20
|
-
"gitHead": "
|
|
20
|
+
"gitHead": "e56916097fe7be2920aab6592a564a8e2ceafd14"
|
|
21
21
|
}
|