cspell-grammar 9.6.4 → 9.7.0

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.
@@ -1,3 +1,5 @@
1
+ import type { ParsedText } from '@cspell/cspell-types';
1
2
  import type { MappedText } from './types.js';
3
+ export declare function appendParsedText(a: ParsedText, b: ParsedText): ParsedText;
2
4
  export declare function appendMappedText(a: MappedText, b: MappedText): MappedText;
3
5
  //# sourceMappingURL=appendMappedText.d.ts.map
@@ -1,19 +1,39 @@
1
1
  import assert from 'node:assert';
2
+ export function appendParsedText(a, b) {
3
+ const adjacent = a.range[1] === b.range[0];
4
+ if (adjacent && !a.map && !b.map) {
5
+ return {
6
+ text: a.text + b.text,
7
+ range: [a.range[0], b.range[1]],
8
+ };
9
+ }
10
+ const aMap = a.map || [a.range[1] - a.range[0], a.text.length];
11
+ const bMap = b.map || [b.range[1] - b.range[0], b.text.length];
12
+ const inject = adjacent ? [] : [b.range[0] - a.range[1], 0];
13
+ const map = [...aMap, ...inject, ...bMap];
14
+ return {
15
+ ...a,
16
+ ...b,
17
+ text: a.text + b.text,
18
+ range: [a.range[0], b.range[1]],
19
+ map,
20
+ };
21
+ }
2
22
  export function appendMappedText(a, b) {
3
- if (!a.map && !b.map) {
23
+ if (!a.offsetMap && !b.offsetMap) {
4
24
  return { text: a.text + b.text };
5
25
  }
6
26
  const aLen = a.text.length;
7
27
  const bLen = b.text.length;
8
- const aMap = [0, 0, ...(a.map || [0, 0, aLen, aLen])];
9
- const bMap = [0, 0, ...(b.map || [0, 0, bLen, bLen])];
28
+ const aMap = [0, 0, ...(a.offsetMap || [0, 0, aLen, aLen])];
29
+ const bMap = [0, 0, ...(b.offsetMap || [0, 0, bLen, bLen])];
10
30
  assert(aMap[aMap.length - 1] === aLen);
11
31
  assert(bMap[bMap.length - 1] === bLen);
12
32
  assert((aMap.length & 1) === 0);
13
33
  assert((bMap.length & 1) === 0);
14
34
  return {
15
35
  text: a.text + b.text,
16
- map: joinMaps(aMap, bMap),
36
+ offsetMap: joinMaps(aMap, bMap),
17
37
  };
18
38
  }
19
39
  function joinMaps(aMap, bMap) {
@@ -12,6 +12,7 @@ export interface MappedText {
12
12
  * where the `[0, 0]` is unnecessary.
13
13
  *
14
14
  */
15
- map?: number[] | undefined;
15
+ offsetMap?: number[] | undefined;
16
+ map?: never;
16
17
  }
17
18
  //# sourceMappingURL=types.d.ts.map
@@ -144,7 +144,7 @@ export function mapRawString(text) {
144
144
  }
145
145
  return {
146
146
  text: t,
147
- map,
147
+ offsetMap: map,
148
148
  };
149
149
  }
150
150
  //# sourceMappingURL=typescript.js.map
@@ -1,2 +1,2 @@
1
- export declare const parsers: import("@cspell/cspell-types/Parser").Parser[];
1
+ export declare const parsers: import("@cspell/cspell-types").Parser[];
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -1,6 +1,7 @@
1
+ import assert from 'node:assert';
1
2
  import { opFlatten, opMap, pipe } from '@cspell/cspell-pipe/sync';
2
3
  import { grammar } from '../../grammars/typescript.js';
3
- import { appendMappedText } from '../../mappers/appendMappedText.js';
4
+ import { appendParsedText } from '../../mappers/appendMappedText.js';
4
5
  import { mapRawString } from '../../mappers/typescript.js';
5
6
  import { compileGrammar } from '../../parser/grammar.js';
6
7
  import { createParser } from '../../parser/parser.js';
@@ -16,7 +17,7 @@ function* transform(texts) {
16
17
  yield {
17
18
  text: mapped.text,
18
19
  scope: scope?.parent,
19
- map: mapped.map,
20
+ map: absMapToRelMap(mapped.offsetMap),
20
21
  range: parsed.range,
21
22
  };
22
23
  continue;
@@ -50,12 +51,10 @@ function* mergeStringResults(results) {
50
51
  yield last;
51
52
  }
52
53
  function mergeParsedText(a, b) {
53
- const abT = appendMappedText(a, b);
54
+ const abT = appendParsedText(a, b);
54
55
  const ab = {
55
- text: abT.text,
56
+ ...abT,
56
57
  scope: a.scope,
57
- range: [a.range[0], b.range[1]],
58
- map: abT.map,
59
58
  delegate: a.delegate,
60
59
  };
61
60
  return ab;
@@ -87,4 +86,22 @@ function doesScopeMatch(s, match) {
87
86
  return false;
88
87
  return typeof s === 'string' ? s.startsWith(match) : s.value.startsWith(match);
89
88
  }
89
+ function absMapToRelMap(map) {
90
+ if (!map)
91
+ return undefined;
92
+ assert((map.length & 1) === 0, 'Map must be pairs of values.');
93
+ const relMap = [];
94
+ let base0 = 0;
95
+ let base1 = 0;
96
+ for (let i = 0; i < map.length; i += 2) {
97
+ const d0 = map[i] - base0;
98
+ const d1 = map[i + 1] - base1;
99
+ base0 += d0;
100
+ base1 += d1;
101
+ if (d0 === 0 && d1 === 0)
102
+ continue;
103
+ relMap.push(d0, d1);
104
+ }
105
+ return relMap;
106
+ }
90
107
  //# sourceMappingURL=TypeScriptParser.js.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public",
5
5
  "provenance": true
6
6
  },
7
- "version": "9.6.4",
7
+ "version": "9.7.0",
8
8
  "description": "Grammar parsing support for cspell",
9
9
  "keywords": [
10
10
  "cspell",
@@ -85,8 +85,8 @@
85
85
  "node": ">=20"
86
86
  },
87
87
  "dependencies": {
88
- "@cspell/cspell-pipe": "9.6.4",
89
- "@cspell/cspell-types": "9.6.4"
88
+ "@cspell/cspell-pipe": "9.7.0",
89
+ "@cspell/cspell-types": "9.7.0"
90
90
  },
91
- "gitHead": "e126c7f5708d4258ada35ba1d29d18952d7f0886"
91
+ "gitHead": "48f64e0bd95b39011af6dc80cd8ae4d519511f73"
92
92
  }