-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support calling a function to get the config (#5126)
- Loading branch information
Showing
9 changed files
with
266 additions
and
1 deletion.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
packages/cspell-config-lib/fixtures/js/module/cspell.custom.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { promises as fs } from 'fs'; | ||
|
||
function expand(pattern, options = { begin: '(', end: ')', sep: '|' }, start = 0) { | ||
const len = pattern.length; | ||
const parts = []; | ||
function push(word) { | ||
if (Array.isArray(word)) { | ||
parts.push(...word); | ||
} else { | ||
parts.push(word); | ||
} | ||
} | ||
let i = start; | ||
let curWord = ''; | ||
while (i < len) { | ||
const ch = pattern[i++]; | ||
if (ch === options.end) { | ||
break; | ||
} | ||
if (ch === options.begin) { | ||
const nested = expand(pattern, options, i); | ||
i = nested.idx; | ||
curWord = nested.parts.flatMap((p) => (Array.isArray(curWord) ? curWord.map((w) => w + p) : [curWord + p])); | ||
continue; | ||
} | ||
if (ch === options.sep) { | ||
push(curWord); | ||
curWord = ''; | ||
continue; | ||
} | ||
curWord = Array.isArray(curWord) ? curWord.map((w) => w + ch) : curWord + ch; | ||
} | ||
push(curWord); | ||
return { parts, idx: i }; | ||
} | ||
|
||
function expandWord(pattern, options = { begin: '(', end: ')', sep: '|' }) { | ||
return expand(pattern, options).parts; | ||
} | ||
|
||
function expandWords(wordList) { | ||
const words = wordList | ||
.split(/\n/g) | ||
.map((w) => w.trim()) | ||
.filter((w) => !!w && !w.startsWith('#')) | ||
.flatMap((w) => expandWord(w)); | ||
return words; | ||
} | ||
|
||
const wordsFile = new URL('words.txt', import.meta.url); | ||
|
||
const wordList = await fs.readFile(wordsFile, 'utf8'); | ||
|
||
const config = { | ||
id: 'async-module', | ||
dictionaryDefinitions: [ | ||
{ | ||
name: 'custom-words', | ||
words: expandWords(wordList), | ||
}, | ||
], | ||
dictionaries: ['custom-words'], | ||
}; | ||
|
||
export default config; |
63 changes: 63 additions & 0 deletions
63
packages/cspell-config-lib/fixtures/js/module/cspell.function.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { promises as fs } from 'fs'; | ||
|
||
function expand(pattern, options = { begin: '(', end: ')', sep: '|' }, start = 0) { | ||
const len = pattern.length; | ||
const parts = []; | ||
function push(word) { | ||
if (Array.isArray(word)) { | ||
parts.push(...word); | ||
} else { | ||
parts.push(word); | ||
} | ||
} | ||
let i = start; | ||
let curWord = ''; | ||
while (i < len) { | ||
const ch = pattern[i++]; | ||
if (ch === options.end) { | ||
break; | ||
} | ||
if (ch === options.begin) { | ||
const nested = expand(pattern, options, i); | ||
i = nested.idx; | ||
curWord = nested.parts.flatMap((p) => (Array.isArray(curWord) ? curWord.map((w) => w + p) : [curWord + p])); | ||
continue; | ||
} | ||
if (ch === options.sep) { | ||
push(curWord); | ||
curWord = ''; | ||
continue; | ||
} | ||
curWord = Array.isArray(curWord) ? curWord.map((w) => w + ch) : curWord + ch; | ||
} | ||
push(curWord); | ||
return { parts, idx: i }; | ||
} | ||
|
||
function expandWord(pattern, options = { begin: '(', end: ')', sep: '|' }) { | ||
return expand(pattern, options).parts; | ||
} | ||
|
||
function expandWords(wordList) { | ||
const words = wordList | ||
.split(/\n/g) | ||
.map((w) => w.trim()) | ||
.filter((w) => !!w && !w.startsWith('#')) | ||
.flatMap((w) => expandWord(w)); | ||
return words; | ||
} | ||
|
||
/** | ||
* @returns {Promise<import('@cspell/cspell-types').CSpellUserSettings>} | ||
*/ | ||
export default async function getConfig() { | ||
const wordsFile = new URL('words.txt', import.meta.url); | ||
|
||
const wordList = await fs.readFile(wordsFile, 'utf8'); | ||
|
||
const config = { | ||
id: 'config-function', | ||
words: expandWords(wordList), | ||
}; | ||
return config; | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/cspell-config-lib/fixtures/js/module/cspell.python.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { readFile } from 'node:fs/promises'; | ||
|
||
/** | ||
* @returns {Promise<import('@cspell/cspell-types').CSpellUserSettings>} | ||
*/ | ||
export default async function getConfig() { | ||
const words = (await readFile(new URL('requirements.txt', import.meta.url), 'utf8')) | ||
.replace(/[.,]|([=<>].*)/g, ' ') | ||
.split(/\s+/g); | ||
return { id: 'python-imports', words }; | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/cspell-config-lib/fixtures/js/module/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
blinker==1.4 | ||
click==7.1.2 | ||
lazr.restfulclient==0.14.2 | ||
lazr.uri==1.0.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# words | ||
(un|re|)check(s|ed|ing|) | ||
(un|re|)test(s|ed|ing|) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/cspell-dictionary/src/util/braceExpansion.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import { expandBraces } from './braceExpansion.js'; | ||
|
||
describe('Validate braceExpansion', () => { | ||
test('expandBraces should return an array of expanded strings', () => { | ||
// Test case 1 | ||
const result1 = expandBraces('a{b,c}d', { begin: '{', end: '}', sep: ',' }); | ||
expect(result1).toEqual(['abd', 'acd']); | ||
|
||
// Test case 2 | ||
const result2 = expandBraces('z{a,b,c}', { begin: '{', end: '}', sep: ',' }); | ||
expect(result2).toEqual(['za', 'zb', 'zc']); | ||
}); | ||
|
||
/* cspell:ignore unchecker */ | ||
|
||
test.each` | ||
text | expected | ||
${'hello'} | ${s('hello')} | ||
${'remember(s|ed|ing|er|)'} | ${s('remembers|remembered|remembering|rememberer|remember')} | ||
${'remember(s|e(d|r)|ing|)'} | ${s('remembers|remembered|rememberer|remembering|remember')} | ||
${'remember(s|e(d|r)|ing|'} | ${s('remembers|remembered|rememberer|remembering|remember')} | ||
${'(un|)check(s|e(d|r)|ing|)'} | ${s('unchecks|checks|unchecked|checked|unchecker|checker|unchecking|checking|uncheck|check')} | ||
${'(un|check(s|e(d|r)|ing|)'} | ${s('un|checks|checked|checker|checking|check')} | ||
${'(un|re|)check(ed|)'} | ${s('unchecked|rechecked|checked|uncheck|recheck|check')} | ||
`('expandBraces $text', ({ text, expected }) => { | ||
expect(expandBraces(text)).toEqual(expected); | ||
}); | ||
}); | ||
|
||
function s(text: string, split = '|'): string[] { | ||
return text.split(split); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
export interface Options { | ||
begin: string; | ||
end: string; | ||
sep: string; | ||
} | ||
|
||
function expand( | ||
pattern: string, | ||
options: Options = { begin: '(', end: ')', sep: '|' }, | ||
start = 0, | ||
): { parts: string[]; idx: number } { | ||
const len = pattern.length; | ||
const parts: string[] = []; | ||
function push(word: string | string[]) { | ||
if (Array.isArray(word)) { | ||
parts.push(...word); | ||
} else { | ||
parts.push(word); | ||
} | ||
} | ||
let i = start; | ||
let curWord: string | string[] = ''; | ||
while (i < len) { | ||
const ch = pattern[i++]; | ||
if (ch === options.end) { | ||
break; | ||
} | ||
if (ch === options.begin) { | ||
const nested = expand(pattern, options, i); | ||
i = nested.idx; | ||
curWord = nested.parts.flatMap((p) => (Array.isArray(curWord) ? curWord.map((w) => w + p) : [curWord + p])); | ||
continue; | ||
} | ||
if (ch === options.sep) { | ||
push(curWord); | ||
curWord = ''; | ||
continue; | ||
} | ||
curWord = Array.isArray(curWord) ? curWord.map((w) => w + ch) : curWord + ch; | ||
} | ||
push(curWord); | ||
return { parts, idx: i }; | ||
} | ||
|
||
export function expandBraces(pattern: string, options: Options = { begin: '(', end: ')', sep: '|' }): string[] { | ||
return expand(pattern, options).parts; | ||
} |