Skip to content

Commit

Permalink
feat: Be able to specify files to spell check within the config.
Browse files Browse the repository at this point in the history
Related to #571
  • Loading branch information
Jason3S committed Feb 13, 2021
1 parent 75eb739 commit 44d3bed
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 46 deletions.
11 changes: 11 additions & 0 deletions cspell.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,13 @@
},
"type": "array"
},
"files": {
"description": "Glob patterns of files to be checked. Glob patterns are relative to the `globRoot` of the configuration file that defines them.",
"items": {
"$ref": "#/definitions/Glob"
},
"type": "array"
},
"flagWords": {
"description": "list of words to always be considered incorrect.",
"items": {
Expand Down Expand Up @@ -611,6 +618,10 @@
"version": {
"default": "0.2",
"description": "Configuration format version of the setting file.",
"enum": [
"0.2",
"0.1"
],
"type": "string"
},
"words": {
Expand Down
101 changes: 69 additions & 32 deletions packages/cspell-lib/src/Settings/CSpellSettingsServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,10 @@ describe('Validate CSpellSettingsServer', () => {
const left = { name: 'Left' };
const right = { name: 'Right' };
expect(mergeSettings(left, right)).toEqual({
words: [],
name: 'Left|Right',
id: '|',
userWords: [],
ignoreWords: [],
flagWords: [],
patterns: [],
enabledLanguageIds: [],
languageSettings: [],
ignoreRegExpList: [],
dictionaries: [],
dictionaryDefinitions: [],
source: { name: 'Left|Right', sources: [left, right] },
});
});
Expand All @@ -54,16 +46,8 @@ describe('Validate CSpellSettingsServer', () => {
enabled: true,
name: '|enabledName',
id: 'left|enabledId',
words: [],
userWords: [],
ignoreWords: [],
flagWords: [],
patterns: [],
enabledLanguageIds: [],
languageSettings: [],
ignoreRegExpList: [],
dictionaries: [],
dictionaryDefinitions: [],
source: { name: 'left|enabledName', sources: [left, enabled] },
});
});
Expand All @@ -77,16 +61,8 @@ describe('Validate CSpellSettingsServer', () => {
enabled: right.enabled,
name: '|',
id: [left.id, right.id].join('|'),
words: [],
userWords: [],
ignoreWords: [],
flagWords: [],
patterns: [],
enabledLanguageIds: [],
languageSettings: [],
ignoreRegExpList: [],
dictionaries: [],
dictionaryDefinitions: [],
source: { name: 'left|right', sources: [left, right] },
});
});
Expand All @@ -96,20 +72,81 @@ describe('Validate CSpellSettingsServer', () => {
enabled: false,
name: '|',
id: '|',
words: [],
userWords: [],
ignoreWords: [],
flagWords: [],
patterns: [],
enabledLanguageIds: [],
languageSettings: [],
ignoreRegExpList: [],
dictionaries: [],
dictionaryDefinitions: [],
source: { name: 'left|right', sources: [{ enabled: true }, { enabled: false }] },
});
});

test('tests mergeSettings with ignorePaths, files, and overrides', () => {
const left: CSpellUserSettings = {
id: 'left',
files: ['left/**/*.*'],
ignorePaths: ['node_modules'],
overrides: [
{
filename: '*.ts',
dictionaries: ['ts-extra'],
},
],
};
const right: CSpellUserSettings = {
id: 'right',
enabled: true,
files: ['right/**/*.*'],
overrides: [{ filename: '*.jsxx', languageId: 'javascript' }], // cspell:ignore jsxx
};
expect(mergeSettings({}, right)).toEqual(right);
expect(mergeSettings(left, {})).toEqual(left);
expect(mergeSettings(left, right)).toEqual({
enabled: right.enabled,
name: '|',
id: [left.id, right.id].join('|'),
enabledLanguageIds: [],
languageSettings: [],
files: left.files?.concat(right.files || []),
ignorePaths: left.ignorePaths?.concat(right.ignorePaths || []),
overrides: left.overrides?.concat(right.overrides || []),
source: { name: 'left|right', sources: [left, right] },
});
});

test('tests mergeSettings with ignorePaths, files, and overrides compatibility', () => {
const left: CSpellUserSettings = {
id: 'left',
files: ['left/**/*.*'],
ignorePaths: ['node_modules'],
overrides: [
{
filename: '*.ts',
dictionaries: ['ts-extra'],
},
],
};
const right: CSpellUserSettings = {
id: 'right',
version: '0.1',
enabled: true,
files: ['right/**/*.*'],
ignorePaths: ['node_modules'],
overrides: [{ filename: '*.jsxx', languageId: 'javascript' }], // cspell:ignore jsxx
};
expect(mergeSettings({}, right)).toEqual(right);
expect(mergeSettings(left, {})).toEqual(left);
expect(mergeSettings(left, right)).toEqual({
enabled: right.enabled,
name: '|',
id: [left.id, right.id].join('|'),
version: right.version,
enabledLanguageIds: [],
languageSettings: [],
files: left.files?.concat(right.files || []),
ignorePaths: right.ignorePaths,
overrides: right.overrides,
source: { name: 'left|right', sources: [left, right] },
});
});

test('tests mergeSettings when left/right are the same', () => {
expect(mergeSettings(_defaultSettings, _defaultSettings)).toBe(_defaultSettings);
});
Expand Down
47 changes: 39 additions & 8 deletions packages/cspell-lib/src/Settings/CSpellSettingsServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,16 @@ export function readSettingsFiles(filenames: string[]): CSpellSettings {
/**
* Merges two lists of strings and removes duplicates. Order is NOT preserved.
*/
function mergeList<T>(left: T[] = [], right: T[] = []) {
const setOfWords = new Set([...left, ...right]);
return [...setOfWords.keys()];
function mergeList(left: undefined, right: undefined): undefined;
function mergeList<T>(left: T[], right: T[]): T[];
function mergeList<T>(left: undefined, right: T[]): T[];
function mergeList<T>(left: T[], right: undefined): T[];
function mergeList<T>(left: T[] | undefined, right: T[] | undefined): T[] | undefined;
function mergeList<T>(left: T[] | undefined, right: T[] | undefined): T[] | undefined {
if (left === undefined) return right;
if (right === undefined) return left;
const uniqueItems = new Set([...left, ...right]);
return [...uniqueItems.keys()];
}

function tagLanguageSettings(tag: string, settings: LanguageSetting[] = []): LanguageSetting[] {
Expand Down Expand Up @@ -271,9 +278,9 @@ function merge(left: CSpellSettings, right: CSpellSettings): CSpellSettings {
const leftId = left.id || left.languageId || '';
const rightId = right.id || right.languageId || '';

const includeRegExpList = takeRightThenLeft(left.includeRegExpList, right.includeRegExpList);
const includeRegExpList = takeRightOtherwiseLeft(left.includeRegExpList, right.includeRegExpList);

const optionals = includeRegExpList.length ? { includeRegExpList } : {};
const optionals = includeRegExpList?.length ? { includeRegExpList } : {};

const settings: CSpellSettings = {
...left,
Expand All @@ -295,14 +302,29 @@ function merge(left: CSpellSettings, right: CSpellSettings): CSpellSettings {
tagLanguageSettings(rightId, right.languageSettings)
),
enabled: right.enabled !== undefined ? right.enabled : left.enabled,
files: mergeList(left.files, right.files),
ignorePaths: versionBasedMergeList(left.ignorePaths, right.ignorePaths, right.version),
overrides: versionBasedMergeList(left.overrides, right.overrides, right.version),
source: mergeSources(left, right),
globRoot: undefined,
import: undefined,
__imports: mergeImportRefs(left, right),
__importRef: undefined,
};
return settings;
}

function versionBasedMergeList<T>(
left: T[] | undefined,
right: T[] | undefined,
version: CSpellSettings['version']
): T[] | undefined {
if (version === '0.1') {
return takeRightOtherwiseLeft(left, right);
}
return mergeList(left, right);
}

function hasLeftAncestor(s: CSpellSettings, left: CSpellSettings): boolean {
return hasAncestor(s, left, 0);
}
Expand Down Expand Up @@ -332,11 +354,20 @@ export function mergeInDocSettings(left: CSpellSettings, right: CSpellSettings):
return merged;
}

function takeRightThenLeft<T>(left: T[] = [], right: T[] = []) {
if (right.length) {
/**
* If right is non-empty return right, otherwise return left.
* @param left - left hand values
* @param right - right hand values
*/
function takeRightOtherwiseLeft(left: undefined, right: undefined): undefined;
function takeRightOtherwiseLeft<T>(left: T[], right: undefined): T[];
function takeRightOtherwiseLeft<T>(left: undefined, right: T[]): T[];
function takeRightOtherwiseLeft<T>(left: T[] | undefined, right: T[] | undefined): T[] | undefined;
function takeRightOtherwiseLeft<T>(left: T[] | undefined, right: T[] | undefined): T[] | undefined {
if (right?.length) {
return right;
}
return left;
return left || right;
}

export function calcOverrideSettings(settings: CSpellSettings, filename: string): CSpellSettings {
Expand Down
11 changes: 11 additions & 0 deletions packages/cspell-types/cspell.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,13 @@
},
"type": "array"
},
"files": {
"description": "Glob patterns of files to be checked. Glob patterns are relative to the `globRoot` of the configuration file that defines them.",
"items": {
"$ref": "#/definitions/Glob"
},
"type": "array"
},
"flagWords": {
"description": "list of words to always be considered incorrect.",
"items": {
Expand Down Expand Up @@ -611,6 +618,10 @@
"version": {
"default": "0.2",
"description": "Configuration format version of the setting file.",
"enum": [
"0.2",
"0.1"
],
"type": "string"
},
"words": {
Expand Down
12 changes: 6 additions & 6 deletions packages/cspell-types/src/settings/CSpellSettingsDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface FileSettings extends ExtendableSettings {
* Configuration format version of the setting file.
* @default "0.2"
*/
version?: string | '0.2' | '0.1';
version?: '0.2' | '0.1';

/** Words to add to dictionary -- should only be in the user config file. */
userWords?: string[];
Expand All @@ -54,11 +54,11 @@ export interface FileSettings extends ExtendableSettings {
*/
globRoot?: FsPath;

// /**
// * Glob patterns of files to be checked.
// * Glob patterns are relative to the `globRoot` of the configuration file that defines them.
// */
// files?: Glob[];
/**
* Glob patterns of files to be checked.
* Glob patterns are relative to the `globRoot` of the configuration file that defines them.
*/
files?: Glob[];

/**
* Glob patterns of files to be ignored
Expand Down

0 comments on commit 44d3bed

Please sign in to comment.