@volar/language-service 2.0.3 → 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);
51
+ cacheData.list = (0, transform_1.transformCompletionList)(cacheData.list, range => map.getSourceRange(range), map.embeddedDocument, context);
52
52
  }
53
53
  }
54
54
  }
@@ -133,7 +133,7 @@ function register(context) {
133
133
  };
134
134
  }
135
135
  if (map) {
136
- completionList = (0, transform_1.transformCompletionList)(completionList, range => map.getSourceRange(range, language_core_1.isCompletionEnabled), document);
136
+ completionList = (0, transform_1.transformCompletionList)(completionList, range => map.getSourceRange(range, language_core_1.isCompletionEnabled), document, context);
137
137
  }
138
138
  lastResult?.results.push({
139
139
  virtualDocumentUri: map ? document.uri : undefined,
@@ -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
@@ -4,7 +4,7 @@ exports.register = void 0;
4
4
  const cancellation_1 = require("../utils/cancellation");
5
5
  const common_1 = require("../utils/common");
6
6
  const featureWorkers_1 = require("../utils/featureWorkers");
7
- const resolveDocumentLink_1 = require("./resolveDocumentLink");
7
+ const transform_1 = require("../utils/transform");
8
8
  const language_core_1 = require("@volar/language-core");
9
9
  function register(context) {
10
10
  return async (uri, token = cancellation_1.NoneCancellationToken) => {
@@ -38,7 +38,7 @@ function register(context) {
38
38
  range,
39
39
  };
40
40
  if (link.target) {
41
- link.target = (0, resolveDocumentLink_1.transformDocumentLinkTarget)(link.target, context);
41
+ link.target = (0, transform_1.transformDocumentLinkTarget)(link.target, context);
42
42
  }
43
43
  return link;
44
44
  })
@@ -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) => {