cspell-lib 10.3.1 → 10.3.2

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,4 +1,4 @@
1
- import type { AdvancedCSpellSettingsWithSourceTrace, CSpellSettingsWithSourceTrace, Parser } from '@cspell/cspell-types';
1
+ import type { AdvancedCSpellSettingsWithSourceTrace, CSpellSettingsWithSourceTrace, DocumentParser, Parser } from '@cspell/cspell-types';
2
2
  import type { OptionalOrUndefined } from '../../util/types.js';
3
3
  import type { DictionaryDefinitionInternal } from './InternalDictionaryDef.js';
4
4
  export declare const SymbolCSpellSettingsInternal: unique symbol;
@@ -7,7 +7,7 @@ export interface CSpellSettingsInternal extends Omit<AdvancedCSpellSettingsWithS
7
7
  dictionaryDefinitions?: DictionaryDefinitionInternal[];
8
8
  }
9
9
  export interface CSpellSettingsInternalFinalized extends CSpellSettingsInternal {
10
- parserFn: Parser | undefined;
10
+ parserFn: Parser | DocumentParser | undefined;
11
11
  finalized: true;
12
12
  ignoreRegExpList: RegExp[];
13
13
  includeRegExpList: RegExp[];
@@ -33,7 +33,7 @@ export async function checkText(text, settings) {
33
33
  export async function checkTextOld(text, settings) {
34
34
  const validationResult = validateText(text, settings);
35
35
  const finalSettings = Settings.finalizeSettings(settings);
36
- const includeRanges = calcTextInclusionRanges(text, finalSettings);
36
+ const includeRanges = calcTextInclusionRanges(text, finalSettings).map(({ startPos, endPos }) => [startPos, endPos]);
37
37
  const issues = await validationResult;
38
38
  return genResult(text, issues, includeRanges);
39
39
  }
@@ -54,22 +54,35 @@ export async function checkTextDocument(doc, options, settings = {}) {
54
54
  return genCheckText(new DocumentValidator(doc, options, settings));
55
55
  }
56
56
  export async function genCheckText(docValidator) {
57
+ docValidator.recordCheckedRanges = true;
57
58
  await docValidator.prepare();
58
59
  const issues = docValidator.checkDocument(true);
59
60
  const preparations = docValidator._getPreparations();
60
61
  assert(preparations);
61
- return genResult(docValidator.document.text, issues, preparations.includeRanges);
62
+ return genResult(docValidator.document.text, issues, docValidator.getRangesChecked());
62
63
  }
63
64
  function genResult(text, issues, includeRanges) {
64
65
  const result = [];
65
66
  let lastPos = 0;
66
- for (const { startPos, endPos } of includeRanges) {
67
+ for (const [startPos, endPos] of includeRanges) {
68
+ if (lastPos < startPos) {
69
+ result.push({
70
+ text: text.slice(lastPos, startPos),
71
+ startPos: lastPos,
72
+ endPos: startPos,
73
+ flagIE: IncludeExcludeFlag.EXCLUDE,
74
+ });
75
+ }
76
+ else {
77
+ const prev = result.at(-1);
78
+ if (prev && prev.flagIE === IncludeExcludeFlag.INCLUDE && prev.endPos === startPos) {
79
+ prev.endPos = endPos;
80
+ prev.text = text.slice(prev.startPos, endPos);
81
+ lastPos = endPos;
82
+ continue;
83
+ }
84
+ }
67
85
  result.push({
68
- text: text.slice(lastPos, startPos),
69
- startPos: lastPos,
70
- endPos: startPos,
71
- flagIE: IncludeExcludeFlag.EXCLUDE,
72
- }, {
73
86
  text: text.slice(startPos, endPos),
74
87
  startPos,
75
88
  endPos,
@@ -34,6 +34,10 @@ export interface DocumentValidatorOptions extends ValidateTextOptions {
34
34
  * If true, the document will be checked even if it would normally be excluded.
35
35
  */
36
36
  forceCheck?: boolean;
37
+ /**
38
+ * If true, the ranges of text that have been checked will be recorded and available for later using the `getCheckedRanges` method.
39
+ */
40
+ recordCheckedRanges?: boolean;
37
41
  }
38
42
  type PerfTimings = Record<string, number>;
39
43
  export declare class DocumentValidator {
@@ -49,6 +53,7 @@ export declare class DocumentValidator {
49
53
  readonly options: DocumentValidatorOptions;
50
54
  readonly perfTiming: PerfTimings;
51
55
  skipValidation: boolean;
56
+ private rangesChecked;
52
57
  static create(doc: TextDocument, options: DocumentValidatorOptions, settingsOrConfigFile: CSpellUserSettings | ICSpellConfigFile): Promise<DocumentValidator>;
53
58
  /**
54
59
  * @param doc - Document to validate
@@ -91,11 +96,6 @@ export declare class DocumentValidator {
91
96
  checkDocumentDirectives(forceCheck?: boolean): ValidationIssue[];
92
97
  get document(): TextDocument;
93
98
  updateDocumentText(text: string): Promise<void>;
94
- /**
95
- * Get the calculated ranges of text that should be included in the spell checking.
96
- * @returns MatchRanges of text to include.
97
- */
98
- getCheckedTextRanges(): MatchRange[];
99
99
  traceWord(word: string): TraceResult;
100
100
  private defaultParser;
101
101
  private _checkParsedText;
@@ -119,6 +119,17 @@ export declare class DocumentValidator {
119
119
  * Internal `cspell-lib` use.
120
120
  */
121
121
  _getPreparations(): Preparations | undefined;
122
+ /**
123
+ * Get the calculated ranges of text that should be included in the spell checking.
124
+ *
125
+ * Use {@link getRangesChecked} to retrieve the ranges of text that have been checked.
126
+ *
127
+ * @returns MatchRanges of text to include.
128
+ */
129
+ getCheckedTextRanges(): MatchRange[];
130
+ getRangesChecked(): Iterable<SimpleRange>;
131
+ get recordCheckedRanges(): boolean;
132
+ set recordCheckedRanges(value: boolean);
122
133
  }
123
134
  declare function sanitizeSuggestion(sug: WordSuggestion): ExtendedSuggestion;
124
135
  interface Preparations {
@@ -34,6 +34,11 @@ export class DocumentValidator {
34
34
  options;
35
35
  perfTiming = {};
36
36
  skipValidation;
37
+ rangesChecked = [];
38
+ /**
39
+ * If true, the ranges of text that have been checked will be recorded and available for later using the `getCheckedRanges` method.
40
+ */
41
+ #recordCheckedRanges;
37
42
  static async create(doc, options, settingsOrConfigFile) {
38
43
  const settings = satisfiesCSpellConfigFile(settingsOrConfigFile)
39
44
  ? await resolveConfigFileImports(settingsOrConfigFile)
@@ -56,6 +61,7 @@ export class DocumentValidator {
56
61
  this.options.numSuggestions = numSuggestions;
57
62
  }
58
63
  this.skipValidation = !!options.skipValidation;
64
+ this.#recordCheckedRanges = !!options.recordCheckedRanges;
59
65
  // console.error(`DocumentValidator: ${doc.uri}`);
60
66
  }
61
67
  get ready() {
@@ -218,7 +224,15 @@ export class DocumentValidator {
218
224
  hasSimpleSuggestions,
219
225
  };
220
226
  }
221
- const issues = [...pipeSync(segmenter(parsedText), opConcatMap(textValidator.validate), opMap(mapToIssue))];
227
+ const skipRecordRanges = (iter) => iter;
228
+ const recordRanges = opMap((m) => {
229
+ this.rangesChecked.push(m.range[0], m.range[1]);
230
+ return m;
231
+ });
232
+ const recordRangesIfNeeded = this.recordCheckedRanges ? recordRanges : skipRecordRanges;
233
+ const issues = [
234
+ ...pipeSync(segmenter(parsedText), recordRangesIfNeeded, opConcatMap(textValidator.validate), opMap(mapToIssue)),
235
+ ];
222
236
  if (!this.options.generateSuggestions) {
223
237
  return issues.map((issue) => {
224
238
  if (!issue.suggestionsEx)
@@ -295,17 +309,10 @@ export class DocumentValidator {
295
309
  return this._document;
296
310
  }
297
311
  async updateDocumentText(text) {
312
+ this.rangesChecked.length = 0;
298
313
  updateTextDocument(this._document, [{ text }]);
299
314
  await this._updatePrep();
300
315
  }
301
- /**
302
- * Get the calculated ranges of text that should be included in the spell checking.
303
- * @returns MatchRanges of text to include.
304
- */
305
- getCheckedTextRanges() {
306
- assert(this._preparations, ERROR_NOT_PREPARED);
307
- return this._preparations.includeRanges;
308
- }
309
316
  traceWord(word) {
310
317
  assert(this._preparations, ERROR_NOT_PREPARED);
311
318
  return traceWord(word, this._preparations.dictionary, this._preparations.config);
@@ -347,7 +354,7 @@ export class DocumentValidator {
347
354
  const parser = this._preparations.finalSettings.parserFn;
348
355
  if (typeof parser !== 'object')
349
356
  return this.defaultParser();
350
- return parser.parse(this.document.text, toFilePathOrHref(documentUriToURL(this.document.uri))).parsedTexts;
357
+ return parseDocumentWithParser(parser, this.document).parsedTexts;
351
358
  }
352
359
  getSuggestions(text) {
353
360
  return this._suggestions.get(text);
@@ -412,6 +419,32 @@ export class DocumentValidator {
412
419
  _getPreparations() {
413
420
  return this._preparations;
414
421
  }
422
+ /**
423
+ * Get the calculated ranges of text that should be included in the spell checking.
424
+ *
425
+ * Use {@link getRangesChecked} to retrieve the ranges of text that have been checked.
426
+ *
427
+ * @returns MatchRanges of text to include.
428
+ */
429
+ getCheckedTextRanges() {
430
+ assert(this._preparations, ERROR_NOT_PREPARED);
431
+ return this._preparations.includeRanges;
432
+ }
433
+ *getRangesChecked() {
434
+ const ranges = this.rangesChecked;
435
+ for (let i = 0; i < ranges.length - 1; i += 2) {
436
+ const start = ranges[i];
437
+ const end = ranges[i + 1];
438
+ yield [start, end];
439
+ }
440
+ }
441
+ get recordCheckedRanges() {
442
+ return this.#recordCheckedRanges;
443
+ }
444
+ set recordCheckedRanges(value) {
445
+ this.rangesChecked.length = 0;
446
+ this.#recordCheckedRanges = value;
447
+ }
415
448
  }
416
449
  function sanitizeSuggestion(sug) {
417
450
  const { word, isPreferred, wordAdjustedToMatchCase } = sug;
@@ -434,6 +467,15 @@ async function searchForDocumentConfig(document, defaultConfig, pnpSettings) {
434
467
  throw e;
435
468
  }
436
469
  }
470
+ function parseDocumentWithParser(parser, document) {
471
+ if (isDocumentParser(parser)) {
472
+ return parser.parseDocument({ text: document.text, url: documentUriToURL(document.uri) });
473
+ }
474
+ return parser.parse(document.text, toFilePathOrHref(documentUriToURL(document.uri)));
475
+ }
476
+ function isDocumentParser(parser) {
477
+ return typeof parser.parseDocument === 'function';
478
+ }
437
479
  function mapSug(sug) {
438
480
  return { cost: 999, ...sug };
439
481
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public",
5
5
  "provenance": true
6
6
  },
7
- "version": "10.3.1",
7
+ "version": "10.3.2",
8
8
  "description": "A library of useful functions used across various cspell tools.",
9
9
  "type": "module",
10
10
  "sideEffects": false,
@@ -75,22 +75,22 @@
75
75
  },
76
76
  "homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-lib#readme",
77
77
  "dependencies": {
78
- "@cspell/cspell-bundled-dicts": "10.3.1",
79
- "@cspell/cspell-performance-monitor": "10.3.1",
80
- "@cspell/cspell-pipe": "10.3.1",
81
- "@cspell/cspell-resolver": "10.3.1",
82
- "@cspell/cspell-types": "10.3.1",
83
- "@cspell/dynamic-import": "10.3.1",
84
- "@cspell/filetypes": "10.3.1",
85
- "@cspell/rpc": "10.3.1",
86
- "@cspell/strong-weak-map": "10.3.1",
87
- "@cspell/url": "10.3.1",
88
- "cspell-config-lib": "10.3.1",
89
- "cspell-dictionary": "10.3.1",
90
- "cspell-glob": "10.3.1",
91
- "cspell-grammar": "10.3.1",
92
- "cspell-io": "10.3.1",
93
- "cspell-trie-lib": "10.3.1",
78
+ "@cspell/cspell-bundled-dicts": "10.3.2",
79
+ "@cspell/cspell-performance-monitor": "10.3.2",
80
+ "@cspell/cspell-pipe": "10.3.2",
81
+ "@cspell/cspell-resolver": "10.3.2",
82
+ "@cspell/cspell-types": "10.3.2",
83
+ "@cspell/dynamic-import": "10.3.2",
84
+ "@cspell/filetypes": "10.3.2",
85
+ "@cspell/rpc": "10.3.2",
86
+ "@cspell/strong-weak-map": "10.3.2",
87
+ "@cspell/url": "10.3.2",
88
+ "cspell-config-lib": "10.3.2",
89
+ "cspell-dictionary": "10.3.2",
90
+ "cspell-glob": "10.3.2",
91
+ "cspell-grammar": "10.3.2",
92
+ "cspell-io": "10.3.2",
93
+ "cspell-trie-lib": "10.3.2",
94
94
  "env-paths": "^4.0.0",
95
95
  "gensequence": "^8.0.8",
96
96
  "import-fresh": "^4.0.0",
@@ -103,7 +103,7 @@
103
103
  "node": ">=22.18.0"
104
104
  },
105
105
  "devDependencies": {
106
- "@cspell/cspell-tools": "10.3.1",
106
+ "@cspell/cspell-tools": "10.3.2",
107
107
  "@cspell/dict-cpp": "^7.1.0",
108
108
  "@cspell/dict-csharp": "^4.0.8",
109
109
  "@cspell/dict-css": "^4.1.2",
@@ -123,5 +123,5 @@
123
123
  "lorem-ipsum": "^3.0.0",
124
124
  "perf-insight": "^2.0.1"
125
125
  },
126
- "gitHead": "793953d1c2b4f1cab257389ff6ecb3ae2dd40230"
126
+ "gitHead": "d43b5ffd5fbb295fcfd06358cdfc7560b3a84a7f"
127
127
  }