From 9a6a0265753592f6b6971d92a8fd638a168adec9 Mon Sep 17 00:00:00 2001 From: Jason Dent Date: Fri, 1 Jul 2022 09:03:38 +0200 Subject: [PATCH] fix: #3172 --- packages/cspell-lib/samples/.cspell.json | 49 +++++-------------- .../cspell-lib/samples/overrides/cspell.json | 15 ++++++ .../cspell-lib/samples/overrides/words.txt | 5 ++ .../cspell-lib/src/Models/TextDocument.ts | 8 +++ .../src/Settings/DictionarySettings.ts | 3 -- .../determineTextDocumentSettings.test.ts | 21 ++++++++ 6 files changed, 60 insertions(+), 41 deletions(-) create mode 100644 packages/cspell-lib/samples/overrides/cspell.json create mode 100644 packages/cspell-lib/samples/overrides/words.txt create mode 100644 packages/cspell-lib/src/textValidation/determineTextDocumentSettings.test.ts diff --git a/packages/cspell-lib/samples/.cspell.json b/packages/cspell-lib/samples/.cspell.json index 7ce71a8e3bf2..b750443e14f3 100644 --- a/packages/cspell-lib/samples/.cspell.json +++ b/packages/cspell-lib/samples/.cspell.json @@ -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", @@ -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 @@ -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"] @@ -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": [] }, @@ -83,15 +61,11 @@ "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): { @@ -99,7 +73,6 @@ "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 diff --git a/packages/cspell-lib/samples/overrides/cspell.json b/packages/cspell-lib/samples/overrides/cspell.json new file mode 100644 index 000000000000..c40fab102220 --- /dev/null +++ b/packages/cspell-lib/samples/overrides/cspell.json @@ -0,0 +1,15 @@ +{ + "overrides": [ + { + "name": "Override Typescript", + "filename": "**/*.ts", + "dictionaryDefinitions": [ + { + "name": "Test Dictionary", + "path": "./words.txt" + } + ], + "dictionaries": ["Test Dictionary"] + } + ] +} diff --git a/packages/cspell-lib/samples/overrides/words.txt b/packages/cspell-lib/samples/overrides/words.txt new file mode 100644 index 000000000000..5625f515c46d --- /dev/null +++ b/packages/cspell-lib/samples/overrides/words.txt @@ -0,0 +1,5 @@ +encrypted +toggle +declare +concepts +NeXt diff --git a/packages/cspell-lib/src/Models/TextDocument.ts b/packages/cspell-lib/src/Models/TextDocument.ts index 14dda78aa562..60c4f6217ba0 100644 --- a/packages/cspell-lib/src/Models/TextDocument.ts +++ b/packages/cspell-lib/src/Models/TextDocument.ts @@ -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; @@ -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 { + 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; diff --git a/packages/cspell-lib/src/Settings/DictionarySettings.ts b/packages/cspell-lib/src/Settings/DictionarySettings.ts index ca06ebf52e19..6cf29fd521c1 100644 --- a/packages/cspell-lib/src/Settings/DictionarySettings.ts +++ b/packages/cspell-lib/src/Settings/DictionarySettings.ts @@ -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; } diff --git a/packages/cspell-lib/src/textValidation/determineTextDocumentSettings.test.ts b/packages/cspell-lib/src/textValidation/determineTextDocumentSettings.test.ts new file mode 100644 index 000000000000..2bd2c0842505 --- /dev/null +++ b/packages/cspell-lib/src/textValidation/determineTextDocumentSettings.test.ts @@ -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); + }); +});