cspell-lib 6.23.1 → 6.24.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,5 +1,15 @@
1
1
  export interface ExtendedSuggestion {
2
+ /**
3
+ * The suggestion.
4
+ */
2
5
  word: string;
6
+ /**
7
+ * The word is preferred above others, except other "preferred" words.
8
+ */
3
9
  isPreferred?: boolean;
10
+ /**
11
+ * The suggested word adjusted to match the original case.
12
+ */
13
+ wordAdjustedToMatchCase?: string;
4
14
  }
5
15
  //# sourceMappingURL=Suggestion.d.ts.map
@@ -3,7 +3,15 @@ export type LanguageSettings = LanguageSetting[];
3
3
  export declare function getDefaultLanguageSettings(): LanguageSettings;
4
4
  export declare function normalizeLanguageId(langId: LanguageId | LanguageId[]): Set<LanguageId>;
5
5
  export declare function normalizeLocale(locale: LocaleId | LocaleId[]): Set<LocaleId>;
6
+ export declare function normalizeLocaleIntl(locale: LocaleId | LocaleId[]): Set<LocaleId>;
6
7
  export declare function isLocaleInSet(locale: LocaleId | LocaleId[], setOfLocals: Set<LocaleId>): boolean;
8
+ /**
9
+ * Test if a locale should be ok with Intl
10
+ * @param locale - locale string
11
+ * @param strict - case must match
12
+ * @returns true if it matches the standard 2 letter or 4 letter forms.
13
+ */
14
+ export declare function isValidLocaleIntlFormat(locale: LocaleId | LocaleId[], strict?: boolean): boolean;
7
15
  export declare function calcSettingsForLanguage(languageSettings: LanguageSettings, languageId: LanguageId, locale: LocaleId): BaseSetting;
8
16
  export declare function calcUserSettingsForLanguage(settings: CSpellUserSettings, languageId: string): CSpellUserSettings;
9
17
  export declare function calcSettingsForLanguageId(baseSettings: CSpellUserSettings, languageId: LanguageId[] | LanguageId): CSpellUserSettings;
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.calcSettingsForLanguageId = exports.calcUserSettingsForLanguage = exports.calcSettingsForLanguage = exports.isLocaleInSet = exports.normalizeLocale = exports.normalizeLanguageId = exports.getDefaultLanguageSettings = void 0;
26
+ exports.calcSettingsForLanguageId = exports.calcUserSettingsForLanguage = exports.calcSettingsForLanguage = exports.isValidLocaleIntlFormat = exports.isLocaleInSet = exports.normalizeLocaleIntl = exports.normalizeLocale = exports.normalizeLanguageId = exports.getDefaultLanguageSettings = void 0;
27
27
  const Memorizer_1 = require("../util/Memorizer");
28
28
  const util_1 = require("../util/util");
29
29
  const SpellSettings = __importStar(require("./CSpellSettingsServer"));
@@ -62,11 +62,34 @@ function normalizeLocale(locale) {
62
62
  return _normalizeLocale(locale);
63
63
  }
64
64
  exports.normalizeLocale = normalizeLocale;
65
+ function normalizeLocaleIntl(locale) {
66
+ const values = [...normalizeLocale(locale)].map((locale) => locale.replace(/^([a-z]{2})-?([a-z]{2})$/, (_, lang, locale) => locale ? `${lang}-${locale.toUpperCase()}` : lang));
67
+ return new Set(values);
68
+ }
69
+ exports.normalizeLocaleIntl = normalizeLocaleIntl;
65
70
  function isLocaleInSet(locale, setOfLocals) {
66
71
  const locales = normalizeLocale(locale);
67
72
  return (0, util_1.doSetsIntersect)(locales, setOfLocals);
68
73
  }
69
74
  exports.isLocaleInSet = isLocaleInSet;
75
+ const regExpValidIntlLocaleStrict = /^[a-z]{2}(-[A-Z]{2})?$/;
76
+ const regExpValidIntlLocale = new RegExp(regExpValidIntlLocaleStrict, 'i');
77
+ /**
78
+ * Test if a locale should be ok with Intl
79
+ * @param locale - locale string
80
+ * @param strict - case must match
81
+ * @returns true if it matches the standard 2 letter or 4 letter forms.
82
+ */
83
+ function isValidLocaleIntlFormat(locale, strict = false) {
84
+ if (typeof locale === 'string')
85
+ return strict ? regExpValidIntlLocaleStrict.test(locale) : regExpValidIntlLocale.test(locale);
86
+ for (const item of locale) {
87
+ if (!isValidLocaleIntlFormat(item, strict))
88
+ return false;
89
+ }
90
+ return locale.length > 0;
91
+ }
92
+ exports.isValidLocaleIntlFormat = isValidLocaleIntlFormat;
70
93
  function calcSettingsForLanguage(languageSettings, languageId, locale) {
71
94
  languageId = languageId.toLowerCase();
72
95
  const allowedLocals = normalizeLocale(locale);
@@ -1,7 +1,16 @@
1
1
  import type { CSpellSettings, LocaleId } from '@cspell/cspell-types';
2
2
  import type { LanguageId } from './LanguageIds';
3
- import type { SuggestionResult } from './SpellingDictionary';
4
- interface SuggestedWordBase extends SuggestionResult {
3
+ import type { SpellingDictionaryCollection, SuggestionResult, SuggestOptions } from './SpellingDictionary';
4
+ export interface WordSuggestion extends SuggestionResult {
5
+ /**
6
+ * The suggested word adjusted to match the original case.
7
+ */
8
+ wordAdjustedToMatchCase?: string;
9
+ }
10
+ interface SuggestedWordBase extends WordSuggestion {
11
+ /**
12
+ * dictionary names
13
+ */
5
14
  dictionaries: string[];
6
15
  }
7
16
  export interface SuggestedWord extends SuggestedWordBase {
@@ -12,7 +21,8 @@ export interface SuggestionsForWordResult {
12
21
  word: string;
13
22
  suggestions: SuggestedWord[];
14
23
  }
15
- export interface SuggestionOptions {
24
+ type FromSuggestOptions = Pick<SuggestOptions, 'numChanges' | 'numSuggestions' | 'includeTies'>;
25
+ export interface SuggestionOptions extends FromSuggestOptions {
16
26
  /**
17
27
  * languageId to use when determining file type.
18
28
  */
@@ -34,18 +44,18 @@ export interface SuggestionOptions {
34
44
  * The number of suggestions to make.
35
45
  * @default 8
36
46
  */
37
- numSuggestions?: number;
47
+ numSuggestions?: number | undefined;
38
48
  /**
39
49
  * Max number of changes / edits to the word to get to a suggestion matching suggestion.
40
50
  * @default 4
41
51
  */
42
- numChanges?: number;
52
+ numChanges?: number | undefined;
43
53
  /**
44
54
  * If multiple suggestions have the same edit / change "cost", then included them even if
45
55
  * it causes more than `numSuggestions` to be returned.
46
56
  * @default true
47
57
  */
48
- includeTies?: boolean;
58
+ includeTies?: boolean | undefined;
49
59
  /**
50
60
  * By default we want to use the default configuration, but there are cases
51
61
  * where someone might not want that.
@@ -55,6 +65,7 @@ export interface SuggestionOptions {
55
65
  }
56
66
  export declare function suggestionsForWords(words: Iterable<string> | AsyncIterable<string>, options?: SuggestionOptions, settings?: CSpellSettings): AsyncIterable<SuggestionsForWordResult>;
57
67
  export declare function suggestionsForWord(word: string, options?: SuggestionOptions, settings?: CSpellSettings): Promise<SuggestionsForWordResult>;
68
+ export declare function calcSuggestionAdjustedToToMatchCase<T extends SuggestionResult>(originalWord: string, sugs: T[], locale: string | string[] | undefined, ignoreCase: boolean, dict: SpellingDictionaryCollection): (T & WordSuggestion)[];
58
69
  export declare class SuggestionError extends Error {
59
70
  readonly code: string;
60
71
  constructor(message: string, code: string);
@@ -22,21 +22,37 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
25
28
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.SuggestionError = exports.suggestionsForWord = exports.suggestionsForWords = void 0;
29
+ exports.SuggestionError = exports.calcSuggestionAdjustedToToMatchCase = exports.suggestionsForWord = exports.suggestionsForWords = void 0;
30
+ const assert_1 = __importDefault(require("assert"));
27
31
  const Settings_1 = require("./Settings");
28
32
  const LanguageSettings_1 = require("./Settings/LanguageSettings");
29
33
  const SpellingDictionary_1 = require("./SpellingDictionary");
34
+ const AutoResolve_1 = require("./util/AutoResolve");
35
+ const memorizeLastCall_1 = require("./util/memorizeLastCall");
30
36
  const util = __importStar(require("./util/util"));
31
- async function* suggestionsForWords(words, options, settings = {}) {
37
+ const emptySuggestionOptions = Object.freeze({});
38
+ const emptyCSpellSettings = Object.freeze({});
39
+ async function* suggestionsForWords(words, options, settings) {
32
40
  for await (const word of words) {
33
41
  yield await suggestionsForWord(word, options, settings);
34
42
  }
35
43
  }
36
44
  exports.suggestionsForWords = suggestionsForWords;
37
- async function suggestionsForWord(word, options, settings = {}) {
38
- const { languageId, locale: language, strict = true, numChanges = 4, numSuggestions = 8, includeTies = true, includeDefaultConfig = true, dictionaries, } = options || {};
39
- const ignoreCase = !strict;
45
+ const memorizeSuggestions = (0, memorizeLastCall_1.memorizeLastCall)(cacheSuggestionsForWord);
46
+ function cacheSuggestionsForWord(options, settings) {
47
+ const cache = (0, AutoResolve_1.createAutoResolveCache)();
48
+ return (word) => cache.get(word, (word) => _suggestionsForWord(word, options, settings));
49
+ }
50
+ function suggestionsForWord(word, options = emptySuggestionOptions, settings = emptyCSpellSettings) {
51
+ return memorizeSuggestions(options, settings)(word);
52
+ }
53
+ exports.suggestionsForWord = suggestionsForWord;
54
+ async function _suggestionsForWord(word, options, settings) {
55
+ const { languageId, locale: language, includeDefaultConfig = true, dictionaries } = options;
40
56
  async function determineDictionaries(config) {
41
57
  const withLocale = (0, Settings_1.mergeSettings)(config, util.clean({
42
58
  language: language || config.language,
@@ -59,12 +75,23 @@ async function suggestionsForWord(word, options, settings = {}) {
59
75
  ? (0, Settings_1.mergeSettings)((0, Settings_1.getDefaultSettings)(settings.loadDefaultConfiguration ?? true), (0, Settings_1.getGlobalSettings)(), settings)
60
76
  : settings;
61
77
  const { dictionaryCollection, allDictionaryCollection } = await determineDictionaries(config);
78
+ return _suggestionsForWordSync(word, options, settings, dictionaryCollection, allDictionaryCollection);
79
+ }
80
+ function _suggestionsForWordSync(word, options, settings, dictionaryCollection, allDictionaryCollection) {
81
+ const extendsDictionaryCollection = allDictionaryCollection || dictionaryCollection;
82
+ const { locale: language, strict = true, numChanges = 4, numSuggestions = 8, includeTies = true, includeDefaultConfig = true, } = options;
83
+ const ignoreCase = !strict;
84
+ const config = includeDefaultConfig
85
+ ? (0, Settings_1.mergeSettings)((0, Settings_1.getDefaultSettings)(settings.loadDefaultConfiguration ?? true), (0, Settings_1.getGlobalSettings)(), settings)
86
+ : settings;
62
87
  const opts = { ignoreCase, numChanges, numSuggestions, includeTies };
63
- const suggestionsByDictionary = dictionaryCollection.dictionaries.map((dict) => dict.suggest(word, opts).map((r) => ({ ...r, dictName: dict.name })));
64
- const combined = combine(util.flatten(suggestionsByDictionary).sort((a, b) => a.cost - b.cost || (a.word <= b.word ? -1 : 1)));
65
- const findOpts = {};
66
- const allSugs = combined.map((sug) => {
67
- const found = allDictionaryCollection.find(sug.word, findOpts);
88
+ const suggestionsByDictionary = dictionaryCollection.dictionaries.flatMap((dict) => dict.suggest(word, opts).map((r) => ({ ...r, dictName: dict.name })));
89
+ const locale = adjustLocale(language || config.language || undefined);
90
+ const collator = Intl.Collator(locale);
91
+ const combined = limitResults(combine(suggestionsByDictionary.sort((a, b) => a.cost - b.cost || collator.compare(a.word, b.word))), numSuggestions, includeTies);
92
+ const sugsAdjusted = calcSuggestionAdjustedToToMatchCase(word, combined, locale, ignoreCase, extendsDictionaryCollection);
93
+ const allSugs = sugsAdjusted.map((sug) => {
94
+ const found = extendsDictionaryCollection.find(sug.word);
68
95
  return {
69
96
  ...sug,
70
97
  forbidden: found?.forbidden || false,
@@ -76,7 +103,6 @@ async function suggestionsForWord(word, options, settings = {}) {
76
103
  suggestions: limitResults(allSugs, numSuggestions, includeTies),
77
104
  };
78
105
  }
79
- exports.suggestionsForWord = suggestionsForWord;
80
106
  function combine(suggestions) {
81
107
  const words = new Map();
82
108
  for (const sug of suggestions) {
@@ -89,6 +115,34 @@ function combine(suggestions) {
89
115
  }
90
116
  return [...words.values()];
91
117
  }
118
+ function adjustLocale(locale) {
119
+ if (!locale)
120
+ return undefined;
121
+ const locales = [...(0, LanguageSettings_1.normalizeLocaleIntl)(locale)].filter((locale) => (0, LanguageSettings_1.isValidLocaleIntlFormat)(locale));
122
+ if (!locales.length)
123
+ return undefined;
124
+ if (locales.length === 1)
125
+ return locales[0];
126
+ return locales;
127
+ }
128
+ function calcSuggestionAdjustedToToMatchCase(originalWord, sugs, locale, ignoreCase, dict) {
129
+ locale = adjustLocale(locale);
130
+ const knownSugs = new Set(sugs.map((sug) => sug.word));
131
+ const matchStyle = { ...analyzeCase(originalWord), locale, ignoreCase };
132
+ /* Add adjusted words */
133
+ return sugs.map((sug) => {
134
+ const alt = matchCase(sug.word, !!sug.isPreferred, matchStyle);
135
+ if (alt === sug.word || knownSugs.has(alt))
136
+ return sug;
137
+ const found = dict.find(alt);
138
+ if (!found || !found.forbidden || !found.noSuggest) {
139
+ knownSugs.add(alt);
140
+ return { ...sug, wordAdjustedToMatchCase: alt };
141
+ }
142
+ return sug;
143
+ });
144
+ }
145
+ exports.calcSuggestionAdjustedToToMatchCase = calcSuggestionAdjustedToToMatchCase;
92
146
  function limitResults(suggestions, numSuggestions, includeTies) {
93
147
  let cost = suggestions[0]?.cost;
94
148
  let i = 0;
@@ -110,6 +164,49 @@ function validateDictionaries(settings, dictionaries) {
110
164
  }
111
165
  }
112
166
  }
167
+ function matchCase(word, isPreferred, style) {
168
+ const locale = style.locale;
169
+ if (style.isMixedCaps) {
170
+ /**
171
+ * Do not try matching mixed caps.
172
+ */
173
+ return word;
174
+ }
175
+ if (hasCaps(word)) {
176
+ if (style.isAllCaps)
177
+ return word.toLocaleUpperCase(locale);
178
+ if (!style.ignoreCase || style.hasCaps || isPreferred)
179
+ return word;
180
+ if (isTitleCase(word) || isAllCaps(word))
181
+ return word.toLocaleLowerCase(locale);
182
+ return word;
183
+ }
184
+ if (!style.hasCaps)
185
+ return word;
186
+ if (style.isAllCaps)
187
+ return word.toLocaleUpperCase(locale);
188
+ (0, assert_1.default)(style.isTitleCase);
189
+ return word.replace(/^\p{L}/u, (firstLetter) => firstLetter.toLocaleUpperCase(locale));
190
+ }
191
+ const regExpHasCaps = /\p{Lu}/u;
192
+ const regExpIsAllCaps = /^[\P{L}\p{Lu}]+$/u;
193
+ const regExpIsTitleCase = /^\p{Lu}[\P{L}\p{Ll}]+$/u;
194
+ function analyzeCase(word) {
195
+ const hasCaps = regExpHasCaps.test(word);
196
+ const isAllCaps = hasCaps && regExpIsAllCaps.test(word);
197
+ const isTitleCase = hasCaps && !isAllCaps && regExpIsTitleCase.test(word);
198
+ const isMixedCaps = hasCaps && !isAllCaps && !isTitleCase;
199
+ return { hasCaps, isAllCaps, isMixedCaps, isTitleCase };
200
+ }
201
+ function hasCaps(word) {
202
+ return regExpHasCaps.test(word);
203
+ }
204
+ function isTitleCase(word) {
205
+ return regExpIsTitleCase.test(word);
206
+ }
207
+ function isAllCaps(word) {
208
+ return regExpIsAllCaps.test(word);
209
+ }
113
210
  class SuggestionError extends Error {
114
211
  constructor(message, code) {
115
212
  super(message);
@@ -0,0 +1,15 @@
1
+ export interface ValidateTextOptions {
2
+ /**
3
+ * Generate suggestions where there are spelling issues.
4
+ */
5
+ generateSuggestions?: boolean;
6
+ /**
7
+ * The number of suggestions to generate. The higher the number the longer it takes.
8
+ */
9
+ numSuggestions?: number;
10
+ /**
11
+ * Verify that the in-document directives are correct.
12
+ */
13
+ validateDirectives?: boolean;
14
+ }
15
+ //# sourceMappingURL=ValidateTextOptions.d.ts.map
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=ValidateTextOptions.js.map
@@ -6,8 +6,8 @@ import type { SpellingDictionaryCollection } from '../SpellingDictionary';
6
6
  import type { MatchRange } from '../util/TextRange';
7
7
  import type { TextValidator } from './lineValidatorFactory';
8
8
  import type { SimpleRange } from './parsedText';
9
+ import type { ValidateTextOptions } from './ValidateTextOptions';
9
10
  import type { ValidationOptions } from './ValidationTypes';
10
- import type { ValidateTextOptions } from './validator';
11
11
  export interface DocumentValidatorOptions extends ValidateTextOptions {
12
12
  /**
13
13
  * Optional path to a configuration file.
@@ -25,7 +25,6 @@ export interface DocumentValidatorOptions extends ValidateTextOptions {
25
25
  noConfigSearch?: boolean;
26
26
  }
27
27
  export declare class DocumentValidator {
28
- readonly options: DocumentValidatorOptions;
29
28
  readonly settings: CSpellUserSettings;
30
29
  private _document;
31
30
  private _ready;
@@ -34,6 +33,7 @@ export declare class DocumentValidator {
34
33
  private _preparations;
35
34
  private _preparationTime;
36
35
  private _suggestions;
36
+ readonly options: DocumentValidatorOptions;
37
37
  /**
38
38
  * @param doc - Document to validate
39
39
  * @param config - configuration to use (not finalized).
@@ -13,16 +13,16 @@ const Settings_1 = require("../Settings");
13
13
  const configLoader_1 = require("../Settings/Controller/configLoader");
14
14
  const InDocSettings_1 = require("../Settings/InDocSettings");
15
15
  const SpellingDictionary_1 = require("../SpellingDictionary");
16
+ const suggestions_1 = require("../suggestions");
16
17
  const errors_1 = require("../util/errors");
17
18
  const simpleCache_1 = require("../util/simpleCache");
18
19
  const timer_1 = require("../util/timer");
19
- const util_1 = require("../util/util");
20
20
  const defaultConstants_1 = require("./defaultConstants");
21
21
  const determineTextDocumentSettings_1 = require("./determineTextDocumentSettings");
22
22
  const lineValidatorFactory_1 = require("./lineValidatorFactory");
23
23
  const parsedText_1 = require("./parsedText");
24
+ const settingsToValidateOptions_1 = require("./settingsToValidateOptions");
24
25
  const textValidator_1 = require("./textValidator");
25
- const validator_1 = require("./validator");
26
26
  const ERROR_NOT_PREPARED = 'Validator Must be prepared before calling this function.';
27
27
  class DocumentValidator {
28
28
  /**
@@ -30,13 +30,17 @@ class DocumentValidator {
30
30
  * @param config - configuration to use (not finalized).
31
31
  */
32
32
  constructor(doc, options, settings) {
33
- this.options = options;
34
33
  this.settings = settings;
35
34
  this._ready = false;
36
35
  this.errors = [];
37
36
  this._preparationTime = -1;
38
37
  this._suggestions = new simpleCache_1.AutoCache((text) => this.genSuggestions(text), 1000);
39
38
  this._document = doc;
39
+ this.options = { ...options };
40
+ const numSuggestions = this.options.numSuggestions ?? settings.numSuggestions;
41
+ if (numSuggestions !== undefined) {
42
+ this.options.numSuggestions = numSuggestions;
43
+ }
40
44
  // console.error(`DocumentValidator: ${doc.uri}`);
41
45
  }
42
46
  get ready() {
@@ -73,7 +77,7 @@ class DocumentValidator {
73
77
  const uri = this._document.uri;
74
78
  const shouldCheck = !matcher.match(uri.fsPath) && (docSettings.enabled ?? true);
75
79
  const finalSettings = (0, Settings_1.finalizeSettings)(docSettings);
76
- const validateOptions = (0, validator_1.settingsToValidateOptions)(finalSettings);
80
+ const validateOptions = (0, settingsToValidateOptions_1.settingsToValidateOptions)(finalSettings);
77
81
  const includeRanges = (0, textValidator_1.calcTextInclusionRanges)(this._document.text, validateOptions);
78
82
  const segmenter = (0, parsedText_1.createMappedTextSegmenter)(includeRanges);
79
83
  const textValidator = (0, lineValidatorFactory_1.textValidatorFactory)(dict, validateOptions);
@@ -121,7 +125,7 @@ class DocumentValidator {
121
125
  const uri = this._document.uri;
122
126
  const shouldCheck = !matcher.match(uri.fsPath) && (docSettings.enabled ?? true);
123
127
  const finalSettings = (0, Settings_1.finalizeSettings)(docSettings);
124
- const validateOptions = (0, validator_1.settingsToValidateOptions)(finalSettings);
128
+ const validateOptions = (0, settingsToValidateOptions_1.settingsToValidateOptions)(finalSettings);
125
129
  const includeRanges = (0, textValidator_1.calcTextInclusionRanges)(this._document.text, validateOptions);
126
130
  const segmenter = (0, parsedText_1.createMappedTextSegmenter)(includeRanges);
127
131
  const textValidator = (0, lineValidatorFactory_1.textValidatorFactory)(dict, validateOptions);
@@ -149,7 +153,7 @@ class DocumentValidator {
149
153
  const dict = (0, SpellingDictionary_1.getDictionaryInternalSync)(docSettings);
150
154
  const shouldCheck = docSettings.enabled ?? true;
151
155
  const finalSettings = (0, Settings_1.finalizeSettings)(docSettings);
152
- const validateOptions = (0, validator_1.settingsToValidateOptions)(finalSettings);
156
+ const validateOptions = (0, settingsToValidateOptions_1.settingsToValidateOptions)(finalSettings);
153
157
  const includeRanges = (0, textValidator_1.calcTextInclusionRanges)(this._document.text, validateOptions);
154
158
  const segmenter = (0, parsedText_1.createMappedTextSegmenter)(includeRanges);
155
159
  const textValidator = (0, lineValidatorFactory_1.textValidatorFactory)(dict, validateOptions);
@@ -320,17 +324,18 @@ class DocumentValidator {
320
324
  (0, assert_1.default)(this._preparations, ERROR_NOT_PREPARED);
321
325
  const settings = this._preparations.docSettings;
322
326
  const dict = this._preparations.dictionary;
323
- const sugOptions = (0, util_1.clean)({
327
+ const sugOptions = {
324
328
  compoundMethod: 0,
325
329
  numSuggestions: this.options.numSuggestions,
326
330
  includeTies: false,
327
331
  ignoreCase: !(settings.caseSensitive ?? false),
328
332
  timeout: settings.suggestionsTimeout,
329
333
  numChanges: settings.suggestionNumChanges,
330
- });
331
- return dict
332
- .suggest(text, sugOptions)
333
- .map(({ word, isPreferred }) => (isPreferred ? { word, isPreferred } : { word }));
334
+ };
335
+ const locale = this._preparations.config.language;
336
+ const rawSuggestions = dict.suggest(text, sugOptions);
337
+ const sugsWithAlt = (0, suggestions_1.calcSuggestionAdjustedToToMatchCase)(text, rawSuggestions, locale, sugOptions.ignoreCase, dict);
338
+ return sugsWithAlt.map(sanitizeSuggestion);
334
339
  }
335
340
  getFinalizedDocSettings() {
336
341
  (0, assert_1.default)(this._ready);
@@ -356,6 +361,16 @@ class DocumentValidator {
356
361
  }
357
362
  }
358
363
  exports.DocumentValidator = DocumentValidator;
364
+ function sanitizeSuggestion(sug) {
365
+ const { word, isPreferred, wordAdjustedToMatchCase } = sug;
366
+ if (isPreferred && wordAdjustedToMatchCase)
367
+ return { word, wordAdjustedToMatchCase, isPreferred };
368
+ if (isPreferred)
369
+ return { word, isPreferred };
370
+ if (wordAdjustedToMatchCase)
371
+ return { word, wordAdjustedToMatchCase };
372
+ return { word };
373
+ }
359
374
  async function searchForDocumentConfig(document, defaultConfig, pnpSettings) {
360
375
  const { uri } = document;
361
376
  if (uri.scheme !== 'file')
@@ -6,7 +6,7 @@ export type { DocumentValidatorOptions } from './docValidator';
6
6
  export { DocumentValidator } from './docValidator';
7
7
  export type { Offset, SimpleRange } from './parsedText';
8
8
  export { calcTextInclusionRanges } from './textValidator';
9
+ export type { ValidateTextOptions } from './ValidateTextOptions';
9
10
  export type { CheckOptions, IncludeExcludeOptions, ValidationOptions } from './ValidationTypes';
10
- export type { ValidateTextOptions } from './validator';
11
11
  export { validateText } from './validator';
12
12
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,4 @@
1
+ import type { CSpellSettingsInternalFinalized } from '../Models/CSpellSettingsInternalDef';
2
+ import type { ValidationOptions } from './ValidationTypes';
3
+ export declare function settingsToValidateOptions(settings: CSpellSettingsInternalFinalized): ValidationOptions;
4
+ //# sourceMappingURL=settingsToValidateOptions.d.ts.map
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.settingsToValidateOptions = void 0;
4
+ function settingsToValidateOptions(settings) {
5
+ const opt = {
6
+ ...settings,
7
+ ignoreCase: !(settings.caseSensitive ?? false),
8
+ };
9
+ return opt;
10
+ }
11
+ exports.settingsToValidateOptions = settingsToValidateOptions;
12
+ //# sourceMappingURL=settingsToValidateOptions.js.map
@@ -1,26 +1,10 @@
1
1
  import type { CSpellUserSettings } from '@cspell/cspell-types';
2
- import type { CSpellSettingsInternalFinalized } from '../Models/CSpellSettingsInternalDef';
3
2
  import type { ValidationIssue } from '../Models/ValidationIssue';
4
- import type { ValidationOptions } from './ValidationTypes';
3
+ import type { ValidateTextOptions } from './ValidateTextOptions';
5
4
  export declare const diagSource = "cSpell Checker";
6
- export interface ValidateTextOptions {
7
- /**
8
- * Generate suggestions where there are spelling issues.
9
- */
10
- generateSuggestions?: boolean;
11
- /**
12
- * The number of suggestions to generate. The higher the number the longer it takes.
13
- */
14
- numSuggestions?: number;
15
- /**
16
- * Verify that the in-document directives are correct.
17
- */
18
- validateDirectives?: boolean;
19
- }
20
5
  /**
21
6
  * @deprecated
22
7
  * @deprecationMessage Use spellCheckDocument
23
8
  */
24
9
  export declare function validateText(text: string, settings: CSpellUserSettings, options?: ValidateTextOptions): Promise<ValidationIssue[]>;
25
- export declare function settingsToValidateOptions(settings: CSpellSettingsInternalFinalized): ValidationOptions;
26
10
  //# sourceMappingURL=validator.d.ts.map
@@ -23,13 +23,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.settingsToValidateOptions = exports.validateText = exports.diagSource = void 0;
26
+ exports.validateText = exports.diagSource = void 0;
27
27
  const cspell_types_1 = require("@cspell/cspell-types");
28
28
  const TextDocument_1 = require("../Models/TextDocument");
29
29
  const Settings = __importStar(require("../Settings"));
30
30
  const InDocSettings_1 = require("../Settings/InDocSettings");
31
31
  const SpellingDictionary_1 = require("../SpellingDictionary");
32
- const util_1 = require("../util/util");
32
+ const settingsToValidateOptions_1 = require("./settingsToValidateOptions");
33
33
  const textValidator_1 = require("./textValidator");
34
34
  exports.diagSource = 'cSpell Checker';
35
35
  /**
@@ -39,7 +39,7 @@ exports.diagSource = 'cSpell Checker';
39
39
  async function validateText(text, settings, options = {}) {
40
40
  const finalSettings = Settings.finalizeSettings(settings);
41
41
  const dict = await (0, SpellingDictionary_1.getDictionaryInternal)(finalSettings);
42
- const spellingIssues = [...(0, textValidator_1.validateText)(text, dict, settingsToValidateOptions(finalSettings))];
42
+ const spellingIssues = [...(0, textValidator_1.validateText)(text, dict, (0, settingsToValidateOptions_1.settingsToValidateOptions)(finalSettings))];
43
43
  const validationIssues = options.validateDirectives || finalSettings.validateDirectives
44
44
  ? (0, InDocSettings_1.validateInDocumentSettings)(text, settings)
45
45
  : [];
@@ -47,14 +47,14 @@ async function validateText(text, settings, options = {}) {
47
47
  if (!options.generateSuggestions) {
48
48
  return issues;
49
49
  }
50
- const sugOptions = (0, util_1.clean)({
50
+ const sugOptions = {
51
51
  numSuggestions: options.numSuggestions,
52
52
  compoundMethod: SpellingDictionary_1.CompoundWordsMethod.NONE,
53
53
  includeTies: false,
54
54
  ignoreCase: !(settings.caseSensitive ?? false),
55
55
  timeout: settings.suggestionsTimeout,
56
56
  numChanges: settings.suggestionNumChanges,
57
- });
57
+ };
58
58
  const withSugs = issues.map((t) => {
59
59
  const text = t.text;
60
60
  const suggestionsEx = dict
@@ -83,12 +83,4 @@ function mapValidationIssues(text, valIssues) {
83
83
  }
84
84
  return issues.map(toValidationIssue);
85
85
  }
86
- function settingsToValidateOptions(settings) {
87
- const opt = {
88
- ...settings,
89
- ignoreCase: !(settings.caseSensitive ?? false),
90
- };
91
- return opt;
92
- }
93
- exports.settingsToValidateOptions = settingsToValidateOptions;
94
86
  //# sourceMappingURL=validator.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cspell-lib",
3
- "version": "6.23.1",
3
+ "version": "6.24.0",
4
4
  "description": "A library of useful functions used across various cspell tools.",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -48,19 +48,19 @@
48
48
  },
49
49
  "homepage": "https://github.com/streetsidesoftware/cspell#readme",
50
50
  "dependencies": {
51
- "@cspell/cspell-bundled-dicts": "6.23.1",
52
- "@cspell/cspell-pipe": "6.23.1",
53
- "@cspell/cspell-types": "6.23.1",
54
- "@cspell/strong-weak-map": "6.23.1",
51
+ "@cspell/cspell-bundled-dicts": "6.24.0",
52
+ "@cspell/cspell-pipe": "6.24.0",
53
+ "@cspell/cspell-types": "6.24.0",
54
+ "@cspell/strong-weak-map": "6.24.0",
55
55
  "clear-module": "^4.1.2",
56
56
  "comment-json": "^4.2.3",
57
57
  "configstore": "^5.0.1",
58
58
  "cosmiconfig": "^8.0.0",
59
- "cspell-dictionary": "6.23.1",
60
- "cspell-glob": "6.23.1",
61
- "cspell-grammar": "6.23.1",
62
- "cspell-io": "6.23.1",
63
- "cspell-trie-lib": "6.23.1",
59
+ "cspell-dictionary": "6.24.0",
60
+ "cspell-glob": "6.24.0",
61
+ "cspell-grammar": "6.24.0",
62
+ "cspell-io": "6.24.0",
63
+ "cspell-trie-lib": "6.24.0",
64
64
  "fast-equals": "^4.0.3",
65
65
  "find-up": "^5.0.0",
66
66
  "gensequence": "^4.0.3",
@@ -92,5 +92,5 @@
92
92
  "rollup-plugin-dts": "^5.1.1",
93
93
  "ts-jest": "^29.0.5"
94
94
  },
95
- "gitHead": "dace8b0625beb2766565f47bc813dc0a45480dc0"
95
+ "gitHead": "0d1e8bf9426cd0bfb814df4f61da12d8aee57ddd"
96
96
  }