mirror of
https://github.com/arduino/arduino-ide.git
synced 2025-06-06 12:16:33 +00:00
Implemented possibility to open sketches via file navigator.
Signed-off-by: jbicker <jan.bicker@typefox.io>
This commit is contained in:
parent
9d3cbf2ea0
commit
92afa48c05
@ -21,7 +21,7 @@ import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service
|
||||
import { SketchFactory } from './sketch-factory';
|
||||
import { ArduinoToolbar } from './toolbar/arduino-toolbar';
|
||||
import { EditorManager } from '@theia/editor/lib/browser';
|
||||
import { open, ContextMenuRenderer, OpenerService, Widget } from '@theia/core/lib/browser';
|
||||
import { ContextMenuRenderer, OpenerService, Widget } from '@theia/core/lib/browser';
|
||||
import { OpenFileDialogProps, FileDialogService } from '@theia/filesystem/lib/browser/file-dialog';
|
||||
import { FileSystem } from '@theia/filesystem/lib/common';
|
||||
import { ArduinoOpenSketchContextMenu } from './arduino-file-menu';
|
||||
@ -196,15 +196,8 @@ export class ArduinoFrontendContribution extends DefaultFrontendApplicationContr
|
||||
// url.hash = path + '?sketch=' + sketch.name
|
||||
// }
|
||||
// this.windowService.openNewWindow(url.toString());
|
||||
|
||||
const fileStat = await this.fileSystem.getFileStat(sketch.uri);
|
||||
if (fileStat) {
|
||||
const sketchFiles = await this.sketches.getSketchFiles(fileStat);
|
||||
sketchFiles.forEach(sketchFile => {
|
||||
const uri = new URI(sketchFile);
|
||||
this.editorManager.open(uri);
|
||||
});
|
||||
}
|
||||
|
||||
this.openSketchFiles(sketch.uri);
|
||||
|
||||
}
|
||||
})
|
||||
@ -228,6 +221,17 @@ export class ArduinoFrontendContribution extends DefaultFrontendApplicationContr
|
||||
})
|
||||
}
|
||||
|
||||
protected async openSketchFiles(uri: string) {
|
||||
const fileStat = await this.fileSystem.getFileStat(uri);
|
||||
if (fileStat) {
|
||||
const sketchFiles = await this.sketches.getSketchFiles(fileStat);
|
||||
sketchFiles.forEach(sketchFile => {
|
||||
const uri = new URI(sketchFile);
|
||||
this.editorManager.open(uri);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a file after prompting the `Open File` dialog. Resolves to `undefined`, if
|
||||
* - the workspace root is not set,
|
||||
@ -247,7 +251,7 @@ export class ArduinoFrontendContribution extends DefaultFrontendApplicationContr
|
||||
if (destinationFileUri) {
|
||||
const destinationFile = await this.fileSystem.getFileStat(destinationFileUri.toString());
|
||||
if (destinationFile && !destinationFile.isDirectory) {
|
||||
await open(this.openerService, destinationFileUri);
|
||||
await this.openSketchFiles(destinationFileUri.toString());
|
||||
return destinationFileUri;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { injectable } from "inversify";
|
||||
import { injectable, inject } from "inversify";
|
||||
import { SketchesService, Sketch } from "../common/protocol/sketches-service";
|
||||
import URI from "@theia/core/lib/common/uri";
|
||||
import { FileStat } from "@theia/filesystem/lib/common";
|
||||
import { FileStat, FileSystem } from "@theia/filesystem/lib/common";
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
@ -10,10 +10,14 @@ export const ALLOWED_FILE_EXTENSIONS = [".c", ".cpp", ".h", ".hh", ".hpp", ".s",
|
||||
@injectable()
|
||||
export class SketchesServiceImpl implements SketchesService {
|
||||
|
||||
@inject(FileSystem)
|
||||
protected readonly filesystem: FileSystem;
|
||||
|
||||
async getSketches(fileStat?: FileStat): Promise<Sketch[]> {
|
||||
const sketches: Sketch[] = [];
|
||||
if (fileStat && fileStat.isDirectory) {
|
||||
const sketchFolderPath = this.getPath(fileStat);
|
||||
const uri = new URI(fileStat.uri);
|
||||
const sketchFolderPath = uri.path.toString()
|
||||
const files = fs.readdirSync(sketchFolderPath);
|
||||
files.forEach(file => {
|
||||
const filePath = path.join(sketchFolderPath, file);
|
||||
@ -32,18 +36,30 @@ export class SketchesServiceImpl implements SketchesService {
|
||||
* Return all allowed files.
|
||||
* File extensions: "c", "cpp", "h", "hh", "hpp", "s", "pde", "ino"
|
||||
*/
|
||||
async getSketchFiles(sketchDir: FileStat): Promise<string[]> {
|
||||
async getSketchFiles(sketchFileStat: FileStat): Promise<string[]> {
|
||||
const files: string[] = [];
|
||||
const sketchDirPath = this.getPath(sketchDir);
|
||||
const sketchDirContents = fs.readdirSync(sketchDirPath);
|
||||
sketchDirContents.forEach(fileName => {
|
||||
const filePath = path.join(sketchDirPath, fileName);
|
||||
if (fs.existsSync(filePath) &&
|
||||
fs.lstatSync(filePath).isFile() &&
|
||||
ALLOWED_FILE_EXTENSIONS.indexOf(path.extname(filePath)) !== -1) {
|
||||
const sketchUri = new URI(sketchFileStat.uri);
|
||||
const sketchPath = sketchUri.path.toString();
|
||||
if (sketchFileStat.isDirectory && this.isSketchFolder(sketchPath, sketchUri.displayName)) {
|
||||
const sketchDirContents = fs.readdirSync(sketchPath);
|
||||
sketchDirContents.forEach(fileName => {
|
||||
const filePath = path.join(sketchPath, fileName);
|
||||
if (fs.existsSync(filePath) &&
|
||||
fs.lstatSync(filePath).isFile() &&
|
||||
ALLOWED_FILE_EXTENSIONS.indexOf(path.extname(filePath)) !== -1) {
|
||||
files.push(filePath);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
const sketchDir = sketchUri.path.dir;
|
||||
if (this.isSketchFolder(sketchDir.toString(), sketchDir.name)) {
|
||||
const sketchFolderStat = await this.filesystem.getFileStat(sketchDir.toString());
|
||||
if (sketchFolderStat) {
|
||||
const sketchDirContents = await this.getSketchFiles(sketchFolderStat);
|
||||
files.push(...sketchDirContents);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
@ -58,10 +74,4 @@ export class SketchesServiceImpl implements SketchesService {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected getPath(fileStat: FileStat) {
|
||||
const fileStatUri = fileStat.uri;
|
||||
const uri = new URI(fileStatUri);
|
||||
return uri.path.toString();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user