Skip to content

Commit

Permalink
GH-210: Finalized selection menu and command contributions.
Browse files Browse the repository at this point in the history
Signed-off-by: Akos Kitta <[email protected]>
  • Loading branch information
kittaakos committed Sep 1, 2017
1 parent b474d4c commit bd1eb40
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 34 deletions.
15 changes: 11 additions & 4 deletions packages/core/src/common/commands-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CommandContribution, CommandRegistry } from './command';
import { injectable } from "inversify";

export namespace CommonCommands {

export const EDIT_MENU = "2_edit";
export const EDIT_MENU_UNDO_GROUP = "1_undo/redo";
export const EDIT_MENU_FIND_REPLACE_GROUP = "2_find/replace";
Expand All @@ -24,6 +25,7 @@ export namespace CommonCommands {

export const EDIT_FIND = 'actions.find';
export const EDIT_REPLACE = 'editor.action.startFindReplaceAction';

}

@injectable()
Expand All @@ -38,26 +40,31 @@ export class CommonMenuContribution implements MenuContribution {
MAIN_MENU_BAR,
CommonCommands.EDIT_MENU,
CommonCommands.EDIT_MENU_UNDO_GROUP], {
commandId: CommonCommands.EDIT_UNDO
commandId: CommonCommands.EDIT_UNDO,
order: '0'
});
registry.registerMenuAction([
MAIN_MENU_BAR,
CommonCommands.EDIT_MENU,
CommonCommands.EDIT_MENU_UNDO_GROUP], {
commandId: CommonCommands.EDIT_REDO
commandId: CommonCommands.EDIT_REDO,
order: '1'
});

// Find/Replace
registry.registerMenuAction([
MAIN_MENU_BAR,
CommonCommands.EDIT_MENU,
CommonCommands.EDIT_MENU_FIND_REPLACE_GROUP], {
commandId: CommonCommands.EDIT_FIND
commandId: CommonCommands.EDIT_FIND,
order: '0'
});
registry.registerMenuAction([
MAIN_MENU_BAR,
CommonCommands.EDIT_MENU,
CommonCommands.EDIT_MENU_FIND_REPLACE_GROUP], {
commandId: CommonCommands.EDIT_REPLACE
commandId: CommonCommands.EDIT_REPLACE,
order: '1'
});
}

Expand Down
38 changes: 27 additions & 11 deletions packages/monaco/src/browser/monaco-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export namespace MonacoSelectionCommands {
export const SELECTION_MENU_COPY_MOVE_GROUP = '2_copy_move_group';
export const SELECTION_MENU_CURSOR_GROUP = '3_cursor_group';

export const SELECTION_SELECT_ALL = 'editor.action.selectAll';
export const SELECTION_SELECT_ALL = 'editor.action.select.all';
export const SELECTION_EXPAND_SELECTION = 'editor.action.smartSelect.grow';
export const SELECTION_SHRINK_SELECTION = 'editor.action.smartSelect.shrink';

Expand All @@ -40,16 +40,15 @@ export namespace MonacoSelectionCommands {
export const SELECTION_MOVE_LINE_UP = 'editor.action.moveLinesUpAction';
export const SELECTION_MOVE_LINE_DOWN = 'editor.action.moveLinesDownAction';

export const SELECTION_SWITCH_TO_MULTI_CURSOR = 'workbench.action.toggleMultiCursorModifier';
export const SELECTION_ADD_CURSOR_ABOVE = 'editor.action.insertCursorAbove';
export const SELECTION_ADD_CURSOR_BELOW = 'editor.action.insertCursorBelow';
export const SELECTION_ADD_CURSOR_TO_LINE_END = 'editor.action.insertCursorAtEndOfEachLineSelected';
export const SELECTION_ADD_NEXT_OCCURRENCE = 'editor.action.addSelectionToNextFindMatch';
export const SELECTION_ADD_PREVIOUS_OCCURRENCE = 'editor.action.addSelectionToPreviousFindMatch';
export const SELECTION_SELECT_ALL_OCCURRENCES = 'editor.action.selectHighlights';

export const ACTIONS: { id: string, label: string }[] = [
{ id: SELECTION_SELECT_ALL, label: 'Select All' },
export const ACTIONS: { id: string, label: string, delegateId?: string }[] = [
{ id: SELECTION_SELECT_ALL, label: 'Select All', delegateId: 'editor.action.selectAll' },
{ id: SELECTION_EXPAND_SELECTION, label: 'Expand Selection' },
{ id: SELECTION_SHRINK_SELECTION, label: 'Shrink Selection' },

Expand All @@ -58,7 +57,6 @@ export namespace MonacoSelectionCommands {
{ id: SELECTION_MOVE_LINE_UP, label: 'Move Line Up' },
{ id: SELECTION_MOVE_LINE_DOWN, label: 'Move Line Down' },

{ id: SELECTION_SWITCH_TO_MULTI_CURSOR, label: SELECTION_SWITCH_TO_MULTI_CURSOR },
{ id: SELECTION_ADD_CURSOR_ABOVE, label: 'Add Cursor Above' },
{ id: SELECTION_ADD_CURSOR_BELOW, label: 'Add Cursor Below' },
{ id: SELECTION_ADD_CURSOR_TO_LINE_END, label: 'Add Cursors to Line Ends' },
Expand Down Expand Up @@ -100,8 +98,26 @@ export class MonacoEditorCommandHandlers implements CommandContribution {
commands.registerHandler(id, handler);
});

[CommonCommands.EDIT_FIND, CommonCommands.EDIT_REPLACE, ...MonacoSelectionCommands.ACTIONS.map(({ id }) => id)].forEach(id => {
commands.registerHandler(id, new EditorCommandHandler(id, this.editorManager, this.selectionService));
[CommonCommands.EDIT_FIND, CommonCommands.EDIT_REPLACE, ...MonacoSelectionCommands.ACTIONS.filter(action => !action.delegateId).map(({ id }) => id)].forEach(id => {
commands.registerHandler(id, this.newHandler(id));
});

// VSCode registers some commands as core commands and not as @editorAction. These have to be treated differently.
[...MonacoSelectionCommands.ACTIONS].forEach(action => {
if (action.delegateId) {
const { id, delegateId } = action;
commands.registerHandler(id, {
execute: () => {
const editor = getCurrent(this.editorManager);
if (editor) {
if (editor) {
editor.focus();
editor.commandService.executeCommand(delegateId!);
}
}
}
});
}
});

for (const menuItem of MenuRegistry.getMenuItems(MenuId.EditorContext)) {
Expand All @@ -120,6 +136,7 @@ export class MonacoEditorCommandHandlers implements CommandContribution {
label
});
});

}

protected newHandler(id: string): CommandHandler {
Expand All @@ -144,20 +161,19 @@ export class EditorCommandHandler implements CommandHandler {
execute(): Promise<any> {
const editor = getCurrent(this.editorManager);
if (editor) {
editor.focus();
return Promise.resolve(editor.runAction(this.id));
}
return Promise.resolve();
}

isVisible(): boolean {
const r = TextEditorSelection.is(this.selectionService.selection);
return r;
return TextEditorSelection.is(this.selectionService.selection);
}

isEnabled(): boolean {
const editor = getCurrent(this.editorManager);
const r = !!editor && editor.isActionSupported(this.id);
return r;
return !!editor && editor.isActionSupported(this.id);
}

}
Expand Down
45 changes: 26 additions & 19 deletions packages/monaco/src/browser/monaco-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,89 +45,96 @@ export class MonacoEditorMenuContribution implements MenuContribution {
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_SELECTION_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_SELECT_ALL
commandId: MonacoSelectionCommands.SELECTION_SELECT_ALL,
order: '0'
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_SELECTION_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_EXPAND_SELECTION
commandId: MonacoSelectionCommands.SELECTION_EXPAND_SELECTION,
order: '2'
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_SELECTION_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_SHRINK_SELECTION
commandId: MonacoSelectionCommands.SELECTION_SHRINK_SELECTION,
order: '3'
});

// Copy and move group
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_COPY_MOVE_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_COPY_LINE_UP
commandId: MonacoSelectionCommands.SELECTION_COPY_LINE_UP,
order: '0'
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_COPY_MOVE_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_COPY_LINE_DOWN
commandId: MonacoSelectionCommands.SELECTION_COPY_LINE_DOWN,
order: '1'
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_COPY_MOVE_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_MOVE_LINE_UP
commandId: MonacoSelectionCommands.SELECTION_MOVE_LINE_UP,
order: '2'
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_COPY_MOVE_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_MOVE_LINE_DOWN
commandId: MonacoSelectionCommands.SELECTION_MOVE_LINE_DOWN,
order: '3'
});

// Cursor group
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_CURSOR_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_SWITCH_TO_MULTI_CURSOR
commandId: MonacoSelectionCommands.SELECTION_ADD_CURSOR_ABOVE,
order: '0'
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_CURSOR_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_ADD_CURSOR_ABOVE
commandId: MonacoSelectionCommands.SELECTION_ADD_CURSOR_BELOW,
order: '1'
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_CURSOR_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_ADD_CURSOR_BELOW
commandId: MonacoSelectionCommands.SELECTION_ADD_CURSOR_TO_LINE_END,
order: '2'
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_CURSOR_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_ADD_CURSOR_TO_LINE_END
commandId: MonacoSelectionCommands.SELECTION_ADD_NEXT_OCCURRENCE,
order: '3'
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_CURSOR_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_ADD_NEXT_OCCURRENCE
commandId: MonacoSelectionCommands.SELECTION_ADD_PREVIOUS_OCCURRENCE,
order: '4'
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_CURSOR_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_ADD_PREVIOUS_OCCURRENCE
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_CURSOR_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_SELECT_ALL_OCCURRENCES
commandId: MonacoSelectionCommands.SELECTION_SELECT_ALL_OCCURRENCES,
order: '5'
});
}
}

0 comments on commit bd1eb40

Please sign in to comment.