Skip to content

Commit

Permalink
feat: Use cspell-dictionary Module (#3686)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Sep 30, 2022
1 parent 1a4559d commit 6441f4b
Show file tree
Hide file tree
Showing 23 changed files with 57 additions and 1,583 deletions.
Expand Up @@ -3,14 +3,15 @@ import { __testing__ } from './SpellingDictionaryFromTrie';

const { outerWordForms } = __testing__;

// cspell:ignore guenstig günstig
// cspell:ignore guenstig günstig Bundesstaat Bundeßtaat

describe('SpellingDictionaryFromTrie', () => {
test.each`
word | repMap | expected
${'hello'} | ${undefined} | ${['hello']}
${'guenstig'} | ${[['ae', 'ä'], ['oe', 'ö'], ['ue', 'ü'], ['ss', 'ß']]} | ${['guenstig', 'günstig']}
${'günstig'} | ${[['ae', 'ä'], ['oe', 'ö'], ['ue', 'ü'], ['ss', 'ß']]} | ${['günstig', 'günstig'.normalize('NFD')]}
word | repMap | expected
${'hello'} | ${undefined} | ${['hello']}
${'guenstig'} | ${[['ae', 'ä'], ['oe', 'ö'], ['ue', 'ü'], ['ss', 'ß']]} | ${['guenstig', 'günstig']}
${'günstig'} | ${[['ae', 'ä'], ['oe', 'ö'], ['ue', 'ü'], ['ss', 'ß']]} | ${['günstig', 'günstig'.normalize('NFD')]}
${'Bundesstaat'} | ${[['ae', 'ä'], ['oe', 'ö'], ['ue', 'ü'], ['ss', 'ß']]} | ${['Bundesstaat', 'Bundeßtaat']}
`('outerWordForms $word', ({ word, repMap, expected }) => {
const mapWord = createMapper(repMap);
expect(outerWordForms(word, mapWord ?? ((a) => a))).toEqual(new Set(expected));
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-dictionary/src/SpellingDictionary/index.ts
@@ -1,5 +1,5 @@
export { CachingDictionary, createCachingDictionary } from './CachingDictionary';
export { createSpellingDictionary } from './createSpellingDictionary';
export { createSpellingDictionary, createFailedToLoadDictionary } from './createSpellingDictionary';
export { createForbiddenWordsDictionary } from './ForbiddenWordsDictionary';
export { createIgnoreWordsDictionary } from './IgnoreWordsDictionary';
export type {
Expand Down
Expand Up @@ -4,6 +4,7 @@ exports[`index verify api 1`] = `
[
"createCachingDictionary",
"createCollection",
"createFailedToLoadDictionary",
"createForbiddenWordsDictionary",
"createIgnoreWordsDictionary",
"createSpellingDictionary",
Expand Down
1 change: 1 addition & 0 deletions packages/cspell-dictionary/src/index.ts
@@ -1,6 +1,7 @@
export {
createCachingDictionary,
createCollection,
createFailedToLoadDictionary,
createForbiddenWordsDictionary,
createIgnoreWordsDictionary,
createSpellingDictionary,
Expand Down
50 changes: 9 additions & 41 deletions packages/cspell-lib/api/api.d.ts
Expand Up @@ -2,9 +2,9 @@
import { Glob, CSpellSettingsWithSourceTrace, AdvancedCSpellSettingsWithSourceTrace, Parser, DictionaryDefinitionPreferred, DictionaryDefinitionAugmented, DictionaryDefinitionCustom, TextOffset, TextDocumentOffset, PnPSettings as PnPSettings$1, ImportFileRef, CSpellUserSettings, Issue, MappedText, ParsedText, LocaleId, CSpellSettings } from '@cspell/cspell-types';
export * from '@cspell/cspell-types';
import * as cspellDictModule from 'cspell-dictionary';
import { SpellingDictionary, SpellingDictionaryOptions, HasOptions, FindResult, SuggestOptions, CachingDictionary, SpellingDictionaryCollection as SpellingDictionaryCollection$1, SuggestionResult as SuggestionResult$1 } from 'cspell-dictionary';
import { CachingDictionary, SpellingDictionaryCollection, SuggestionResult } from 'cspell-dictionary';
export { SpellingDictionary, SpellingDictionaryCollection, SuggestOptions, SuggestionCollector, SuggestionResult } from 'cspell-dictionary';
import { CompoundWordsMethod, SuggestionResult, SuggestionCollector, WeightMap } from 'cspell-trie-lib';
import { WeightMap } from 'cspell-trie-lib';
export { CompoundWordsMethod } from 'cspell-trie-lib';
export { asyncIterableToArray, readFile, readFileSync, writeToFile, writeToFileIterable, writeToFileIterableP } from 'cspell-io';
import { URI } from 'vscode-uri';
Expand Down Expand Up @@ -115,35 +115,8 @@ declare namespace index_link_d {
};
}

declare function identityString(w: string): string;
declare class SpellingDictionaryCollection implements SpellingDictionary {
readonly dictionaries: SpellingDictionary[];
readonly name: string;
readonly options: SpellingDictionaryOptions;
readonly mapWord: typeof identityString;
readonly type = "SpellingDictionaryCollection";
readonly source: string;
readonly isDictionaryCaseSensitive: boolean;
readonly containsNoSuggestWords: boolean;
constructor(dictionaries: SpellingDictionary[], name: string);
has(word: string, hasOptions?: HasOptions): boolean;
find(word: string, hasOptions?: HasOptions): FindResult | undefined;
isNoSuggestWord(word: string, options?: HasOptions): boolean;
isForbidden(word: string): boolean;
suggest(word: string, numSuggestions?: number, compoundMethod?: CompoundWordsMethod, numChanges?: number, ignoreCase?: boolean): SuggestionResult[];
suggest(word: string, suggestOptions: SuggestOptions): SuggestionResult[];
_suggest(word: string, suggestOptions: SuggestOptions): SuggestionResult[];
get size(): number;
genSuggestions(collector: SuggestionCollector, suggestOptions: SuggestOptions): void;
getErrors(): Error[];
private _isForbiddenInDict;
private _isNoSuggestWord;
}
declare function createCollection$1(dictionaries: SpellingDictionary[], name: string): SpellingDictionaryCollection;

interface IterableLike<T> {
[Symbol.iterator]: () => Iterator<T> | IterableIterator<T>;
}
declare const createSpellingDictionary: typeof cspellDictModule.createSpellingDictionary;
declare const createCollection: typeof cspellDictModule.createCollection;

/**
* The keys of an object where the values cannot be undefined.
Expand Down Expand Up @@ -179,6 +152,8 @@ interface DictionaryDefinitionInternal extends Readonly<DictionaryDefinitionPref
readonly __source?: string | undefined;
}

declare function refreshDictionaryCache(maxAge?: number): Promise<void>;

declare type LoadOptions = DictionaryDefinitionInternal;

declare class SpellingDictionaryLoadError extends Error {
Expand All @@ -190,13 +165,6 @@ declare class SpellingDictionaryLoadError extends Error {
}
declare function isSpellingDictionaryLoadError(e: Error): e is SpellingDictionaryLoadError;

declare function createSpellingDictionary$1(wordList: readonly string[] | IterableLike<string>, name: string, source: string, options: SpellingDictionaryOptions | undefined): SpellingDictionary;

declare const createSpellingDictionary: typeof createSpellingDictionary$1 | typeof cspellDictModule.createSpellingDictionary;
declare const createCollection: typeof createCollection$1 | typeof cspellDictModule.createCollection;

declare function refreshDictionaryCache(maxAge?: number): Promise<void>;

declare function stringToRegExp(pattern: string | RegExp, defaultFlags?: string, forceFlags?: string): RegExp | undefined;

declare function splitCamelCaseWordWithOffset(wo: TextOffset): Array<TextOffset>;
Expand Down Expand Up @@ -664,7 +632,7 @@ declare class DocumentValidator {
interface Preparations {
/** loaded config */
config: CSpellSettingsInternal;
dictionary: SpellingDictionaryCollection$1;
dictionary: SpellingDictionaryCollection;
/** configuration after applying in-doc settings */
docSettings: CSpellSettingsInternal;
finalSettings: CSpellSettingsInternalFinalized;
Expand Down Expand Up @@ -788,7 +756,7 @@ declare function fileToDocument(file: string, text: string, languageId?: string,
declare function fileToDocument(file: string, text?: string, languageId?: string, locale?: string): Document | DocumentWithText;
declare function fileToTextDocument(file: string): Promise<TextDocument>;

interface SuggestedWordBase extends SuggestionResult$1 {
interface SuggestedWordBase extends SuggestionResult {
dictionaries: string[];
}
interface SuggestedWord extends SuggestedWordBase {
Expand Down Expand Up @@ -900,6 +868,6 @@ interface ResolveFileResult {
declare function resolveFile(filename: string, relativeTo: string): ResolveFileResult;

declare function clearCachedFiles(): Promise<void>;
declare function getDictionary(settings: CSpellUserSettings): Promise<SpellingDictionaryCollection$1>;
declare function getDictionary(settings: CSpellUserSettings): Promise<SpellingDictionaryCollection>;

export { CheckTextInfo, ConfigurationDependencies, CreateTextDocumentParams, DetermineFinalDocumentSettingsResult, Document, DocumentValidator, DocumentValidatorOptions, ENV_CSPELL_GLOB_ROOT, ExcludeFilesGlobMap, ExclusionFunction, exclusionHelper_d as ExclusionHelper, FeatureFlag, FeatureFlags, ImportError, ImportFileRefWithError, IncludeExcludeFlag, IncludeExcludeOptions, index_link_d as Link, Logger, SpellCheckFileOptions, SpellCheckFileResult, SpellingDictionaryLoadError, SuggestedWord, SuggestionError, SuggestionOptions, SuggestionsForWordResult, text_d as Text, TextDocument, TextDocumentLine, TextInfoItem, TraceOptions, TraceResult, UnknownFeatureFlagError, ValidationIssue, calcOverrideSettings, checkFilenameMatchesGlob, checkText, checkTextDocument, clearCachedFiles, clearCachedSettingsFiles, combineTextAndLanguageSettings, combineTextAndLanguageSettings as constructSettingsForText, createSpellingDictionary, createCollection as createSpellingDictionaryCollection, createTextDocument, currentSettingsFileVersion, defaultConfigFilenames, defaultFileName, defaultFileName as defaultSettingsFilename, determineFinalDocumentSettings, extractDependencies, extractImportErrors, fileToDocument, fileToTextDocument, finalizeSettings, getCachedFileSize, getDefaultBundledSettings, getDefaultSettings, getDictionary, getGlobalSettings, getLanguagesForBasename as getLanguageIdsForBaseFilename, getLanguagesForExt, getLogger, getSources, getSystemFeatureFlags, isBinaryFile, isSpellingDictionaryLoadError, loadConfig, loadPnP, loadPnPSync, mergeInDocSettings, mergeSettings, readRawSettings, readSettings, readSettingsFiles, refreshDictionaryCache, resolveFile, searchForConfig, sectionCSpell, setLogger, spellCheckDocument, spellCheckFile, suggestionsForWord, suggestionsForWords, traceWords, traceWordsAsync, updateTextDocument, validateText };
15 changes: 9 additions & 6 deletions packages/cspell-lib/src/SpellingDictionary/Dictionaries.ts
@@ -1,8 +1,15 @@
import {
createCollection,
createForbiddenWordsDictionary,
createIgnoreWordsDictionary,
createSpellingDictionary,
SpellingDictionary,
SpellingDictionaryCollection,
} from 'cspell-dictionary';
import { CSpellSettingsInternal, DictionaryDefinitionInternal } from '../Models/CSpellSettingsInternalDef';
import { calcDictionaryDefsToLoad } from '../Settings/DictionarySettings';
import { isDefined } from '../util/util';
import { loadDictionary, loadDictionarySync, refreshCacheEntries } from './DictionaryLoader';
import { getSpellDictInterface, SpellingDictionary, SpellingDictionaryCollection } from './SpellingDictionary';

export function loadDictionaryDefs(defsToLoad: DictionaryDefinitionInternal[]): Promise<SpellingDictionary>[] {
return defsToLoad.map(loadDictionary);
Expand Down Expand Up @@ -33,8 +40,6 @@ function _getDictionaryInternal(
spellDictionaries: SpellingDictionary[]
): SpellingDictionaryCollection {
const { words = emptyWords, userWords = emptyWords, flagWords = emptyWords, ignoreWords = emptyWords } = settings;
const { createSpellingDictionary, createIgnoreWordsDictionary, createCollection, createForbiddenWordsDictionary } =
getSpellDictInterface();

const settingsWordsDictionary = createSpellingDictionary(words, '[words]', 'From Settings `words`', {
caseSensitive: true,
Expand All @@ -51,9 +56,7 @@ function _getDictionaryInternal(
'[ignoreWords]',
'From Settings `ignoreWords`'
);
const flagWordsDictionary = createForbiddenWordsDictionary(flagWords, '[flagWords]', 'From Settings `flagWords`', {
weightMap: undefined,
});
const flagWordsDictionary = createForbiddenWordsDictionary(flagWords, '[flagWords]', 'From Settings `flagWords`');
const dictionaries = [
...spellDictionaries,
settingsWordsDictionary,
Expand Down
@@ -1,15 +1,15 @@
import { opConcatMap, opFilter, opMap, pipe } from '@cspell/cspell-pipe/sync';
import type { DictionaryFileTypes } from '@cspell/cspell-types';
import { CSpellIO, Stats } from 'cspell-io';
import { DictionaryDefinitionInternal } from '../../Models/CSpellSettingsInternalDef';
import { toError } from '../../util/errors';
import {
createFailedToLoadDictionary,
createSpellingDictionary,
} from '../SpellingDictionaryLibOld/createSpellingDictionary';
import { SpellingDictionary } from '../SpellingDictionaryLibOld/SpellingDictionary';
createSpellingDictionaryFromTrieFile,
SpellingDictionary,
} from 'cspell-dictionary';
import { CSpellIO, Stats } from 'cspell-io';
import { DictionaryDefinitionInternal } from '../../Models/CSpellSettingsInternalDef';
import { toError } from '../../util/errors';
import { SpellingDictionaryLoadError } from '../SpellingDictionaryError';
import { createSpellingDictionaryTrie } from '../SpellingDictionaryLibOld/SpellingDictionaryFromTrie';

const MAX_AGE = 10000;

Expand Down Expand Up @@ -156,7 +156,12 @@ export class DictionaryLoader {
private loadEntry(uri: string, options: LoadOptions, now = Date.now()): CacheEntry {
options = this.normalizeOptions(uri, options);
const pDictionary = load(this.reader, uri, options).catch((e) =>
createFailedToLoadDictionary(new SpellingDictionaryLoadError(uri, options, e, 'failed to load'))
createFailedToLoadDictionary(
options.name,
uri,
new SpellingDictionaryLoadError(uri, options, e, 'failed to load'),
options
)
);
const pStat = this.getStat(uri);
const pending = Promise.all([pDictionary, pStat]);
Expand Down Expand Up @@ -201,7 +206,10 @@ export class DictionaryLoader {
} catch (e) {
const error = toError(e);
const dictionary = createFailedToLoadDictionary(
new SpellingDictionaryLoadError(uri, options, error, 'failed to load')
options.name,
uri,
new SpellingDictionaryLoadError(uri, options, error, 'failed to load'),
options
);
const pending = Promise.resolve([dictionary, stat] as const);
return {
Expand Down Expand Up @@ -351,10 +359,10 @@ function loadSimpleWordListSync(readLinesSync: ReaderSync, filename: string, opt

async function loadTrie(readLines: Reader, filename: string, options: LoadOptions) {
const lines = await readLines(filename);
return createSpellingDictionaryTrie(lines, options.name, filename, options);
return createSpellingDictionaryFromTrieFile(lines, options.name, filename, options);
}

function loadTrieSync(readLinesSync: ReaderSync, filename: string, options: LoadOptions) {
const lines = readLinesSync(filename);
return createSpellingDictionaryTrie(lines, options.name, filename, options);
return createSpellingDictionaryFromTrieFile(lines, options.name, filename, options);
}
Expand Up @@ -2,7 +2,7 @@ import { CSpellIO } from 'cspell-io';
import { DictionaryDefinitionInternal } from '../Models/CSpellSettingsInternalDef';
import { getCSpellIO } from '../static';
import { DictionaryLoader } from './DictionaryController';
import { SpellingDictionary } from './SpellingDictionaryLibOld/SpellingDictionary';
import { SpellingDictionary } from 'cspell-dictionary';
export type { LoadOptions } from './DictionaryController';

let loader: DictionaryLoader | undefined;
Expand Down
11 changes: 3 additions & 8 deletions packages/cspell-lib/src/SpellingDictionary/SpellingDictionary.ts
@@ -1,19 +1,15 @@
import * as cspellDictModule from 'cspell-dictionary';
import { getSystemFeatureFlags } from '../FeatureFlags';
import { SpellingDictionaryLibOld } from './SpellingDictionaryLibOld';
export { CompoundWordsMethod } from 'cspell-trie-lib';

const SpellingDictionaryModule = {
createCollection: cspellDictModule.createCollection,
createForbiddenWordsDictionary: cspellDictModule.createForbiddenWordsDictionary,
createSpellingDictionary: cspellDictModule.createSpellingDictionary,
createIgnoreWordsDictionary: cspellDictModule.createIgnoreWordsDictionary,
createSpellingDictionaryFromTrieFile: cspellDictModule.createSpellingDictionaryFromTrieFile,
} as const;

type SpellDictInterface = typeof SpellingDictionaryModule | typeof SpellingDictionaryLibOld;

const flagUseCSpellDictionary = 'use-cspell-dictionary';
getSystemFeatureFlags().register(flagUseCSpellDictionary, 'Use the CSpell Dictionary module.');
type SpellDictInterface = typeof SpellingDictionaryModule;

export type {
FindOptions,
Expand All @@ -29,8 +25,7 @@ export type {
} from 'cspell-dictionary';

export function getSpellDictInterface(): SpellDictInterface {
const useModule = getSystemFeatureFlags().getFlagBool(flagUseCSpellDictionary) ?? true;
return useModule ? SpellingDictionaryModule : SpellingDictionaryLibOld;
return SpellingDictionaryModule;
}

export const createSpellingDictionary = getSpellDictInterface().createSpellingDictionary;
Expand Down

0 comments on commit 6441f4b

Please sign in to comment.