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

WIP register Monaco command used by vscode extensions #4275

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 6 additions & 7 deletions packages/monaco/src/browser/monaco-command-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface MonacoEditorCommandHandler {
@injectable()
export class MonacoCommandRegistry {

/* @deprecated Monaco command is registered as it is, without prefixing */
public static MONACO_COMMAND_PREFIX = 'monaco.';

constructor(
Expand All @@ -36,20 +37,18 @@ export class MonacoCommandRegistry {
@inject(SelectionService) protected readonly selectionService: SelectionService
) { }

/* @deprecated Monaco command is registered as it is, without prefixing */
protected prefix(command: string): string {
return MonacoCommandRegistry.MONACO_COMMAND_PREFIX + command;
return command;
}

validate(command: string): string | undefined {
const monacoCommand = this.prefix(command);
return this.commands.commandIds.indexOf(monacoCommand) !== -1 ? monacoCommand : undefined;
const monacoCommand = this.commands.getCommand(command);
return monacoCommand && monacoCommand.label ? monacoCommand.id : undefined;
}

registerCommand(command: Command, handler: MonacoEditorCommandHandler): void {
this.commands.registerCommand({
...command,
id: this.prefix(command.id)
}, this.newHandler(handler));
this.commands.registerCommand(command, this.newHandler(handler));
}

registerHandler(command: string, handler: MonacoEditorCommandHandler): void {
Expand Down
3 changes: 3 additions & 0 deletions packages/monaco/src/browser/monaco-command-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export class MonacoCommandService implements ICommandService {
));
}
}
getDelegate(): ICommandService | undefined {
return this.delegate;
}

// tslint:disable-next-line:no-any
executeCommand(commandId: any, ...args: any[]): monaco.Promise<any> {
Expand Down
89 changes: 89 additions & 0 deletions packages/monaco/src/browser/monaco-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export namespace MonacoCommands {
const label = command.label;
const iconClass = iconClasses.get(id);
ACTIONS.push({ id, label, iconClass });
} else {
ACTIONS.push({ id });
}
}
}
Expand Down Expand Up @@ -239,10 +241,97 @@ export class MonacoEditorCommandHandlers implements CommandContribution {
}

protected registerMonacoActionCommands(): void {
const missingMonacoCommands = new Set([
'undo',
'redo',
// https://code.visualstudio.com/docs/getstarted/keybindings#_basic-editing
'editor.action.clipboardCutAction',
'editor.action.clipboardCopyAction',
'editor.action.deleteLines',
'editor.action.insertLineAfter',
'editor.action.insertLineBefore',
'editor.action.moveLinesDownAction',
'editor.action.moveLinesUpAction',
'editor.action.copyLinesDownAction',
'editor.action.copyLinesUpAction',
'editor.action.addSelectionToNextFindMatch',
'editor.action.moveSelectionToNextFindMatch',
'cursorUndo',
'editor.action.insertCursorAtEndOfEachLineSelected',
'editor.action.selectHighlights',
'editor.action.changeAll',
'expandLineSelection',
'editor.action.insertCursorBelow',
'editor.action.insertCursorAbove',
'editor.action.jumpToBracket',
'editor.action.indentLines',
'editor.action.outdentLines',
'cursorHome',
'cursorEnd',
'cursorBottom',
'cursorTop',
'scrollLineDown',
'scrollLineUp',
'scrollPageDown',
'scrollPageUp',
'editor.fold',
'editor.unfold',
'editor.foldRecursively',
'editor.unfoldRecursively',
'editor.foldAll',
'editor.unfoldAll',
'editor.action.addCommentLine',
'editor.action.removeCommentLine',
'editor.action.commentLine',
'editor.action.blockComment',
'actions.find',
'editor.action.startFindReplaceAction',
'editor.action.nextMatchFindAction',
'editor.action.previousMatchFindAction',
'editor.action.selectAllMatches',
'toggleFindCaseSensitive',
'toggleFindRegex',
'toggleFindWholeWord',
'editor.action.toggleTabFocusMode',
// Should be reimplemented by plugin extension, not part of Monaco
// 'toggleRenderWhitespace',
// 'editor.action.toggleWordWrap',
// 'workbench.action.editor.changeLanguageMode',
// https://code.visualstudio.com/docs/getstarted/keybindings#_rich-languages-editing
'editor.action.triggerSuggest',
'editor.action.triggerParameterHints',
'editor.action.formatDocument',
'editor.action.formatSelection',
'editor.action.showHover',
// Requier Monaco upgrade
// 'editor.action.revealDefinition',
// 'editor.action.peekDefinition',
// 'editor.action.revealDefinitionAside',
'editor.action.quickFix',
'editor.action.referenceSearch.trigger',
'editor.action.rename',
'editor.action.inPlaceReplace.down',
'editor.action.inPlaceReplace.up',
'editor.action.smartSelect.grow',
'editor.action.smartSelect.shrink',
'editor.action.trimTrailingWhitespace',
]);
for (const action of MonacoCommands.ACTIONS) {
missingMonacoCommands.delete(action.id);
const handler = this.newMonacoActionHandler(action);
this.registry.registerCommand(action, handler);
}
for (const id of missingMonacoCommands) {
const command = monaco.commands.CommandsRegistry.getCommand(id);
if (command) {
missingMonacoCommands.delete(id);
const handler = this.newCommandHandler(id);
this.registry.registerCommand({ id }, handler);
}
}
if (missingMonacoCommands.size) {
console.error('Missing VS Code commands', JSON.stringify([...missingMonacoCommands], undefined, 2));
}
}
protected newMonacoActionHandler(action: MonacoCommand): MonacoEditorCommandHandler {
const delegate = action.delegate;
Expand Down
7 changes: 6 additions & 1 deletion packages/monaco/src/browser/monaco-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
EditorMouseEvent
} from '@theia/editor/lib/browser';
import { MonacoEditorModel } from './monaco-editor-model';
import { MonacoCommandService } from './monaco-command-service';

import IEditorConstructionOptions = monaco.editor.IEditorConstructionOptions;
import IModelDeltaDecoration = monaco.editor.IModelDeltaDecoration;
Expand Down Expand Up @@ -362,7 +363,11 @@ export class MonacoEditor implements TextEditor {
}

get commandService(): monaco.commands.ICommandService {
return this.editor._commandService;
const commandService = this.editor._commandService;
if (commandService instanceof MonacoCommandService) {
return commandService.getDelegate() || commandService;
}
return commandService;
}

get instantiationService(): monaco.instantiation.IInstantiationService {
Expand Down
10 changes: 10 additions & 0 deletions packages/monaco/src/typings/monaco/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ declare module monaco.commands {
executeCommand(commandId: string, ...args: any[]): monaco.Promise<any>;
}

export interface ICommand {
id: string;
}

export interface ICommandRegistry {
getCommand(id: string): ICommand | undefined;
}

export const CommandsRegistry: ICommandRegistry;

}

declare module monaco.actions {
Expand Down