Skip to content

Commit

Permalink
fix: #3172
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Jul 1, 2022
1 parent 79399db commit 33eba70
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 41 deletions.
49 changes: 11 additions & 38 deletions packages/cspell-lib/samples/.cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,13 @@
"language": "en",
"allowCompoundWords": false,
// words - list of words to be always considered correct
"words": [
"gensequence",
"xregexp",
"sampletrace",
"findall"
],
"words": ["gensequence", "xregexp", "sampletrace", "findall"],
"maxNumberOfProblems": 10000,
"ignorePaths": [
"dictionaries/**",
"node_modules/**",
"vscode-extension/**",
".git/**",
".vscode/**"
],
"ignorePaths": ["dictionaries/**", "node_modules/**", "vscode-extension/**", ".git/**", ".vscode/**"],
// flagWords - list of words to be always considered incorrect
// This is useful for offensive words and common spelling errors.
// For example "hte" should be "the"
"flagWords": [
"hte"
],
"flagWords": ["hte"],
"dictionaryDefinitions": [
{
"name": "local words",
Expand All @@ -37,9 +24,7 @@
"path": "./.missing.txt"
}
],
"dictionaries": [
"local words"
],
"dictionaries": ["local words"],
"languageSettings": [
{
// VSCode languageId. i.e. typescript, java, go, cpp, javascript, markdown, latex
Expand All @@ -50,14 +35,9 @@
"locale": "*",
// By default the whole text of a file is included for spell checking
// Adding patterns to the "includeRegExpList" to only include matching patterns
"includeRegExpList": [
"CStyleComment",
"string"
],
"includeRegExpList": ["CStyleComment", "string"],
// To exclude patterns, add them to "ignoreRegExpList"
"ignoreRegExpList": [
"/#include.*/"
],
"ignoreRegExpList": ["/#include.*/"],
// regex patterns than can be used with ignoreRegExpList or includeRegExpList
// Example: "pattern": [{ "name": "mdash", "pattern": "—" }]
// This could be included in "ignoreRegExpList": ["mdash"]
Expand All @@ -68,9 +48,7 @@
}
],
// List of dictionaries to enable by name in `dictionaryDefinitions`
"dictionaries": [
"cpp"
],
"dictionaries": ["cpp"],
// Dictionary definitions can also be supplied here. They are only used iff "languageId" and "local" match.
"dictionaryDefinitions": []
},
Expand All @@ -83,23 +61,18 @@
"overrides": [
{
"filename": "**/{*.py}",
"ignoreRegExpList": [
"/'s\\b/"
]
"ignoreRegExpList": ["/'s\\b/"]
},
{
"filename": "**/{*.c,*.cpp}",
"ignoreRegExpList": [
"/'s\\b/"
]
"ignoreRegExpList": ["/'s\\b/"]
},
// Force `*.txt` to use the Dutch dictionary (Dutch dictionary needs to be installed separately):
{
"language": "nl",
"filename": "**/dutch/*.txt"
}
],
"import": [
"@cspell/dict-nl-nl/cspell-ext.json"
]
"import": ["@cspell/dict-nl-nl/cspell-ext.json", "./overrides/cspell.json"]
}
// cspell:ignore hte mdash
15 changes: 15 additions & 0 deletions packages/cspell-lib/samples/overrides/cspell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"overrides": [
{
"name": "Override Typescript",
"filename": "**/*.ts",
"dictionaryDefinitions": [
{
"name": "Test Dictionary",
"path": "./words.txt"
}
],
"dictionaries": ["Test Dictionary"]
}
]
}
5 changes: 5 additions & 0 deletions packages/cspell-lib/samples/overrides/words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
encrypted
toggle
declare
concepts
NeXt
8 changes: 8 additions & 0 deletions packages/cspell-lib/src/Models/TextDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getLanguagesForBasename } from '../LanguageIds';
import * as Uri from './Uri';
import { TextDocument as VsTextDocument } from 'vscode-languageserver-textdocument';
import assert from 'assert';
import { promises as fs } from 'fs';

export type DocumentUri = Uri.Uri;

Expand Down Expand Up @@ -192,4 +193,11 @@ function isTextDocumentImpl(doc: TextDocument | unknown): doc is TextDocumentImp
return doc instanceof TextDocumentImpl;
}

export async function loadTextDocument(filename: string | DocumentUri, languageId?: string): Promise<TextDocument> {
const uri = Uri.toUri(filename);
const content = await fs.readFile(uri.fsPath, 'utf8');

return createTextDocument({ uri, languageId, content });
}

export const isTextDocument: (doc: TextDocument | unknown) => doc is TextDocument = isTextDocumentImpl;
3 changes: 0 additions & 3 deletions packages/cspell-lib/src/Settings/DictionarySettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ export function mapDictDefToInternal(
pathToSettingsFile: string
): DictionaryDefinitionInternalWithSource {
if (isDictionaryDefinitionWithSource(def)) {
if (def.__source !== pathToSettingsFile) {
throw new Error('Trying to normalize a dictionary definition with a different source.');
}
return def;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { determineTextDocumentSettings } from './determineTextDocumentSettings';
import * as path from 'path';
import { loadConfig } from '../Settings';
import { loadTextDocument } from '../Models/TextDocument';

const samples = path.resolve(__dirname, '../../samples');
const cfgPath = path.join(samples, '.cspell.json');
const oc = expect.objectContaining;

describe('determineTextDocumentSettings', () => {
test.each`
file | configFile | expected
${__filename} | ${cfgPath} | ${oc({ languageId: 'typescript' })}
`('determineTextDocumentSettings', async ({ file, configFile, expected }) => {
const cfg = await loadConfig(configFile);
const doc = await loadTextDocument(file);
const settings = determineTextDocumentSettings(doc, cfg);
expect(settings.dictionaries).toContain('Test Dictionary');
expect(settings).toEqual(expected);
});
});

0 comments on commit 33eba70

Please sign in to comment.