From 576f96a5023610256c87262790f844326b45df4a Mon Sep 17 00:00:00 2001 From: jbicker Date: Wed, 22 Jan 2020 11:35:28 +0100 Subject: [PATCH] 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 --- ...debug-frontend-application-contribution.ts | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/arduino-debugger-extension/src/browser/arduino-debug-frontend-application-contribution.ts b/arduino-debugger-extension/src/browser/arduino-debug-frontend-application-contribution.ts index b4d61668..68737b00 100644 --- a/arduino-debugger-extension/src/browser/arduino-debug-frontend-application-contribution.ts +++ b/arduino-debugger-extension/src/browser/arduino-debug-frontend-application-contribution.ts @@ -1,11 +1,16 @@ 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 { TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar'; import { DebugFrontendApplicationContribution, DebugCommands } from '@theia/debug/lib/browser/debug-frontend-application-contribution'; import { DebugSessionOptions } from "@theia/debug/lib/browser/debug-session-options"; import { EditorMode } from "arduino-ide-extension/lib/browser/editor-mode"; 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() export class ArduinoDebugFrontendApplicationContribution extends DebugFrontendApplicationContribution { @@ -13,6 +18,21 @@ export class ArduinoDebugFrontendApplicationContribution extends DebugFrontendAp @inject(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 { const configurations = this.configurations as ArduinoDebugConfigurationManager; let current = debugSessionOptions ? debugSessionOptions : configurations.current; @@ -31,7 +51,28 @@ export class ArduinoDebugFrontendApplicationContribution extends DebugFrontendAp } }; } - await this.manager.start(current); + 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); + } else { + this.messageService.error('Please open a valid INO file.') + } + } else { + await this.manager.start(current); + } } }