Skip to content

Commit 468b8ab

Browse files
authored
fix: Support loading btrie dictionaries in cspell-lib (#8311)
1 parent 4313191 commit 468b8ab

File tree

18 files changed

+343
-100
lines changed

18 files changed

+343
-100
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# BTrie Dictionaries
2+
3+
This is an example config file with both a path to a text file and a path to a BTrie file.
4+
5+
The btrie can be build using:
6+
7+
```
8+
cspell-tools btrie ./btrie-words.txt
9+
```
10+
11+
It contains some words not found in `words.txt`. Example: `Exxtra`.
Binary file not shown.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Amsterdam
2+
Angles
3+
cities
4+
City
5+
Configuration
6+
Default
7+
define
8+
Definitions
9+
Delhi
10+
dictionaries
11+
dictionary
12+
false
13+
file
14+
Francisco
15+
fully
16+
Inline
17+
Las
18+
load
19+
London
20+
Mexico
21+
name
22+
New
23+
Paris
24+
possible
25+
San
26+
words
27+
York
28+
Exxtra
29+
btrie
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json
2+
3+
dictionaryDefinitions:
4+
- name: words
5+
path: ./btrie-words.btrie.gz
6+
- name: words-btrie
7+
path: ./words.txt
8+
btrie: ./btrie-words.btrie.gz
9+
10+
dictionaries:
11+
- words
12+
- words-btrie
13+
14+
overrides:
15+
- filename: test.md
16+
dictionaries:
17+
- '!words'
18+
- 'words-btrie'
19+
- filename: test2.md
20+
dictionaries:
21+
- 'words'
22+
- '!words-btrie'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Test File
2+
3+
This also has Exxtra words.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
New York
2+
New Amsterdam
3+
Las Angles
4+
San Francisco
5+
New Delhi
6+
Mexico City
7+
London
8+
Paris
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Test File
2+
3+
This also has Exxtra words.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Amsterdam
2+
Angles
3+
btrie
4+
cities
5+
City
6+
Configuration
7+
Default
8+
define
9+
Definitions
10+
Delhi
11+
dictionaries
12+
dictionary
13+
false
14+
file
15+
Francisco
16+
fully
17+
Inline
18+
Las
19+
load
20+
London
21+
Mexico
22+
name
23+
New
24+
Paris
25+
possible
26+
San
27+
words
28+
York

packages/cspell-lib/src/lib/Settings/DictionarySettings.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,14 @@ function fixPath(def: DictionaryDefinitionInternal): DictionaryDefinitionInterna
5454
return def;
5555
}
5656
const newPath = fixDicPath(def.path, def.file);
57+
const newBTriePath = def.btrie ? fixDicPath(def.btrie, def.file) : undefined;
58+
// console.error('fixPath %o => %o', def.path, newPath);
59+
// console.error('fixBTriePath %o => %o', def.btrie, newBTriePath);
5760
return {
5861
...def,
5962
file: undefined,
6063
path: newPath,
64+
btrie: newBTriePath,
6165
};
6266
}
6367

@@ -178,6 +182,7 @@ class _DictionaryDefinitionInternalWithSource implements DictionaryFileDefinitio
178182
const {
179183
path: relPath = '',
180184
file = '',
185+
btrie,
181186
addWords,
182187
description,
183188
dictionaryInformation,
@@ -194,12 +199,14 @@ class _DictionaryDefinitionInternalWithSource implements DictionaryFileDefinitio
194199
const name = determineName(filePath, def);
195200

196201
const resolvedPath = toFilePathOrHref(resolveRelativeTo(filePath, defaultPath));
202+
let bTriePath = btrie ? fixDicPath(btrie, file) : undefined;
203+
bTriePath = bTriePath ? toFilePathOrHref(resolveRelativeTo(bTriePath, defaultPath)) : undefined;
197204

198205
const ddi: DDI = {
199206
name,
200207
file: undefined,
201208
path: resolvedPath,
202-
btrie: undefined,
209+
btrie: bTriePath,
203210
addWords,
204211
description,
205212
dictionaryInformation,

packages/cspell-lib/src/lib/SpellingDictionary/DictionaryController/DictionaryLoader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class DictionaryLoader {
101101
if (entry) {
102102
return entry.pending.then(([dictionary]) => dictionary);
103103
}
104-
const loadedEntry = this.loadEntry(def.path, def);
104+
const loadedEntry = this.loadEntry(def.btrie || def.path, def);
105105
this.setCacheEntry(key, loadedEntry, def);
106106
this.keepAliveCache.set(def, loadedEntry);
107107
return loadedEntry.pending.then(([dictionary]) => dictionary);

0 commit comments

Comments
 (0)