/
Dictionaries.ts
70 lines (62 loc) · 2.96 KB
/
Dictionaries.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { CSpellSettingsInternal, DictionaryDefinitionInternal } from '../Models/CSpellSettingsInternalDef';
import { calcDictionaryDefsToLoad } from '../Settings/DictionarySettings';
import { isDefined } from '../util/util';
import { createForbiddenWordsDictionary, createSpellingDictionary } from './createSpellingDictionary';
import { loadDictionary, loadDictionarySync, refreshCacheEntries } from './DictionaryLoader';
import { SpellingDictionary } from './SpellingDictionary';
import { createCollection, SpellingDictionaryCollection } from './SpellingDictionaryCollection';
export function loadDictionaryDefs(defsToLoad: DictionaryDefinitionInternal[]): Promise<SpellingDictionary>[] {
return defsToLoad.map(loadDictionary);
}
export function loadDictionaryDefsSync(defsToLoad: DictionaryDefinitionInternal[]): SpellingDictionary[] {
return defsToLoad.map(loadDictionarySync);
}
export function refreshDictionaryCache(maxAge?: number): Promise<void> {
return refreshCacheEntries(maxAge);
}
const emptyWords: readonly string[] = Object.freeze([]);
export async function getDictionaryInternal(settings: CSpellSettingsInternal): Promise<SpellingDictionaryCollection> {
const spellDictionaries = await Promise.all(loadDictionaryDefs(calcDictionaryDefsToLoad(settings)));
return _getDictionaryInternal(settings, spellDictionaries);
}
export function getDictionaryInternalSync(settings: CSpellSettingsInternal): SpellingDictionaryCollection {
const spellDictionaries = loadDictionaryDefsSync(calcDictionaryDefsToLoad(settings));
return _getDictionaryInternal(settings, spellDictionaries);
}
function _getDictionaryInternal(
settings: CSpellSettingsInternal,
spellDictionaries: SpellingDictionary[]
): SpellingDictionaryCollection {
const { words = emptyWords, userWords = emptyWords, flagWords = emptyWords, ignoreWords = emptyWords } = settings;
const settingsWordsDictionary = createSpellingDictionary(words, '[words]', 'From Settings `words`', {
caseSensitive: true,
weightMap: undefined,
});
const settingsUserWordsDictionary = userWords.length
? createSpellingDictionary(userWords, '[userWords]', 'From Settings `userWords`', {
caseSensitive: true,
weightMap: undefined,
})
: undefined;
const ignoreWordsDictionary = createSpellingDictionary(
ignoreWords,
'[ignoreWords]',
'From Settings `ignoreWords`',
{
caseSensitive: true,
noSuggest: true,
weightMap: undefined,
}
);
const flagWordsDictionary = createForbiddenWordsDictionary(flagWords, '[flagWords]', 'From Settings `flagWords`', {
weightMap: undefined,
});
const dictionaries = [
...spellDictionaries,
settingsWordsDictionary,
settingsUserWordsDictionary,
ignoreWordsDictionary,
flagWordsDictionary,
].filter(isDefined);
return createCollection(dictionaries, 'dictionary collection');
}