@volar/language-service 2.0.4 → 2.1.0

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.
@@ -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
- sourceFileDocument: TextDocument;
8
- virtualFileDocument: TextDocument;
7
+ sourceDocument: TextDocument;
8
+ embeddedDocument: TextDocument;
9
9
  map: SourceMap<Data>;
10
- constructor(sourceFileDocument: TextDocument, virtualFileDocument: TextDocument, map: SourceMap<Data>);
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<string>, import("@volar/language-core").SourceFile] | readonly [undefined, undefined];
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(sourceFileDocument, virtualFileDocument, map) {
8
- this.sourceFileDocument = sourceFileDocument;
9
- this.virtualFileDocument = virtualFileDocument;
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.virtualFileDocument, this.sourceFileDocument, 'generatedOffsets', 'sourceOffsets')) {
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.sourceFileDocument, this.virtualFileDocument, 'sourceOffsets', 'generatedOffsets')) {
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.virtualFileDocument.offsetAt(position), mapping.generatedOffsets, mapping.sourceOffsets, mapping.lengths);
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.sourceFileDocument.positionAt(offset);
92
+ return this.sourceDocument.positionAt(offset);
93
93
  }
94
94
  }
95
95
  matchGeneratedPosition(position, mapping) {
96
- let offset = (0, language_core_1.translateOffset)(this.sourceFileDocument.offsetAt(position), mapping.sourceOffsets, mapping.generatedOffsets, mapping.lengths);
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.virtualFileDocument.positionAt(offset);
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?.virtualFileDocument.uri,
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.sourceFileDocument.positionAt(0),
131
- end: map.sourceFileDocument.positionAt(map.sourceFileDocument.getText().length),
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.virtualFileDocument.uri.substring(map.virtualFileDocument.uri.lastIndexOf('/') + 1)
142
- ? map.sourceFileDocument.uri.substring(map.sourceFileDocument.uri.lastIndexOf('/') + 1)
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.sourceFileDocument.uri,
144
+ uri: map.sourceDocument.uri,
145
145
  // TS Bug: `range: range` not works
146
146
  range: {
147
147
  start: range.start,
@@ -42,8 +42,8 @@ function register(context) {
42
42
  if (minStart !== undefined && maxEnd !== undefined) {
43
43
  yield {
44
44
  range: {
45
- start: map.virtualFileDocument.positionAt(minStart),
46
- end: map.virtualFileDocument.positionAt(maxEnd),
45
+ start: map.embeddedDocument.positionAt(minStart),
46
+ end: map.embeddedDocument.positionAt(maxEnd),
47
47
  },
48
48
  codeActionContext: _codeActionContext,
49
49
  };
@@ -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.virtualFileDocument, mapped, completionContext, token);
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.virtualFileDocument.uri,
48
+ virtualDocumentUri: map.embeddedDocument.uri,
49
49
  };
50
50
  }
51
- cacheData.list = (0, transform_1.transformCompletionList)(cacheData.list, range => map.getSourceRange(range), map.virtualFileDocument, context);
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.virtualFileDocument, mappedPosition, map, _data);
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.sourceFileDocument.uri;
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.sourceFileDocument.uri !== uri) {
80
+ if (targetMap && targetMap.sourceDocument.uri !== uri) {
81
81
  return {
82
82
  ...link,
83
- targetUri: targetMap.sourceFileDocument.uri,
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
- const errors = Object.values(lastResponse).flatMap(({ errors }) => errors);
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?.forEach(error => {
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.sourceFileDocument.uri,
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 initialIndentLanguageId = await context.env.getConfiguration?.('volar.format.initialIndent') ?? { html: true };
28
- let tempSourceSnapshot = sourceFile.snapshot;
29
- let tempVirtualFile = sourceFile.generated.languagePlugin.createVirtualCode(uri, sourceFile.languageId, sourceFile.snapshot, context.language.files);
30
- const originalDocument = document;
31
- let level = 0;
32
- while (true) {
33
- const embeddedCodes = (0, featureWorkers_1.getEmbeddedFilesByLevel)(context, sourceFile.id, tempVirtualFile, level++);
34
- if (embeddedCodes.length === 0) {
35
- break;
36
- }
37
- let edits = [];
38
- const toPatchIndent = [];
39
- for (const code of embeddedCodes) {
40
- if (!code.mappings.some(mapping => (0, language_core_1.isFormattingEnabled)(mapping.data))) {
41
- continue;
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
- edits = edits.filter(edit => (0, common_1.isInsideRange)(range, edit.range));
86
- if (edits.length > 0) {
87
- const newText = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, edits);
88
- document = vscode_languageserver_textdocument_1.TextDocument.create(document.uri, document.languageId, document.version + 1, newText);
89
- tempSourceSnapshot = (0, common_1.stringToSnapshot)(newText);
90
- tempVirtualFile = sourceFile.generated.languagePlugin.updateVirtualCode(uri, tempVirtualFile, tempSourceSnapshot, context.language.files);
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
- if (level > 1) {
93
- const baseIndent = options.insertSpaces ? ' '.repeat(options.tabSize) : '\t';
94
- const editLines = new Set();
95
- if (onTypeParams) {
96
- for (const edit of edits) {
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
- for (const item of toPatchIndent) {
103
- let virtualCode;
104
- for (const file of (0, language_core_1.forEachEmbeddedCode)(tempVirtualFile)) {
105
- if (file.id === item.virtualCodeId) {
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(virtualCode, uri, sourceFile.languageId, tempSourceSnapshot);
66
+ const docMap = createDocMap(code, uri, sourceFile.languageId, tempSourceSnapshot);
111
67
  if (!docMap) {
112
68
  continue;
113
69
  }
114
- const indentSensitiveLines = new Set();
115
- for (const service of item.service.provideFormattingIndentSensitiveLines ? [item.service] : context.services.map(service => service[1])) {
116
- if (token.isCancellationRequested) {
117
- break;
118
- }
119
- if (service.provideFormattingIndentSensitiveLines) {
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
- let indentEdits = patchIndents(document, item.isCodeBlock, docMap.map, initialIndentLanguageId[docMap.virtualFileDocument.languageId] ? baseIndent : '');
132
- indentEdits = indentEdits.filter(edit => {
133
- for (let line = edit.range.start.line; line <= edit.range.end.line; line++) {
134
- if (indentSensitiveLines.has(line) && !edit.newText.includes('\n')) {
135
- return false;
136
- }
137
- if (onTypeParams && !editLines.has(line)) {
138
- return false;
139
- }
140
- if (!(0, common_1.isInsideRange)(range, edit.range)) {
141
- return false;
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
- if (document.getText() === originalDocument.getText()) {
156
- return;
120
+ finally {
121
+ context.language.files.delete(sourceFile.id + '.tmp');
157
122
  }
158
- const editRange = {
159
- start: originalDocument.positionAt(0),
160
- end: originalDocument.positionAt(originalDocument.getText().length),
161
- };
162
- const textEdit = {
163
- range: editRange,
164
- newText: document.getText(),
165
- };
166
- return [textEdit];
167
- async function tryFormat(document, range, ch) {
168
- let formatRange = range;
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 formatRange && 'character' in formatRange) {
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, formatRange, ch, options, token);
161
+ edits = await service[1].provideOnTypeFormattingEdits?.(document, rangeOrPosition, ch, options, codeOptions, token);
181
162
  }
182
163
  }
183
- else if (ch === undefined && 'start' in formatRange && 'end' in formatRange) {
184
- edits = await service[1].provideDocumentFormattingEdits?.(document, formatRange, options, token);
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 patchIndents(document, isCodeBlock, map, initialIndent) {
215
- const indentTextEdits = [];
216
- if (!isCodeBlock) {
217
- initialIndent = '';
218
- }
219
- for (let i = 0; i < map.mappings.length; i++) {
220
- const mapping = map.mappings[i];
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 indentTextEdits;
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
- let result;
24
- const start = document.offsetAt(range.start);
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.virtualFileDocument.positionAt(result.start),
48
- end: map.virtualFileDocument.positionAt(result.end),
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.sourceFileDocument.uri;
25
+ reference.uri = map.sourceDocument.uri;
26
26
  reference.range = range;
27
27
  return reference;
28
28
  }
@@ -40,8 +40,8 @@ function register(context) {
40
40
  }
41
41
  if (minStart !== undefined && maxEnd !== undefined) {
42
42
  yield {
43
- start: map.virtualFileDocument.positionAt(minStart),
44
- end: map.virtualFileDocument.positionAt(maxEnd),
43
+ start: map.embeddedDocument.positionAt(minStart),
44
+ end: map.embeddedDocument.positionAt(maxEnd),
45
45
  };
46
46
  }
47
47
  }, async (service, document, arg) => {
@@ -67,7 +67,7 @@ function register(context) {
67
67
  .map((_inlayHint) => {
68
68
  const position = map.getSourcePosition(_inlayHint.position, language_core_1.isInlayHintsEnabled);
69
69
  const edits = _inlayHint.textEdits
70
- ?.map(textEdit => (0, transform_1.transformTextEdit)(textEdit, range => map.getSourceRange(range), map.virtualFileDocument))
70
+ ?.map(textEdit => (0, transform_1.transformTextEdit)(textEdit, range => map.getSourceRange(range), map.embeddedDocument))
71
71
  .filter(common_1.notEmpty);
72
72
  if (position) {
73
73
  return {
@@ -52,7 +52,7 @@ function register(context) {
52
52
  const range = map.getSourceRange(reference.range, language_core_1.isReferencesEnabled);
53
53
  if (range) {
54
54
  results.push({
55
- uri: map.sourceFileDocument.uri,
55
+ uri: map.sourceDocument.uri,
56
56
  range,
57
57
  });
58
58
  }
@@ -41,17 +41,14 @@ function register(context) {
41
41
  }
42
42
  return 0;
43
43
  });
44
- for (let i = 1; i < pluginResults.length; i++) {
45
- let root = pluginResults[i - 1];
46
- while (root.parent) {
47
- root = root.parent;
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
- let parent = pluginResults[i];
50
- while (parent && !(0, common_1.isInsideRange)(parent.range, root.range)) {
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.sourceFileDocument.uri, range };
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.virtualFileDocument, context);
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][]>;
@@ -69,6 +63,10 @@ export interface ServicePlugin {
69
63
  autoFormatTriggerCharacters?: string[];
70
64
  create(context: ServiceContext): ServicePluginInstance;
71
65
  }
66
+ export interface EmbeddedCodeFormattingOptions {
67
+ level: number;
68
+ initialIndentLevel: number;
69
+ }
72
70
  export interface ServicePluginInstance<P = any> {
73
71
  provide?: P;
74
72
  isAdditionalCompletion?: boolean;
@@ -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;
@@ -1,10 +1,16 @@
1
1
  import type * as ts from 'typescript';
2
2
  import type * as vscode from 'vscode-languageserver-protocol';
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): {
5
+ start: number;
6
+ end: number;
7
+ } | undefined;
3
8
  export declare function getOverlapRange(range1Start: number, range1End: number, range2Start: number, range2End: number): {
4
9
  start: number;
5
10
  end: number;
6
11
  } | undefined;
7
12
  export declare function isInsideRange(parent: vscode.Range, child: vscode.Range): boolean;
13
+ export declare function isEqualRange(a: vscode.Range, b: vscode.Range): boolean;
8
14
  export declare function stringToSnapshot(str: string): ts.IScriptSnapshot;
9
15
  export declare function sleep(ms: number): Promise<unknown>;
10
16
  export declare function notEmpty<T>(value: T | null | undefined): value is T;
@@ -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.getOverlapRange = void 0;
3
+ exports.notEmpty = exports.sleep = exports.stringToSnapshot = exports.isEqualRange = exports.isInsideRange = exports.getOverlapRange = 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);
@@ -29,6 +75,13 @@ function isInsideRange(parent, child) {
29
75
  return true;
30
76
  }
31
77
  exports.isInsideRange = isInsideRange;
78
+ function isEqualRange(a, b) {
79
+ return a.start.line === b.start.line
80
+ && a.start.character === b.start.character
81
+ && a.end.line === b.end.line
82
+ && a.end.character === b.end.character;
83
+ }
84
+ exports.isEqualRange = isEqualRange;
32
85
  function stringToSnapshot(str) {
33
86
  return {
34
87
  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<string>): Generator<SourceMapWithDocuments<CodeInformation>>;
9
- export declare function getEmbeddedFilesByLevel(context: ServiceContext, sourceFileUri: string, rootFile: VirtualCode, level: number): VirtualCode<string>[];
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.virtualFileDocument, mappedArg, map), 'service ' + serviceId + ' crashed on ' + map.virtualFileDocument.uri);
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
- for (const embeddedCode of current.embeddedCodes) {
81
- yield* eachEmbeddedDocument(context, embeddedCode, rootCode);
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.sourceFileDocument.uri);
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
- let nextLevel = [];
100
+ const nextLevel = [];
99
101
  for (const file of embeddedFilesByLevel[embeddedFilesByLevel.length - 1]) {
100
- nextLevel = nextLevel.concat(file.embeddedCodes.filter(file => !context.disabledVirtualFileUris.has(context.documents.getVirtualCodeUri(sourceFileUri, file.id))));
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
  }
@@ -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.sourceFileDocument.uri;
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.sourceFileDocument.uri;
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.sourceFileDocument.uri] ??= [];
296
- sourceResult.changes[map.sourceFileDocument.uri].push({
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.sourceFileDocument.uri] ??= [];
307
- sourceResult.changes[map.sourceFileDocument.uri].push({ newText: tsEdit.newText, range });
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.sourceFileDocument.uri,
330
- version: versions[map.sourceFileDocument.uri] ?? null,
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.sourceFileDocument.uri,
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.sourceFileDocument.uri,
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.0.4",
3
+ "version": "2.1.0",
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.0.4",
15
+ "@volar/language-core": "2.1.0",
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": "b3dc544424c95f8b72e832fbaa0f85a34442389b"
20
+ "gitHead": "09e1792f0adafb02caf89a5a45a6fcaaf3177808"
21
21
  }