Skip to content

Commit

Permalink
fix: Commands
Browse files Browse the repository at this point in the history
- Rename disableCurrentLanguage to disableCurrentFileType
- Rename enableCurrentLanguage to enableCurrentFileType
- Add toggleVisible
- Add show
- Add hide
  • Loading branch information
Jason3S committed Mar 4, 2024
1 parent c81fb77 commit 04e2fa7
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 36 deletions.
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
19 changes: 13 additions & 6 deletions packages/client/src/extension.ts
Expand Up @@ -76,13 +76,16 @@ export async function activate(context: ExtensionContext): Promise<ExtensionApi>
}

const configWatcher = vscode.workspace.createFileSystemWatcher(settings.configFileLocationGlob);
const decorator = new SpellingIssueDecorator(issueTracker);
const decorator = new SpellingIssueDecorator(context, issueTracker);
const decoratorExclusions = new SpellingExclusionsDecorator(context, client);
activateIssueViewer(context, issueTracker);
activateFileIssuesViewer(context, issueTracker);

const extensionCommand: InjectableCommandHandlers = {
'cSpell.toggleTraceMode': () => decoratorExclusions.toggleEnabled(),
'cSpell.toggleTraceMode': () => decoratorExclusions.toggleVisible(),
'cSpell.toggleVisible': () => decorator.toggleVisible(),
'cSpell.show': () => (decorator.visible = true),
'cSpell.hide': () => (decorator.visible = false),
};

// Push the disposable to the context's subscriptions so that the
Expand Down Expand Up @@ -195,25 +198,29 @@ export async function activate(context: ExtensionContext): Promise<ExtensionApi>
disableLocale: (target: ConfigTargetLegacy | boolean, locale: string) => commands.enableDisableLocaleLegacy(target, locale, false),
};

const getConfigurationForDocument = (doc: vscode.TextDocument) => client.getConfigurationForDocument(doc);

const server = {
registerConfig,
triggerGetSettings,
enableLanguageId: commands.enableLanguageIdCmd,
disableLanguageId: commands.disableLanguageIdCmd,
enableCurrentLanguage: commands.enableCurrentLanguage,
disableCurrentLanguage: commands.disableCurrentLanguage,
enableCurrentFileType: commands.enableCurrentFileType,
disableCurrentFileType: commands.disableCurrentFileType,
addWordToUserDictionary: addWords.addWordToUserDictionary,
addWordToWorkspaceDictionary: addWords.addWordToWorkspaceDictionary,
enableLocale: methods.enableLocale,
disableLocale: methods.disableLocale,
updateSettings: () => false,
cSpellClient: () => client,
getConfigurationForDocument: (doc: vscode.TextDocument) => client.getConfigurationForDocument(doc),
getConfigurationForDocument,

// Legacy
enableLocal: methods.enableLocale,
disableLocal: methods.disableLocale,
};
enableCurrentLanguage: commands.enableCurrentFileType,
disableCurrentLanguage: commands.disableCurrentFileType,
} as const satisfies ExtensionApi & { getConfigurationForDocument: typeof getConfigurationForDocument };

activateWebview(context);

Expand Down
19 changes: 17 additions & 2 deletions packages/client/src/extensionApi.ts
Expand Up @@ -8,14 +8,29 @@ export interface ExtensionApi {
triggerGetSettings(): void;
enableLanguageId(languageId: string, uri?: string): Thenable<void>;
disableLanguageId(languageId: string, uri?: string): Thenable<void>;
enableCurrentLanguage(): Thenable<void>;
disableCurrentLanguage(): Thenable<void>;
enableCurrentFileType(): Thenable<void>;
disableCurrentFileType(): Thenable<void>;
addWordToUserDictionary(word: string): Thenable<void>;
addWordToWorkspaceDictionary(word: string, uri?: string | null | Uri): Thenable<void>;
enableLocale(target: ConfigTargetLegacy, locale: string): Thenable<void>;
disableLocale(target: ConfigTargetLegacy, locale: string): Thenable<void>;
updateSettings(): boolean;
cSpellClient(): CSpellClient;

/**
* @deprecated use {@link ExtensionApi.enableLocale}
*/
enableLocal(isGlobal: boolean, locale: string): Thenable<void>;
/**
* @deprecated use {@link ExtensionApi.disableLocale}
*/
disableLocal(isGlobal: boolean, locale: string): Thenable<void>;
/**
* @deprecated use {@link ExtensionApi.enableCurrentFileType}
*/
enableCurrentLanguage(): Thenable<void>;
/**
* @deprecated use {@link ExtensionApi.disableCurrentFileType}
*/
disableCurrentLanguage(): Thenable<void>;
}

0 comments on commit 04e2fa7

Please sign in to comment.