Skip to content

Commit

Permalink
GH-210: Registered selection commands, handlers and menu items.
Browse files Browse the repository at this point in the history
Signed-off-by: Akos Kitta <[email protected]>
  • Loading branch information
kittaakos authored and akosyakov committed Sep 6, 2017
1 parent 29505f7 commit d27a739
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 9 deletions.
71 changes: 66 additions & 5 deletions packages/monaco/src/browser/monaco-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,68 @@

import { injectable, inject } from "inversify";
import { ProtocolToMonacoConverter } from "monaco-languageclient/lib";
<<<<<<< HEAD
import { CommandHandler, CommandContribution, CommandRegistry, SelectionService } from '@theia/core/lib/common';
import { CommonCommands } from '@theia/core/lib/browser';
import { EditorManager, TextEditorSelection, SHOW_REFERENCES } from '@theia/editor/lib/browser';
=======
>>>>>>> 9748b0e... GH-210: Registered `selection` commands, handlers and menu items.
import { Position, Location } from "@theia/languages/lib/common";
import { EditorManager, TextEditorSelection, SHOW_REFERENCES } from '@theia/editor/lib/browser';
import { CommandHandler, CommandContribution, CommandRegistry, CommonCommands, SelectionService } from '@theia/core/lib/common';
import { getCurrent, MonacoEditor } from './monaco-editor';
import MenuRegistry = monaco.actions.MenuRegistry;
import MenuId = monaco.actions.MenuId;

/**
* Editor commands for the `Selection` menu contribution.
*/
export namespace MonacoSelectionCommands {

export const SELECTION_MENU = '3_selection';

export const SELECTION_MENU_SELECTION_GROUP = '1_selection_group';
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_EXPAND_SELECTION = 'editor.action.smartSelect.grow';
export const SELECTION_SHRINK_SELECTION = 'editor.action.smartSelect.shrink';

export const SELECTION_COPY_LINE_UP = 'editor.action.copyLinesUpAction';
export const SELECTION_COPY_LINE_DOWN = 'editor.action.copyLinesDownAction';
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' },
{ id: SELECTION_EXPAND_SELECTION, label: 'Expand Selection' },
{ id: SELECTION_SHRINK_SELECTION, label: 'Shrink Selection' },

{ id: SELECTION_COPY_LINE_UP, label: 'Copy Line Up' },
{ id: SELECTION_COPY_LINE_DOWN, label: 'Copy Line Down' },
{ 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' },
{ id: SELECTION_ADD_NEXT_OCCURRENCE, label: 'Add Next Occurrence' },
{ id: SELECTION_ADD_PREVIOUS_OCCURRENCE, label: 'Add Previous Occurrence' },
{ id: SELECTION_SELECT_ALL_OCCURRENCES, label: 'Select All Occurrences' }
];

}

@injectable()
export class MonacoEditorCommandHandlers implements CommandContribution {

Expand All @@ -25,6 +79,7 @@ export class MonacoEditorCommandHandlers implements CommandContribution {
) { }

registerCommands(commands: CommandRegistry) {

commands.registerCommand(SHOW_REFERENCES, {
execute: (uri: string, position: Position, locations: Location[]) => {
const editor = getCurrent(this.editorManager);
Expand All @@ -45,11 +100,9 @@ export class MonacoEditorCommandHandlers implements CommandContribution {
commands.registerHandler(id, handler);
});

const findHandler = new EditorCommandHandler(CommonCommands.EDIT_FIND, this.editorManager, this.selectionService);
commands.registerHandler(CommonCommands.EDIT_FIND, findHandler);

const replaceHandler = new EditorCommandHandler(CommonCommands.EDIT_REPLACE, this.editorManager, this.selectionService);
commands.registerHandler(CommonCommands.EDIT_REPLACE, replaceHandler);
[CommonCommands.EDIT_FIND, CommonCommands.EDIT_REPLACE, ...MonacoSelectionCommands.ACTIONS.map(({ id }) => id)].forEach(id => {
commands.registerHandler(id, new EditorCommandHandler(id, this.editorManager, this.selectionService));
});

for (const menuItem of MenuRegistry.getMenuItems(MenuId.EditorContext)) {
const { id, title, iconClass } = menuItem.command;
Expand All @@ -59,6 +112,14 @@ export class MonacoEditorCommandHandlers implements CommandContribution {
label: title
}, this.newHandler(id));
}

MonacoSelectionCommands.ACTIONS.forEach(entry => {
const { id, label } = entry;
commands.registerCommand({
id,
label
});
});
}

protected newHandler(id: string): CommandHandler {
Expand Down
105 changes: 101 additions & 4 deletions packages/monaco/src/browser/monaco-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,128 @@
*/

import { injectable } from "inversify";
<<<<<<< HEAD
import { MenuContribution, MenuModelRegistry } from "@theia/core/lib/common";
import { CommonCommands } from '@theia/core/lib/browser';
=======
import { MenuContribution, MenuModelRegistry, CommonCommands, MAIN_MENU_BAR } from "@theia/core/lib/common";
>>>>>>> 9748b0e... GH-210: Registered `selection` commands, handlers and menu items.
import { EDITOR_CONTEXT_MENU_ID } from "@theia/editor/lib/browser";
import { MonacoSelectionCommands } from "./monaco-command";
import MenuRegistry = monaco.actions.MenuRegistry;
import MenuId = monaco.actions.MenuId;
import IMenuItem = monaco.actions.IMenuItem;

@injectable()
export class MonacoEditorMenuContribution implements MenuContribution {
registerMenus(registry: MenuModelRegistry) {
// Explicitly register the Edit Submenu

registry.registerMenuAction([EDITOR_CONTEXT_MENU_ID, "1_undo/redo"], {
commandId: CommonCommands.EDIT_UNDO
});
registry.registerMenuAction([EDITOR_CONTEXT_MENU_ID, "1_undo/redo"], {
commandId: CommonCommands.EDIT_REDO
});

const wrap: (item: IMenuItem) => { path: string[], commandId: string } = (item) => {
return { path: [EDITOR_CONTEXT_MENU_ID, (item.group || "")], commandId: item.command.id };
};
const wrap: (item: IMenuItem) => { path: string[], commandId: string } = item =>
({ path: [EDITOR_CONTEXT_MENU_ID, (item.group || "")], commandId: item.command.id });

MenuRegistry.getMenuItems(MenuId.EditorContext)
.map(item => wrap(item))
.forEach(props => registry.registerMenuAction(props.path, { commandId: props.commandId }));


// Explicitly register `Selection` menu.
registry.registerSubmenu([MAIN_MENU_BAR], MonacoSelectionCommands.SELECTION_MENU, "Selection");

// Selection group
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_SELECTION_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_SELECT_ALL
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_SELECTION_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_EXPAND_SELECTION
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_SELECTION_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_SHRINK_SELECTION
});

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

// Cursor group
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_CURSOR_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_SWITCH_TO_MULTI_CURSOR
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_CURSOR_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_ADD_CURSOR_ABOVE
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_CURSOR_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_ADD_CURSOR_BELOW
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_CURSOR_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_ADD_CURSOR_TO_LINE_END
});
registry.registerMenuAction([
MAIN_MENU_BAR,
MonacoSelectionCommands.SELECTION_MENU,
MonacoSelectionCommands.SELECTION_MENU_CURSOR_GROUP], {
commandId: MonacoSelectionCommands.SELECTION_ADD_NEXT_OCCURRENCE
});
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
});
}
}

0 comments on commit d27a739

Please sign in to comment.