Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions cspell.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@
"type": "boolean"
},
"flagWords": {
"description": "List of words to always be considered incorrect.",
"description": "List of words to always be considered incorrect. Words found in `flagWords` override `words`.\n\nFormat of `flagWords`\n- single word entry - `word`\n- with suggestions - `word:suggestion` or `word->suggestion, suggestions`\n\nExample: ```ts \"flagWords\": [ \"color: colour\", \"incase: in case, encase\", \"canot->cannot\", \"cancelled->canceled\" ] ```",
"items": {
"type": "string"
},
Expand Down Expand Up @@ -769,7 +769,7 @@
"type": "array"
},
"words": {
"description": "List of words to be always considered correct.",
"description": "List of words to be considered correct.",
"items": {
"type": "string"
},
Expand Down Expand Up @@ -853,7 +853,7 @@
"description": "Glob pattern or patterns to match against."
},
"flagWords": {
"description": "List of words to always be considered incorrect.",
"description": "List of words to always be considered incorrect. Words found in `flagWords` override `words`.\n\nFormat of `flagWords`\n- single word entry - `word`\n- with suggestions - `word:suggestion` or `word->suggestion, suggestions`\n\nExample: ```ts \"flagWords\": [ \"color: colour\", \"incase: in case, encase\", \"canot->cannot\", \"cancelled->canceled\" ] ```",
"items": {
"type": "string"
},
Expand Down Expand Up @@ -963,7 +963,7 @@
"type": "boolean"
},
"words": {
"description": "List of words to be always considered correct.",
"description": "List of words to be considered correct.",
"items": {
"type": "string"
},
Expand Down Expand Up @@ -1293,7 +1293,7 @@
"type": "array"
},
"flagWords": {
"description": "List of words to always be considered incorrect.",
"description": "List of words to always be considered incorrect. Words found in `flagWords` override `words`.\n\nFormat of `flagWords`\n- single word entry - `word`\n- with suggestions - `word:suggestion` or `word->suggestion, suggestions`\n\nExample: ```ts \"flagWords\": [ \"color: colour\", \"incase: in case, encase\", \"canot->cannot\", \"cancelled->canceled\" ] ```",
"items": {
"type": "string"
},
Expand Down Expand Up @@ -1501,7 +1501,7 @@
"description": "Configuration format version of the settings file.\n\nThis controls how the settings in the configuration file behave."
},
"words": {
"description": "List of words to be always considered correct.",
"description": "List of words to be considered correct.",
"items": {
"type": "string"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { opMap, pipe } from '@cspell/cspell-pipe/sync';
import type { CompoundWordsMethod, SuggestionResult, Trie } from 'cspell-trie-lib';
import { buildTrieFast, parseDictionaryLines } from 'cspell-trie-lib';

import { defaultOptions } from './createSpellingDictionary';
import * as Defaults from './defaults';
import type {
FindResult,
Expand All @@ -13,6 +12,7 @@ import type {
SuggestArgs,
SuggestOptions,
} from './SpellingDictionary';
import { defaultOptions } from './SpellingDictionary';
import { SpellingDictionaryFromTrie } from './SpellingDictionaryFromTrie';
import { suggestArgsToSuggestOptions } from './SpellingDictionaryMethods';
import type { TyposDictionary } from './TyposDictionary';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { DictionaryInformation, ReplaceMap } from '@cspell/cspell-types';
import type { CompoundWordsMethod, SuggestionCollector, SuggestionResult, WeightMap } from 'cspell-trie-lib';

export { CompoundWordsMethod, SuggestionCollector, SuggestionResult } from 'cspell-trie-lib';
export type { DictionaryDefinitionInline } from '@cspell/cspell-types';
export { CompoundWordsMethod, type SuggestionCollector, type SuggestionResult } from 'cspell-trie-lib';

export interface SearchOptions {
/**
Expand Down Expand Up @@ -165,3 +166,7 @@ export type SuggestArgs =
ignoreCase?: boolean
) => SuggestionResult[]
>;

export const defaultOptions: SpellingDictionaryOptions = Object.freeze({
weightMap: undefined,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { DictionaryDefinitionInline } from '@cspell/cspell-types';

import { createInlineSpellingDictionary } from './createInlineSpellingDictionary';

describe('Validate createSpellingDictionary', () => {
test('createInlineSpellingDictionary', () => {
const words = ['one', 'two', 'three', 'left-right'];
const def: DictionaryDefinitionInline = {
name: 'test-dict',
words,
};
const d = createInlineSpellingDictionary(def, __filename);
words.forEach((w) => expect(d.has(w)).toBe(true));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { isDefined } from '../util/util';
import { createSpellingDictionary } from './createSpellingDictionary';
import { createFlagWordsDictionary } from './FlagWordsDictionary';
import { createIgnoreWordsDictionary } from './IgnoreWordsDictionary';
import type { DictionaryDefinitionInline, SpellingDictionary } from './SpellingDictionary';
import { createCollection } from './SpellingDictionaryCollection';

export function createInlineSpellingDictionary(
inlineDict: DictionaryDefinitionInline,
source: string
): SpellingDictionary {
const { words, flagWords, ignoreWords, name } = inlineDict;

const dictSources = [
words && createSpellingDictionary(words, name + '-words', source, inlineDict),
flagWords && createFlagWordsDictionary(flagWords, name + '-flag-words', source),
ignoreWords && createIgnoreWordsDictionary(ignoreWords, name + '-ignore-words', source),
].filter(isDefined);

return createCollection(dictSources, name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import { deepEqual } from 'fast-equals';
import type { IterableLike } from '../util/IterableLike';
import { AutoWeakCache, SimpleCache } from '../util/simpleCache';
import type { DictionaryInfo, SpellingDictionary, SpellingDictionaryOptions } from './SpellingDictionary';
import { defaultOptions } from './SpellingDictionary';
import { SpellingDictionaryFromTrie } from './SpellingDictionaryFromTrie';
import { createWeightMapFromDictionaryInformation } from './SpellingDictionaryMethods';

export const defaultOptions: SpellingDictionaryOptions = Object.freeze({
weightMap: undefined,
});

type CreateSpellingDictionaryParams = Parameters<typeof createSpellingDictionary>;

const cachedDictionaries = new AutoWeakCache<CreateSpellingDictionaryParams, SpellingDictionary>(
Expand Down
2 changes: 2 additions & 0 deletions packages/cspell-dictionary/src/SpellingDictionary/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
export { CachingDictionary, createCachingDictionary } from './CachingDictionary';
export { createInlineSpellingDictionary } from './createInlineSpellingDictionary';
export { createFailedToLoadDictionary, createSpellingDictionary } from './createSpellingDictionary';
export {
createFlagWordsDictionary,
createFlagWordsDictionary as createForbiddenWordsDictionary,
} from './FlagWordsDictionary';
export { createIgnoreWordsDictionary } from './IgnoreWordsDictionary';
export type {
DictionaryDefinitionInline,
FindOptions,
FindResult,
HasOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ exports[`index verify api 1`] = `
"createFlagWordsDictionary",
"createForbiddenWordsDictionary",
"createIgnoreWordsDictionary",
"createInlineSpellingDictionary",
"createSpellingDictionary",
"createSpellingDictionaryFromTrieFile",
]
Expand Down
1 change: 1 addition & 0 deletions packages/cspell-dictionary/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export {
createFlagWordsDictionary,
createForbiddenWordsDictionary,
createIgnoreWordsDictionary,
createInlineSpellingDictionary,
createSpellingDictionary,
createSpellingDictionaryFromTrieFile,
} from './SpellingDictionary';
17 changes: 9 additions & 8 deletions packages/cspell-lib/api/api.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/// <reference types="node" />
import { Glob, CSpellSettingsWithSourceTrace, TextOffset, TextDocumentOffset, AdvancedCSpellSettingsWithSourceTrace, Parser, DictionaryDefinitionPreferred, DictionaryDefinitionAugmented, DictionaryDefinitionCustom, PnPSettings as PnPSettings$1, ImportFileRef, CSpellUserSettings, Issue, MappedText, ParsedText, LocaleId, CSpellSettings } from '@cspell/cspell-types';
import { Glob, CSpellSettingsWithSourceTrace, TextOffset, TextDocumentOffset, AdvancedCSpellSettingsWithSourceTrace, Parser, DictionaryDefinitionInline, DictionaryDefinitionPreferred, DictionaryDefinitionAugmented, DictionaryDefinitionCustom, PnPSettings as PnPSettings$1, ImportFileRef, CSpellUserSettings, Issue, MappedText, ParsedText, LocaleId, CSpellSettings } from '@cspell/cspell-types';
export * from '@cspell/cspell-types';
import { URI } from 'vscode-uri';
import { WeightMap } from 'cspell-trie-lib';
export { CompoundWordsMethod } from 'cspell-trie-lib';
import * as cspellDictModule from 'cspell-dictionary';
import { CachingDictionary, SpellingDictionaryCollection, SuggestionResult } from 'cspell-dictionary';
export { SpellingDictionary, SpellingDictionaryCollection, SuggestOptions, SuggestionCollector, SuggestionResult } from 'cspell-dictionary';
export { SpellingDictionary, SpellingDictionaryCollection, SuggestOptions, SuggestionCollector, SuggestionResult, createSpellingDictionary, createCollection as createSpellingDictionaryCollection } from 'cspell-dictionary';
export { asyncIterableToArray, readFile, readFileSync, writeToFile, writeToFileIterable, writeToFileIterableP } from 'cspell-io';

type ExclusionFunction = (fileUri: string) => boolean;
Expand Down Expand Up @@ -365,7 +364,12 @@ interface CSpellSettingsInternalFinalized extends CSpellSettingsInternal {
includeRegExpList: RegExp[];
}
type DictionaryDefinitionCustomUniqueFields = Omit<DictionaryDefinitionCustom, keyof DictionaryDefinitionPreferred>;
interface DictionaryDefinitionInternal extends Readonly<DictionaryDefinitionPreferred>, Readonly<Partial<DictionaryDefinitionCustomUniqueFields>>, Readonly<DictionaryDefinitionAugmented> {
type DictionaryDefinitionInternal = DictionaryFileDefinitionInternal | DictionaryDefinitionInlineInternal;
type DictionaryDefinitionInlineInternal = DictionaryDefinitionInline & {
/** The path to the config file that contains this dictionary definition */
readonly __source?: string | undefined;
};
interface DictionaryFileDefinitionInternal extends Readonly<DictionaryDefinitionPreferred>, Readonly<Partial<DictionaryDefinitionCustomUniqueFields>>, Readonly<DictionaryDefinitionAugmented> {
/**
* Optional weight map used to improve suggestions.
*/
Expand Down Expand Up @@ -501,9 +505,6 @@ interface ValidationIssue extends ValidationResult {

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

declare const createSpellingDictionary: typeof cspellDictModule.createSpellingDictionary;
declare const createCollection: typeof cspellDictModule.createCollection;

type LoadOptions = DictionaryDefinitionInternal;

declare class SpellingDictionaryLoadError extends Error {
Expand Down Expand Up @@ -898,4 +899,4 @@ declare function clearCachedFiles(): Promise<void>;
*/
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 };
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, 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 };
3 changes: 3 additions & 0 deletions packages/cspell-lib/fixtures/dictionaries/inline/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Inline Dictionaries

It is possible to fully define a dictionary in the file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
dictionaryDefinitions:
- name: cities
words:
- Amsterdam
- Angles
- cities
- City
- Configuration
- Default
- define
- Definitions
- Delhi
- dictionaries
- dictionary
- "false"
- file
- Francisco
- fully
- Inline
- Las
- load
- London
- Mexico
- name
- New
- Paris
- possible
- San
- words
- York

dictionaries:
- cities
loadDefaultConfiguration: false
8 changes: 8 additions & 0 deletions packages/cspell-lib/fixtures/dictionaries/inline/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
New York
New Amsterdam
Las Angles
San Francisco
New Delhi
Mexico City
London
Paris
11 changes: 3 additions & 8 deletions packages/cspell-lib/src/LanguageIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* ```
*/

import { autoResolve } from './util/AutoResolve';

// cspell:ignore cljs cljx cson iname pcregrep fsscript gradle shtml xhtml mdoc aspx jshtm gitconfig bowerrc
// cspell:ignore jshintrc jscsrc eslintrc babelrc webmanifest mdown markdn psgi phtml pssc psrc gypi rhistory
// cspell:ignore rprofile cshtml gemspec cginc ebuild zshrc zprofile zlogin zlogout zshenv dsql ascx axml
Expand Down Expand Up @@ -310,15 +312,8 @@ function doesSetContainAnyOf(

export function buildLanguageExtensionMapSet(defs: LanguageDefinitions): ExtensionToLanguageIdMapSet {
return defs.reduce((map, def) => {
function getMapSet(value: string) {
const found = map.get(value);
if (found) return found;
const s = new Set<string>();
map.set(value, s);
return s;
}
function addId(value: string) {
getMapSet(value).add(def.id);
autoResolve(map, value, () => new Set<string>()).add(def.id);
}

def.extensions.forEach(addId);
Expand Down
25 changes: 23 additions & 2 deletions packages/cspell-lib/src/Models/CSpellSettingsInternalDef.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type {
AdvancedCSpellSettingsWithSourceTrace,
CSpellSettingsWithSourceTrace,
DictionaryDefinition,
DictionaryDefinitionAugmented,
DictionaryDefinitionCustom,
DictionaryDefinitionInline,
DictionaryDefinitionPreferred,
Parser,
} from '@cspell/cspell-types';
Expand All @@ -27,7 +29,14 @@ export interface CSpellSettingsInternalFinalized extends CSpellSettingsInternal

type DictionaryDefinitionCustomUniqueFields = Omit<DictionaryDefinitionCustom, keyof DictionaryDefinitionPreferred>;

export interface DictionaryDefinitionInternal
export type DictionaryDefinitionInternal = DictionaryFileDefinitionInternal | DictionaryDefinitionInlineInternal;

export type DictionaryDefinitionInlineInternal = DictionaryDefinitionInline & {
/** The path to the config file that contains this dictionary definition */
readonly __source?: string | undefined;
};

export interface DictionaryFileDefinitionInternal
extends Readonly<DictionaryDefinitionPreferred>,
Readonly<Partial<DictionaryDefinitionCustomUniqueFields>>,
Readonly<DictionaryDefinitionAugmented> {
Expand All @@ -39,10 +48,14 @@ export interface DictionaryDefinitionInternal
readonly __source?: string | undefined;
}

export interface DictionaryDefinitionInternalWithSource extends DictionaryDefinitionInternal {
export interface DictionaryFileDefinitionInternalWithSource extends DictionaryFileDefinitionInternal {
readonly __source: string;
}

export type DictionaryDefinitionInternalWithSource = DictionaryDefinitionInternal & {
readonly __source: string;
};

export function createCSpellSettingsInternal(
parts: OptionalOrUndefined<Partial<CSpellSettingsInternal>> = {}
): CSpellSettingsInternal {
Expand All @@ -60,3 +73,11 @@ export function isCSpellSettingsInternal(
): cs is CSpellSettingsInternal {
return !!(<CSpellSettingsInternal>cs)[SymbolCSpellSettingsInternal];
}

export function isDictionaryDefinitionInlineInternal(
def: DictionaryDefinitionInternal | DictionaryDefinitionInline | DictionaryDefinition
): def is DictionaryDefinitionInlineInternal {
if (def.path) return false;
const defInline = def as DictionaryDefinitionInline;
return !!(defInline.words || defInline.flagWords || defInline.ignoreWords);
}
Loading