From 29505f78e5c57846e43dd7a74994e8abda881f08 Mon Sep 17 00:00:00 2001 From: Akos Kitta Date: Tue, 29 Aug 2017 11:49:44 +0200 Subject: [PATCH] GH-210: Registered find/replace commands and menu items. Signed-off-by: Akos Kitta --- packages/core/src/common/commands-common.ts | 100 ++++++++++++++++++ .../editor/src/browser/editor-keybinding.ts | 7 +- packages/monaco/src/browser/monaco-command.ts | 22 ++-- .../src/browser/monaco-model-resolver.ts | 4 +- 4 files changed, 118 insertions(+), 15 deletions(-) create mode 100644 packages/core/src/common/commands-common.ts diff --git a/packages/core/src/common/commands-common.ts b/packages/core/src/common/commands-common.ts new file mode 100644 index 0000000000000..65a88ca1f7930 --- /dev/null +++ b/packages/core/src/common/commands-common.ts @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2017 TypeFox and others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + */ + +import { MAIN_MENU_BAR, MenuContribution, MenuModelRegistry } from './menu'; +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"; + export const EDIT_MENU_COPYPASTE_GROUP = "2_copy"; + + export const EDIT_CUT = 'edit_cut'; + export const EDIT_COPY = 'edit_copy'; + export const EDIT_PASTE = 'edit_paste'; + + export const EDIT_UNDO = 'undo'; + export const EDIT_REDO = 'redo'; + + export const EDIT_FIND = 'actions.find'; + export const EDIT_REPLACE = 'editor.action.startFindReplaceAction'; +} + +@injectable() +export class CommonMenuContribution implements MenuContribution { + + registerMenus(registry: MenuModelRegistry): void { + // Explicitly register the Edit Submenu + registry.registerSubmenu([MAIN_MENU_BAR], CommonCommands.EDIT_MENU, "Edit"); + + // Undo/Redo + registry.registerMenuAction([ + MAIN_MENU_BAR, + CommonCommands.EDIT_MENU, + CommonCommands.EDIT_MENU_UNDO_GROUP], { + commandId: CommonCommands.EDIT_UNDO + }); + registry.registerMenuAction([ + MAIN_MENU_BAR, + CommonCommands.EDIT_MENU, + CommonCommands.EDIT_MENU_UNDO_GROUP], { + commandId: CommonCommands.EDIT_REDO + }); + // Find/Replace + registry.registerMenuAction([ + MAIN_MENU_BAR, + CommonCommands.EDIT_MENU, + CommonCommands.EDIT_MENU_FIND_REPLACE_GROUP], { + commandId: CommonCommands.EDIT_FIND + }); + registry.registerMenuAction([ + MAIN_MENU_BAR, + CommonCommands.EDIT_MENU, + CommonCommands.EDIT_MENU_FIND_REPLACE_GROUP], { + commandId: CommonCommands.EDIT_REPLACE + }); + } + +} + +@injectable() +export class CommonCommandContribution implements CommandContribution { + + registerCommands(commandRegistry: CommandRegistry): void { + commandRegistry.registerCommand({ + id: CommonCommands.EDIT_CUT, + label: 'Cut' + }); + commandRegistry.registerCommand({ + id: CommonCommands.EDIT_COPY, + label: 'Copy', + }); + commandRegistry.registerCommand({ + id: CommonCommands.EDIT_PASTE, + label: 'Paste' + }); + commandRegistry.registerCommand({ + id: CommonCommands.EDIT_UNDO, + label: 'Undo' + }); + commandRegistry.registerCommand({ + id: CommonCommands.EDIT_REDO, + label: 'Redo' + }); + commandRegistry.registerCommand({ + id: CommonCommands.EDIT_FIND, + label: 'Find' + }); + commandRegistry.registerCommand({ + id: CommonCommands.EDIT_REPLACE, + label: 'Replace' + }); + } + +} \ No newline at end of file diff --git a/packages/editor/src/browser/editor-keybinding.ts b/packages/editor/src/browser/editor-keybinding.ts index 5b7567b275010..32dff49f30a4d 100644 --- a/packages/editor/src/browser/editor-keybinding.ts +++ b/packages/editor/src/browser/editor-keybinding.ts @@ -6,10 +6,9 @@ */ import { injectable, inject } from "inversify"; -import { - KeybindingContext, Keybinding, KeybindingContribution, KeybindingRegistry, KeyCode, Key, Modifier -} from "@theia/core/lib/common"; import { EditorManager } from "./editor-manager"; +import { KeyCode, Key, Modifier } from "@theia/core/lib/common/keys"; +import { KeybindingContext, Keybinding, KeybindingContribution, KeybindingRegistry } from "@theia/core/lib/common/keybinding"; @injectable() export class EditorKeybindingContext implements KeybindingContext { @@ -49,4 +48,4 @@ export class EditorKeybindingContribution implements KeybindingContribution { } -} \ No newline at end of file +} diff --git a/packages/monaco/src/browser/monaco-command.ts b/packages/monaco/src/browser/monaco-command.ts index bf3270f8e0c58..4e2d0bacc5aab 100644 --- a/packages/monaco/src/browser/monaco-command.ts +++ b/packages/monaco/src/browser/monaco-command.ts @@ -7,12 +7,10 @@ import { injectable, inject } from "inversify"; import { ProtocolToMonacoConverter } from "monaco-languageclient/lib"; -import { - CommandHandler, CommandContribution, CommandRegistry, SelectionService -} from '@theia/core/lib/common'; +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'; -import { Position, Location } from "@theia/languages/lib/common" +import { Position, Location } from "@theia/languages/lib/common"; import { getCurrent, MonacoEditor } from './monaco-editor'; import MenuRegistry = monaco.actions.MenuRegistry; import MenuId = monaco.actions.MenuId; @@ -42,13 +40,17 @@ export class MonacoEditorCommandHandlers implements CommandContribution { }); [CommonCommands.EDIT_UNDO, CommonCommands.EDIT_REDO].forEach(id => { - const doExecute = (editor: MonacoEditor, ...args: any[]): any => { - return editor.getControl().cursor.trigger('keyboard', id, args); - }; + const doExecute = (editor: MonacoEditor, ...args: any[]): any => editor.getControl().cursor.trigger('keyboard', id, args); const handler = this.newClipboardHandler(id, doExecute); 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); + for (const menuItem of MenuRegistry.getMenuItems(MenuId.EditorContext)) { const { id, title, iconClass } = menuItem.command; commands.registerCommand({ @@ -87,12 +89,14 @@ export class EditorCommandHandler implements CommandHandler { } isVisible(): boolean { - return TextEditorSelection.is(this.selectionService.selection); + const r = TextEditorSelection.is(this.selectionService.selection); + return r; } isEnabled(): boolean { const editor = getCurrent(this.editorManager); - return !!editor && editor.isActionSupported(this.id); + const r = !!editor && editor.isActionSupported(this.id); + return r; } } diff --git a/packages/monaco/src/browser/monaco-model-resolver.ts b/packages/monaco/src/browser/monaco-model-resolver.ts index 2f07853b3a77f..f2008c70086d1 100644 --- a/packages/monaco/src/browser/monaco-model-resolver.ts +++ b/packages/monaco/src/browser/monaco-model-resolver.ts @@ -51,7 +51,7 @@ export class MonacoModelResolver implements ITextModelResolverService { object: model, dispose: () => removeReference.dispose() - } + }; removeReference = references.push(reference); return reference; } @@ -81,6 +81,6 @@ export class MonacoModelResolver implements ITextModelResolverService { dispose(): void { // no-op } - } + }; } }