|
| 1 | +import { inject, injectable } from 'inversify'; |
| 2 | +import { EditorManager } from '@theia/editor/lib/browser'; |
| 3 | +import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor'; |
| 4 | +import { Contribution, Command, MenuModelRegistry, KeybindingRegistry, CommandRegistry } from './contribution'; |
| 5 | +import { ArduinoMenus } from '../menu/arduino-menus'; |
| 6 | +import { ClipboardService } from '@theia/core/lib/browser/clipboard-service'; |
| 7 | + |
| 8 | +@injectable() |
| 9 | +export class EditContributions extends Contribution { |
| 10 | + |
| 11 | + @inject(ClipboardService) |
| 12 | + protected readonly clipboardService: ClipboardService; |
| 13 | + |
| 14 | + @inject(EditorManager) |
| 15 | + protected readonly editorManager: EditorManager; |
| 16 | + |
| 17 | + registerCommands(registry: CommandRegistry): void { |
| 18 | + registry.registerCommand(EditContributions.Commands.GO_TO_LINE, { execute: () => this.run('editor.action.gotoLine') }); |
| 19 | + registry.registerCommand(EditContributions.Commands.TOGGLE_COMMENT, { execute: () => this.run('editor.action.commentLine') }); |
| 20 | + registry.registerCommand(EditContributions.Commands.INDENT_LINES, { execute: () => this.run('editor.action.indentLines') }); |
| 21 | + registry.registerCommand(EditContributions.Commands.OUTDENT_LINES, { execute: () => this.run('editor.action.outdentLines') }); |
| 22 | + registry.registerCommand(EditContributions.Commands.COPY_FOR_FORUM, { |
| 23 | + execute: async () => { |
| 24 | + const value = await this.currentValue(); |
| 25 | + if (value !== undefined) { |
| 26 | + this.clipboardService.writeText(`[code] |
| 27 | +${value} |
| 28 | +[/code]`) |
| 29 | + } |
| 30 | + } |
| 31 | + }); |
| 32 | + registry.registerCommand(EditContributions.Commands.COPY_FOR_MARKDOWN, { |
| 33 | + execute: async () => { |
| 34 | + const value = await this.currentValue(); |
| 35 | + if (value !== undefined) { |
| 36 | + this.clipboardService.writeText(`\`\`\`cpp |
| 37 | +${value} |
| 38 | +\`\`\``) |
| 39 | + } |
| 40 | + } |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + registerMenus(registry: MenuModelRegistry): void { |
| 45 | + registry.registerMenuAction(ArduinoMenus.EDIT__TEXT_CONTROL_GROUP, { |
| 46 | + commandId: EditContributions.Commands.COPY_FOR_FORUM.id, |
| 47 | + label: 'Copy for Forum', |
| 48 | + }); |
| 49 | + registry.registerMenuAction(ArduinoMenus.EDIT__TEXT_CONTROL_GROUP, { |
| 50 | + commandId: EditContributions.Commands.COPY_FOR_MARKDOWN.id, |
| 51 | + label: 'Copy for GitHub [Markdown]', |
| 52 | + }); |
| 53 | + registry.registerMenuAction(ArduinoMenus.EDIT__TEXT_CONTROL_GROUP, { |
| 54 | + commandId: EditContributions.Commands.GO_TO_LINE.id, |
| 55 | + label: 'Go to Line...', |
| 56 | + }); |
| 57 | + |
| 58 | + registry.registerMenuAction(ArduinoMenus.EDIT__CODE_CONTROL_GROUP, { |
| 59 | + commandId: EditContributions.Commands.TOGGLE_COMMENT.id, |
| 60 | + label: 'Comment/Uncomment', |
| 61 | + order: '0' |
| 62 | + }); |
| 63 | + registry.registerMenuAction(ArduinoMenus.EDIT__CODE_CONTROL_GROUP, { |
| 64 | + commandId: EditContributions.Commands.INDENT_LINES.id, |
| 65 | + label: 'Increase Indent', |
| 66 | + order: '1' |
| 67 | + }); |
| 68 | + registry.registerMenuAction(ArduinoMenus.EDIT__CODE_CONTROL_GROUP, { |
| 69 | + commandId: EditContributions.Commands.OUTDENT_LINES.id, |
| 70 | + label: 'Decrease Indent', |
| 71 | + order: '2' |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + registerKeybindings(registry: KeybindingRegistry): void { |
| 76 | + registry.registerKeybinding({ |
| 77 | + command: EditContributions.Commands.COPY_FOR_FORUM.id, |
| 78 | + keybinding: 'CtrlCmd+Shift+C' |
| 79 | + }); |
| 80 | + registry.registerKeybinding({ |
| 81 | + command: EditContributions.Commands.GO_TO_LINE.id, |
| 82 | + keybinding: 'CtrlCmd+L' |
| 83 | + }); |
| 84 | + |
| 85 | + registry.registerKeybinding({ |
| 86 | + command: EditContributions.Commands.TOGGLE_COMMENT.id, |
| 87 | + keybinding: 'CtrlCmd+/' |
| 88 | + }); |
| 89 | + registry.registerKeybinding({ |
| 90 | + command: EditContributions.Commands.INDENT_LINES.id, |
| 91 | + keybinding: 'Tab' |
| 92 | + }); |
| 93 | + registry.registerKeybinding({ |
| 94 | + command: EditContributions.Commands.OUTDENT_LINES.id, |
| 95 | + keybinding: 'Shift+Tab' |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + protected async current(): Promise<MonacoEditor | undefined> { |
| 100 | + const editor = this.editorManager.currentEditor?.editor; |
| 101 | + return editor instanceof MonacoEditor ? editor : undefined; |
| 102 | + } |
| 103 | + |
| 104 | + protected async currentValue(): Promise<string | undefined> { |
| 105 | + return this.editorManager.currentEditor?.editor.document.getText(); |
| 106 | + } |
| 107 | + |
| 108 | + protected async run(commandId: string): Promise<any> { |
| 109 | + const editor = await this.current(); |
| 110 | + if (editor) { |
| 111 | + const action = editor.getControl().getAction(commandId); |
| 112 | + if (action) { |
| 113 | + return action.run(); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | +} |
| 119 | + |
| 120 | +export namespace EditContributions { |
| 121 | + export namespace Commands { |
| 122 | + export const COPY_FOR_FORUM: Command = { |
| 123 | + id: 'arduino-copy-for-forum' |
| 124 | + }; |
| 125 | + export const COPY_FOR_MARKDOWN: Command = { |
| 126 | + id: 'arduino-copy-for-markdown' |
| 127 | + }; |
| 128 | + export const GO_TO_LINE: Command = { |
| 129 | + id: 'arduino-go-to-line' |
| 130 | + }; |
| 131 | + export const TOGGLE_COMMENT: Command = { |
| 132 | + id: 'arduino-toggle-comment' |
| 133 | + }; |
| 134 | + export const INDENT_LINES: Command = { |
| 135 | + id: 'arduino-indent-lines' |
| 136 | + }; |
| 137 | + export const OUTDENT_LINES: Command = { |
| 138 | + id: 'arduino-outdent-lines' |
| 139 | + }; |
| 140 | + } |
| 141 | +} |
0 commit comments