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

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.
@@ -41,8 +41,8 @@ function register(context) {
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)
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
46
  continue;
47
47
  const maps = [...context.documents.getMapsByVirtualFileName(embedded.fileName)];
48
48
  const map = (_c = maps.find(map => map[1].sourceFileDocument.uri === document.uri)) === null || _c === void 0 ? void 0 : _c[1];
@@ -60,10 +60,15 @@ function register(context) {
60
60
  start: map.virtualFileDocument.positionAt(0),
61
61
  end: map.virtualFileDocument.positionAt(map.virtualFileDocument.getText().length),
62
62
  });
63
+ if (virtualCodeEdits) {
64
+ toPatchIndentUris.push({
65
+ uri: map.virtualFileDocument.uri,
66
+ isCodeBlock,
67
+ });
68
+ }
63
69
  }
64
70
  if (!virtualCodeEdits)
65
71
  continue;
66
- toPatchIndentUris.push(map.virtualFileDocument.uri);
67
72
  for (const textEdit of virtualCodeEdits) {
68
73
  const range = map.toSourceRange(textEdit.range);
69
74
  if (range) {
@@ -84,8 +89,8 @@ function register(context) {
84
89
  if (level > 1) {
85
90
  const baseIndent = options.insertSpaces ? ' '.repeat(options.tabSize) : '\t';
86
91
  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);
92
+ for (const [_, map] of context.documents.getMapsByVirtualFileUri(toPatchIndentUri.uri)) {
93
+ let indentEdits = patchIndents(document, toPatchIndentUri.isCodeBlock, map.map, initialIndentLanguageId[map.virtualFileDocument.languageId] ? baseIndent : '');
89
94
  indentEdits = indentEdits.filter(edit => (0, common_1.isInsideRange)(range, edit.range));
90
95
  if (indentEdits.length > 0) {
91
96
  const newText = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, indentEdits);
@@ -144,21 +149,22 @@ function register(context) {
144
149
  });
145
150
  }
146
151
  exports.register = register;
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;
152
+ function patchIndents(document, isCodeBlock, map, initialIndent) {
150
153
  const indentTextEdits = [];
154
+ if (!isCodeBlock) {
155
+ initialIndent = '';
156
+ }
151
157
  for (let i = 0; i < map.mappings.length; i++) {
152
158
  const mapping = map.mappings[i];
153
159
  const firstLineIndent = getBaseIndent(mapping.sourceRange[0]);
154
160
  const text = document.getText().substring(mapping.sourceRange[0], mapping.sourceRange[1]);
155
- if (text.indexOf('\n') === -1) {
156
- continue;
157
- }
158
161
  const lines = text.split('\n');
159
162
  const baseIndent = firstLineIndent + initialIndent;
160
163
  let lineOffset = lines[0].length + 1;
161
- if (insertFirstNewline && i === 0 && !text.startsWith('\n')) {
164
+ let insertedFinalNewLine = false;
165
+ if (!text.trim())
166
+ continue;
167
+ if (isCodeBlock && text.trimStart().length === text.length) {
162
168
  indentTextEdits.push({
163
169
  newText: '\n' + baseIndent,
164
170
  range: {
@@ -167,11 +173,22 @@ function patchInterpolationIndent(document, map, initialIndent, data) {
167
173
  },
168
174
  });
169
175
  }
170
- if (baseIndent) {
171
- for (let i = 1; i < lines.length - 1; i++) {
172
- if (lines[i] !== '') {
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;
185
+ }
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;
173
190
  indentTextEdits.push({
174
- newText: baseIndent,
191
+ newText: isLastLine ? firstLineIndent : baseIndent,
175
192
  range: {
176
193
  start: document.positionAt(mapping.sourceRange[0] + lineOffset),
177
194
  end: document.positionAt(mapping.sourceRange[0] + lineOffset),
@@ -181,24 +198,6 @@ function patchInterpolationIndent(document, map, initialIndent, data) {
181
198
  lineOffset += lines[i].length + 1;
182
199
  }
183
200
  }
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
- });
201
- }
202
201
  }
203
202
  return indentTextEdits;
204
203
  function getBaseIndent(pos) {
@@ -50,15 +50,14 @@ function register(context) {
50
50
  }, (plugin, document, arg) => {
51
51
  var _a, _b;
52
52
  return (_b = (_a = plugin.inlayHints) === null || _a === void 0 ? void 0 : _a.on) === null || _b === void 0 ? void 0 : _b.call(_a, document, arg);
53
- }, (inlayHints, map) => inlayHints.map(_inlayHint => {
53
+ }, (inlayHints, map) => inlayHints.map((_inlayHint) => {
54
54
  var _a;
55
55
  if (!map)
56
56
  return _inlayHint;
57
57
  const position = map.toSourcePosition(_inlayHint.position);
58
58
  const edits = (_a = _inlayHint.textEdits) === null || _a === void 0 ? void 0 : _a.map(textEdit => transformer.asTextEdit(textEdit, range => map.toSourceRange(range), map.virtualFileDocument)).filter(shared.notEmpty);
59
59
  if (position) {
60
- return Object.assign(Object.assign({}, _inlayHint), { position,
61
- edits });
60
+ return Object.assign(Object.assign({}, _inlayHint), { position, textEdits: edits });
62
61
  }
63
62
  }).filter(shared.notEmpty), arr => arr.flat());
64
63
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/language-service",
3
- "version": "1.2.0-alpha.11",
3
+ "version": "1.2.0-alpha.13",
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.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",
16
+ "@volar/language-core": "1.2.0-alpha.13",
17
+ "@volar/shared": "1.2.0-alpha.13",
18
+ "@volar/source-map": "1.2.0-alpha.13",
19
+ "@volar/typescript-faster": "1.2.0-alpha.13",
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": "b9b3d607df5772c00edda17cfda05281c3e7c06f"
26
+ "gitHead": "2b935afad2e4e4e307ae525387f66516e496ba24"
27
27
  }