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

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,34 @@ 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 enabledOnType = typeof embedded.capabilities.documentFormatting === 'object' ? !!embedded.capabilities.documentFormatting.onType : true;
45
+ if (onTypeParams && !enabledOnType)
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);
71
- }
59
+ virtualCodeEdits = yield tryFormat(map.virtualFileDocument, {
60
+ start: map.virtualFileDocument.positionAt(0),
61
+ end: map.virtualFileDocument.positionAt(map.virtualFileDocument.getText().length),
62
+ });
72
63
  }
73
- if (!_edits)
64
+ if (!virtualCodeEdits)
74
65
  continue;
75
- for (const textEdit of _edits) {
66
+ toPatchIndentUris.push(map.virtualFileDocument.uri);
67
+ for (const textEdit of virtualCodeEdits) {
76
68
  const range = map.toSourceRange(textEdit.range);
77
69
  if (range) {
78
70
  edits.push({
@@ -82,20 +74,25 @@ function register(context) {
82
74
  }
83
75
  }
84
76
  }
77
+ edits = edits.filter(edit => (0, common_1.isInsideRange)(range, edit.range));
85
78
  if (edits.length > 0) {
86
79
  const newText = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, edits);
87
80
  document = vscode_languageserver_textdocument_1.TextDocument.create(document.uri, document.languageId, document.version + 1, newText);
88
81
  context.core.virtualFiles.updateSource(shared.uriToFileName(document.uri), (0, common_1.stringToSnapshot)(document.getText()), undefined);
89
82
  edited = true;
90
83
  }
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;
84
+ if (level > 1) {
85
+ const baseIndent = options.insertSpaces ? ' '.repeat(options.tabSize) : '\t';
86
+ for (const toPatchIndentUri of toPatchIndentUris) {
87
+ for (const [file, map] of context.documents.getMapsByVirtualFileUri(toPatchIndentUri)) {
88
+ let indentEdits = patchInterpolationIndent(document, map.map, initialIndentLanguageId[map.virtualFileDocument.languageId] ? baseIndent : '', file.capabilities.documentFormatting);
89
+ indentEdits = indentEdits.filter(edit => (0, common_1.isInsideRange)(range, edit.range));
90
+ if (indentEdits.length > 0) {
91
+ const newText = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, indentEdits);
92
+ document = vscode_languageserver_textdocument_1.TextDocument.create(document.uri, document.languageId, document.version + 1, newText);
93
+ context.core.virtualFiles.updateSource(shared.uriToFileName(document.uri), (0, common_1.stringToSnapshot)(document.getText()), undefined);
94
+ edited = true;
95
+ }
99
96
  }
100
97
  }
101
98
  }
@@ -121,7 +118,7 @@ function register(context) {
121
118
  embeddedFilesByLevel.push(nextLevel);
122
119
  }
123
120
  }
124
- function tryFormat(isEmbedded, document, range, ch) {
121
+ function tryFormat(document, range, ch) {
125
122
  var _a, _b;
126
123
  return __awaiter(this, void 0, void 0, function* () {
127
124
  let formatRange = range;
@@ -132,11 +129,11 @@ function register(context) {
132
129
  edits = yield ((_a = plugin.formatOnType) === null || _a === void 0 ? void 0 : _a.call(plugin, document, formatRange, ch, options));
133
130
  }
134
131
  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 })));
132
+ edits = yield ((_b = plugin.format) === null || _b === void 0 ? void 0 : _b.call(plugin, document, formatRange, options));
136
133
  }
137
134
  }
138
135
  catch (err) {
139
- console.error(err);
136
+ console.warn(err);
140
137
  }
141
138
  if (!edits)
142
139
  continue;
@@ -147,28 +144,61 @@ function register(context) {
147
144
  });
148
145
  }
149
146
  exports.register = register;
150
- function patchInterpolationIndent(document, map) {
147
+ function patchInterpolationIndent(document, map, initialIndent, data) {
148
+ const insertFirstNewline = typeof data === 'object' ? data.insertFirstNewline : false;
149
+ const insertFinalNewline = typeof data === 'object' ? data.insertFinalNewline : false;
151
150
  const indentTextEdits = [];
152
- for (const mapped of map.mappings) {
153
- const baseIndent = getBaseIndent(mapped.sourceRange[0]);
154
- if (baseIndent === '') {
155
- continue;
156
- }
157
- const text = document.getText().substring(mapped.sourceRange[0], mapped.sourceRange[1]);
151
+ for (let i = 0; i < map.mappings.length; i++) {
152
+ const mapping = map.mappings[i];
153
+ const firstLineIndent = getBaseIndent(mapping.sourceRange[0]);
154
+ const text = document.getText().substring(mapping.sourceRange[0], mapping.sourceRange[1]);
158
155
  if (text.indexOf('\n') === -1) {
159
156
  continue;
160
157
  }
161
158
  const lines = text.split('\n');
162
- for (let i = 1; i < lines.length; i++) {
163
- lines[i] = baseIndent + lines[i];
159
+ const baseIndent = firstLineIndent + initialIndent;
160
+ let lineOffset = lines[0].length + 1;
161
+ if (insertFirstNewline && i === 0 && !text.startsWith('\n')) {
162
+ indentTextEdits.push({
163
+ newText: '\n' + baseIndent,
164
+ range: {
165
+ start: document.positionAt(mapping.sourceRange[0]),
166
+ end: document.positionAt(mapping.sourceRange[0]),
167
+ },
168
+ });
169
+ }
170
+ if (baseIndent) {
171
+ for (let i = 1; i < lines.length - 1; i++) {
172
+ if (lines[i] !== '') {
173
+ indentTextEdits.push({
174
+ newText: baseIndent,
175
+ range: {
176
+ start: document.positionAt(mapping.sourceRange[0] + lineOffset),
177
+ end: document.positionAt(mapping.sourceRange[0] + lineOffset),
178
+ },
179
+ });
180
+ }
181
+ lineOffset += lines[i].length + 1;
182
+ }
183
+ }
184
+ if (firstLineIndent) {
185
+ indentTextEdits.push({
186
+ newText: firstLineIndent,
187
+ range: {
188
+ start: document.positionAt(mapping.sourceRange[0] + lineOffset),
189
+ end: document.positionAt(mapping.sourceRange[0] + lineOffset),
190
+ },
191
+ });
192
+ }
193
+ if (insertFinalNewline && i === map.mappings.length - 1 && !text.endsWith('\n')) {
194
+ indentTextEdits.push({
195
+ newText: '\n',
196
+ range: {
197
+ start: document.positionAt(mapping.sourceRange[1]),
198
+ end: document.positionAt(mapping.sourceRange[1]),
199
+ },
200
+ });
164
201
  }
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
202
  }
173
203
  return indentTextEdits;
174
204
  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.11",
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.11",
17
+ "@volar/shared": "1.2.0-alpha.11",
18
+ "@volar/source-map": "1.2.0-alpha.11",
19
+ "@volar/typescript-faster": "1.2.0-alpha.11",
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": "b9b3d607df5772c00edda17cfda05281c3e7c06f"
27
27
  }