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

[plugin] allow to override built-in commands #7592

Merged
merged 2 commits into from
Apr 16, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import { ViewColumn } from '@theia/plugin-ext/lib/plugin/types-impl';
import { WorkspaceCommands } from '@theia/workspace/lib/browser';
import { WorkspaceService, WorkspaceInput } from '@theia/workspace/lib/browser/workspace-service';
import { DiffService } from '@theia/workspace/lib/browser/diff-service';
import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor';
import { inject, injectable } from 'inversify';
import { Position } from '@theia/plugin-ext/lib/common/plugin-api-rpc';
import { URI } from 'vscode-uri';
Expand All @@ -65,10 +64,6 @@ export namespace VscodeCommands {
export const DIFF: Command = {
id: 'vscode.diff'
};

export const SET_CONTEXT: Command = {
id: 'setContext'
};
}

@injectable()
Expand Down Expand Up @@ -165,14 +160,6 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
}
});

commands.registerCommand(VscodeCommands.SET_CONTEXT, {
isVisible: () => false,
akosyakov marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line @typescript-eslint/no-explicit-any
execute: (contextKey: any, contextValue: any) => {
this.contextKeyService.createKey(String(contextKey), contextValue);
}
});

// https://code.visualstudio.com/docs/getstarted/keybindings#_navigation
/*
* internally, in VS Code, any widget opened in the main area is represented as an editor
Expand Down Expand Up @@ -203,14 +190,6 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
commands.registerCommand({ id: 'workbench.action.openSettings' }, {
execute: () => commands.executeCommand(CommonCommands.OPEN_PREFERENCES.id)
});
commands.registerCommand({ id: 'default:type' }, {
execute: args => {
const editor = MonacoEditor.getCurrent(this.editorManager);
if (editor) {
editor.trigger('keyboard', 'type', args);
}
}
});
commands.registerCommand({ id: 'workbench.action.files.save', }, {
execute: (uri?: monaco.Uri) => {
if (uri) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,12 @@ export class PluginContributionHandler {
}

registerCommand(command: Command): Disposable {
const toDispose = new DisposableCollection();
toDispose.push(this.commands.registerCommand(command, {
if (this.hasCommand(command.id)) {
console.warn(`command '${command.id}' already registered`);
return Disposable.NULL;
}

const commandHandler: CommandHandler = {
execute: async (...args) => {
const handler = this.commandHandlers.get(command.id);
if (!handler) {
Expand All @@ -339,13 +343,26 @@ export class PluginContributionHandler {
isEnabled(): boolean { return true; },
// Visibility rules are defined via the `menus` contribution point.
isVisible(): boolean { return true; }
}));
};

const toDispose = new DisposableCollection();
if (this.commands.getCommand(command.id)) {
// overriding built-in command, i.e. `type` by the VSCodeVim extension
toDispose.push(this.commands.registerHandler(command.id, commandHandler));
} else {
toDispose.push(this.commands.registerCommand(command, commandHandler));
}
this.commandHandlers.set(command.id, undefined);
toDispose.push(Disposable.create(() => this.commandHandlers.delete(command.id)));
return toDispose;
}

registerCommandHandler(id: string, execute: CommandHandler['execute']): Disposable {
if (this.hasCommandHandler(id)) {
console.warn(`command handler '${id}' already registered`);
return Disposable.NULL;
}

this.commandHandlers.set(id, execute);
this.onDidRegisterCommandHandlerEmitter.fire(id);
return Disposable.create(() => this.commandHandlers.set(id, undefined));
Expand Down