implemented a few more edit contributions.

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
Akos Kitta 2020-07-18 15:43:55 +02:00
parent c0cadf2b9b
commit 540a6215e4
10 changed files with 148 additions and 171 deletions

View File

@ -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);
});

View File

@ -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;
}

View File

@ -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<Sketch | undefined> {
protected async currentSketch(): Promise<Sketch | undefined> {
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<MonacoEditor | undefined> {
const editor = this.editorManager.currentEditor?.editor;
return editor instanceof MonacoEditor ? editor : undefined;
}
protected async run(commandId: string): Promise<any> {
const editor = await this.current();
if (editor) {
const action = editor.getControl().getAction(commandId);
if (action) {
return action.run();
}
}
}
}
export namespace Contribution {
export function configure<T>(bind: interfaces.Bind, serviceIdentifier: interfaces.ServiceIdentifier<T>): void {
bind(serviceIdentifier).toSelf().inSingletonScope();

View File

@ -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<void> {
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'
};
}
}

View File

@ -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<MonacoEditor | undefined> {
const editor = this.editorManager.currentEditor?.editor;
return editor instanceof MonacoEditor ? editor : undefined;
}
protected async currentValue(): Promise<string | undefined> {
return this.editorManager.currentEditor?.editor.document.getText();
}
protected async run(commandId: string): Promise<any> {
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'
};
}
}

View File

@ -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<void> {
return this.run('editor.action.gotoLine');
}
}
export namespace GoToLine {
export namespace Commands {
export const GO_TO_LINE: Command = {
id: 'arduino-go-to-line'
};
}
}

View File

@ -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<boolean> {
const sketch = await this.getCurrentSketch();
const sketch = await this.currentSketch();
if (!sketch) {
return false;
}

View File

@ -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<void> {
return this.run('editor.action.commentLine');
}
}
export namespace ToggleComment {
export namespace Commands {
export const TOGGLE_COMMENT: Command = {
id: 'arduino-toggle-comment'
};
}
}

View File

@ -57,7 +57,7 @@ export class UploadSketch extends SketchContribution {
}
async uploadSketch(): Promise<void> {
const sketch = await this.getCurrentSketch();
const sketch = await this.currentSketch();
if (!sketch) {
return;
}

View File

@ -53,7 +53,7 @@ export class VerifySketch extends SketchContribution {
}
async verifySketch(): Promise<void> {
const sketch = await this.getCurrentSketch();
const sketch = await this.currentSketch();
if (!sketch) {
return;
}