Validate workspace/current editor and open file on debug start

Tests whether the current workspace is a sketchfolder and
opens the respective ino file
or whether  the current editor is a ino file.

Signed-off-by: jbicker <jan.bicker@typefox.io>
This commit is contained in:
jbicker 2020-01-22 11:35:28 +01:00 committed by Miro Spönemann
parent 2ba95947de
commit 576f96a502

View File

@ -1,11 +1,16 @@
import { injectable, inject } from 'inversify'; import { injectable, inject } from 'inversify';
import { MenuModelRegistry } from '@theia/core'; import { MenuModelRegistry, Path, MessageService } from '@theia/core';
import { KeybindingRegistry } from '@theia/core/lib/browser'; import { KeybindingRegistry } from '@theia/core/lib/browser';
import { TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar'; import { TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { DebugFrontendApplicationContribution, DebugCommands } from '@theia/debug/lib/browser/debug-frontend-application-contribution'; import { DebugFrontendApplicationContribution, DebugCommands } from '@theia/debug/lib/browser/debug-frontend-application-contribution';
import { DebugSessionOptions } from "@theia/debug/lib/browser/debug-session-options"; import { DebugSessionOptions } from "@theia/debug/lib/browser/debug-session-options";
import { EditorMode } from "arduino-ide-extension/lib/browser/editor-mode"; import { EditorMode } from "arduino-ide-extension/lib/browser/editor-mode";
import { ArduinoDebugConfigurationManager } from './arduino-debug-configuration-manager'; import { ArduinoDebugConfigurationManager } from './arduino-debug-configuration-manager';
import { ArduinoWorkspaceService } from 'arduino-ide-extension/lib/browser/arduino-workspace-service';
import { SketchesService } from 'arduino-ide-extension/lib/common/protocol/sketches-service';
import { FileSystem } from '@theia/filesystem/lib/common';
import URI from '@theia/core/lib/common/uri';
import { EditorManager } from '@theia/editor/lib/browser';
@injectable() @injectable()
export class ArduinoDebugFrontendApplicationContribution extends DebugFrontendApplicationContribution { export class ArduinoDebugFrontendApplicationContribution extends DebugFrontendApplicationContribution {
@ -13,6 +18,21 @@ export class ArduinoDebugFrontendApplicationContribution extends DebugFrontendAp
@inject(EditorMode) @inject(EditorMode)
protected readonly editorMode: EditorMode; protected readonly editorMode: EditorMode;
@inject(ArduinoWorkspaceService)
protected readonly workspaceService: ArduinoWorkspaceService;
@inject(SketchesService)
protected readonly sketchesService: SketchesService;
@inject(FileSystem)
protected readonly fileSystem: FileSystem;
@inject(EditorManager)
protected readonly editorManager: EditorManager;
@inject(MessageService)
protected readonly messageService: MessageService;
async start(noDebug?: boolean, debugSessionOptions?: DebugSessionOptions): Promise<void> { async start(noDebug?: boolean, debugSessionOptions?: DebugSessionOptions): Promise<void> {
const configurations = this.configurations as ArduinoDebugConfigurationManager; const configurations = this.configurations as ArduinoDebugConfigurationManager;
let current = debugSessionOptions ? debugSessionOptions : configurations.current; let current = debugSessionOptions ? debugSessionOptions : configurations.current;
@ -31,7 +51,28 @@ export class ArduinoDebugFrontendApplicationContribution extends DebugFrontendAp
} }
}; };
} }
if (current.configuration.type === 'arduino') {
const wsUri = await this.workspaceService.getDefaultWorkspaceUri();
let sketchFileURI: URI | undefined;
if (wsUri && await this.sketchesService.isSketchFolder(wsUri)) {
const wsPath = new Path(wsUri);
const sketchFilePath = wsPath.join(wsPath.name + '.ino').toString();
sketchFileURI = new URI(sketchFilePath);
} else if (this.editorManager.currentEditor) {
const editorURI = this.editorManager.currentEditor.getResourceUri();
if (editorURI && editorURI.path && editorURI.path.ext === '.ino') {
sketchFileURI = editorURI;
}
}
if (sketchFileURI) {
await this.editorManager.open(sketchFileURI);
await this.manager.start(current); await this.manager.start(current);
} else {
this.messageService.error('Please open a valid INO file.')
}
} else {
await this.manager.start(current);
}
} }
} }