@weapp-vite/volar 2.1.10 → 2.1.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.
package/dist/index.cjs CHANGED
@@ -239,6 +239,25 @@ function resolveEmbeddedJsonBlock(fileName, sfc, embeddedCode, tsModule, hasSche
239
239
  }
240
240
  //#endregion
241
241
  //#region src/parseSfc.ts
242
+ function advancePosition(position, source, start = 0, end = source.length) {
243
+ let lines = 0;
244
+ let lastNewline = -1;
245
+ for (let index = start; index < end; index += 1) if (source.charCodeAt(index) === 10) {
246
+ lines += 1;
247
+ lastNewline = index;
248
+ }
249
+ return {
250
+ offset: position.offset + end - start,
251
+ line: position.line + lines,
252
+ column: lastNewline < 0 ? position.column + end - start : end - lastNewline
253
+ };
254
+ }
255
+ function applyTextChangeToPosition(position, oldChangeEnd, newChangeEnd, lengthDiff) {
256
+ const oldLine = position.line;
257
+ position.offset += lengthDiff;
258
+ position.line += newChangeEnd.line - oldChangeEnd.line;
259
+ if (oldLine === oldChangeEnd.line) position.column += newChangeEnd.column - oldChangeEnd.column;
260
+ }
242
261
  function parseAttr(property, node) {
243
262
  if (!property.value) return true;
244
263
  const { source, start } = property.value.loc;
@@ -257,9 +276,10 @@ function repairIncompleteTemplate(descriptor, errors) {
257
276
  const endTagOffset = template.content.lastIndexOf("<");
258
277
  const endTagText = template.content.slice(endTagOffset).trimEnd();
259
278
  if ("</template>".startsWith(endTagText)) {
260
- template.loc.end.offset = template.loc.start.offset + endTagOffset;
261
- template.content = template.content.slice(0, endTagOffset);
262
- template.loc.source = template.content;
279
+ const repairedContent = template.content.slice(0, endTagOffset);
280
+ template.loc.end = advancePosition(template.loc.start, template.content, 0, endTagOffset);
281
+ template.content = repairedContent;
282
+ template.loc.source = repairedContent;
263
283
  }
264
284
  }
265
285
  }
@@ -273,11 +293,7 @@ function createBlock(compilerDom, node, source) {
273
293
  content = source.slice(start.offset, end.offset);
274
294
  } else {
275
295
  const offset = node.loc.source.indexOf("</");
276
- if (offset > -1) start = {
277
- line: start.line,
278
- column: start.column + offset,
279
- offset: start.offset + offset
280
- };
296
+ if (offset > -1) start = advancePosition(start, node.loc.source, 0, offset);
281
297
  end = { ...start };
282
298
  }
283
299
  const attrs = {};
@@ -374,6 +390,9 @@ function updateSfc(sfc, change) {
374
390
  const oldContent = hitBlock.content;
375
391
  const relativeStart = change.start - hitBlock.loc.start.offset;
376
392
  const relativeEnd = change.end - hitBlock.loc.start.offset;
393
+ const changeStartPosition = advancePosition(hitBlock.loc.start, oldContent, 0, relativeStart);
394
+ const oldChangeEndPosition = advancePosition(changeStartPosition, oldContent, relativeStart, relativeEnd);
395
+ const newChangeEndPosition = advancePosition(changeStartPosition, change.newText);
377
396
  const newContent = oldContent.slice(0, relativeStart) + change.newText + oldContent.slice(relativeEnd);
378
397
  const endTagRE = new RegExp(`</\\s*${hitBlock.type}\\s*>`);
379
398
  if (endTagRE.test(oldContent) !== endTagRE.test(newContent)) return;
@@ -381,8 +400,8 @@ function updateSfc(sfc, change) {
381
400
  hitBlock.loc.source = newContent;
382
401
  const lengthDiff = change.newText.length - (change.end - change.start);
383
402
  for (const block of blocks) {
384
- if (block.loc.start.offset > change.end) block.loc.start.offset += lengthDiff;
385
- if (block.loc.end.offset >= change.end) block.loc.end.offset += lengthDiff;
403
+ if (block.loc.start.offset > change.end) applyTextChangeToPosition(block.loc.start, oldChangeEndPosition, newChangeEndPosition, lengthDiff);
404
+ if (block.loc.end.offset >= change.end) applyTextChangeToPosition(block.loc.end, oldChangeEndPosition, newChangeEndPosition, lengthDiff);
386
405
  }
387
406
  const source = sfc.descriptor.source;
388
407
  sfc.descriptor.source = source.slice(0, change.start) + change.newText + source.slice(change.end);
package/dist/index.mjs CHANGED
@@ -215,6 +215,25 @@ function resolveEmbeddedJsonBlock(fileName, sfc, embeddedCode, tsModule, hasSche
215
215
  }
216
216
  //#endregion
217
217
  //#region src/parseSfc.ts
218
+ function advancePosition(position, source, start = 0, end = source.length) {
219
+ let lines = 0;
220
+ let lastNewline = -1;
221
+ for (let index = start; index < end; index += 1) if (source.charCodeAt(index) === 10) {
222
+ lines += 1;
223
+ lastNewline = index;
224
+ }
225
+ return {
226
+ offset: position.offset + end - start,
227
+ line: position.line + lines,
228
+ column: lastNewline < 0 ? position.column + end - start : end - lastNewline
229
+ };
230
+ }
231
+ function applyTextChangeToPosition(position, oldChangeEnd, newChangeEnd, lengthDiff) {
232
+ const oldLine = position.line;
233
+ position.offset += lengthDiff;
234
+ position.line += newChangeEnd.line - oldChangeEnd.line;
235
+ if (oldLine === oldChangeEnd.line) position.column += newChangeEnd.column - oldChangeEnd.column;
236
+ }
218
237
  function parseAttr(property, node) {
219
238
  if (!property.value) return true;
220
239
  const { source, start } = property.value.loc;
@@ -233,9 +252,10 @@ function repairIncompleteTemplate(descriptor, errors) {
233
252
  const endTagOffset = template.content.lastIndexOf("<");
234
253
  const endTagText = template.content.slice(endTagOffset).trimEnd();
235
254
  if ("</template>".startsWith(endTagText)) {
236
- template.loc.end.offset = template.loc.start.offset + endTagOffset;
237
- template.content = template.content.slice(0, endTagOffset);
238
- template.loc.source = template.content;
255
+ const repairedContent = template.content.slice(0, endTagOffset);
256
+ template.loc.end = advancePosition(template.loc.start, template.content, 0, endTagOffset);
257
+ template.content = repairedContent;
258
+ template.loc.source = repairedContent;
239
259
  }
240
260
  }
241
261
  }
@@ -249,11 +269,7 @@ function createBlock(compilerDom, node, source) {
249
269
  content = source.slice(start.offset, end.offset);
250
270
  } else {
251
271
  const offset = node.loc.source.indexOf("</");
252
- if (offset > -1) start = {
253
- line: start.line,
254
- column: start.column + offset,
255
- offset: start.offset + offset
256
- };
272
+ if (offset > -1) start = advancePosition(start, node.loc.source, 0, offset);
257
273
  end = { ...start };
258
274
  }
259
275
  const attrs = {};
@@ -350,6 +366,9 @@ function updateSfc(sfc, change) {
350
366
  const oldContent = hitBlock.content;
351
367
  const relativeStart = change.start - hitBlock.loc.start.offset;
352
368
  const relativeEnd = change.end - hitBlock.loc.start.offset;
369
+ const changeStartPosition = advancePosition(hitBlock.loc.start, oldContent, 0, relativeStart);
370
+ const oldChangeEndPosition = advancePosition(changeStartPosition, oldContent, relativeStart, relativeEnd);
371
+ const newChangeEndPosition = advancePosition(changeStartPosition, change.newText);
353
372
  const newContent = oldContent.slice(0, relativeStart) + change.newText + oldContent.slice(relativeEnd);
354
373
  const endTagRE = new RegExp(`</\\s*${hitBlock.type}\\s*>`);
355
374
  if (endTagRE.test(oldContent) !== endTagRE.test(newContent)) return;
@@ -357,8 +376,8 @@ function updateSfc(sfc, change) {
357
376
  hitBlock.loc.source = newContent;
358
377
  const lengthDiff = change.newText.length - (change.end - change.start);
359
378
  for (const block of blocks) {
360
- if (block.loc.start.offset > change.end) block.loc.start.offset += lengthDiff;
361
- if (block.loc.end.offset >= change.end) block.loc.end.offset += lengthDiff;
379
+ if (block.loc.start.offset > change.end) applyTextChangeToPosition(block.loc.start, oldChangeEndPosition, newChangeEndPosition, lengthDiff);
380
+ if (block.loc.end.offset >= change.end) applyTextChangeToPosition(block.loc.end, oldChangeEndPosition, newChangeEndPosition, lengthDiff);
362
381
  }
363
382
  const source = sfc.descriptor.source;
364
383
  sfc.descriptor.source = source.slice(0, change.start) + change.newText + source.slice(change.end);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weapp-vite/volar",
3
- "version": "2.1.10",
3
+ "version": "2.1.12",
4
4
  "description": "Volar plugin for weapp-vite - Provides IntelliSense and type checking for WeChat mini-program config blocks",
5
5
  "author": "ice breaker <1324318532@qq.com>",
6
6
  "license": "MIT",
@@ -48,7 +48,7 @@
48
48
  "registry": "https://registry.npmjs.org"
49
49
  },
50
50
  "dependencies": {
51
- "@weapp-core/schematics": "6.2.6"
51
+ "@weapp-core/schematics": "6.2.8"
52
52
  },
53
53
  "scripts": {
54
54
  "benchmark": "node --import tsx benchmark/sfc.ts",