Skip to content

Commit

Permalink
fix: correct how dictionaries are disabled (#1229)
Browse files Browse the repository at this point in the history
* fix: Add Sample for turning off dictionaries
* ci: Only cspell-action on main branch
* fix: fix typo in readme
* fix: correct how dictionaries are disabled.

fix: #1215
  • Loading branch information
Jason3S committed May 11, 2021
1 parent d9dc2dc commit 60975ea
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/cspell-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: cspell-action
on:
pull_request:
push:
branches:
- master

jobs:
cspell:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"bootstrap": "lerna bootstrap --no-ci",
"lint": "eslint . --fix && prettier -w \"**/*.{md,yaml,yml,json}\"",
"lint-ci": "eslint . && prettier -c \"**/*.{md,yaml,yml,json}\"",
"prepare": "npm run symlink && npm run clean-build",
"prepare": "npm run clean-build",
"pub-lerna": "lerna publish from-git",
"pub-version": "lerna version --conventional-commits",
"pub-version-pre": "lerna version --conventional-commits --conventional-prerelease",
Expand Down
3 changes: 1 addition & 2 deletions packages/Samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
"version": "5.0.1",
"description": "Sample files used during unit tests",
"private": true,
"main": "index.js",
"scripts": {
"test": "echo \"No test specified\""
},
"keywords": [],
"author": "",
"license": "ISC"
"license": "MIT"
}
12 changes: 12 additions & 0 deletions packages/Samples/tests/exclude_dictionary/cspell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "0.1",
"language": "en",
"dictionaries": [
"!softwareTerms"
],
"enabled": true,
"words": [],
"ignoreWords": [],
"flagWords": [],
"ignoreRegExpList": []
}
2 changes: 2 additions & 0 deletions packages/Samples/tests/exclude_dictionary/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This file contains HIPAA, HIPAA, RABC, and RBAC as words.
Lowercase versions of hipaa, hippa, rabc, and rbac also.
16 changes: 16 additions & 0 deletions packages/cspell-lib/src/Settings/DictionarySettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ describe('Validate DictionarySettings', () => {
expect(dicts).toEqual(expected);
});

test.each`
ids | expected
${'!php, php, cpp, !!cpp'} | ${'cpp'}
${'!php, php, !!php, !!cpp'} | ${'cpp, php'}
${'!!!!!!!!!!cpp, !cpp'} | ${'cpp'}
`('validate dictionary exclusions $ids', ({ ids, expected }: { ids: string; expected: string }) => {
const dictIds = ids.split(',');
const expectedIds = expected.split(',').map((id) => id.trim());
const mapDefs = DictSettings.filterDictDefsToLoad(dictIds, defaultSettings.dictionaryDefinitions!);
const dicts = mapDefs
.map((a) => a[1])
.map((def) => def.name!)
.sort();
expect(dicts).toEqual(expectedIds);
});

test('tests that the files exist', () => {
const defaultDicts = defaultSettings.dictionaryDefinitions!;
const dictIds = defaultDicts.map((def) => def.name);
Expand Down
46 changes: 37 additions & 9 deletions packages/cspell-lib/src/Settings/DictionarySettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,47 @@ export interface DictionaryDefinitionWithSource extends DictionaryDefinitionPref

export type DefMapArrayItem = [string, DictionaryDefinitionPreferred];

/**
* Combines the list of desired dictionaries with the list of dictionary
* definitions. Order does not matter, but the number of leading `!` does.
*
* Excluding dictionaries.
* - Adding `!` to a dictId will remove the dictionary.
* - Adding `!!` will add it back.
*
* @param dictIds - dictionaries desired
* @param defs - dictionary definitions
* @returns map from dictIds to definitions
*/
export function filterDictDefsToLoad(dictIds: DictionaryId[], defs: DictionaryDefinition[]): DefMapArrayItem[] {
// Process the dictIds in order, if it starts with a '!', remove it from the set.
const dictIdSet = dictIds
const negPrefixRegEx = /^!+/;

// Collect the ids based upon the `!` depth.
const dictIdMap = dictIds
.map((id) => id.trim())
.filter((id) => !!id)
.reduce((dictSet, id) => {
if (id[0] === '!') {
dictSet.delete(id.slice(1));
} else {
dictSet.add(id);
.reduce((dictDepthMap, id) => {
const pfx = id.match(negPrefixRegEx);
const depth = pfx?.[0]?.length || 0;
const _dictSet = dictDepthMap.get(depth);
const dictSet = _dictSet || new Set<DictionaryId>();
if (!_dictSet) {
dictDepthMap.set(depth, dictSet);
}
return dictSet;
}, new Set<DictionaryId>());
dictSet.add(id.slice(depth));
return dictDepthMap;
}, new Map<number, Set<DictionaryId>>());

const orderedSets = [...dictIdMap].sort((a, b) => a[0] - b[0]);
const dictIdSet = orderedSets.reduce((dictIdSet, [depth, ids]) => {
if (depth & 1) {
[...ids].forEach((id) => dictIdSet.delete(id));
} else {
[...ids].forEach((id) => dictIdSet.add(id));
}
return dictIdSet;
}, new Set<DictionaryId>());

const activeDefs: DefMapArrayItem[] = defs
.filter(({ name }) => dictIdSet.has(name))
.map((def) => ({ ...def, path: getFullPathName(def) }))
Expand Down
14 changes: 13 additions & 1 deletion packages/cspell/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,14 +467,26 @@ The spell checker includes a set of default dictionaries.
### Disabling a Dictionary

It is possible to prevent a dictionary from being loaded. This is useful if you want to use your own dictionary or just
off an existing dictionary.
turn off an existing dictionary.

#### Disable Default cpp Dictionary

```javascript
"dictionaries": ["!cpp"],
"overrides": [
{
"filename": "legacy/**/*.cpp",
"dictionaries": ["!!cpp"], // add it back for *.cpp files under the legacy folder
},
]
```

The number of `!`s is important.

- `!cpp` remove `cpp` dictionary
- `!!cpp` add it back
- `!!!cpp` remove it again.

## LanguageSettings

The Language Settings allow configuration to be based upon the programming language and/or the e.
Expand Down

0 comments on commit 60975ea

Please sign in to comment.