Skip to content

Commit

Permalink
fix: Make sure Custom Dictionaries use workspaceRoot (#485)
Browse files Browse the repository at this point in the history
* doc: Update CHANGELOG.md

* fix: make sure workspace root is used with custom dicts

* Use a regex so the test works on windows.
  • Loading branch information
Jason3S authored May 9, 2020
1 parent c0945c9 commit 70a375a
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 21 deletions.
22 changes: 16 additions & 6 deletions packages/_server/src/documentSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ describe('Validate workspace substitution resolver', () => {
};

test('resolveSettings Imports', () => {
const resolver = debugExports.createWorkspaceNamesResolver(workspaces[1], workspaces);
const resolver = debugExports.createWorkspaceNamesResolver(workspaces[1], workspaces, undefined);
const result = debugExports.resolveSettings(settingsImports, resolver);
expect(result.import).toEqual([
'cspell.json',
Expand All @@ -373,7 +373,7 @@ describe('Validate workspace substitution resolver', () => {
});

test('resolveSettings ignorePaths', () => {
const resolver = debugExports.createWorkspaceNamesResolver(workspaceFolders.client, workspaces);
const resolver = debugExports.createWorkspaceNamesResolver(workspaceFolders.client, workspaces, undefined);
const result = debugExports.resolveSettings(settingsIgnorePaths, resolver);
expect(result.ignorePaths).toEqual([
'**/node_modules/**',
Expand All @@ -384,7 +384,7 @@ describe('Validate workspace substitution resolver', () => {
});

test('resolveSettings dictionaryDefinitions', () => {
const resolver = debugExports.createWorkspaceNamesResolver(workspaces[1], workspaces);
const resolver = debugExports.createWorkspaceNamesResolver(workspaces[1], workspaces, undefined);
const result = debugExports.resolveSettings(settingsDictionaryDefinitions, resolver);
expect(result.dictionaryDefinitions).toEqual([
expect.objectContaining({ name: 'My Dictionary', path: `${rootUri.fsPath}/words.txt`}),
Expand All @@ -393,7 +393,7 @@ describe('Validate workspace substitution resolver', () => {
});

test('resolveSettings languageSettings', () => {
const resolver = debugExports.createWorkspaceNamesResolver(workspaces[1], workspaces);
const resolver = debugExports.createWorkspaceNamesResolver(workspaces[1], workspaces, undefined);
const result = debugExports.resolveSettings(settingsLanguageSettings, resolver);
expect(result?.languageSettings?.[0]).toEqual({
languageId: 'typescript',
Expand All @@ -405,7 +405,7 @@ describe('Validate workspace substitution resolver', () => {
});

test('resolveSettings overrides', () => {
const resolver = debugExports.createWorkspaceNamesResolver(workspaces[1], workspaces);
const resolver = debugExports.createWorkspaceNamesResolver(workspaces[1], workspaces, undefined);
const result = debugExports.resolveSettings(settingsOverride, resolver);
expect(result?.overrides?.[0]?.languageSettings?.[0]).toEqual({
languageId: 'typescript',
Expand Down Expand Up @@ -435,7 +435,7 @@ describe('Validate workspace substitution resolver', () => {
...cspellConfigCustomWorkspaceDictionary,
dictionaries: [ 'typescript' ],
}
const resolver = debugExports.createWorkspaceNamesResolver(workspaces[1], workspaces);
const resolver = debugExports.createWorkspaceNamesResolver(workspaces[1], workspaces, 'custom root');
const result = debugExports.resolveSettings(settings, resolver);
expect(result.dictionaries).toEqual([
'Global Dictionary',
Expand All @@ -452,5 +452,15 @@ describe('Validate workspace substitution resolver', () => {
'Folder Dictionary',
'Folder Dictionary 2',
]);
expect(result.dictionaryDefinitions).toEqual(expect.arrayContaining([
expect.objectContaining({
name: 'Folder Dictionary',
path: 'custom root/packages/_server/words.txt',
}),
expect.objectContaining({
name: 'Folder Dictionary 2',
path: expect.stringMatching(/^[/\\]path to root[/\\]root[/\\]client[/\\]words2\.txt$/),
}),
]));
});
});
27 changes: 20 additions & 7 deletions packages/_server/src/documentSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export class DocumentSettings {
private async resolveWorkspacePaths(settings: CSpellUserSettings, docUri: string): Promise<CSpellUserSettings> {
const folders = await this.folders;
const folder = await this.findMatchingFolder(docUri);
const resolver = createWorkspaceNamesResolver(folder, folders);
const resolver = createWorkspaceNamesResolver(folder, folders, settings.workspaceRootPath);
return resolveSettings(settings, resolver);
}

Expand Down Expand Up @@ -354,14 +354,22 @@ interface FolderPath {
path: string;
}

function createWorkspaceNamesResolver(folder: WorkspaceFolder, folders: WorkspaceFolder[]): WorkspacePathResolver {
function createWorkspaceNamesResolver(
folder: WorkspaceFolder,
folders: WorkspaceFolder[],
root: string | undefined
): WorkspacePathResolver {
return {
resolveFile: createWorkspaceNamesFilePathResolver(folder, folders),
resolveFile: createWorkspaceNamesFilePathResolver(folder, folders, root),
resolveGlob: createWorkspaceNamesGlobPathResolver(folder, folders),
}
}

function createWorkspaceNamesFilePathResolver(folder: WorkspaceFolder, folders: WorkspaceFolder[]): WorkspacePathResolverFn {
function createWorkspaceNamesFilePathResolver(
folder: WorkspaceFolder,
folders: WorkspaceFolder[],
root: string | undefined
): WorkspacePathResolverFn {
function toFolderPath(w: WorkspaceFolder): FolderPath {
return {
name: w.name,
Expand All @@ -370,7 +378,8 @@ function createWorkspaceNamesFilePathResolver(folder: WorkspaceFolder, folders:
}
return createWorkspaceNameToPathResolver(
toFolderPath(folder),
folders.map(toFolderPath)
folders.map(toFolderPath),
root
);
}

Expand Down Expand Up @@ -417,10 +426,14 @@ function createWorkspaceNameToGlobResolver(folder: FolderPath, folders: FolderPa
};
}

function createWorkspaceNameToPathResolver(folder: FolderPath, folders: FolderPath[]): WorkspacePathResolverFn {
function createWorkspaceNameToPathResolver(
folder: FolderPath,
folders: FolderPath[],
root: string | undefined
): WorkspacePathResolverFn {
const folderPairs = [['${workspaceFolder}', folder.path] as [string, string]]
.concat([
['.', folders[0]?.path || folder.path],
['.', root || folders[0]?.path || folder.path],
['~', os.homedir()],
])
.concat(folders.map(folder =>
Expand Down
85 changes: 77 additions & 8 deletions packages/client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [1.9.0-alpha.0](https://github.com/streetsidesoftware/vscode-spell-checker/compare/v1.8.1-alpha.0...v1.9.0-alpha.0) (2020-05-09)
# [1.9.0-alpha.0](https://github.com/streetsidesoftware/vscode-spell-checker/compare/v1.8.0...v1.9.0-alpha.0) (2020-05-09)


### Bug Fixes
Expand All @@ -15,16 +15,88 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

* Custom Dictionaries ([#482](https://github.com/streetsidesoftware/vscode-spell-checker/issues/482)) ([075ec87](https://github.com/streetsidesoftware/vscode-spell-checker/commit/075ec875fab13a6912529c067c0f85e2ba3f5e67))

## Defining the workspace root path

* `workspaceRootPath` - By default, the workspace root path matches the first folder listed in the
workspace. If a `cspell.json` exists at the workspace root, it will be used and merged with any folder level
`cspell.json` files found.

Note: the reason behind this change was to make the behaviour in multi-root workspace more predictable.

### Example
``` jsonc
"cSpell.workspaceRootPath": "${workspaceFolder:client}/.."
```

## [1.8.1-alpha.0](https://github.com/streetsidesoftware/vscode-spell-checker/compare/v1.8.0...v1.8.1-alpha.0) (2020-05-02)
## Custom Dictionaries

**Note:** Version bump only for package code-spell-checker
It is now possible to tell the extension to use custom dictionaries. Three new configuration settings have been added.
* `customUserDictionaries` - for defining custom user level dictionaries
* `customWorkspaceDictionaries` - for defining custom workspace level dictionaries
* `customFolderDictionaries` - for defining custom folder level dictionaries

### User Example
``` jsonc
"cSpell.customUserDictionaries": [
{
"name": "my words", // name of dictionary (should be unique)
"description": "These are the words I use in all projects.",
"path": "~/custom-words.txt",
"addWords": true // Add Word to User Dictionary will add words to this file.
},
{
"name": "company terms",
"description": "These are terms used by my company.",
"path": "~/gist/company-terms/company-terms.txt",
"addWords": false // Do not add to the company terms.
}
]
```

### Workspace Example:
``` jsonc
"cSpell.customWorkspaceDictionaries": [
{
"name": "project words", // name of dictionary (should be unique)
"description": "These are words for this project.",
// Relative to the "client" folder in the workspace.
"path": "${workspaceFolder:client}/project-words.txt",
"addWords": true // Add Word to Workspace Dictionary will add words to this file.
}
]
```

### Folder Example:
``` jsonc
"cSpell.customFolderDictionaries": [
{
"name": "folder words", // name of dictionary (should be unique)
"description": "These are words for this project.",
// Relative to the current folder in the workspace.
"path": "${workspaceFolder}/folder-words.txt",
"addWords": false // Do NOT add to folder words.
},
{
"name": "project words", // name of dictionary (should be unique)
"description": "These are words for this project.",
// Relative to the "client" folder in the workspace.
"path": "${workspaceFolder:client}/project-words.txt",
"addWords": true // Add Word to Workspace Dictionary when adding folder words.
}
]
```

### Custom Dictionary Paths

Dictionary paths can be absolute or relative based upon the following patterns:

* `~` - relative to the user home directory
* `.` - relative to the `workspaceRootPath`
* `${workspaceFolder}` - relative to current workspace folder
* `${workspaceFolder:[folder name]}` - `[folder name]` is one of the folders in the workspace.

## Adding to Dictionary

## Adding words to Dictionaries

### Editor Context Menu (right-click)

Expand All @@ -44,7 +116,7 @@ These can be bound to keyboard shortcuts.
* Command: `cSpell.disableForGlobal`


# [1.8.0](https://github.com/streetsidesoftware/vscode-spell-checker/compare/v1.8.0-alpha.2...v1.8.0) (2020-02-23)
# [1.8.0](https://github.com/streetsidesoftware/vscode-spell-checker/compare/v1.7.24...v1.8.0) (2020-02-23)

Performance enhancements and a few features.

Expand Down Expand Up @@ -96,9 +168,6 @@ for a multiroot with folders `client`, `server`, `common`, it is possible to spe

Fixes [can't enable and disable without reloading window · Issue #384](https://github.com/streetsidesoftware/vscode-spell-checker/issues/384)


# [1.8.0-alpha.2](https://github.com/streetsidesoftware/vscode-spell-checker/compare/v1.8.0-alpha.1...v1.8.0-alpha.2) (2020-02-23)

### Features

* Support enableFileTypes ([#439](https://github.com/streetsidesoftware/vscode-spell-checker/issues/439)) ([2fde3bc](https://github.com/streetsidesoftware/vscode-spell-checker/commit/2fde3bc5c658ee51da5a56580aa1370bf8174070))
Expand Down

0 comments on commit 70a375a

Please sign in to comment.