Skip to content

Commit 540a621

Browse files
author
Akos Kitta
committed
implemented a few more edit contributions.
Signed-off-by: Akos Kitta <kittaakos@typefox.io>
1 parent c0cadf2 commit 540a621

10 files changed

+148
-171
lines changed

arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ import { SaveSketch } from './contributions/save-sketch';
9696
import { VerifySketch } from './contributions/verify-sketch';
9797
import { UploadSketch } from './contributions/upload-sketch';
9898
import { CommonFrontendContribution } from './customization/core/common-frontend-contribution';
99-
import { CopyToForum } from './contributions/copy-to-forum';
100-
import { GoToLine } from './contributions/go-to-line';
101-
import { ToggleComment } from './contributions/toggle-comment';
99+
import { EditContributions } from './contributions/edit-contributions';
102100

103101
const ElementQueries = require('css-element-queries/src/ElementQueries');
104102

@@ -327,7 +325,5 @@ export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Un
327325
Contribution.configure(bind, SaveAsSketch);
328326
Contribution.configure(bind, VerifySketch);
329327
Contribution.configure(bind, UploadSketch);
330-
Contribution.configure(bind, CopyToForum);
331-
Contribution.configure(bind, GoToLine);
332-
Contribution.configure(bind, ToggleComment);
328+
Contribution.configure(bind, EditContributions);
333329
});

arduino-ide-extension/src/browser/contributions/close-sketch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class CloseSketch extends SketchContribution {
1515
registerCommands(registry: CommandRegistry): void {
1616
registry.registerCommand(CloseSketch.Commands.CLOSE_SKETCH, {
1717
execute: async () => {
18-
const sketch = await this.getCurrentSketch();
18+
const sketch = await this.currentSketch();
1919
if (!sketch) {
2020
return;
2121
}

arduino-ide-extension/src/browser/contributions/contribution.ts

+1-26
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import { TabBarToolbarContribution, TabBarToolbarRegistry } from '@theia/core/li
1111
import { Command, CommandRegistry, CommandContribution, CommandService } from '@theia/core/lib/common/command';
1212
import { SketchesService, ConfigService, FileSystemExt, Sketch } from '../../common/protocol';
1313
import { EditorMode } from '../editor-mode';
14-
import { EditorManager } from '@theia/editor/lib/browser';
15-
import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor';
1614

1715
export { Command, CommandRegistry, MenuModelRegistry, KeybindingRegistry, TabBarToolbarRegistry, URI, Sketch };
1816

@@ -63,7 +61,7 @@ export abstract class SketchContribution extends Contribution {
6361
@inject(SketchesService)
6462
protected readonly sketchService: SketchesService;
6563

66-
protected async getCurrentSketch(): Promise<Sketch | undefined> {
64+
protected async currentSketch(): Promise<Sketch | undefined> {
6765
const sketches = (await Promise.all(this.workspaceService.tryGetRoots().map(({ uri }) => this.sketchService.getSketchFolder(uri)))).filter(notEmpty);
6866
if (!sketches.length) {
6967
return;
@@ -76,29 +74,6 @@ export abstract class SketchContribution extends Contribution {
7674

7775
}
7876

79-
@injectable()
80-
export abstract class EditorContribution extends Contribution {
81-
82-
@inject(EditorManager)
83-
protected readonly editorManager: EditorManager;
84-
85-
protected async current(): Promise<MonacoEditor | undefined> {
86-
const editor = this.editorManager.currentEditor?.editor;
87-
return editor instanceof MonacoEditor ? editor : undefined;
88-
}
89-
90-
protected async run(commandId: string): Promise<any> {
91-
const editor = await this.current();
92-
if (editor) {
93-
const action = editor.getControl().getAction(commandId);
94-
if (action) {
95-
return action.run();
96-
}
97-
}
98-
}
99-
100-
}
101-
10277
export namespace Contribution {
10378
export function configure<T>(bind: interfaces.Bind, serviceIdentifier: interfaces.ServiceIdentifier<T>): void {
10479
bind(serviceIdentifier).toSelf().inSingletonScope();

arduino-ide-extension/src/browser/contributions/copy-to-forum.ts

-53
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
}

arduino-ide-extension/src/browser/contributions/go-to-line.ts

-41
This file was deleted.

arduino-ide-extension/src/browser/contributions/save-as-sketch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class SaveAsSketch extends SketchContribution {
3232
* Resolves `true` if the sketch was successfully saved as something.
3333
*/
3434
async saveAs({ execOnlyIfTemp, openAfterMove }: SaveAsSketch.Options = SaveAsSketch.Options.DEFAULT): Promise<boolean> {
35-
const sketch = await this.getCurrentSketch();
35+
const sketch = await this.currentSketch();
3636
if (!sketch) {
3737
return false;
3838
}

arduino-ide-extension/src/browser/contributions/toggle-comment.ts

-41
This file was deleted.

arduino-ide-extension/src/browser/contributions/upload-sketch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class UploadSketch extends SketchContribution {
5757
}
5858

5959
async uploadSketch(): Promise<void> {
60-
const sketch = await this.getCurrentSketch();
60+
const sketch = await this.currentSketch();
6161
if (!sketch) {
6262
return;
6363
}

arduino-ide-extension/src/browser/contributions/verify-sketch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class VerifySketch extends SketchContribution {
5353
}
5454

5555
async verifySketch(): Promise<void> {
56-
const sketch = await this.getCurrentSketch();
56+
const sketch = await this.currentSketch();
5757
if (!sketch) {
5858
return;
5959
}

0 commit comments

Comments
 (0)