Skip to content

Commit 92afa48

Browse files
committed
Implemented possibility to open sketches via file navigator.
Signed-off-by: jbicker <jan.bicker@typefox.io>
1 parent 9d3cbf2 commit 92afa48

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

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

+15-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service
2121
import { SketchFactory } from './sketch-factory';
2222
import { ArduinoToolbar } from './toolbar/arduino-toolbar';
2323
import { EditorManager } from '@theia/editor/lib/browser';
24-
import { open, ContextMenuRenderer, OpenerService, Widget } from '@theia/core/lib/browser';
24+
import { ContextMenuRenderer, OpenerService, Widget } from '@theia/core/lib/browser';
2525
import { OpenFileDialogProps, FileDialogService } from '@theia/filesystem/lib/browser/file-dialog';
2626
import { FileSystem } from '@theia/filesystem/lib/common';
2727
import { ArduinoOpenSketchContextMenu } from './arduino-file-menu';
@@ -196,15 +196,8 @@ export class ArduinoFrontendContribution extends DefaultFrontendApplicationContr
196196
// url.hash = path + '?sketch=' + sketch.name
197197
// }
198198
// this.windowService.openNewWindow(url.toString());
199-
200-
const fileStat = await this.fileSystem.getFileStat(sketch.uri);
201-
if (fileStat) {
202-
const sketchFiles = await this.sketches.getSketchFiles(fileStat);
203-
sketchFiles.forEach(sketchFile => {
204-
const uri = new URI(sketchFile);
205-
this.editorManager.open(uri);
206-
});
207-
}
199+
200+
this.openSketchFiles(sketch.uri);
208201

209202
}
210203
})
@@ -228,6 +221,17 @@ export class ArduinoFrontendContribution extends DefaultFrontendApplicationContr
228221
})
229222
}
230223

224+
protected async openSketchFiles(uri: string) {
225+
const fileStat = await this.fileSystem.getFileStat(uri);
226+
if (fileStat) {
227+
const sketchFiles = await this.sketches.getSketchFiles(fileStat);
228+
sketchFiles.forEach(sketchFile => {
229+
const uri = new URI(sketchFile);
230+
this.editorManager.open(uri);
231+
});
232+
}
233+
}
234+
231235
/**
232236
* Opens a file after prompting the `Open File` dialog. Resolves to `undefined`, if
233237
* - the workspace root is not set,
@@ -247,7 +251,7 @@ export class ArduinoFrontendContribution extends DefaultFrontendApplicationContr
247251
if (destinationFileUri) {
248252
const destinationFile = await this.fileSystem.getFileStat(destinationFileUri.toString());
249253
if (destinationFile && !destinationFile.isDirectory) {
250-
await open(this.openerService, destinationFileUri);
254+
await this.openSketchFiles(destinationFileUri.toString());
251255
return destinationFileUri;
252256
}
253257
}

arduino-ide-extension/src/node/sketches-service-impl.ts

+28-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { injectable } from "inversify";
1+
import { injectable, inject } from "inversify";
22
import { SketchesService, Sketch } from "../common/protocol/sketches-service";
33
import URI from "@theia/core/lib/common/uri";
4-
import { FileStat } from "@theia/filesystem/lib/common";
4+
import { FileStat, FileSystem } from "@theia/filesystem/lib/common";
55
import * as fs from 'fs';
66
import * as path from 'path';
77

@@ -10,10 +10,14 @@ export const ALLOWED_FILE_EXTENSIONS = [".c", ".cpp", ".h", ".hh", ".hpp", ".s",
1010
@injectable()
1111
export class SketchesServiceImpl implements SketchesService {
1212

13+
@inject(FileSystem)
14+
protected readonly filesystem: FileSystem;
15+
1316
async getSketches(fileStat?: FileStat): Promise<Sketch[]> {
1417
const sketches: Sketch[] = [];
1518
if (fileStat && fileStat.isDirectory) {
16-
const sketchFolderPath = this.getPath(fileStat);
19+
const uri = new URI(fileStat.uri);
20+
const sketchFolderPath = uri.path.toString()
1721
const files = fs.readdirSync(sketchFolderPath);
1822
files.forEach(file => {
1923
const filePath = path.join(sketchFolderPath, file);
@@ -32,18 +36,30 @@ export class SketchesServiceImpl implements SketchesService {
3236
* Return all allowed files.
3337
* File extensions: "c", "cpp", "h", "hh", "hpp", "s", "pde", "ino"
3438
*/
35-
async getSketchFiles(sketchDir: FileStat): Promise<string[]> {
39+
async getSketchFiles(sketchFileStat: FileStat): Promise<string[]> {
3640
const files: string[] = [];
37-
const sketchDirPath = this.getPath(sketchDir);
38-
const sketchDirContents = fs.readdirSync(sketchDirPath);
39-
sketchDirContents.forEach(fileName => {
40-
const filePath = path.join(sketchDirPath, fileName);
41-
if (fs.existsSync(filePath) &&
42-
fs.lstatSync(filePath).isFile() &&
43-
ALLOWED_FILE_EXTENSIONS.indexOf(path.extname(filePath)) !== -1) {
41+
const sketchUri = new URI(sketchFileStat.uri);
42+
const sketchPath = sketchUri.path.toString();
43+
if (sketchFileStat.isDirectory && this.isSketchFolder(sketchPath, sketchUri.displayName)) {
44+
const sketchDirContents = fs.readdirSync(sketchPath);
45+
sketchDirContents.forEach(fileName => {
46+
const filePath = path.join(sketchPath, fileName);
47+
if (fs.existsSync(filePath) &&
48+
fs.lstatSync(filePath).isFile() &&
49+
ALLOWED_FILE_EXTENSIONS.indexOf(path.extname(filePath)) !== -1) {
4450
files.push(filePath);
51+
}
52+
});
53+
} else {
54+
const sketchDir = sketchUri.path.dir;
55+
if (this.isSketchFolder(sketchDir.toString(), sketchDir.name)) {
56+
const sketchFolderStat = await this.filesystem.getFileStat(sketchDir.toString());
57+
if (sketchFolderStat) {
58+
const sketchDirContents = await this.getSketchFiles(sketchFolderStat);
59+
files.push(...sketchDirContents);
60+
}
4561
}
46-
});
62+
}
4763
return files;
4864
}
4965

@@ -58,10 +74,4 @@ export class SketchesServiceImpl implements SketchesService {
5874
}
5975
return false;
6076
}
61-
62-
protected getPath(fileStat: FileStat) {
63-
const fileStatUri = fileStat.uri;
64-
const uri = new URI(fileStatUri);
65-
return uri.path.toString();
66-
}
6777
}

0 commit comments

Comments
 (0)