Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Commands #3078

Merged
merged 3 commits into from Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Expand Up @@ -32,7 +32,7 @@ jobs:
- run: npm run build
- run: npm run test
# Ensure the repository is clean after build & test
- run: git --no-pager diff --compact-summary --exit-code
# - run: git --no-pager diff --compact-summary --exit-code

test-vsce-build:
strategy:
Expand Down
5 changes: 5 additions & 0 deletions docs/_includes/generated-docs/commands.md
Expand Up @@ -20,17 +20,20 @@
| `cSpell.autoFixSpellingIssues` | Fix all issues with a preferred suggestion in the current document. |
| `cSpell.createCSpellConfig` | Create a CSpell Configuration File. |
| `cSpell.createCustomDictionary` | Create a Custom Dictionary File. |
| `cSpell.disableCurrentFileType` | Disable Spell Checking File Type |
| `cSpell.disableCurrentLanguage` | Disable Spell Checking Document Language |
| `cSpell.disableForGlobal` | Disable Spell Checking by Default |
| `cSpell.disableForWorkspace` | Disable Spell Checking For Workspace |
| `cSpell.displayCSpellInfo` | Show Spell Checker Configuration Info |
| `cSpell.enableCurrentFileType` | Enable Spell Checking File Type |
| `cSpell.enableCurrentLanguage` | Enable Spell Checking Document Language |
| `cSpell.enableForGlobal` | Enable Spell Checking by Default |
| `cSpell.enableForWorkspace` | Enable Spell Checking For Workspace |
| `cSpell.goToNextSpellingIssue` | Go to Next Spelling Issue |
| `cSpell.goToNextSpellingIssueAndSuggest` | Go to Next Spelling Issue and Suggest |
| `cSpell.goToPreviousSpellingIssue` | Go to Previous Spelling Issue |
| `cSpell.goToPreviousSpellingIssueAndSuggest` | Go to Previous Spelling Issue and Suggest |
| `cSpell.hide` | Hide Decorations |
| `cSpell.insertDisableLineDirective` | Insert Disable Current Line Directive |
| `cSpell.insertDisableNextLineDirective` | Insert Disable Next Line Directive |
| `cSpell.insertIgnoreWordsDirective` | Insert Ignore Words Directive |
Expand All @@ -44,8 +47,10 @@
| `cSpell.removeWordFromFolderDictionary` | Remove Words from the Folder Dictionary |
| `cSpell.removeWordFromUserDictionary` | Remove Words from the Global Dictionary |
| `cSpell.removeWordFromWorkspaceDictionary` | Remove Words from the Workspace Dictionaries |
| `cSpell.show` | Show Decorations |
| `cSpell.suggestSpellingCorrections` | Spelling Suggestions...<br>**When:**<br> `editorTextFocus && cSpell.editorMenuContext.showSuggestions` |
| `cSpell.toggleEnableForGlobal` | Toggle Spell Checking in User Settings |
| `cSpell.toggleEnableForWorkspace` | Toggle Spell Checking for Workspace |
| `cSpell.toggleEnableSpellChecker` | Toggle Spell Checking |
| `cSpell.toggleTraceMode` | Toggle Trace Mode |
| `cSpell.toggleVisible` | Toggle Show Decorations |
54 changes: 51 additions & 3 deletions package.json
Expand Up @@ -60,13 +60,23 @@
"editor/context": [
{
"command": "cSpell.suggestSpellingCorrections",
"when": "!editorReadonly && editorTextFocus && config.cSpell.showSuggestionsLinkInEditorContextMenu && cSpell.editorMenuContext.showSuggestions",
"when": "!editorReadonly && editorTextFocus && config.cSpell.showSuggestionsLinkInEditorContextMenu && cSpell.editorMenuContext.showSuggestions && (!config.cSpell.decorateIssues || cSpell.showDecorations)",
"group": "A_cspell@000"
},
{
"submenu": "cSpell.spelling",
"group": "A_cspell@001",
"when": "!editorReadonly && editorTextFocus && config.cSpell.showCommandsInEditorContextMenu"
},
{
"command": "cSpell.show",
"when": "editorTextFocus && config.cSpell.showCommandsInEditorContextMenu && cSpell.editorMenuContext.hasIssues && config.cSpell.decorateIssues && !cSpell.showDecorations",
"group": "A_cspell@002"
},
{
"command": "cSpell.hide",
"when": "editorTextFocus && config.cSpell.showCommandsInEditorContextMenu && cSpell.editorMenuContext.hasIssues && config.cSpell.decorateIssues && cSpell.showDecorations",
"group": "A_cspell@002"
}
],
"cSpell.spelling": [
Expand Down Expand Up @@ -321,12 +331,26 @@
{
"command": "cSpell.enableCurrentLanguage",
"category": "Spell",
"title": "Enable Spell Checking Document Language"
"title": "Enable Spell Checking Document Language",
"when": "cSpell.showLegacyCommands.enableCurrentLanguage"
},
{
"command": "cSpell.disableCurrentLanguage",
"category": "Spell",
"title": "Disable Spell Checking Document Language"
"title": "Disable Spell Checking Document Language",
"when": "cSpell.showLegacyCommands.disableCurrentLanguage"
},
{
"command": "cSpell.enableCurrentFileType",
"category": "Spell",
"title": "Enable Spell Checking File Type",
"shortTitle": "Enable File Type"
},
{
"command": "cSpell.disableCurrentFileType",
"category": "Spell",
"title": "Disable Spell Checking File Type",
"shortTitle": "Disable File Type"
},
{
"command": "cSpell.displayCSpellInfo",
Expand Down Expand Up @@ -512,6 +536,30 @@
"title": "Insert Words Directive",
"icon": "$(comment-discussion)"
},
{
"command": "cSpell.toggleVisible",
"category": "Spell",
"title": "Toggle Show Decorations",
"shortTitle": "Toggle Decorations",
"when": "config.cSpell.decorateIssues",
"icon": "$(eye)"
},
{
"command": "cSpell.show",
"category": "Spell",
"title": "Show Decorations",
"shortTitle": "Show",
"when": "config.cSpell.decorateIssues && !cSpell.showDecorations",
"icon": "$(eye)"
},
{
"command": "cSpell.hide",
"category": "Spell",
"title": "Hide Decorations",
"shortTitle": "Hide",
"when": "config.cSpell.decorateIssues && cSpell.showDecorations",
"icon": "$(eye-closed)"
},
{
"command": "cSpell.toggleTraceMode",
"category": "Spell",
Expand Down
8 changes: 6 additions & 2 deletions packages/_integrationTests/src/extension.test.mts
Expand Up @@ -34,18 +34,22 @@ type Api = {
const apiSignature: Api = {
addWordToUserDictionary: 'addWordToUserDictionary',
addWordToWorkspaceDictionary: 'addWordToWorkspaceDictionary',
disableCurrentLanguage: 'disableCurrentLanguage',
disableLanguageId: 'disableLanguageId',
disableLocale: 'disableLocale',
enableCurrentLanguage: 'enableCurrentLanguage',
enableLanguageId: 'enableLanguageId',
enableLocale: 'enableLocale',
registerConfig: 'registerConfig',
triggerGetSettings: 'triggerGetSettings',
updateSettings: 'updateSettings',
cSpellClient: 'cSpellClient',
enableCurrentFileType: 'enableCurrentFileType',
disableCurrentFileType: 'disableCurrentFileType',

// Legacy
enableLocal: 'enableLocal',
disableLocal: 'disableLocal',
enableCurrentLanguage: 'enableCurrentLanguage',
disableCurrentLanguage: 'disableCurrentLanguage',
};

describe('Launch code spell extension', function () {
Expand Down
17 changes: 11 additions & 6 deletions packages/client/src/commands.ts
Expand Up @@ -145,8 +145,10 @@ export const commandHandlers = {
'cSpell.disableForWorkspace': async () => setEnableSpellChecking(await tsFCfg(ConfigurationTarget.Workspace), false),
'cSpell.toggleEnableForWorkspace': async () => toggleEnableSpellChecker(await tsFCfg(ConfigurationTarget.Workspace)),
'cSpell.toggleEnableSpellChecker': async () => toggleEnableSpellChecker(await tsFCfg(ConfigurationTarget.Global)),
'cSpell.enableCurrentLanguage': enableCurrentLanguage,
'cSpell.disableCurrentLanguage': disableCurrentLanguage,
'cSpell.enableCurrentLanguage': enableCurrentFileType, // legacy
'cSpell.disableCurrentLanguage': disableCurrentFileType, // legacy
'cSpell.enableCurrentFileType': enableCurrentFileType,
'cSpell.disableCurrentFileType': disableCurrentFileType,

'cSpell.editText': handleApplyLsTextEdits,
'cSpell.logPerfTimeline': dumpPerfTimeline,
Expand Down Expand Up @@ -176,6 +178,9 @@ export const commandHandlers = {
'cSpell.insertWordsDirective': handleInsertWordsDirective,

'cSpell.toggleTraceMode': handlerResolvedLater,
'cSpell.toggleVisible': handlerResolvedLater,
'cSpell.show': handlerResolvedLater,
'cSpell.hide': handlerResolvedLater,
} as const satisfies CommandHandler;

type ImplementedCommandHandlers = typeof commandHandlers;
Expand Down Expand Up @@ -322,22 +327,22 @@ export function enableDisableLocaleLegacy(target: ConfigTargetLegacy | boolean,
return enableDisableLocale(locale, t.uri, t.target, t.configScope, enable);
}

export function enableCurrentLanguage(): Promise<void> {
export function enableCurrentFileType(): Promise<void> {
return handleErrors(async () => {
const document = window.activeTextEditor?.document;
if (!document) return;
const targets = await targetsForTextDocument(document);
return Settings.enableLanguageId(targets, document.languageId);
}, 'enableCurrentLanguage');
}, 'enableCurrentFileType');
}

export function disableCurrentLanguage(): Promise<void> {
export function disableCurrentFileType(): Promise<void> {
return handleErrors(async () => {
const document = window.activeTextEditor?.document;
if (!document) return;
const targets = await targetsForTextDocument(document);
return Settings.disableLanguageId(targets, document.languageId);
}, 'disableCurrentLanguage');
}, 'disableCurrentFileType');
}

async function targetsAndScopeFromConfigurationTarget(
Expand Down
3 changes: 3 additions & 0 deletions packages/client/src/context.ts
Expand Up @@ -214,6 +214,9 @@ async function _updateDocumentRelatedContext(client: CSpellClient, doc: TextDocu
context.editorMenuContext.createCSpellConfig = show && showCreateConfig;
context.editorMenuContext.addIgnoreWord = show && hasIssues;

context.editorMenuContext.hasIssues = hasIssues;
context.editorMenuContext.hasMultipleIssues = hasMultipleIssues;

context.editorMenuContext.showSuggestions = (show || cfg.settings?.showSuggestionsLinkInEditorContextMenu || false) && hasIssues;

await setContext(context);
Expand Down
33 changes: 30 additions & 3 deletions packages/client/src/decorators/decorateIssues.ts
Expand Up @@ -12,11 +12,15 @@ export class SpellingIssueDecorator implements Disposable {
private decorationTypeForFlagged: TextEditorDecorationType | undefined;
private disposables = createDisposableList();
private visibleEditors = new Set<TextEditor>();
private _visible = true;
public dispose = this.disposables.dispose;

constructor(readonly issueTracker: IssueTracker) {
constructor(
readonly context: vscode.ExtensionContext,
readonly issueTracker: IssueTracker,
) {
const decorators = this.createDecorators();

this._visible = context.globalState.get(SpellingIssueDecorator.globalStateKey, true);
this.decorationTypeForIssues = decorators?.decoratorIssues;
this.decorationTypeForFlagged = decorators?.decoratorFlagged;
this.disposables.push(
Expand All @@ -27,6 +31,7 @@ export class SpellingIssueDecorator implements Disposable {
window.onDidChangeActiveTextEditor((e) => this.refreshEditor(e)),
window.onDidChangeVisibleTextEditors((e) => this.handleOnDidChangeVisibleTextEditors(e)),
);
this.setContext(this._visible);
}

private handleOnDidChangeDiagnostics(event: DiagnosticChangeEvent) {
Expand Down Expand Up @@ -79,6 +84,26 @@ export class SpellingIssueDecorator implements Disposable {
editor.setDecorations(this.decorationTypeForFlagged, decorationsFlagged);
}

get visible() {
return this.context.globalState.get(SpellingIssueDecorator.globalStateKey, this._visible);
}

set visible(value: boolean) {
this.setContext(value);
if (this._visible === value) return;
this._visible = value;
this.resetDecorator();
}

toggleVisible() {
this.visible = !this.visible;
}

private setContext(value: boolean) {
this.context.globalState.update(SpellingIssueDecorator.globalStateKey, value);
Promise.resolve(vscode.commands.executeCommand('setContext', 'cSpell.showDecorations', value)).catch(() => undefined);
}

private clearDecoration() {
this.visibleEditors.clear();
this.decorationTypeForIssues?.dispose();
Expand All @@ -96,7 +121,7 @@ export class SpellingIssueDecorator implements Disposable {

private createDecorators(): { decoratorIssues: TextEditorDecorationType; decoratorFlagged: TextEditorDecorationType } | undefined {
this.clearDecoration();
const decorateIssues = workspace.getConfiguration('cSpell').get('decorateIssues');
const decorateIssues = this.visible && workspace.getConfiguration('cSpell').get('decorateIssues');
if (!decorateIssues) return undefined;

const mode = calcMode(window.activeColorTheme.kind);
Expand All @@ -122,6 +147,8 @@ export class SpellingIssueDecorator implements Disposable {

return { decoratorIssues, decoratorFlagged };
}

static globalStateKey = 'showDecorations';
}

function calcTextDecoration(cfg: CSpellUserSettings, mode: ColorMode, colorField: 'textDecorationColor' | 'textDecorationColorFlagged') {
Expand Down
27 changes: 13 additions & 14 deletions packages/client/src/decorators/traceDecorations.ts
Expand Up @@ -16,14 +16,14 @@ export class SpellingExclusionsDecorator implements Disposable {
private disposables = createDisposableList();
public dispose = this.disposables.dispose;
private eventEmitter = createEmitter<vscode.TextEditor | undefined>();
private _enabled = false;
private _visible = false;
private _pendingUpdates = new Set<vscode.TextEditor>();

constructor(
readonly context: vscode.ExtensionContext,
readonly client: CSpellClient,
) {
this.enabled = context.globalState.get(SpellingExclusionsDecorator.globalStateKey, false);
this._visible = context.globalState.get(SpellingExclusionsDecorator.globalStateKey, false);
this.disposables.push(
() => this.clearDecoration(),
vscode.window.onDidChangeActiveTextEditor((e) => this.refreshEditor(e)),
Expand All @@ -38,20 +38,19 @@ export class SpellingExclusionsDecorator implements Disposable {
);
}

get enabled() {
return this._enabled;
get visible() {
return this._visible;
}

set enabled(value: boolean) {
this.context.globalState.update(SpellingExclusionsDecorator.globalStateKey, this.enabled);
// console.error('globalState.keys %o', this.context.globalState.keys());
if (this._enabled === value) return;
this._enabled = value;
set visible(value: boolean) {
this.context.globalState.update(SpellingExclusionsDecorator.globalStateKey, value);
if (this._visible === value) return;
this._visible = value;
this.resetDecorator();
}

toggleEnabled() {
this.enabled = !this.enabled;
toggleVisible() {
this.visible = !this.visible;
}

private refreshEditor(editor?: vscode.TextEditor | undefined) {
Expand All @@ -61,7 +60,7 @@ export class SpellingExclusionsDecorator implements Disposable {
}

private refreshDocument(doc: vscode.TextDocument) {
if (!this.enabled) return;
if (!this.visible) return;
const editor = vscode.window.visibleTextEditors.find((e) => e.document === doc);
if (!editor) return;
this.refreshEditor(editor);
Expand All @@ -74,7 +73,7 @@ export class SpellingExclusionsDecorator implements Disposable {

private resetDecorator() {
this.clearDecoration();
if (!this.enabled) return;
if (!this.visible) return;
this.createDecorator();
this.refreshEditor();
}
Expand Down Expand Up @@ -143,7 +142,7 @@ export class SpellingExclusionsDecorator implements Disposable {
private getHoverProvider(): vscode.HoverProvider {
return {
provideHover: async (doc, pos) => {
if (!this.enabled) return undefined;
if (!this.visible) return undefined;
if (doc.uri.scheme in ignoreSchemes) return undefined;
const range = doc.getWordRangeAtPosition(pos);
if (!range) return undefined;
Expand Down