From 540a6215e44b0346a63e6fc42e04f1b29b7d822f Mon Sep 17 00:00:00 2001 From: Akos Kitta Date: Sat, 18 Jul 2020 15:43:55 +0200 Subject: [PATCH] implemented a few more edit contributions. Signed-off-by: Akos Kitta --- .../browser/arduino-ide-frontend-module.ts | 8 +- .../src/browser/contributions/close-sketch.ts | 2 +- .../src/browser/contributions/contribution.ts | 27 +--- .../browser/contributions/copy-to-forum.ts | 53 ------- .../contributions/edit-contributions.ts | 141 ++++++++++++++++++ .../src/browser/contributions/go-to-line.ts | 41 ----- .../browser/contributions/save-as-sketch.ts | 2 +- .../browser/contributions/toggle-comment.ts | 41 ----- .../browser/contributions/upload-sketch.ts | 2 +- .../browser/contributions/verify-sketch.ts | 2 +- 10 files changed, 148 insertions(+), 171 deletions(-) delete mode 100644 arduino-ide-extension/src/browser/contributions/copy-to-forum.ts create mode 100644 arduino-ide-extension/src/browser/contributions/edit-contributions.ts delete mode 100644 arduino-ide-extension/src/browser/contributions/go-to-line.ts delete mode 100644 arduino-ide-extension/src/browser/contributions/toggle-comment.ts diff --git a/arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts b/arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts index bd8cab80..19460d28 100644 --- a/arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts +++ b/arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts @@ -96,9 +96,7 @@ import { SaveSketch } from './contributions/save-sketch'; import { VerifySketch } from './contributions/verify-sketch'; import { UploadSketch } from './contributions/upload-sketch'; import { CommonFrontendContribution } from './customization/core/common-frontend-contribution'; -import { CopyToForum } from './contributions/copy-to-forum'; -import { GoToLine } from './contributions/go-to-line'; -import { ToggleComment } from './contributions/toggle-comment'; +import { EditContributions } from './contributions/edit-contributions'; const ElementQueries = require('css-element-queries/src/ElementQueries'); @@ -327,7 +325,5 @@ export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Un Contribution.configure(bind, SaveAsSketch); Contribution.configure(bind, VerifySketch); Contribution.configure(bind, UploadSketch); - Contribution.configure(bind, CopyToForum); - Contribution.configure(bind, GoToLine); - Contribution.configure(bind, ToggleComment); + Contribution.configure(bind, EditContributions); }); diff --git a/arduino-ide-extension/src/browser/contributions/close-sketch.ts b/arduino-ide-extension/src/browser/contributions/close-sketch.ts index 687b1d25..92a1617e 100644 --- a/arduino-ide-extension/src/browser/contributions/close-sketch.ts +++ b/arduino-ide-extension/src/browser/contributions/close-sketch.ts @@ -15,7 +15,7 @@ export class CloseSketch extends SketchContribution { registerCommands(registry: CommandRegistry): void { registry.registerCommand(CloseSketch.Commands.CLOSE_SKETCH, { execute: async () => { - const sketch = await this.getCurrentSketch(); + const sketch = await this.currentSketch(); if (!sketch) { return; } diff --git a/arduino-ide-extension/src/browser/contributions/contribution.ts b/arduino-ide-extension/src/browser/contributions/contribution.ts index fb3da59d..248315d1 100644 --- a/arduino-ide-extension/src/browser/contributions/contribution.ts +++ b/arduino-ide-extension/src/browser/contributions/contribution.ts @@ -11,8 +11,6 @@ import { TabBarToolbarContribution, TabBarToolbarRegistry } from '@theia/core/li import { Command, CommandRegistry, CommandContribution, CommandService } from '@theia/core/lib/common/command'; import { SketchesService, ConfigService, FileSystemExt, Sketch } from '../../common/protocol'; import { EditorMode } from '../editor-mode'; -import { EditorManager } from '@theia/editor/lib/browser'; -import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor'; export { Command, CommandRegistry, MenuModelRegistry, KeybindingRegistry, TabBarToolbarRegistry, URI, Sketch }; @@ -63,7 +61,7 @@ export abstract class SketchContribution extends Contribution { @inject(SketchesService) protected readonly sketchService: SketchesService; - protected async getCurrentSketch(): Promise { + protected async currentSketch(): Promise { const sketches = (await Promise.all(this.workspaceService.tryGetRoots().map(({ uri }) => this.sketchService.getSketchFolder(uri)))).filter(notEmpty); if (!sketches.length) { return; @@ -76,29 +74,6 @@ export abstract class SketchContribution extends Contribution { } -@injectable() -export abstract class EditorContribution extends Contribution { - - @inject(EditorManager) - protected readonly editorManager: EditorManager; - - protected async current(): Promise { - const editor = this.editorManager.currentEditor?.editor; - return editor instanceof MonacoEditor ? editor : undefined; - } - - protected async run(commandId: string): Promise { - const editor = await this.current(); - if (editor) { - const action = editor.getControl().getAction(commandId); - if (action) { - return action.run(); - } - } - } - -} - export namespace Contribution { export function configure(bind: interfaces.Bind, serviceIdentifier: interfaces.ServiceIdentifier): void { bind(serviceIdentifier).toSelf().inSingletonScope(); diff --git a/arduino-ide-extension/src/browser/contributions/copy-to-forum.ts b/arduino-ide-extension/src/browser/contributions/copy-to-forum.ts deleted file mode 100644 index 250e6dd3..00000000 --- a/arduino-ide-extension/src/browser/contributions/copy-to-forum.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { inject, injectable } from 'inversify'; -import { EditorContribution, Command, MenuModelRegistry, KeybindingRegistry, CommandRegistry } from './contribution'; -import { ClipboardService } from '@theia/core/lib/browser/clipboard-service'; -import { ArduinoMenus } from '../menu/arduino-menus'; - -@injectable() -export class CopyToForum extends EditorContribution { - - @inject(ClipboardService) - protected readonly clipboardService: ClipboardService; - - registerCommands(registry: CommandRegistry): void { - registry.registerCommand(CopyToForum.Commands.COPY_TO_FORUM, { - execute: () => this.copyToForum() - }); - } - - registerMenus(registry: MenuModelRegistry): void { - registry.registerMenuAction(ArduinoMenus.EDIT__TEXT_CONTROL_GROUP, { - commandId: CopyToForum.Commands.COPY_TO_FORUM.id, - label: 'Copy to Forum', - order: '2' - }); - } - - registerKeybindings(registry: KeybindingRegistry): void { - registry.registerKeybinding({ - command: CopyToForum.Commands.COPY_TO_FORUM.id, - keybinding: 'CtrlCmd+Shift+C' - }); - } - - async copyToForum(): Promise { - const editor = await this.current(); - if (editor) { - const value = editor.getControl().getModel()?.getValue(); - if (value !== undefined) { - return this.clipboardService.writeText(`[code] -${value} -[/code]`); - } - } - } - -} - -export namespace CopyToForum { - export namespace Commands { - export const COPY_TO_FORUM: Command = { - id: 'arduino-copy-to-forum' - }; - } -} diff --git a/arduino-ide-extension/src/browser/contributions/edit-contributions.ts b/arduino-ide-extension/src/browser/contributions/edit-contributions.ts new file mode 100644 index 00000000..b803b2b4 --- /dev/null +++ b/arduino-ide-extension/src/browser/contributions/edit-contributions.ts @@ -0,0 +1,141 @@ +import { inject, injectable } from 'inversify'; +import { EditorManager } from '@theia/editor/lib/browser'; +import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor'; +import { Contribution, Command, MenuModelRegistry, KeybindingRegistry, CommandRegistry } from './contribution'; +import { ArduinoMenus } from '../menu/arduino-menus'; +import { ClipboardService } from '@theia/core/lib/browser/clipboard-service'; + +@injectable() +export class EditContributions extends Contribution { + + @inject(ClipboardService) + protected readonly clipboardService: ClipboardService; + + @inject(EditorManager) + protected readonly editorManager: EditorManager; + + registerCommands(registry: CommandRegistry): void { + registry.registerCommand(EditContributions.Commands.GO_TO_LINE, { execute: () => this.run('editor.action.gotoLine') }); + registry.registerCommand(EditContributions.Commands.TOGGLE_COMMENT, { execute: () => this.run('editor.action.commentLine') }); + registry.registerCommand(EditContributions.Commands.INDENT_LINES, { execute: () => this.run('editor.action.indentLines') }); + registry.registerCommand(EditContributions.Commands.OUTDENT_LINES, { execute: () => this.run('editor.action.outdentLines') }); + registry.registerCommand(EditContributions.Commands.COPY_FOR_FORUM, { + execute: async () => { + const value = await this.currentValue(); + if (value !== undefined) { + this.clipboardService.writeText(`[code] +${value} +[/code]`) + } + } + }); + registry.registerCommand(EditContributions.Commands.COPY_FOR_MARKDOWN, { + execute: async () => { + const value = await this.currentValue(); + if (value !== undefined) { + this.clipboardService.writeText(`\`\`\`cpp +${value} +\`\`\``) + } + } + }); + } + + registerMenus(registry: MenuModelRegistry): void { + registry.registerMenuAction(ArduinoMenus.EDIT__TEXT_CONTROL_GROUP, { + commandId: EditContributions.Commands.COPY_FOR_FORUM.id, + label: 'Copy for Forum', + }); + registry.registerMenuAction(ArduinoMenus.EDIT__TEXT_CONTROL_GROUP, { + commandId: EditContributions.Commands.COPY_FOR_MARKDOWN.id, + label: 'Copy for GitHub [Markdown]', + }); + registry.registerMenuAction(ArduinoMenus.EDIT__TEXT_CONTROL_GROUP, { + commandId: EditContributions.Commands.GO_TO_LINE.id, + label: 'Go to Line...', + }); + + registry.registerMenuAction(ArduinoMenus.EDIT__CODE_CONTROL_GROUP, { + commandId: EditContributions.Commands.TOGGLE_COMMENT.id, + label: 'Comment/Uncomment', + order: '0' + }); + registry.registerMenuAction(ArduinoMenus.EDIT__CODE_CONTROL_GROUP, { + commandId: EditContributions.Commands.INDENT_LINES.id, + label: 'Increase Indent', + order: '1' + }); + registry.registerMenuAction(ArduinoMenus.EDIT__CODE_CONTROL_GROUP, { + commandId: EditContributions.Commands.OUTDENT_LINES.id, + label: 'Decrease Indent', + order: '2' + }); + } + + registerKeybindings(registry: KeybindingRegistry): void { + registry.registerKeybinding({ + command: EditContributions.Commands.COPY_FOR_FORUM.id, + keybinding: 'CtrlCmd+Shift+C' + }); + registry.registerKeybinding({ + command: EditContributions.Commands.GO_TO_LINE.id, + keybinding: 'CtrlCmd+L' + }); + + registry.registerKeybinding({ + command: EditContributions.Commands.TOGGLE_COMMENT.id, + keybinding: 'CtrlCmd+/' + }); + registry.registerKeybinding({ + command: EditContributions.Commands.INDENT_LINES.id, + keybinding: 'Tab' + }); + registry.registerKeybinding({ + command: EditContributions.Commands.OUTDENT_LINES.id, + keybinding: 'Shift+Tab' + }); + } + + protected async current(): Promise { + const editor = this.editorManager.currentEditor?.editor; + return editor instanceof MonacoEditor ? editor : undefined; + } + + protected async currentValue(): Promise { + return this.editorManager.currentEditor?.editor.document.getText(); + } + + protected async run(commandId: string): Promise { + const editor = await this.current(); + if (editor) { + const action = editor.getControl().getAction(commandId); + if (action) { + return action.run(); + } + } + } + +} + +export namespace EditContributions { + export namespace Commands { + export const COPY_FOR_FORUM: Command = { + id: 'arduino-copy-for-forum' + }; + export const COPY_FOR_MARKDOWN: Command = { + id: 'arduino-copy-for-markdown' + }; + export const GO_TO_LINE: Command = { + id: 'arduino-go-to-line' + }; + export const TOGGLE_COMMENT: Command = { + id: 'arduino-toggle-comment' + }; + export const INDENT_LINES: Command = { + id: 'arduino-indent-lines' + }; + export const OUTDENT_LINES: Command = { + id: 'arduino-outdent-lines' + }; + } +} diff --git a/arduino-ide-extension/src/browser/contributions/go-to-line.ts b/arduino-ide-extension/src/browser/contributions/go-to-line.ts deleted file mode 100644 index e4fffcf4..00000000 --- a/arduino-ide-extension/src/browser/contributions/go-to-line.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { injectable } from 'inversify'; -import { EditorContribution, Command, MenuModelRegistry, KeybindingRegistry, CommandRegistry } from './contribution'; -import { ArduinoMenus } from '../menu/arduino-menus'; - -@injectable() -export class GoToLine extends EditorContribution { - - registerCommands(registry: CommandRegistry): void { - registry.registerCommand(GoToLine.Commands.GO_TO_LINE, { - execute: () => this.goToLine() - }); - } - - registerMenus(registry: MenuModelRegistry): void { - registry.registerMenuAction(ArduinoMenus.EDIT__TEXT_CONTROL_GROUP, { - commandId: GoToLine.Commands.GO_TO_LINE.id, - label: 'Go to Line...', - order: '6' - }); - } - - registerKeybindings(registry: KeybindingRegistry): void { - registry.registerKeybinding({ - command: GoToLine.Commands.GO_TO_LINE.id, - keybinding: 'CtrlCmd+L' - }); - } - - async goToLine(): Promise { - return this.run('editor.action.gotoLine'); - } - -} - -export namespace GoToLine { - export namespace Commands { - export const GO_TO_LINE: Command = { - id: 'arduino-go-to-line' - }; - } -} diff --git a/arduino-ide-extension/src/browser/contributions/save-as-sketch.ts b/arduino-ide-extension/src/browser/contributions/save-as-sketch.ts index a9ac2ee9..0b5f9753 100644 --- a/arduino-ide-extension/src/browser/contributions/save-as-sketch.ts +++ b/arduino-ide-extension/src/browser/contributions/save-as-sketch.ts @@ -32,7 +32,7 @@ export class SaveAsSketch extends SketchContribution { * Resolves `true` if the sketch was successfully saved as something. */ async saveAs({ execOnlyIfTemp, openAfterMove }: SaveAsSketch.Options = SaveAsSketch.Options.DEFAULT): Promise { - const sketch = await this.getCurrentSketch(); + const sketch = await this.currentSketch(); if (!sketch) { return false; } diff --git a/arduino-ide-extension/src/browser/contributions/toggle-comment.ts b/arduino-ide-extension/src/browser/contributions/toggle-comment.ts deleted file mode 100644 index 75983ce1..00000000 --- a/arduino-ide-extension/src/browser/contributions/toggle-comment.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { injectable } from 'inversify'; -import { EditorContribution, Command, MenuModelRegistry, KeybindingRegistry, CommandRegistry } from './contribution'; -import { ArduinoMenus } from '../menu/arduino-menus'; - -@injectable() -export class ToggleComment extends EditorContribution { - - registerCommands(registry: CommandRegistry): void { - registry.registerCommand(ToggleComment.Commands.TOGGLE_COMMENT, { - execute: () => this.toggleComment() - }); - } - - registerMenus(registry: MenuModelRegistry): void { - registry.registerMenuAction(ArduinoMenus.EDIT__CODE_CONTROL_GROUP, { - commandId: ToggleComment.Commands.TOGGLE_COMMENT.id, - label: 'Comment/Uncomment', - order: '0' - }); - } - - registerKeybindings(registry: KeybindingRegistry): void { - registry.registerKeybinding({ - command: ToggleComment.Commands.TOGGLE_COMMENT.id, - keybinding: 'CtrlCmd+L' - }); - } - - async toggleComment(): Promise { - return this.run('editor.action.commentLine'); - } - -} - -export namespace ToggleComment { - export namespace Commands { - export const TOGGLE_COMMENT: Command = { - id: 'arduino-toggle-comment' - }; - } -} diff --git a/arduino-ide-extension/src/browser/contributions/upload-sketch.ts b/arduino-ide-extension/src/browser/contributions/upload-sketch.ts index ceac4bdb..b8182ba8 100644 --- a/arduino-ide-extension/src/browser/contributions/upload-sketch.ts +++ b/arduino-ide-extension/src/browser/contributions/upload-sketch.ts @@ -57,7 +57,7 @@ export class UploadSketch extends SketchContribution { } async uploadSketch(): Promise { - const sketch = await this.getCurrentSketch(); + const sketch = await this.currentSketch(); if (!sketch) { return; } diff --git a/arduino-ide-extension/src/browser/contributions/verify-sketch.ts b/arduino-ide-extension/src/browser/contributions/verify-sketch.ts index 2cf3643c..6c52783c 100644 --- a/arduino-ide-extension/src/browser/contributions/verify-sketch.ts +++ b/arduino-ide-extension/src/browser/contributions/verify-sketch.ts @@ -53,7 +53,7 @@ export class VerifySketch extends SketchContribution { } async verifySketch(): Promise { - const sketch = await this.getCurrentSketch(); + const sketch = await this.currentSketch(); if (!sketch) { return; }