Skip to content

Commit

Permalink
Add support to disable a dictionary by name
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed May 5, 2018
1 parent 5707159 commit 5a7d47a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,42 @@ The spell checker includes a set of default dictionaries.
* **filetypes** - list of file typescript
* **npm** - list of top 500+ package names on npm.

### Dictionary Definition

* **name** - The reference name of the dictionary, used with program language settings
* **description** - Optional description
* **path** - Path to the file, can be relative or absolute. Relative path is relative to the
current `cspell.json` file.
* **repMap** - Optional replacement map use to replace character prior to searching the dictionary.
Example:
```javascript
// Replace various tick marks with a single '
"repMap": [["'|`|’", "'"]]
```
// Use Compounds
* **useCompounds** - allow compound words


```javascript
// Define each dictionary. Relative paths are relative to the config file.
"dictionaryDefinitions": [
{ "name": "spanish", "path": "./spanish-words.txt"},
{ "name": "ruby", "path": "./ruby.txt"},
{ "name": "company-terms", "path": "./corp-terms.txt"}
],
```

### 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.

#### Disable Default cpp Dictionary

```javascript
"dictionaries": ["!cpp"],
```

## LanguageSettings

The Language Settings allow configuration to be based upon the programming langauge and/or the local.
Expand Down
25 changes: 21 additions & 4 deletions src/Settings/DictionarySettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,31 @@ const defaultSettings = getDefaultSettings();
describe('Validate DictionarySettings', () => {
it('expects default to not be empty', () => {
const mapDefs = DictSettings.filterDictDefsToLoad(['php', 'wordsEn', 'unknown', 'en_us'], defaultSettings.dictionaryDefinitions!);
const files = mapDefs.map(a => a[1]).map(def => def.path!);
expect(files.filter(a => a.includes('php.txt'))).to.be.lengthOf(1);
expect(files.filter(a => a.includes('wordsEn.trie'))).to.be.lengthOf(0);
expect(files.filter(a => a.includes('en_US.trie'))).to.be.lengthOf(1);
const files = mapDefs.map(a => a[1]).map(def => def.name!);
expect(mapDefs).to.be.lengthOf(2);
expect(files.filter(a => a.includes('php'))).to.be.lengthOf(1);
expect(files.filter(a => a.includes('wordsEn'))).to.be.lengthOf(0);
expect(files.filter(a => a.includes('en_us'))).to.be.lengthOf(1);
expect(files.filter(a => a.includes('unknown'))).to.be.empty;
// console.log(mapDefs);
});

it('tests exclusions and empty ids', () => {
const ids = [
'php',
'cpp', // add cpp
'wordsEn',
' ', // empty entry
'unknown',
'!cpp', // remove cpp
'en_us',
];
const expected = ['php', 'en_us'].sort();
const mapDefs = DictSettings.filterDictDefsToLoad(ids, defaultSettings.dictionaryDefinitions!);
const dicts = mapDefs.map(a => a[1]).map(def => def.name!).sort();
expect(dicts).to.be.deep.equal(expected);
});

it('tests that the files exist', () => {
const defaultDicts = defaultSettings.dictionaryDefinitions!;
const dictIds = defaultDicts.map(def => def.name);
Expand Down
13 changes: 12 additions & 1 deletion src/Settings/DictionarySettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ const dictionaryPath = () => path.join(__dirname, '..', '..', 'dist', 'dictionar
export type DefMapArrayItem = [string, DictionaryDefinition];

export function filterDictDefsToLoad(dictIds: DictionaryId[], defs: DictionaryDefinition[]): DefMapArrayItem[] {
const dictIdSet = new Set(dictIds);
// Process the dictIds in order, if it starts with a '!', remove it from the set.
const dictIdSet = dictIds
.map(id => id.trim())
.filter(id => !!id)
.reduce((dictSet, id) => {
if (id[0] === '!') {
dictSet.delete(id.slice(1));
} else {
dictSet.add(id);
}
return dictSet;
}, new Set<DictionaryId>());
const activeDefs: DefMapArrayItem[] = defs
.filter(({name}) => dictIdSet.has(name))
.map(def => ({...def, path: getFullPathName(def)}))
Expand Down

0 comments on commit 5a7d47a

Please sign in to comment.