@volar/source-map 2.3.0-alpha.9 → 2.3.1

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/index.d.ts CHANGED
@@ -1,5 +1,2 @@
1
- export * from 'muggle-string';
2
1
  export * from './lib/sourceMap';
3
2
  export * from './lib/translateOffset';
4
- export * from './lib/buildMappings';
5
- export * from './lib/buildStacks';
package/index.js CHANGED
@@ -14,9 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("muggle-string"), exports);
18
17
  __exportStar(require("./lib/sourceMap"), exports);
19
18
  __exportStar(require("./lib/translateOffset"), exports);
20
- __exportStar(require("./lib/buildMappings"), exports);
21
- __exportStar(require("./lib/buildStacks"), exports);
22
19
  //# sourceMappingURL=index.js.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.binarySearch = void 0;
3
+ exports.binarySearch = binarySearch;
4
4
  function binarySearch(values, searchValue) {
5
5
  let low = 0;
6
6
  let high = values.length - 1;
@@ -25,5 +25,4 @@ function binarySearch(values, searchValue) {
25
25
  const finalHigh = Math.min(Math.max(low, high, 0), values.length - 1);
26
26
  return { low: finalLow, high: finalHigh, match };
27
27
  }
28
- exports.binarySearch = binarySearch;
29
28
  //# sourceMappingURL=binarySearch.js.map
@@ -1,4 +1,4 @@
1
- export type CodeRangeKey = 'sourceOffsets' | 'generatedOffsets';
1
+ type CodeRangeKey = 'sourceOffsets' | 'generatedOffsets';
2
2
  export interface Mapping<Data = unknown> {
3
3
  sourceOffsets: number[];
4
4
  generatedOffsets: number[];
@@ -11,9 +11,13 @@ export declare class SourceMap<Data = unknown> {
11
11
  private sourceCodeOffsetsMemo;
12
12
  private generatedCodeOffsetsMemo;
13
13
  constructor(mappings: Mapping<Data>[]);
14
- getSourceOffsets(generatedOffset: number): Generator<readonly [number, Mapping<Data>], void, unknown>;
15
- getGeneratedOffsets(sourceOffset: number): Generator<readonly [number, Mapping<Data>], void, unknown>;
16
- findMatching(offset: number, fromRange: CodeRangeKey, toRange: CodeRangeKey): Generator<readonly [number, Mapping<Data>], void, unknown>;
14
+ toSourceRange(generatedStart: number, generatedEnd: number, fallbackToAnyMatch: boolean, filter?: (data: Data) => boolean): Generator<[mappedStart: number, mappedEnd: number, startMapping: Mapping<Data>, endMapping: Mapping<Data>], any, unknown>;
15
+ toGeneratedRange(sourceStart: number, sourceEnd: number, fallbackToAnyMatch: boolean, filter?: (data: Data) => boolean): Generator<[mappedStart: number, mappedEnd: number, startMapping: Mapping<Data>, endMapping: Mapping<Data>], any, unknown>;
16
+ toSourceLocation(generatedOffset: number, filter?: (data: Data) => boolean): Generator<readonly [number, Mapping<Data>], void, unknown>;
17
+ toGeneratedLocation(sourceOffset: number, filter?: (data: Data) => boolean): Generator<readonly [number, Mapping<Data>], void, unknown>;
18
+ findMatchingOffsets(offset: number, fromRange: CodeRangeKey, filter?: (data: Data) => boolean): Generator<readonly [number, Mapping<Data>], void, unknown>;
19
+ findMatchingStartEnd(start: number, end: number, fallbackToAnyMatch: boolean, fromRange: CodeRangeKey, filter?: (data: Data) => boolean): Generator<[mappedStart: number, mappedEnd: number, startMapping: Mapping<Data>, endMapping: Mapping<Data>]>;
17
20
  private getMemoBasedOnRange;
18
21
  private createMemo;
19
22
  }
23
+ export {};
package/lib/sourceMap.js CHANGED
@@ -7,25 +7,35 @@ class SourceMap {
7
7
  constructor(mappings) {
8
8
  this.mappings = mappings;
9
9
  }
10
- getSourceOffsets(generatedOffset) {
11
- return this.findMatching(generatedOffset, 'generatedOffsets', 'sourceOffsets');
10
+ toSourceRange(generatedStart, generatedEnd, fallbackToAnyMatch, filter) {
11
+ return this.findMatchingStartEnd(generatedStart, generatedEnd, fallbackToAnyMatch, 'generatedOffsets', filter);
12
12
  }
13
- getGeneratedOffsets(sourceOffset) {
14
- return this.findMatching(sourceOffset, 'sourceOffsets', 'generatedOffsets');
13
+ toGeneratedRange(sourceStart, sourceEnd, fallbackToAnyMatch, filter) {
14
+ return this.findMatchingStartEnd(sourceStart, sourceEnd, fallbackToAnyMatch, 'sourceOffsets', filter);
15
15
  }
16
- *findMatching(offset, fromRange, toRange) {
16
+ toSourceLocation(generatedOffset, filter) {
17
+ return this.findMatchingOffsets(generatedOffset, 'generatedOffsets', filter);
18
+ }
19
+ toGeneratedLocation(sourceOffset, filter) {
20
+ return this.findMatchingOffsets(sourceOffset, 'sourceOffsets', filter);
21
+ }
22
+ *findMatchingOffsets(offset, fromRange, filter) {
17
23
  const memo = this.getMemoBasedOnRange(fromRange);
18
24
  if (memo.offsets.length === 0) {
19
25
  return;
20
26
  }
21
27
  const { low: start, high: end } = (0, binarySearch_1.binarySearch)(memo.offsets, offset);
22
28
  const skip = new Set();
29
+ const toRange = fromRange == 'sourceOffsets' ? 'generatedOffsets' : 'sourceOffsets';
23
30
  for (let i = start; i <= end; i++) {
24
31
  for (const mapping of memo.mappings[i]) {
25
32
  if (skip.has(mapping)) {
26
33
  continue;
27
34
  }
28
35
  skip.add(mapping);
36
+ if (filter && !filter(mapping.data)) {
37
+ continue;
38
+ }
29
39
  const mapped = (0, translateOffset_1.translateOffset)(offset, mapping[fromRange], mapping[toRange], getLengths(mapping, fromRange), getLengths(mapping, toRange));
30
40
  if (mapped !== undefined) {
31
41
  yield [mapped, mapping];
@@ -33,6 +43,34 @@ class SourceMap {
33
43
  }
34
44
  }
35
45
  }
46
+ *findMatchingStartEnd(start, end, fallbackToAnyMatch, fromRange, filter) {
47
+ const toRange = fromRange == 'sourceOffsets' ? 'generatedOffsets' : 'sourceOffsets';
48
+ const mappedStarts = [];
49
+ let hadMatch = false;
50
+ for (const [mappedStart, mapping] of this.findMatchingOffsets(start, fromRange)) {
51
+ if (filter && !filter(mapping.data)) {
52
+ continue;
53
+ }
54
+ mappedStarts.push([mappedStart, mapping]);
55
+ const mappedEnd = (0, translateOffset_1.translateOffset)(end, mapping[fromRange], mapping[toRange], getLengths(mapping, fromRange), getLengths(mapping, toRange));
56
+ if (mappedEnd !== undefined) {
57
+ hadMatch = true;
58
+ yield [mappedStart, mappedEnd, mapping, mapping];
59
+ }
60
+ }
61
+ if (!hadMatch && fallbackToAnyMatch) {
62
+ for (const [mappedStart, mappingStart] of mappedStarts) {
63
+ for (const [mappedEnd, mappingEnd] of this.findMatchingOffsets(end, fromRange)) {
64
+ if (filter && !filter(mappingEnd.data) || mappedEnd < mappedStart) {
65
+ continue;
66
+ }
67
+ yield [mappedStart, mappedEnd, mappingStart, mappingEnd];
68
+ break;
69
+ }
70
+ ;
71
+ }
72
+ }
73
+ }
36
74
  getMemoBasedOnRange(fromRange) {
37
75
  return fromRange === 'sourceOffsets'
38
76
  ? this.sourceCodeOffsetsMemo ??= this.createMemo('sourceOffsets')
@@ -1,17 +1,29 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.translateOffset = void 0;
3
+ exports.translateOffset = translateOffset;
4
4
  function translateOffset(start, fromOffsets, toOffsets, fromLengths, toLengths = fromLengths) {
5
- for (let i = 0; i < fromOffsets.length; i++) {
6
- const fromOffset = fromOffsets[i];
7
- const fromLength = fromLengths[i];
5
+ const isSorted = fromOffsets.every((value, index) => index === 0 || fromOffsets[index - 1] <= value);
6
+ if (!isSorted) {
7
+ throw new Error('fromOffsets must be sorted in ascending order');
8
+ }
9
+ let low = 0;
10
+ let high = fromOffsets.length - 1;
11
+ while (low <= high) {
12
+ const mid = Math.floor((low + high) / 2);
13
+ const fromOffset = fromOffsets[mid];
14
+ const fromLength = fromLengths[mid];
8
15
  if (start >= fromOffset && start <= fromOffset + fromLength) {
9
- const toLength = toLengths[i];
10
- const toOffset = toOffsets[i];
16
+ const toLength = toLengths[mid];
17
+ const toOffset = toOffsets[mid];
11
18
  let rangeOffset = Math.min(start - fromOffset, toLength);
12
19
  return toOffset + rangeOffset;
13
20
  }
21
+ else if (start < fromOffset) {
22
+ high = mid - 1;
23
+ }
24
+ else {
25
+ low = mid + 1;
26
+ }
14
27
  }
15
28
  }
16
- exports.translateOffset = translateOffset;
17
29
  //# sourceMappingURL=translateOffset.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/source-map",
3
- "version": "2.3.0-alpha.9",
3
+ "version": "2.3.1",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "**/*.js",
@@ -11,8 +11,5 @@
11
11
  "url": "https://github.com/volarjs/volar.js.git",
12
12
  "directory": "packages/source-map"
13
13
  },
14
- "dependencies": {
15
- "muggle-string": "^0.4.0"
16
- },
17
- "gitHead": "3f741930343896dfc464a893ebe5f3619bb1a1aa"
14
+ "gitHead": "51742317a2950abd97e1a1a266b2c137bede4ad3"
18
15
  }
@@ -1,3 +0,0 @@
1
- import type { Segment } from 'muggle-string';
2
- import type { Mapping } from './sourceMap';
3
- export declare function buildMappings<T>(chunks: Segment<T>[]): Mapping<T>[];
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildMappings = void 0;
4
- function buildMappings(chunks) {
5
- let length = 0;
6
- const mappings = [];
7
- for (const segment of chunks) {
8
- if (typeof segment === 'string') {
9
- length += segment.length;
10
- }
11
- else {
12
- mappings.push({
13
- sourceOffsets: [segment[2]],
14
- generatedOffsets: [length],
15
- lengths: [segment[0].length],
16
- data: segment[3],
17
- });
18
- length += segment[0].length;
19
- }
20
- }
21
- return mappings;
22
- }
23
- exports.buildMappings = buildMappings;
24
- //# sourceMappingURL=buildMappings.js.map
@@ -1,6 +0,0 @@
1
- import type { Segment, StackNode } from 'muggle-string';
2
- export interface Stack {
3
- source: string;
4
- range: [number, number];
5
- }
6
- export declare function buildStacks<T>(chunks: Segment<T>[], stacks: StackNode[]): Stack[];
@@ -1,28 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildStacks = void 0;
4
- function buildStacks(chunks, stacks) {
5
- let offset = 0;
6
- let index = 0;
7
- const result = [];
8
- for (const stack of stacks) {
9
- const start = offset;
10
- for (let i = 0; i < stack.length; i++) {
11
- const segment = chunks[index + i];
12
- if (typeof segment === 'string') {
13
- offset += segment.length;
14
- }
15
- else {
16
- offset += segment[0].length;
17
- }
18
- }
19
- index += stack.length;
20
- result.push({
21
- source: stack.stack,
22
- range: [start, offset],
23
- });
24
- }
25
- return result;
26
- }
27
- exports.buildStacks = buildStacks;
28
- //# sourceMappingURL=buildStacks.js.map