@volar/language-service 1.2.0-alpha.10 → 1.2.0-alpha.12

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.
@@ -20,13 +20,13 @@ function register(context) {
20
20
  if (!document)
21
21
  return;
22
22
  range !== null && range !== void 0 ? range : (range = vscode.Range.create(document.positionAt(0), document.positionAt(document.getText().length)));
23
- const initialIndentLanguageId = (_b = yield ((_a = context.env.configurationHost) === null || _a === void 0 ? void 0 : _a.getConfiguration('volar.format.initialIndent'))) !== null && _b !== void 0 ? _b : { html: true };
24
23
  const source = context.documents.getSourceByUri(document.uri);
25
24
  if (!source) {
26
25
  return onTypeParams
27
- ? yield tryFormat(false, document, onTypeParams.position, onTypeParams.ch)
28
- : yield tryFormat(false, document, range, undefined);
26
+ ? yield tryFormat(document, onTypeParams.position, onTypeParams.ch)
27
+ : yield tryFormat(document, range, undefined);
29
28
  }
29
+ const initialIndentLanguageId = (_b = yield ((_a = context.env.configurationHost) === null || _a === void 0 ? void 0 : _a.getConfiguration('volar.format.initialIndent'))) !== null && _b !== void 0 ? _b : { html: true };
30
30
  const originalSnapshot = source.snapshot;
31
31
  const rootVirtualFile = source.root;
32
32
  const originalDocument = document;
@@ -37,42 +37,39 @@ function register(context) {
37
37
  if (embeddedFiles.length === 0)
38
38
  break;
39
39
  let edits = [];
40
- let toPatchIndentUri;
40
+ const toPatchIndentUris = [];
41
41
  for (const embedded of embeddedFiles) {
42
42
  if (!embedded.capabilities.documentFormatting)
43
43
  continue;
44
+ const isCodeBlock = embedded.mappings.length === 1 && embedded.mappings[0].generatedRange[0] === 0 && embedded.mappings[0].generatedRange[1] === embedded.snapshot.getLength();
45
+ if (onTypeParams && !isCodeBlock)
46
+ continue;
44
47
  const maps = [...context.documents.getMapsByVirtualFileName(embedded.fileName)];
45
48
  const map = (_c = maps.find(map => map[1].sourceFileDocument.uri === document.uri)) === null || _c === void 0 ? void 0 : _c[1];
46
49
  if (!map)
47
50
  continue;
48
- let _edits;
51
+ let virtualCodeEdits;
49
52
  if (onTypeParams) {
50
53
  const embeddedPosition = map.toGeneratedPosition(onTypeParams.position);
51
54
  if (embeddedPosition) {
52
- _edits = yield tryFormat(true, map.virtualFileDocument, embeddedPosition, onTypeParams.ch);
55
+ virtualCodeEdits = yield tryFormat(map.virtualFileDocument, embeddedPosition, onTypeParams.ch);
53
56
  }
54
57
  }
55
58
  else {
56
- let genRange = map.toGeneratedRange(range);
57
- if (!genRange) {
58
- const firstMapping = map.map.mappings.sort((a, b) => a.sourceRange[0] - b.sourceRange[0])[0];
59
- const lastMapping = map.map.mappings.sort((a, b) => b.sourceRange[0] - a.sourceRange[0])[0];
60
- if (firstMapping && document.offsetAt(range.start) < firstMapping.sourceRange[0]
61
- && lastMapping && document.offsetAt(range.end) > lastMapping.sourceRange[1]) {
62
- genRange = {
63
- start: map.virtualFileDocument.positionAt(firstMapping.generatedRange[0]),
64
- end: map.virtualFileDocument.positionAt(lastMapping.generatedRange[1]),
65
- };
66
- }
67
- }
68
- if (genRange) {
69
- toPatchIndentUri = map.virtualFileDocument.uri;
70
- _edits = yield tryFormat(true, map.virtualFileDocument, genRange);
59
+ virtualCodeEdits = yield tryFormat(map.virtualFileDocument, {
60
+ start: map.virtualFileDocument.positionAt(0),
61
+ end: map.virtualFileDocument.positionAt(map.virtualFileDocument.getText().length),
62
+ });
63
+ if (virtualCodeEdits) {
64
+ toPatchIndentUris.push({
65
+ uri: map.virtualFileDocument.uri,
66
+ isCodeBlock,
67
+ });
71
68
  }
72
69
  }
73
- if (!_edits)
70
+ if (!virtualCodeEdits)
74
71
  continue;
75
- for (const textEdit of _edits) {
72
+ for (const textEdit of virtualCodeEdits) {
76
73
  const range = map.toSourceRange(textEdit.range);
77
74
  if (range) {
78
75
  edits.push({
@@ -82,20 +79,25 @@ function register(context) {
82
79
  }
83
80
  }
84
81
  }
82
+ edits = edits.filter(edit => (0, common_1.isInsideRange)(range, edit.range));
85
83
  if (edits.length > 0) {
86
84
  const newText = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, edits);
87
85
  document = vscode_languageserver_textdocument_1.TextDocument.create(document.uri, document.languageId, document.version + 1, newText);
88
86
  context.core.virtualFiles.updateSource(shared.uriToFileName(document.uri), (0, common_1.stringToSnapshot)(document.getText()), undefined);
89
87
  edited = true;
90
88
  }
91
- if (toPatchIndentUri) {
92
- for (const [_, map] of context.documents.getMapsByVirtualFileUri(toPatchIndentUri)) {
93
- const indentEdits = patchInterpolationIndent(document, map.map);
94
- if (indentEdits.length > 0) {
95
- const newText = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, indentEdits);
96
- document = vscode_languageserver_textdocument_1.TextDocument.create(document.uri, document.languageId, document.version + 1, newText);
97
- context.core.virtualFiles.updateSource(shared.uriToFileName(document.uri), (0, common_1.stringToSnapshot)(document.getText()), undefined);
98
- edited = true;
89
+ if (level > 1) {
90
+ const baseIndent = options.insertSpaces ? ' '.repeat(options.tabSize) : '\t';
91
+ for (const toPatchIndentUri of toPatchIndentUris) {
92
+ for (const [_, map] of context.documents.getMapsByVirtualFileUri(toPatchIndentUri.uri)) {
93
+ let indentEdits = patchIndents(document, toPatchIndentUri.isCodeBlock, map.map, initialIndentLanguageId[map.virtualFileDocument.languageId] ? baseIndent : '');
94
+ indentEdits = indentEdits.filter(edit => (0, common_1.isInsideRange)(range, edit.range));
95
+ if (indentEdits.length > 0) {
96
+ const newText = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, indentEdits);
97
+ document = vscode_languageserver_textdocument_1.TextDocument.create(document.uri, document.languageId, document.version + 1, newText);
98
+ context.core.virtualFiles.updateSource(shared.uriToFileName(document.uri), (0, common_1.stringToSnapshot)(document.getText()), undefined);
99
+ edited = true;
100
+ }
99
101
  }
100
102
  }
101
103
  }
@@ -121,7 +123,7 @@ function register(context) {
121
123
  embeddedFilesByLevel.push(nextLevel);
122
124
  }
123
125
  }
124
- function tryFormat(isEmbedded, document, range, ch) {
126
+ function tryFormat(document, range, ch) {
125
127
  var _a, _b;
126
128
  return __awaiter(this, void 0, void 0, function* () {
127
129
  let formatRange = range;
@@ -132,11 +134,11 @@ function register(context) {
132
134
  edits = yield ((_a = plugin.formatOnType) === null || _a === void 0 ? void 0 : _a.call(plugin, document, formatRange, ch, options));
133
135
  }
134
136
  else if (ch === undefined && vscode.Range.is(formatRange)) {
135
- edits = yield ((_b = plugin.format) === null || _b === void 0 ? void 0 : _b.call(plugin, document, formatRange, Object.assign(Object.assign({}, options), { initialIndent: isEmbedded ? !!initialIndentLanguageId[document.languageId] : false })));
137
+ edits = yield ((_b = plugin.format) === null || _b === void 0 ? void 0 : _b.call(plugin, document, formatRange, options));
136
138
  }
137
139
  }
138
140
  catch (err) {
139
- console.error(err);
141
+ console.warn(err);
140
142
  }
141
143
  if (!edits)
142
144
  continue;
@@ -147,28 +149,55 @@ function register(context) {
147
149
  });
148
150
  }
149
151
  exports.register = register;
150
- function patchInterpolationIndent(document, map) {
152
+ function patchIndents(document, isCodeBlock, map, initialIndent) {
151
153
  const indentTextEdits = [];
152
- for (const mapped of map.mappings) {
153
- const baseIndent = getBaseIndent(mapped.sourceRange[0]);
154
- if (baseIndent === '') {
154
+ if (!isCodeBlock) {
155
+ initialIndent = '';
156
+ }
157
+ for (let i = 0; i < map.mappings.length; i++) {
158
+ const mapping = map.mappings[i];
159
+ const firstLineIndent = getBaseIndent(mapping.sourceRange[0]);
160
+ const text = document.getText().substring(mapping.sourceRange[0], mapping.sourceRange[1]);
161
+ const lines = text.split('\n');
162
+ const baseIndent = firstLineIndent + initialIndent;
163
+ let lineOffset = lines[0].length + 1;
164
+ let insertedFinalNewLine = false;
165
+ if (!text.trim())
155
166
  continue;
167
+ if (isCodeBlock && text.trimStart().length === text.length) {
168
+ indentTextEdits.push({
169
+ newText: '\n' + baseIndent,
170
+ range: {
171
+ start: document.positionAt(mapping.sourceRange[0]),
172
+ end: document.positionAt(mapping.sourceRange[0]),
173
+ },
174
+ });
156
175
  }
157
- const text = document.getText().substring(mapped.sourceRange[0], mapped.sourceRange[1]);
158
- if (text.indexOf('\n') === -1) {
159
- continue;
176
+ if (isCodeBlock && text.trimEnd().length === text.length) {
177
+ indentTextEdits.push({
178
+ newText: '\n',
179
+ range: {
180
+ start: document.positionAt(mapping.sourceRange[1]),
181
+ end: document.positionAt(mapping.sourceRange[1]),
182
+ },
183
+ });
184
+ insertedFinalNewLine = true;
160
185
  }
161
- const lines = text.split('\n');
162
- for (let i = 1; i < lines.length; i++) {
163
- lines[i] = baseIndent + lines[i];
186
+ if (baseIndent && lines.length > 1) {
187
+ for (let i = 1; i < lines.length; i++) {
188
+ if (lines[i] !== '' || i === lines.length - 1) {
189
+ const isLastLine = i === lines.length - 1 && !insertedFinalNewLine;
190
+ indentTextEdits.push({
191
+ newText: isLastLine ? firstLineIndent : baseIndent,
192
+ range: {
193
+ start: document.positionAt(mapping.sourceRange[0] + lineOffset),
194
+ end: document.positionAt(mapping.sourceRange[0] + lineOffset),
195
+ },
196
+ });
197
+ }
198
+ lineOffset += lines[i].length + 1;
199
+ }
164
200
  }
165
- indentTextEdits.push({
166
- newText: lines.join('\n'),
167
- range: {
168
- start: document.positionAt(mapped.sourceRange[0]),
169
- end: document.positionAt(mapped.sourceRange[1]),
170
- },
171
- });
172
201
  }
173
202
  return indentTextEdits;
174
203
  function getBaseIndent(pos) {
@@ -60,7 +60,7 @@ function register(context) {
60
60
  const diagnostics = codeActionContext.diagnostics.filter(diagnostic => {
61
61
  const data = diagnostic.data;
62
62
  if (data && data.version !== sourceDocument.version) {
63
- console.warn('[volar/plugin-api] diagnostic version mismatch', data.version, sourceDocument.version);
63
+ // console.warn('[volar/plugin-api] diagnostic version mismatch', data.version, sourceDocument.version);
64
64
  return false;
65
65
  }
66
66
  return (data === null || data === void 0 ? void 0 : data.type) === 'plugin' && (data === null || data === void 0 ? void 0 : data.pluginOrRuleId) === pluginId;
@@ -100,7 +100,7 @@ function register(context) {
100
100
  for (const diagnostic of codeActionContext.diagnostics) {
101
101
  const data = diagnostic.data;
102
102
  if (data && data.version !== sourceDocument.version) {
103
- console.warn('[volar/rules-api] diagnostic version mismatch', data.version, sourceDocument.version);
103
+ // console.warn('[volar/rules-api] diagnostic version mismatch', data.version, sourceDocument.version);
104
104
  continue;
105
105
  }
106
106
  if ((data === null || data === void 0 ? void 0 : data.type) === 'rule') {
package/out/types.d.ts CHANGED
@@ -86,9 +86,7 @@ export interface LanguageServicePluginInstance {
86
86
  getFoldingRanges?(document: TextDocument): NullableResult<vscode.FoldingRange[]>;
87
87
  getSelectionRanges?(document: TextDocument, positions: vscode.Position[]): NullableResult<vscode.SelectionRange[]>;
88
88
  getSignatureHelp?(document: TextDocument, position: vscode.Position, context?: vscode.SignatureHelpContext): NullableResult<vscode.SignatureHelp>;
89
- format?(document: TextDocument, range: vscode.Range, options: vscode.FormattingOptions & {
90
- initialIndent: boolean;
91
- }): NullableResult<vscode.TextEdit[]>;
89
+ format?(document: TextDocument, range: vscode.Range, options: vscode.FormattingOptions): NullableResult<vscode.TextEdit[]>;
92
90
  formatOnType?(document: TextDocument, position: vscode.Position, key: string, options: vscode.FormattingOptions): NullableResult<vscode.TextEdit[]>;
93
91
  definition?: {
94
92
  on?(document: TextDocument, position: vscode.Position): NullableResult<vscode.LocationLink[]>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/language-service",
3
- "version": "1.2.0-alpha.10",
3
+ "version": "1.2.0-alpha.12",
4
4
  "main": "out/index.js",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -13,15 +13,15 @@
13
13
  "directory": "packages/language-service"
14
14
  },
15
15
  "dependencies": {
16
- "@volar/language-core": "1.2.0-alpha.10",
17
- "@volar/shared": "1.2.0-alpha.10",
18
- "@volar/source-map": "1.2.0-alpha.10",
19
- "@volar/typescript-faster": "1.2.0-alpha.10",
16
+ "@volar/language-core": "1.2.0-alpha.12",
17
+ "@volar/shared": "1.2.0-alpha.12",
18
+ "@volar/source-map": "1.2.0-alpha.12",
19
+ "@volar/typescript-faster": "1.2.0-alpha.12",
20
20
  "vscode-html-languageservice": "^5.0.4",
21
21
  "vscode-json-languageservice": "^5.2.0",
22
22
  "vscode-languageserver-protocol": "^3.17.3",
23
23
  "vscode-languageserver-textdocument": "^1.0.8",
24
24
  "vscode-uri": "^3.0.7"
25
25
  },
26
- "gitHead": "c0b7580b14966d28d7c6df5da9baa41111f9f8d9"
26
+ "gitHead": "cac0a7e75c45bd217f98b8d2f66a03860cc89e0c"
27
27
  }