@volar/source-map 0.40.1 → 0.40.4

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/out/index.d.ts CHANGED
@@ -50,7 +50,10 @@ export declare type Mapping<T> = MappingBase & {
50
50
  additional?: MappingBase[];
51
51
  };
52
52
  export declare class SourceMapBase<Data = undefined> {
53
- mappings: Mapping<Data>[];
53
+ private __mappings;
54
+ private __memo;
55
+ get mappings(): Mapping<Data>[];
56
+ set mappings(value: Mapping<Data>[]);
54
57
  constructor(_mappings?: Mapping<Data>[]);
55
58
  getSourceRange(start: number, end?: number, filter?: (data: Data) => boolean): [{
56
59
  start: number;
@@ -72,5 +75,6 @@ export declare class SourceMapBase<Data = undefined> {
72
75
  start: number;
73
76
  end: number;
74
77
  }, Data], void, unknown>;
78
+ private binarySearchMemo;
75
79
  private getRange;
76
80
  }
package/out/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  Object.defineProperty(exports, "__esModule", { value: true });
2
2
  exports.SourceMapBase = exports.Mode = void 0;
3
+ const reactivity_1 = require("@vue/reactivity");
3
4
  var Mode;
4
5
  (function (Mode) {
5
6
  /**
@@ -41,8 +42,72 @@ var Mode;
41
42
  })(Mode = exports.Mode || (exports.Mode = {}));
42
43
  class SourceMapBase {
43
44
  constructor(_mappings) {
45
+ this.__mappings = (0, reactivity_1.shallowRef)([]);
46
+ this.__memo = (0, reactivity_1.computed)(() => {
47
+ const self = this;
48
+ const source = createMemo('sourceRange');
49
+ const mapped = createMemo('mappedRange');
50
+ return {
51
+ source,
52
+ mapped,
53
+ };
54
+ function createMemo(key) {
55
+ const offsets = new Set();
56
+ for (const mapping of self.mappings) {
57
+ offsets.add(mapping[key].start);
58
+ offsets.add(mapping[key].end);
59
+ if (mapping.additional) {
60
+ for (const addition of mapping.additional) {
61
+ offsets.add(addition[key].start);
62
+ offsets.add(addition[key].end);
63
+ }
64
+ }
65
+ }
66
+ const arr = [...offsets].sort((a, b) => a - b).map(offset => ({ offset, mappings: new Set() }));
67
+ for (const mapping of self.mappings) {
68
+ const startIndex = binarySearch(mapping[key].start);
69
+ const endIndex = binarySearch(mapping[key].end);
70
+ for (let i = startIndex; i <= endIndex; i++) {
71
+ arr[i].mappings.add(mapping);
72
+ }
73
+ if (mapping.additional) {
74
+ for (const addition of mapping.additional) {
75
+ const startIndex = binarySearch(addition[key].start);
76
+ const endIndex = binarySearch(addition[key].end);
77
+ for (let i = startIndex; i <= endIndex; i++) {
78
+ arr[i].mappings.add(mapping);
79
+ }
80
+ }
81
+ }
82
+ }
83
+ return arr;
84
+ function binarySearch(start) {
85
+ let low = 0;
86
+ let high = arr.length - 1;
87
+ while (low <= high) {
88
+ const mid = Math.floor((low + high) / 2);
89
+ const midValue = arr[mid];
90
+ if (midValue.offset < start) {
91
+ low = mid + 1;
92
+ }
93
+ else if (midValue.offset > start) {
94
+ high = mid - 1;
95
+ }
96
+ else {
97
+ return mid;
98
+ }
99
+ }
100
+ }
101
+ }
102
+ });
44
103
  this.mappings = _mappings !== null && _mappings !== void 0 ? _mappings : [];
45
104
  }
105
+ get mappings() {
106
+ return this.__mappings.value;
107
+ }
108
+ set mappings(value) {
109
+ this.__mappings.value = value;
110
+ }
46
111
  getSourceRange(start, end, filter) {
47
112
  for (const mapped of this.getRanges(start, end !== null && end !== void 0 ? end : start, false, filter)) {
48
113
  return mapped;
@@ -60,26 +125,61 @@ class SourceMapBase {
60
125
  return this.getRanges(start, end !== null && end !== void 0 ? end : start, true, filter);
61
126
  }
62
127
  *getRanges(startOffset, endOffset, sourceToTarget, filter) {
63
- for (const mapping of this.mappings) {
64
- if (filter && !filter(mapping.data))
65
- continue;
66
- const mapped = this.getRange(startOffset, endOffset, sourceToTarget, mapping.mode, mapping.sourceRange, mapping.mappedRange, mapping.data);
67
- if (mapped) {
68
- yield getMapped(mapped);
69
- }
70
- else if (mapping.additional) {
71
- for (const other of mapping.additional) {
72
- const mapped = this.getRange(startOffset, endOffset, sourceToTarget, other.mode, other.sourceRange, other.mappedRange, mapping.data);
73
- if (mapped) {
74
- yield getMapped(mapped);
75
- break; // only return first match additional range
128
+ const memo = this.__memo.value;
129
+ const _memo = sourceToTarget ? memo.source : memo.mapped;
130
+ if (_memo.length === 0)
131
+ return;
132
+ const { low: start, high: end, } = startOffset === endOffset ? this.binarySearchMemo(_memo, startOffset) : {
133
+ low: this.binarySearchMemo(_memo, startOffset).low,
134
+ high: this.binarySearchMemo(_memo, endOffset).high,
135
+ };
136
+ const skip = new Set();
137
+ for (let i = start; i <= end; i++) {
138
+ for (const mapping of _memo[i].mappings) {
139
+ if (skip.has(mapping)) {
140
+ continue;
141
+ }
142
+ skip.add(mapping);
143
+ if (filter && !filter(mapping.data))
144
+ continue;
145
+ const mapped = this.getRange(startOffset, endOffset, sourceToTarget, mapping.mode, mapping.sourceRange, mapping.mappedRange, mapping.data);
146
+ if (mapped) {
147
+ yield mapped;
148
+ }
149
+ else if (mapping.additional) {
150
+ for (const other of mapping.additional) {
151
+ const mapped = this.getRange(startOffset, endOffset, sourceToTarget, other.mode, other.sourceRange, other.mappedRange, mapping.data);
152
+ if (mapped) {
153
+ yield mapped;
154
+ break; // only return first match additional range
155
+ }
76
156
  }
77
157
  }
78
158
  }
79
159
  }
80
- function getMapped(mapped) {
81
- return mapped;
160
+ }
161
+ binarySearchMemo(array, start) {
162
+ let low = 0;
163
+ let high = array.length - 1;
164
+ while (low <= high) {
165
+ const mid = Math.floor((low + high) / 2);
166
+ const midValue = array[mid];
167
+ if (midValue.offset < start) {
168
+ low = mid + 1;
169
+ }
170
+ else if (midValue.offset > start) {
171
+ high = mid - 1;
172
+ }
173
+ else {
174
+ low = mid;
175
+ high = mid;
176
+ break;
177
+ }
82
178
  }
179
+ return {
180
+ low: Math.max(Math.min(low, high, array.length - 1), 0),
181
+ high: Math.min(Math.max(low, high, 0), array.length - 1),
182
+ };
83
183
  }
84
184
  getRange(start, end, sourceToTarget, mode, sourceRange, targetRange, data) {
85
185
  const mappedToRange = sourceToTarget ? targetRange : sourceRange;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/source-map",
3
- "version": "0.40.1",
3
+ "version": "0.40.4",
4
4
  "main": "out/index.js",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -12,5 +12,8 @@
12
12
  "url": "https://github.com/johnsoncodehk/volar.git",
13
13
  "directory": "packages/source-map"
14
14
  },
15
- "gitHead": "4f92ef83a905c6d46cc92653ad87e0aec10f4e53"
15
+ "dependencies": {
16
+ "@vue/reactivity": "3.2.37"
17
+ },
18
+ "gitHead": "018d260d7bd00b06def4bcd600bf8d1e9347a7ec"
16
19
  }