Skip to content

Commit

Permalink
feat: Add setting to hide add to code actions (#2432)
Browse files Browse the repository at this point in the history
* Add setting to hide add to code actions `cSpell.hideAddToDictionaryCodeActions`
  • Loading branch information
Jason3S authored Jan 7, 2023
1 parent 2ab8f7e commit abdee0d
Show file tree
Hide file tree
Showing 39 changed files with 1,624 additions and 1,600 deletions.
20 changes: 20 additions & 0 deletions docs/_includes/generated-docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ Default
| ------------------------------------------------------------------------------------------- | -------------------- | ------------------------------------------------------------------------------- |
| [`cSpell.autoFormatConfigFile`](#cspellautoformatconfigfile) | window | Auto Format Configuration File |
| [`cSpell.diagnosticLevel`](#cspelldiagnosticlevel) | resource | Set Diagnostic Reporting Level |
| [`cSpell.hideAddToDictionaryCodeActions`](#cspellhideaddtodictionarycodeactions) | resource | Hide the options to add words to dictionaries or settings. |
| [`cSpell.maxDuplicateProblems`](#cspellmaxduplicateproblems) | resource | The maximum number of times the same word can be flagged as an error in a file. |
| [`cSpell.maxNumberOfProblems`](#cspellmaxnumberofproblems) | resource | Controls the maximum number of spelling errors per document. |
| [`cSpell.minWordLength`](#cspellminwordlength) | resource | The minimum length of a word before checking it against a dictionary. |
Expand Down Expand Up @@ -361,6 +362,25 @@ Default

---

### `cSpell.hideAddToDictionaryCodeActions`

Name
: `cSpell.hideAddToDictionaryCodeActions`

Type
: boolean

Scope
: resource

Description
: Hide the options to add words to dictionaries or settings.

Default
: _`false`_

---

### `cSpell.maxDuplicateProblems`

Name
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,12 @@
"title": "Set Diagnostic Reporting Level",
"type": "string"
},
"cSpell.hideAddToDictionaryCodeActions": {
"default": false,
"markdownDescription": "Hide the options to add words to dictionaries or settings.",
"scope": "resource",
"type": "boolean"
},
"cSpell.maxDuplicateProblems": {
"default": 5,
"description": "The maximum number of times the same word can be flagged as an error in a file.",
Expand Down
4 changes: 2 additions & 2 deletions packages/___buildTools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
},
"devDependencies": {
"@types/deep-equal": "^1.0.1",
"@types/jest": "^28.1.8",
"@types/jest": "^29.2.5",
"@types/json-schema": "^7.0.11",
"ts-jest": "^28.0.8"
"ts-jest": "^29.0.3"
},
"dependencies": {
"commander": "^9.5.0"
Expand Down
6 changes: 3 additions & 3 deletions packages/__utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
"watch": "tsc -p . --watch"
},
"devDependencies": {
"@types/jest": "^28.1.8",
"jest": "^28.1.3",
"ts-jest": "^28.0.8"
"@types/jest": "^29.2.5",
"jest": "^29.2.5",
"ts-jest": "^29.0.3"
},
"dependencies": {
"vscode-uri": "^3.0.7"
Expand Down
2 changes: 1 addition & 1 deletion packages/_integrationTests/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "integration-tests",
"version": "2.0.0-alpha.1",
"version": "2.0.0",
"description": "Integration tests for the spell checker extension",
"private": true,
"main": "out/index.js",
Expand Down
8 changes: 4 additions & 4 deletions packages/_server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "server",
"private": true,
"description": "Spell Checker Server",
"version": "2.0.0-alpha.1",
"version": "2.0.0",
"author": "Jason Dent",
"license": "MIT",
"repository": {
Expand All @@ -26,7 +26,7 @@
"devDependencies": {
"@cspell/cspell-types": "^6.18.1",
"@types/fs-extra": "^9.0.13",
"@types/jest": "^28.1.8",
"@types/jest": "^29.2.5",
"@types/micromatch": "^4.0.2",
"@types/node": "^18.11.18",
"common-utils": "1.0.0",
Expand All @@ -35,13 +35,13 @@
"fs-extra": "^11.1.0",
"gensequence": "^4.0.3",
"iconv-lite": "^0.6.3",
"jest": "^28.1.3",
"jest": "^29.2.5",
"lorem-ipsum": "^2.0.8",
"micromatch": "^4.0.5",
"minimatch": "^5.1.2",
"node-watch": "^0.7.3",
"rxjs": "^7.8.0",
"ts-jest": "^28.0.8",
"ts-jest": "^29.0.3",
"ts-json-schema-generator": "^1.2.0",
"ts-loader": "^9.4.2",
"typescript": "^4.9.4",
Expand Down
6 changes: 6 additions & 0 deletions packages/_server/spell-checker-config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1754,6 +1754,12 @@
"title": "Set Diagnostic Reporting Level",
"type": "string"
},
"cSpell.hideAddToDictionaryCodeActions": {
"default": false,
"markdownDescription": "Hide the options to add words to dictionaries or settings.",
"scope": "resource",
"type": "boolean"
},
"cSpell.maxDuplicateProblems": {
"default": 5,
"description": "The maximum number of times the same word can be flagged as an error in a file.",
Expand Down
22 changes: 11 additions & 11 deletions packages/_server/src/__snapshots__/commands.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Validate Commands clientCommands.addWordsToConfigFileFromServer 1`] = `
Object {
"arguments": Array [
Array [
{
"arguments": [
[
"one",
"two",
],
"file:///document.ts",
Object {
{
"name": "Workspace Config",
"uri": "file:///cspell.json",
},
Expand All @@ -19,14 +19,14 @@ Object {
`;

exports[`Validate Commands clientCommands.addWordsToDictionaryFileFromServer 1`] = `
Object {
"arguments": Array [
Array [
{
"arguments": [
[
"one",
"two",
],
"file:///document.ts",
Object {
{
"name": "Custom Terms",
"uri": "file:///dictionary.txt",
},
Expand All @@ -37,9 +37,9 @@ Object {
`;

exports[`Validate Commands clientCommands.addWordsToVSCodeSettingsFromServer 1`] = `
Object {
"arguments": Array [
Array [
{
"arguments": [
[
"one",
"two",
],
Expand Down
2 changes: 1 addition & 1 deletion packages/_server/src/clientApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const stub: any = {
};
const connection = stub as Connection;

const mockConnection = jest.mocked(connection, true);
const mockConnection = jest.mocked(connection);

describe('Validate Client Api', () => {
beforeEach(() => {
Expand Down
5 changes: 3 additions & 2 deletions packages/_server/src/codeActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export function onCodeActionHandler(
settingsVersion: number;
settings: Promise<SettingsDictPair>;
}

const sugGen = new SuggestionGenerator(getSettings);
const settingsCache = new Map<string, CacheEntry>();

Expand Down Expand Up @@ -128,7 +127,9 @@ export function onCodeActionHandler(
const targets = calculateConfigTargets(docSetting, wConfig);
debugTargets && logTargets(targets);

actions.push(...generateTargetActions(textDocument, spellCheckerDiags, word, targets));
if (!docSetting.hideAddToDictionaryCodeActions) {
actions.push(...generateTargetActions(textDocument, spellCheckerDiags, word, targets));
}
}
return actions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ exports[`Validate configToJson configToJson empty config 1`] = `"{}"`;

exports[`Validate configToJson configToJson sampleConfig 1`] = `
"{
\\"version\\": \\"0.2\\",
\\"patterns\\": [
"version": "0.2",
"patterns": [
{
\\"name\\": \\"word\\",
\\"pattern\\": \\"/^\\\\\\\\s*word/gim\\"
"name": "word",
"pattern": "/^\\\\s*word/gim"
}
],
\\"ignoreRegExpList\\": [
\\"/'.*'/g\\"
"ignoreRegExpList": [
"/'.*'/g"
]
}"
`;
82 changes: 74 additions & 8 deletions packages/_server/src/config/configTargetsHelper.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { calculateConfigTargets, __testing__ } from './configTargetsHelper';
import { WorkspaceConfigForDocument } from '../api';
import { URI } from 'vscode-uri';
import { searchForConfig } from 'cspell-lib';
import { extractCSpellFileConfigurations, extractTargetDictionaries } from './documentSettings';
import { mustBeDefined } from 'common-utils/util.js';
import { ConfigTargetCSpell, ConfigTargetDictionary, ConfigTargetVSCode } from './configTargets';
import { CSpellUserSettings, searchForConfig } from 'cspell-lib';
import * as Path from 'path';
import { URI } from 'vscode-uri';
import { WorkspaceConfigForDocument } from '../api';
import { ConfigTargetCSpell, ConfigTargetDictionary, ConfigTargetVSCode } from './configTargets';
import { calculateConfigTargets, __testing__ } from './configTargetsHelper';
import { DictionaryDef } from './cspellConfig/CustomDictionary';
import { extractCSpellFileConfigurations, extractTargetDictionaries } from './documentSettings';

const { workspaceConfigToTargets, cspellToTargets, dictionariesToTargets, sortTargets } = __testing__;

const col = new Intl.Collator();

describe('Validate configTargetsHelper', () => {
test('workspaceConfigToTargets in single root workspace', () => {
const wConfig: WorkspaceConfigForDocument = {
Expand Down Expand Up @@ -160,7 +163,7 @@ describe('Validate configTargetsHelper', () => {
]);
});

test('calculateConfigTargets', async () => {
test('calculateConfigTargets user', async () => {
const wConfig: WorkspaceConfigForDocument = {
uri: URI.file(__filename).toString(),
workspaceFile: undefined,
Expand All @@ -171,7 +174,8 @@ describe('Validate configTargetsHelper', () => {
ignoreWords: {},
};
const cfg = mustBeDefined(await searchForConfig(__dirname));
const r = calculateConfigTargets(cfg, wConfig);
const settings = { ...cfg };
const r = calculateConfigTargets(settings, wConfig);
expect(r).toEqual([
oc<ConfigTargetDictionary>({
kind: 'dictionary',
Expand Down Expand Up @@ -209,8 +213,70 @@ describe('Validate configTargetsHelper', () => {
}),
]);
});

test('calculateConfigTargets workspace', async () => {
const wConfig: WorkspaceConfigForDocument = {
uri: URI.file(__filename).toString(),
workspaceFile: undefined,
workspaceFolder: URI.file(__dirname).toString(),
words: {
workspace: true,
},
ignoreWords: {},
};
const cfg = mustBeDefined(await searchForConfig(__dirname));
const defs: DictionaryDef[] = [cd('custom-words', 'path/to/custom-words.txt', false)];
const dictionaries: string[] = (cfg.dictionaries || []).concat('custom-words');
const settings: CSpellUserSettings = { ...cfg, dictionaryDefinitions: defs, dictionaries };
const r = calculateConfigTargets(settings, wConfig).sort((a, b) => col.compare(a.kind, b.kind) || col.compare(a.name, b.name));
expect(r).toEqual([
oc<ConfigTargetCSpell>({
kind: 'cspell',
name: '_server/cspell.json',
scope: 'unknown',
configUri: expect.stringContaining('_server/cspell.json'),
has: { words: undefined, ignoreWords: undefined },
}),
oc<ConfigTargetCSpell>({
kind: 'cspell',
name: expect.stringContaining('cSpell.json'),
scope: 'unknown',
configUri: expect.not.stringContaining('_server/cspell.json'),
has: { words: undefined, ignoreWords: true },
}),
// oc<ConfigTargetDictionary>({
// kind: 'dictionary',
// name: 'custom-words',
// scope: 'unknown',
// dictionaryUri: expect.stringContaining('custom-words.txt'),
// }),
oc<ConfigTargetVSCode>({
kind: 'vscode',
scope: 'user',
name: 'User',
docUri: expect.stringContaining('file:'),
has: { words: undefined, ignoreWords: undefined },
}),
oc<ConfigTargetVSCode>({
kind: 'vscode',
scope: 'workspace',
name: 'Workspace',
docUri: expect.stringContaining('file:'),
has: { words: true, ignoreWords: undefined },
}),
]);
});
});

function cd(name: string, path: string, addWords?: boolean, noSuggest?: boolean): DictionaryDef {
return {
name,
path,
addWords,
noSuggest,
};
}

function oc<T>(v: Partial<T>): T {
return expect.objectContaining(v);
}
15 changes: 15 additions & 0 deletions packages/_server/src/config/cspellConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ describe('cspellConfig', () => {
},
],
}),
s({
customDictionaries: {
'no-suggest': {
path: '${workspaceFolder}/no-sug.txt',
description: 'Allowed, but not to be encouraged.',
noSuggest: true,
},
},
}),
// s({
// addWordsTo: {
// cspell: true,
// '#company-terms': true,
// },
// }),
];

test('sampleSettings', () => {
Expand Down
Loading

0 comments on commit abdee0d

Please sign in to comment.