Skip to content

Commit d9daf86

Browse files
author
Christian Weichel
committed
Implemented "New Sketch"
1 parent 5077d95 commit d9daf86

File tree

5 files changed

+101
-3
lines changed

5 files changed

+101
-3
lines changed

arduino-ide-extension/src/browser/arduino-commands.ts

+6
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ export namespace ArduinoCommands {
1212
label: 'Upload Sketch'
1313
}
1414

15+
export const NEW_SKETCH: Command = {
16+
id: "arduino-new-sketch",
17+
label: 'New Sketch',
18+
category: 'File'
19+
}
20+
1521
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { injectable } from "inversify";
2+
import { MenuContribution, MenuModelRegistry } from "@theia/core";
3+
import { CommonMenus } from "@theia/core/lib/browser";
4+
import { ArduinoCommands } from "./arduino-commands";
5+
6+
@injectable()
7+
export class ArduinoFileMenuContribution implements MenuContribution {
8+
9+
registerMenus(registry: MenuModelRegistry) {
10+
registry.registerMenuAction([...CommonMenus.FILE, '0_new_sletch'], {
11+
commandId: ArduinoCommands.NEW_SKETCH.id
12+
})
13+
}
14+
15+
}

arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx

+76-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ import { ConnectedBoards } from './components/connected-boards';
1212
import { CoreService } from '../common/protocol/core-service';
1313
import { WorkspaceServiceExt } from './workspace-service-ext';
1414
import { ToolOutputServiceClient } from '../common/protocol/tool-output-service';
15-
import { ConfirmDialog } from '@theia/core/lib/browser';
15+
import { ConfirmDialog, OpenerService } from '@theia/core/lib/browser';
1616
import { QuickPickService } from '@theia/core/lib/common/quick-pick-service';
1717
import { BoardsListWidgetFrontendContribution } from './boards/boards-widget-frontend-contribution';
1818
import { BoardsNotificationService } from './boards-notification-service';
19-
19+
import { FileSystem, FileStat } from '@theia/filesystem/lib/common';
20+
import { WorkspaceRootUriAwareCommandHandler } from '@theia/workspace/lib/browser/workspace-commands';
21+
import { SelectionService } from '@theia/core';
22+
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
2023

2124
@injectable()
2225
export class ArduinoFrontendContribution extends DefaultFrontendApplicationContribution implements TabBarToolbarContribution, CommandContribution {
@@ -45,6 +48,19 @@ export class ArduinoFrontendContribution extends DefaultFrontendApplicationContr
4548
@inject(BoardsNotificationService)
4649
protected readonly boardsNotificationService: BoardsNotificationService;
4750

51+
@inject(FileSystem)
52+
protected readonly fileSystem: FileSystem;
53+
54+
@inject(OpenerService)
55+
protected readonly openerService: OpenerService;
56+
57+
@inject(WorkspaceService)
58+
protected readonly workspaceService: WorkspaceService;
59+
60+
@inject(SelectionService)
61+
protected readonly selectionService: SelectionService;
62+
63+
4864
@postConstruct()
4965
protected async init(): Promise<void> {
5066
// This is a hack. Otherwise, the backend services won't bind.
@@ -114,6 +130,64 @@ export class ArduinoFrontendContribution extends DefaultFrontendApplicationContr
114130
}
115131
}
116132
});
133+
registry.registerCommand(ArduinoCommands.NEW_SKETCH, new WorkspaceRootUriAwareCommandHandler(this.workspaceService, this.selectionService, {
134+
execute: async uri => {
135+
const parent = await this.getDirectory(uri)
136+
if (!parent) {
137+
return;
138+
}
139+
140+
const parentUri = new URI(parent.uri);
141+
const monthNames = ["january", "february", "march", "april", "may", "june",
142+
"july", "august", "september", "october", "november", "december"
143+
];
144+
const today = new Date();
145+
146+
const sketchBaseName = `sketch_${monthNames[today.getMonth()]}${today.getDay()}`;
147+
let sketchName: string | undefined;
148+
for (let i = 97; i < 97 + 26; i++) {
149+
let sketchNameCandidate = `${sketchBaseName}${String.fromCharCode(i)}`;
150+
if (await this.fileSystem.exists(parentUri.resolve(sketchNameCandidate).toString())) {
151+
continue;
152+
}
153+
154+
sketchName = sketchNameCandidate;
155+
break;
156+
}
157+
158+
if (!sketchName) {
159+
new ConfirmDialog({ title: "New sketch", msg: "Cannot create a unique sketch name", ok: "Ok" }).open();
160+
return;
161+
}
162+
163+
try {
164+
const sketchDir = parentUri.resolve(sketchName);
165+
const sketchFile = sketchDir.resolve(`${sketchName}.ino`);
166+
this.fileSystem.createFolder(sketchDir.toString());
167+
this.fileSystem.createFile(sketchFile.toString(), { content: `
168+
void setup() {
169+
170+
}
171+
172+
void loop() {
173+
174+
}
175+
` });
176+
const opener = await this.openerService.getOpener(sketchFile)
177+
opener.open(sketchFile, { reveal: true });
178+
} catch (e) {
179+
new ConfirmDialog({ title: "New sketch", msg: "Cannot create new sketch: " + e, ok: "Ok" }).open();
180+
}
181+
}
182+
}))
183+
}
184+
185+
protected async getDirectory(candidate: URI): Promise<FileStat | undefined> {
186+
const stat = await this.fileSystem.getFileStat(candidate.toString());
187+
if (stat && stat.isDirectory) {
188+
return stat;
189+
}
190+
return this.fileSystem.getFileStat(candidate.parent.toString());
117191
}
118192

119193
private async onNoBoardsInstalled() {

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

+3
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service
2626
import { AWorkspaceService } from './arduino-workspace-service';
2727
import { ThemeService } from '@theia/core/lib/browser/theming';
2828
import { ArduinoTheme } from './arduino-theme';
29+
import { ArduinoFileMenuContribution } from './arduino-file-menu';
30+
import { MenuContribution } from '@theia/core';
2931

3032
export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Unbind, isBound: interfaces.IsBound, rebind: interfaces.Rebind) => {
3133
// Commands and toolbar items
3234
bind(ArduinoFrontendContribution).toSelf().inSingletonScope();
3335
bind(CommandContribution).toService(ArduinoFrontendContribution);
3436
bind(TabBarToolbarContribution).toService(ArduinoFrontendContribution);
37+
bind(MenuContribution).to(ArduinoFileMenuContribution).inSingletonScope();
3538

3639
// `ino` TextMate grammar
3740
bind(LanguageGrammarDefinitionContribution).to(ArduinoLanguageGrammarContribution).inSingletonScope();

arduino-ide-extension/tslint.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"forin": false,
77
"indent": [true, "spaces"],
88
"max-line-length": [true, 180],
9-
"no-trailing-whitespace": true,
9+
"no-trailing-whitespace": false,
1010
"no-unused-expression": true,
1111
"no-use-before-declare": true,
1212
"no-var-keyword": true,

0 commit comments

Comments
 (0)