cspell-lib 5.18.4 → 5.19.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.
Files changed (66) hide show
  1. package/dist/LanguageIds.d.ts +1 -1
  2. package/dist/LanguageIds.js +16 -11
  3. package/dist/Models/CSpellSettingsInternalDef.d.ts +3 -2
  4. package/dist/Models/CSpellSettingsInternalDef.js +3 -2
  5. package/dist/Models/TextDocument.d.ts +52 -0
  6. package/dist/Models/TextDocument.js +73 -0
  7. package/dist/Models/Uri.d.ts +5 -0
  8. package/dist/Models/Uri.js +16 -0
  9. package/dist/Settings/CSpellSettingsServer.d.ts +10 -8
  10. package/dist/Settings/CSpellSettingsServer.js +6 -2
  11. package/dist/Settings/DefaultSettings.js +11 -2
  12. package/dist/Settings/DictionarySettings.js +7 -9
  13. package/dist/Settings/ImportError.d.ts +1 -1
  14. package/dist/Settings/InDocSettings.js +7 -2
  15. package/dist/Settings/LanguageSettings.js +5 -1
  16. package/dist/Settings/TextDocumentSettings.d.ts +2 -1
  17. package/dist/Settings/TextDocumentSettings.js +5 -1
  18. package/dist/Settings/configLoader.d.ts +11 -1
  19. package/dist/Settings/configLoader.js +54 -13
  20. package/dist/Settings/link.js +8 -3
  21. package/dist/SpellingDictionary/Dictionaries.d.ts +2 -0
  22. package/dist/SpellingDictionary/Dictionaries.js +17 -5
  23. package/dist/SpellingDictionary/DictionaryLoader.d.ts +21 -2
  24. package/dist/SpellingDictionary/DictionaryLoader.js +179 -16
  25. package/dist/SpellingDictionary/SpellingDictionaryCollection.d.ts +0 -1
  26. package/dist/SpellingDictionary/SpellingDictionaryCollection.js +4 -10
  27. package/dist/SpellingDictionary/SpellingDictionaryFromTrie.d.ts +1 -1
  28. package/dist/SpellingDictionary/SpellingDictionaryFromTrie.js +4 -3
  29. package/dist/SpellingDictionary/SpellingDictionaryMethods.js +3 -2
  30. package/dist/SpellingDictionary/index.js +5 -1
  31. package/dist/index.d.ts +7 -4
  32. package/dist/index.js +15 -7
  33. package/dist/spellCheckFile.js +14 -51
  34. package/dist/suggestions.js +8 -4
  35. package/dist/textValidation/determineTextDocumentSettings.d.ts +17 -0
  36. package/dist/textValidation/determineTextDocumentSettings.js +63 -0
  37. package/dist/textValidation/docValidator.d.ts +45 -0
  38. package/dist/textValidation/docValidator.js +181 -0
  39. package/dist/textValidation/index.d.ts +7 -0
  40. package/dist/textValidation/index.js +12 -0
  41. package/dist/{textValidator.d.ts → textValidation/textValidator.d.ts} +18 -10
  42. package/dist/{textValidator.js → textValidation/textValidator.js} +59 -60
  43. package/dist/textValidation/validator.d.ts +31 -0
  44. package/dist/textValidation/validator.js +131 -0
  45. package/dist/trace.js +8 -4
  46. package/dist/util/Memorizer.d.ts +11 -0
  47. package/dist/util/Memorizer.js +15 -1
  48. package/dist/util/TextRange.js +5 -1
  49. package/dist/util/errors.d.ts +8 -0
  50. package/dist/util/errors.js +15 -1
  51. package/dist/util/fileReader.d.ts +1 -0
  52. package/dist/util/fileReader.js +6 -1
  53. package/dist/util/memorizerWeak.d.ts +6 -0
  54. package/dist/util/memorizerWeak.js +44 -0
  55. package/dist/util/resolveFile.js +5 -1
  56. package/dist/util/text.d.ts +9 -10
  57. package/dist/util/text.js +10 -4
  58. package/dist/util/types.d.ts +12 -0
  59. package/dist/util/util.d.ts +9 -1
  60. package/dist/util/util.js +17 -1
  61. package/dist/validator.d.ts +2 -31
  62. package/dist/validator.js +5 -124
  63. package/dist/wordListHelper.js +7 -5
  64. package/package.json +19 -17
  65. package/dist/SpellingDictionary/DictionaryLoaderTypes.d.ts +0 -1
  66. package/dist/SpellingDictionary/DictionaryLoaderTypes.js +0 -2
package/dist/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -22,7 +26,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
22
26
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
23
27
  };
24
28
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.getDictionary = exports.clearCachedFiles = exports.ExclusionHelper = exports.Link = exports.Text = exports.validateText = exports.IncludeExcludeFlag = exports.checkText = exports.resolveFile = exports.setLogger = exports.getLogger = exports.SuggestionError = exports.suggestionsForWords = exports.suggestionsForWord = exports.traceWordsAsync = exports.traceWords = exports.SpellingDictionaryLoadError = exports.SpellingDictionaryCollection = exports.refreshDictionaryCache = exports.isSpellingDictionaryLoadError = exports.createSpellingDictionary = exports.CompoundWordsMethod = exports.spellCheckFile = exports.spellCheckDocument = exports.isBinaryFile = exports.fileToDocument = exports.determineFinalDocumentSettings = exports.constructSettingsForText = exports.combineTextAndLanguageSettings = exports.defaultSettingsFilename = exports.getLanguagesForExt = void 0;
29
+ exports.getDictionary = exports.clearCachedFiles = exports.ExclusionHelper = exports.Link = exports.Text = exports.validateText = exports.IncludeExcludeFlag = exports.checkText = exports.resolveFile = exports.setLogger = exports.getLogger = exports.traceWordsAsync = exports.traceWords = exports.DocumentValidator = exports.suggestionsForWords = exports.suggestionsForWord = exports.SuggestionError = exports.SpellingDictionaryLoadError = exports.SpellingDictionaryCollection = exports.refreshDictionaryCache = exports.isSpellingDictionaryLoadError = exports.createSpellingDictionary = exports.CompoundWordsMethod = exports.spellCheckFile = exports.spellCheckDocument = exports.isBinaryFile = exports.fileToDocument = exports.determineFinalDocumentSettings = exports.constructSettingsForText = exports.combineTextAndLanguageSettings = exports.defaultSettingsFilename = exports.createTextDocument = exports.getLanguagesForExt = void 0;
26
30
  const ExclusionHelper = __importStar(require("./exclusionHelper"));
27
31
  exports.ExclusionHelper = ExclusionHelper;
28
32
  const Settings_1 = require("./Settings");
@@ -32,11 +36,13 @@ exports.Link = Link;
32
36
  const SpellingDictionary_1 = require("./SpellingDictionary");
33
37
  const Text = __importStar(require("./util/text"));
34
38
  exports.Text = Text;
39
+ __exportStar(require("@cspell/cspell-types"), exports);
35
40
  __exportStar(require("cspell-io"), exports);
36
41
  var LanguageIds_1 = require("./LanguageIds");
37
42
  Object.defineProperty(exports, "getLanguagesForExt", { enumerable: true, get: function () { return LanguageIds_1.getLanguagesForExt; } });
43
+ var TextDocument_1 = require("./Models/TextDocument");
44
+ Object.defineProperty(exports, "createTextDocument", { enumerable: true, get: function () { return TextDocument_1.createTextDocument; } });
38
45
  __exportStar(require("./Settings"), exports);
39
- __exportStar(require("@cspell/cspell-types"), exports);
40
46
  var Settings_2 = require("./Settings");
41
47
  Object.defineProperty(exports, "defaultSettingsFilename", { enumerable: true, get: function () { return Settings_2.defaultFileName; } });
42
48
  var TextDocumentSettings_1 = require("./Settings/TextDocumentSettings");
@@ -55,13 +61,15 @@ Object.defineProperty(exports, "isSpellingDictionaryLoadError", { enumerable: tr
55
61
  Object.defineProperty(exports, "refreshDictionaryCache", { enumerable: true, get: function () { return SpellingDictionary_2.refreshDictionaryCache; } });
56
62
  Object.defineProperty(exports, "SpellingDictionaryCollection", { enumerable: true, get: function () { return SpellingDictionary_2.SpellingDictionaryCollection; } });
57
63
  Object.defineProperty(exports, "SpellingDictionaryLoadError", { enumerable: true, get: function () { return SpellingDictionary_2.SpellingDictionaryLoadError; } });
58
- var trace_1 = require("./trace");
59
- Object.defineProperty(exports, "traceWords", { enumerable: true, get: function () { return trace_1.traceWords; } });
60
- Object.defineProperty(exports, "traceWordsAsync", { enumerable: true, get: function () { return trace_1.traceWordsAsync; } });
61
64
  var suggestions_1 = require("./suggestions");
65
+ Object.defineProperty(exports, "SuggestionError", { enumerable: true, get: function () { return suggestions_1.SuggestionError; } });
62
66
  Object.defineProperty(exports, "suggestionsForWord", { enumerable: true, get: function () { return suggestions_1.suggestionsForWord; } });
63
67
  Object.defineProperty(exports, "suggestionsForWords", { enumerable: true, get: function () { return suggestions_1.suggestionsForWords; } });
64
- Object.defineProperty(exports, "SuggestionError", { enumerable: true, get: function () { return suggestions_1.SuggestionError; } });
68
+ var textValidation_1 = require("./textValidation");
69
+ Object.defineProperty(exports, "DocumentValidator", { enumerable: true, get: function () { return textValidation_1.DocumentValidator; } });
70
+ var trace_1 = require("./trace");
71
+ Object.defineProperty(exports, "traceWords", { enumerable: true, get: function () { return trace_1.traceWords; } });
72
+ Object.defineProperty(exports, "traceWordsAsync", { enumerable: true, get: function () { return trace_1.traceWordsAsync; } });
65
73
  var logger_1 = require("./util/logger");
66
74
  Object.defineProperty(exports, "getLogger", { enumerable: true, get: function () { return logger_1.getLogger; } });
67
75
  Object.defineProperty(exports, "setLogger", { enumerable: true, get: function () { return logger_1.setLogger; } });
@@ -1,36 +1,16 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
2
  Object.defineProperty(exports, "__esModule", { value: true });
22
3
  exports.fileToDocument = exports.isBinaryFile = exports.isBinaryDoc = exports.determineFinalDocumentSettings = exports.spellCheckDocument = exports.spellCheckFile = void 0;
23
4
  const cspell_glob_1 = require("cspell-glob");
24
5
  const fs_extra_1 = require("fs-extra");
25
- const path = __importStar(require("path"));
26
6
  const vscode_uri_1 = require("vscode-uri");
7
+ const determineTextDocumentSettings_1 = require("./textValidation/determineTextDocumentSettings");
27
8
  const LanguageIds_1 = require("./LanguageIds");
9
+ const TextDocument_1 = require("./Models/TextDocument");
28
10
  const Settings_1 = require("./Settings");
29
- const TextDocumentSettings_1 = require("./Settings/TextDocumentSettings");
30
11
  const errors_1 = require("./util/errors");
31
- const Memorizer_1 = require("./util/Memorizer");
12
+ const util_1 = require("./util/util");
32
13
  const validator_1 = require("./validator");
33
- const getLanguagesForExt = (0, Memorizer_1.memorizer)(LanguageIds_1.getLanguagesForExt);
34
14
  const defaultEncoding = 'utf8';
35
15
  /**
36
16
  * Spell Check a file
@@ -96,7 +76,7 @@ async function spellCheckFullDocument(document, options, settings) {
96
76
  }
97
77
  const useSearchForConfig = (!options.noConfigSearch && !settings.noConfigSearch) || options.noConfigSearch === false;
98
78
  const pLocalConfig = options.configFile
99
- ? catchError(loadConfigFile(options.configFile, settings))
79
+ ? catchError((0, Settings_1.loadConfig)(options.configFile, settings))
100
80
  : useSearchForConfig
101
81
  ? catchError(searchForDocumentConfig(document, settings, settings))
102
82
  : undefined;
@@ -119,7 +99,7 @@ async function spellCheckFullDocument(document, options, settings) {
119
99
  const uri = vscode_uri_1.URI.parse(document.uri);
120
100
  const shouldCheck = !matcher.match(uri.fsPath) && ((_c = docSettings.settings.enabled) !== null && _c !== void 0 ? _c : true);
121
101
  const { generateSuggestions, numSuggestions } = options;
122
- const validateOptions = { generateSuggestions, numSuggestions };
102
+ const validateOptions = (0, util_1.clean)({ generateSuggestions, numSuggestions });
123
103
  const issues = shouldCheck ? await (0, validator_1.validateText)(document.text, docSettings.settings, validateOptions) : [];
124
104
  const result = {
125
105
  document,
@@ -139,9 +119,6 @@ async function searchForDocumentConfig(document, defaultConfig, pnpSettings) {
139
119
  return Promise.resolve(defaultConfig);
140
120
  return (0, Settings_1.searchForConfig)(u.fsPath, pnpSettings).then((s) => s || defaultConfig);
141
121
  }
142
- async function loadConfigFile(filename, pnpSettings) {
143
- return (0, Settings_1.loadConfig)(filename, pnpSettings);
144
- }
145
122
  async function readDocument(filename, encoding = defaultEncoding) {
146
123
  const text = await (0, fs_extra_1.readFile)(filename, encoding);
147
124
  const uri = vscode_uri_1.URI.file(filename).toString();
@@ -175,23 +152,15 @@ function isDocumentWithText(doc) {
175
152
  * @param settings - The near final settings. Should already be the combination of all configuration files.
176
153
  */
177
154
  function determineFinalDocumentSettings(document, settings) {
178
- var _a;
179
- const uri = vscode_uri_1.URI.parse(document.uri);
180
- const filename = uri.fsPath;
181
- const settingsWithDefaults = (0, Settings_1.mergeSettings)((0, Settings_1.getDefaultSettings)(), (0, Settings_1.getGlobalSettings)(), settings);
182
- const fileSettings = (0, Settings_1.calcOverrideSettings)(settingsWithDefaults, filename);
183
- const languageIds = ((_a = fileSettings === null || fileSettings === void 0 ? void 0 : fileSettings.languageId) === null || _a === void 0 ? void 0 : _a.length)
184
- ? fileSettings.languageId
185
- : document.languageId
186
- ? document.languageId
187
- : getLanguageForFilename(filename);
188
- if (document.locale) {
189
- fileSettings.language = document.locale;
190
- }
191
- const config = (0, TextDocumentSettings_1.combineTextAndLanguageSettings)(fileSettings, document.text, languageIds);
155
+ const doc = (0, TextDocument_1.createTextDocument)({
156
+ uri: document.uri,
157
+ content: document.text,
158
+ languageId: document.languageId,
159
+ locale: document.locale,
160
+ });
192
161
  return {
193
162
  document,
194
- settings: config,
163
+ settings: (0, determineTextDocumentSettings_1.determineTextDocumentSettings)(doc, settings),
195
164
  };
196
165
  }
197
166
  exports.determineFinalDocumentSettings = determineFinalDocumentSettings;
@@ -209,22 +178,16 @@ function isBinaryFile(filenameUri, languageId) {
209
178
  return (0, LanguageIds_1.isGeneratedFile)(filename);
210
179
  }
211
180
  exports.isBinaryFile = isBinaryFile;
212
- function getLanguageForFilename(filename) {
213
- const basename = path.basename(filename);
214
- const ext = path.extname(basename);
215
- const languagesExt = getLanguagesForExt(ext);
216
- return languagesExt.length ? languagesExt : (0, LanguageIds_1.getLanguagesForExt)(basename);
217
- }
218
181
  function normalizeLanguageIds(languageId) {
219
182
  return (Array.isArray(languageId) ? languageId.join(',') : languageId).split(',').map((s) => s.trim());
220
183
  }
221
184
  function fileToDocument(file, text, languageId, locale) {
222
- return {
185
+ return (0, util_1.clean)({
223
186
  uri: vscode_uri_1.URI.file(file).toString(),
224
187
  text,
225
188
  languageId,
226
189
  locale,
227
- };
190
+ });
228
191
  }
229
192
  exports.fileToDocument = fileToDocument;
230
193
  //# sourceMappingURL=spellCheckFile.js.map
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -35,13 +39,13 @@ async function suggestionsForWord(word, options, settings = {}) {
35
39
  const ignoreCase = !strict;
36
40
  async function determineDictionaries(config) {
37
41
  var _a, _b;
38
- const withLocale = (0, Settings_1.mergeSettings)(config, {
42
+ const withLocale = (0, Settings_1.mergeSettings)(config, util.clean({
39
43
  language: language || config.language,
40
44
  // dictionaries: dictionaries?.length ? dictionaries : config.dictionaries,
41
- });
45
+ }));
42
46
  const withLanguageId = (0, LanguageSettings_1.calcSettingsForLanguageId)(withLocale, (_a = languageId !== null && languageId !== void 0 ? languageId : withLocale.languageId) !== null && _a !== void 0 ? _a : 'plaintext');
43
47
  const settings = (0, Settings_1.finalizeSettings)(withLanguageId);
44
- settings.dictionaries = (dictionaries === null || dictionaries === void 0 ? void 0 : dictionaries.length) ? dictionaries : settings.dictionaries;
48
+ settings.dictionaries = (dictionaries === null || dictionaries === void 0 ? void 0 : dictionaries.length) ? dictionaries : settings.dictionaries || [];
45
49
  validateDictionaries(settings, dictionaries);
46
50
  const dictionaryCollection = await (0, SpellingDictionary_1.getDictionaryInternal)(settings);
47
51
  settings.dictionaries = ((_b = settings.dictionaryDefinitions) === null || _b === void 0 ? void 0 : _b.map((def) => def.name)) || [];
@@ -0,0 +1,17 @@
1
+ import { CSpellUserSettings } from '@cspell/cspell-types';
2
+ import { CSpellSettingsInternal } from '../Models/CSpellSettingsInternalDef';
3
+ import { TextDocument } from '../Models/TextDocument';
4
+ /**
5
+ * Combines all relevant setting values into a final configuration to be used for spell checking.
6
+ * It applies any overrides and appropriate language settings by taking into account the document type (languageId)
7
+ * the locale (natural language) and any in document settings.
8
+ *
9
+ * Note: this method will not search for configuration files. Configuration files should already be merged into `settings`.
10
+ * It is NOT necessary to include the cspell defaultSettings or globalSettings. They will be applied within this function.
11
+ * @param document - The document to be spell checked. Note: if the URI doesn't have a path, overrides cannot be applied.
12
+ * `locale` - if defined will be used unless it is overridden by an in-document setting.
13
+ * `languageId` - if defined will be used to select appropriate file type dictionaries.
14
+ * @param settings - The near final settings. Should already be the combination of all configuration files.
15
+ */
16
+ export declare function determineTextDocumentSettings(doc: TextDocument, settings: CSpellUserSettings): CSpellSettingsInternal;
17
+ //# sourceMappingURL=determineTextDocumentSettings.d.ts.map
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.determineTextDocumentSettings = void 0;
27
+ const path = __importStar(require("path"));
28
+ const LanguageIds_1 = require("../LanguageIds");
29
+ const Settings_1 = require("../Settings");
30
+ const TextDocumentSettings_1 = require("../Settings/TextDocumentSettings");
31
+ /**
32
+ * Combines all relevant setting values into a final configuration to be used for spell checking.
33
+ * It applies any overrides and appropriate language settings by taking into account the document type (languageId)
34
+ * the locale (natural language) and any in document settings.
35
+ *
36
+ * Note: this method will not search for configuration files. Configuration files should already be merged into `settings`.
37
+ * It is NOT necessary to include the cspell defaultSettings or globalSettings. They will be applied within this function.
38
+ * @param document - The document to be spell checked. Note: if the URI doesn't have a path, overrides cannot be applied.
39
+ * `locale` - if defined will be used unless it is overridden by an in-document setting.
40
+ * `languageId` - if defined will be used to select appropriate file type dictionaries.
41
+ * @param settings - The near final settings. Should already be the combination of all configuration files.
42
+ */
43
+ function determineTextDocumentSettings(doc, settings) {
44
+ var _a;
45
+ const filename = doc.uri.fsPath;
46
+ const settingsWithDefaults = (0, Settings_1.mergeSettings)((0, Settings_1.getDefaultSettings)(), (0, Settings_1.getGlobalSettings)(), settings);
47
+ const fileSettings = (0, Settings_1.calcOverrideSettings)(settingsWithDefaults, filename);
48
+ const languageIds = ((_a = fileSettings === null || fileSettings === void 0 ? void 0 : fileSettings.languageId) === null || _a === void 0 ? void 0 : _a.length)
49
+ ? fileSettings.languageId
50
+ : doc.languageId
51
+ ? doc.languageId
52
+ : getLanguageForFilename(filename);
53
+ if (doc.locale) {
54
+ fileSettings.language = doc.locale;
55
+ }
56
+ return (0, TextDocumentSettings_1.combineTextAndLanguageSettings)(fileSettings, doc.text, languageIds);
57
+ }
58
+ exports.determineTextDocumentSettings = determineTextDocumentSettings;
59
+ function getLanguageForFilename(filename) {
60
+ const basename = path.basename(filename);
61
+ return (0, LanguageIds_1.getLanguagesForBasename)(basename);
62
+ }
63
+ //# sourceMappingURL=determineTextDocumentSettings.js.map
@@ -0,0 +1,45 @@
1
+ import type { CSpellUserSettings } from '@cspell/cspell-types';
2
+ import { TextDocument } from '../Models/TextDocument';
3
+ import { ValidateTextOptions, ValidationIssue } from './validator';
4
+ export interface DocumentValidatorOptions extends ValidateTextOptions {
5
+ /**
6
+ * Optional path to a configuration file.
7
+ * If given, it will be used instead of searching for a configuration file.
8
+ */
9
+ configFile?: string;
10
+ /**
11
+ * Prevents searching for local configuration files
12
+ * By default the spell checker looks for configuration files
13
+ * starting at the location of given filename.
14
+ * If `configFile` is defined it will still be loaded instead of searching.
15
+ * `false` will override the value in `settings.noConfigSearch`.
16
+ * @defaultValue undefined
17
+ */
18
+ noConfigSearch?: boolean;
19
+ }
20
+ export declare class DocumentValidator {
21
+ readonly options: DocumentValidatorOptions;
22
+ readonly settings: CSpellUserSettings;
23
+ private _document;
24
+ private _ready;
25
+ readonly errors: Error[];
26
+ private _prepared;
27
+ private _preparations;
28
+ /**
29
+ * @param doc - Document to validate
30
+ * @param config - configuration to use (not finalized).
31
+ */
32
+ constructor(doc: TextDocument, options: DocumentValidatorOptions, settings: CSpellUserSettings);
33
+ get ready(): boolean;
34
+ prepareSync(): void;
35
+ prepare(): Promise<void>;
36
+ private _prepareAsync;
37
+ checkText(range: SimpleRange, _text: string, _scope: string[]): ValidationIssue[];
38
+ get document(): TextDocument;
39
+ private addPossibleError;
40
+ private catchError;
41
+ private errorCatcherWrapper;
42
+ }
43
+ export declare type Offset = number;
44
+ export declare type SimpleRange = readonly [Offset, Offset];
45
+ //# sourceMappingURL=docValidator.d.ts.map
@@ -0,0 +1,181 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DocumentValidator = void 0;
7
+ const assert_1 = __importDefault(require("assert"));
8
+ const Settings_1 = require("../Settings");
9
+ const configLoader_1 = require("../Settings/configLoader");
10
+ const SpellingDictionary_1 = require("../SpellingDictionary");
11
+ const errors_1 = require("../util/errors");
12
+ const util_1 = require("../util/util");
13
+ const determineTextDocumentSettings_1 = require("./determineTextDocumentSettings");
14
+ const textValidator_1 = require("./textValidator");
15
+ const validator_1 = require("./validator");
16
+ class DocumentValidator {
17
+ /**
18
+ * @param doc - Document to validate
19
+ * @param config - configuration to use (not finalized).
20
+ */
21
+ constructor(doc, options, settings) {
22
+ this.options = options;
23
+ this.settings = settings;
24
+ this._ready = false;
25
+ this.errors = [];
26
+ this._document = doc;
27
+ }
28
+ get ready() {
29
+ return this._ready;
30
+ }
31
+ prepareSync() {
32
+ var _a, _b;
33
+ // @todo
34
+ // Determine doc settings.
35
+ // Calc include ranges
36
+ // Load dictionaries
37
+ if (this._ready)
38
+ return;
39
+ const { options, settings } = this;
40
+ const useSearchForConfig = (!options.noConfigSearch && !settings.noConfigSearch) || options.noConfigSearch === false;
41
+ const optionsConfigFile = options.configFile;
42
+ const localConfig = optionsConfigFile
43
+ ? this.errorCatcherWrapper(() => (0, configLoader_1.loadConfigSync)(optionsConfigFile, settings))
44
+ : useSearchForConfig
45
+ ? this.errorCatcherWrapper(() => searchForDocumentConfigSync(this._document, settings, settings))
46
+ : undefined;
47
+ this.addPossibleError((_a = localConfig === null || localConfig === void 0 ? void 0 : localConfig.__importRef) === null || _a === void 0 ? void 0 : _a.error);
48
+ const config = localConfig ? (0, Settings_1.mergeSettings)(settings, localConfig) : settings;
49
+ const docSettings = (0, determineTextDocumentSettings_1.determineTextDocumentSettings)(this._document, config);
50
+ const dict = (0, SpellingDictionary_1.getDictionaryInternalSync)(docSettings);
51
+ const shouldCheck = (_b = docSettings.enabled) !== null && _b !== void 0 ? _b : true;
52
+ const validateOptions = (0, validator_1.settingsToValidateOptions)(docSettings);
53
+ const includeRanges = (0, textValidator_1.calcTextInclusionRanges)(this._document.text, docSettings);
54
+ const segmenter = (0, textValidator_1.mapLineSegmentAgainstRangesFactory)(includeRanges);
55
+ const lineValidator = (0, textValidator_1.lineValidatorFactory)(dict, validateOptions);
56
+ this._preparations = {
57
+ dictionary: dict,
58
+ docSettings,
59
+ shouldCheck,
60
+ validateOptions,
61
+ includeRanges,
62
+ segmenter,
63
+ lineValidator,
64
+ };
65
+ this._ready = true;
66
+ }
67
+ async prepare() {
68
+ if (this._ready)
69
+ return;
70
+ if (this._prepared)
71
+ return this._prepared;
72
+ this._prepared = this._prepareAsync();
73
+ return this._prepared;
74
+ }
75
+ async _prepareAsync() {
76
+ var _a, _b;
77
+ (0, assert_1.default)(!this._ready);
78
+ const { options, settings } = this;
79
+ const useSearchForConfig = (!options.noConfigSearch && !settings.noConfigSearch) || options.noConfigSearch === false;
80
+ const pLocalConfig = options.configFile
81
+ ? this.catchError((0, Settings_1.loadConfig)(options.configFile, settings))
82
+ : useSearchForConfig
83
+ ? this.catchError(searchForDocumentConfig(this._document, settings, settings))
84
+ : undefined;
85
+ const localConfig = await pLocalConfig;
86
+ this.addPossibleError((_a = localConfig === null || localConfig === void 0 ? void 0 : localConfig.__importRef) === null || _a === void 0 ? void 0 : _a.error);
87
+ const config = localConfig ? (0, Settings_1.mergeSettings)(settings, localConfig) : settings;
88
+ const docSettings = (0, determineTextDocumentSettings_1.determineTextDocumentSettings)(this._document, config);
89
+ const dict = await (0, SpellingDictionary_1.getDictionaryInternal)(docSettings);
90
+ const shouldCheck = (_b = docSettings.enabled) !== null && _b !== void 0 ? _b : true;
91
+ const validateOptions = (0, validator_1.settingsToValidateOptions)(docSettings);
92
+ const includeRanges = (0, textValidator_1.calcTextInclusionRanges)(this._document.text, docSettings);
93
+ const segmenter = (0, textValidator_1.mapLineSegmentAgainstRangesFactory)(includeRanges);
94
+ const lineValidator = (0, textValidator_1.lineValidatorFactory)(dict, validateOptions);
95
+ this._preparations = {
96
+ dictionary: dict,
97
+ docSettings,
98
+ shouldCheck,
99
+ validateOptions,
100
+ includeRanges,
101
+ segmenter,
102
+ lineValidator,
103
+ };
104
+ this._ready = true;
105
+ }
106
+ checkText(range, _text, _scope) {
107
+ var _a;
108
+ (0, assert_1.default)(this._ready);
109
+ (0, assert_1.default)(this._preparations);
110
+ // Determine settings for text range
111
+ // Slice text based upon include ranges
112
+ // Check text against dictionaries.
113
+ const offset = range[0];
114
+ const offsetEnd = range[1];
115
+ const text = this._document.text.slice(offset, offsetEnd);
116
+ const line = this._document.lineAt(offset);
117
+ const lineSeg = {
118
+ line,
119
+ segment: {
120
+ text,
121
+ offset,
122
+ },
123
+ };
124
+ const issues = [...this._preparations.lineValidator(lineSeg)];
125
+ if (!this.options.generateSuggestions) {
126
+ return issues;
127
+ }
128
+ const settings = this._preparations.docSettings;
129
+ const dict = this._preparations.dictionary;
130
+ const sugOptions = (0, util_1.clean)({
131
+ compoundMethod: 0,
132
+ numSuggestions: this.options.numSuggestions,
133
+ includeTies: false,
134
+ ignoreCase: !((_a = settings.caseSensitive) !== null && _a !== void 0 ? _a : false),
135
+ timeout: settings.suggestionsTimeout,
136
+ numChanges: settings.suggestionNumChanges,
137
+ });
138
+ const withSugs = issues.map((t) => {
139
+ const suggestions = dict.suggest(t.text, sugOptions).map((r) => r.word);
140
+ return { ...t, suggestions };
141
+ });
142
+ return withSugs;
143
+ }
144
+ get document() {
145
+ return this._document;
146
+ }
147
+ addPossibleError(error) {
148
+ if (!error)
149
+ return;
150
+ error = this.errors.push((0, errors_1.toError)(error));
151
+ }
152
+ catchError(p) {
153
+ return p.catch((error) => {
154
+ this.addPossibleError(error);
155
+ return undefined;
156
+ });
157
+ }
158
+ errorCatcherWrapper(fn) {
159
+ try {
160
+ return fn();
161
+ }
162
+ catch (error) {
163
+ this.addPossibleError(error);
164
+ }
165
+ return undefined;
166
+ }
167
+ }
168
+ exports.DocumentValidator = DocumentValidator;
169
+ async function searchForDocumentConfig(document, defaultConfig, pnpSettings) {
170
+ const { uri } = document;
171
+ if (uri.scheme !== 'file')
172
+ return Promise.resolve(defaultConfig);
173
+ return (0, Settings_1.searchForConfig)(uri.fsPath, pnpSettings).then((s) => s || defaultConfig);
174
+ }
175
+ function searchForDocumentConfigSync(document, defaultConfig, pnpSettings) {
176
+ const { uri } = document;
177
+ if (uri.scheme !== 'file')
178
+ defaultConfig;
179
+ return (0, configLoader_1.searchForConfigSync)(uri.fsPath, pnpSettings) || defaultConfig;
180
+ }
181
+ //# sourceMappingURL=docValidator.js.map
@@ -0,0 +1,7 @@
1
+ export { calcTextInclusionRanges } from './textValidator';
2
+ export type { CheckOptions, IncludeExcludeOptions, ValidationOptions, ValidationResult } from './textValidator';
3
+ export { checkText, IncludeExcludeFlag, validateText } from './validator';
4
+ export type { CheckTextInfo, TextInfoItem, ValidateTextOptions, ValidationIssue } from './validator';
5
+ export { DocumentValidator } from './docValidator';
6
+ export type { DocumentValidatorOptions } from './docValidator';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DocumentValidator = exports.validateText = exports.IncludeExcludeFlag = exports.checkText = exports.calcTextInclusionRanges = void 0;
4
+ var textValidator_1 = require("./textValidator");
5
+ Object.defineProperty(exports, "calcTextInclusionRanges", { enumerable: true, get: function () { return textValidator_1.calcTextInclusionRanges; } });
6
+ var validator_1 = require("./validator");
7
+ Object.defineProperty(exports, "checkText", { enumerable: true, get: function () { return validator_1.checkText; } });
8
+ Object.defineProperty(exports, "IncludeExcludeFlag", { enumerable: true, get: function () { return validator_1.IncludeExcludeFlag; } });
9
+ Object.defineProperty(exports, "validateText", { enumerable: true, get: function () { return validator_1.validateText; } });
10
+ var docValidator_1 = require("./docValidator");
11
+ Object.defineProperty(exports, "DocumentValidator", { enumerable: true, get: function () { return docValidator_1.DocumentValidator; } });
12
+ //# sourceMappingURL=index.js.map
@@ -1,7 +1,8 @@
1
- import type { TextOffset } from '@cspell/cspell-types';
1
+ import type { TextOffset as TextOffsetRW } from '@cspell/cspell-types';
2
2
  import { Sequence } from 'gensequence';
3
- import { SpellingDictionary } from './SpellingDictionary/SpellingDictionary';
4
- import * as TextRange from './util/TextRange';
3
+ import { SpellingDictionary } from '../SpellingDictionary/SpellingDictionary';
4
+ import * as TextRange from '../util/TextRange';
5
+ declare type TextOffsetRO = Readonly<TextOffsetRW>;
5
6
  export interface ValidationOptions extends IncludeExcludeOptions {
6
7
  maxNumberOfProblems?: number;
7
8
  maxDuplicateProblems?: number;
@@ -20,12 +21,12 @@ export interface IncludeExcludeOptions {
20
21
  includeRegExpList?: (RegExp | string)[];
21
22
  }
22
23
  export interface WordRangeAcc {
23
- textOffset: TextOffset;
24
+ textOffset: TextOffsetRO;
24
25
  isIncluded: boolean;
25
26
  rangePos: number;
26
27
  }
27
- export interface ValidationResult extends TextOffset {
28
- line: TextOffset;
28
+ export interface ValidationResult extends TextOffsetRW {
29
+ line: TextOffsetRW;
29
30
  isFlagged?: boolean;
30
31
  isFound?: boolean;
31
32
  }
@@ -35,19 +36,26 @@ export declare const defaultMinWordLength = 4;
35
36
  export declare const minWordSplitLen = 3;
36
37
  export declare function validateText(text: string, dict: SpellingDictionary, options: ValidationOptions): Sequence<ValidationResult>;
37
38
  export declare function calcTextInclusionRanges(text: string, options: IncludeExcludeOptions): TextRange.MatchRange[];
38
- export declare function isWordValid(dict: SpellingDictionary, wo: TextOffset, line: TextOffset, options: HasWordOptions): boolean;
39
+ export interface LineSegment {
40
+ line: TextOffsetRO;
41
+ segment: TextOffsetRO;
42
+ }
43
+ export declare type LineValidator = (line: LineSegment) => Iterable<ValidationResult>;
44
+ export declare function lineValidatorFactory(dict: SpellingDictionary, options: ValidationOptions): LineValidator;
45
+ export declare function isWordValid(dict: SpellingDictionary, wo: TextOffsetRO, line: TextOffsetRO, options: HasWordOptions): boolean;
39
46
  export interface HasWordOptions {
40
47
  ignoreCase: boolean;
41
48
  useCompounds: boolean | undefined;
42
49
  }
43
50
  export declare function hasWordCheck(dict: SpellingDictionary, word: string, options: HasWordOptions): boolean;
44
51
  /**
45
- * Returns a mapper function that will
52
+ * Returns a mapper function that will segment a TextOffset based upon the includeRanges.
53
+ * This function is optimized for forward scanning. It will perform poorly for randomly ordered offsets.
46
54
  * @param includeRanges Allowed ranges for words.
47
55
  */
48
- declare function mapTextOffsetsAgainstRanges(includeRanges: TextRange.MatchRange[]): (wo: TextOffset) => Iterable<TextOffset>;
56
+ export declare function mapLineSegmentAgainstRangesFactory(includeRanges: TextRange.MatchRange[]): (lineSeg: LineSegment) => LineSegment[];
49
57
  export declare const _testMethods: {
50
- mapWordsAgainstRanges: typeof mapTextOffsetsAgainstRanges;
58
+ mapWordsAgainstRanges: typeof mapLineSegmentAgainstRangesFactory;
51
59
  };
52
60
  export {};
53
61
  //# sourceMappingURL=textValidator.d.ts.map