Skip to content

Commit

Permalink
GH-210: Registered find/replace commands 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 ba27a62 commit 29505f7
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 15 deletions.
100 changes: 100 additions & 0 deletions packages/core/src/common/commands-common.ts
Original file line number Diff line number Diff line change
@@ -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'
});
}

}
7 changes: 3 additions & 4 deletions packages/editor/src/browser/editor-keybinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -49,4 +48,4 @@ export class EditorKeybindingContribution implements KeybindingContribution {

}

}
}
22 changes: 13 additions & 9 deletions packages/monaco/src/browser/monaco-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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;
}

}
Expand Down
4 changes: 2 additions & 2 deletions packages/monaco/src/browser/monaco-model-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class MonacoModelResolver implements ITextModelResolverService {
object: model,
dispose: () =>
removeReference.dispose()
}
};
removeReference = references.push(reference);
return reference;
}
Expand Down Expand Up @@ -81,6 +81,6 @@ export class MonacoModelResolver implements ITextModelResolverService {
dispose(): void {
// no-op
}
}
};
}
}

0 comments on commit 29505f7

Please sign in to comment.