From b0115eabee6d7ea7d3257b8fada6c5c5991cda91 Mon Sep 17 00:00:00 2001 From: Akos Kitta Date: Fri, 6 Jan 2023 14:39:49 +0100 Subject: [PATCH] fix: aligned `Add File...` behavior with IDE 1.x - code files will be copied to sketch folder root - other files go under the `data` folder in the sketch folder root Closes #284 Signed-off-by: Akos Kitta --- .../src/browser/contributions/add-file.ts | 21 ++++++++++++++++--- .../src/common/protocol/sketches-service.ts | 15 +++---------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/arduino-ide-extension/src/browser/contributions/add-file.ts b/arduino-ide-extension/src/browser/contributions/add-file.ts index a5d616b0c..5605ba7d5 100644 --- a/arduino-ide-extension/src/browser/contributions/add-file.ts +++ b/arduino-ide-extension/src/browser/contributions/add-file.ts @@ -7,6 +7,7 @@ import { CommandRegistry, MenuModelRegistry, URI, + Sketch, } from './contribution'; import { FileDialogService } from '@theia/filesystem/lib/browser'; import { nls } from '@theia/core/lib/common'; @@ -46,9 +47,7 @@ export class AddFile extends SketchContribution { if (!toAddUri) { return; } - const sketchUri = new URI(sketch.uri); - const filename = toAddUri.path.base; - const targetUri = sketchUri.resolve('data').resolve(filename); + const { uri: targetUri, filename } = this.resolveTarget(sketch, toAddUri); const exists = await this.fileService.exists(targetUri); if (exists) { const { response } = await remote.dialog.showMessageBox({ @@ -80,6 +79,22 @@ export class AddFile extends SketchContribution { } ); } + + // https://github.com/arduino/arduino-ide/issues/284#issuecomment-1364533662 + // File the file to add has one of the following extension, it goes to the sketch folder root: .ino, .h, .cpp, .c, .S + // Otherwise, the files goes to the `data` folder inside the sketch folder root. + private resolveTarget( + sketch: Sketch, + toAddUri: URI + ): { uri: URI; filename: string } { + const path = toAddUri.path; + const filename = path.base; + let root = new URI(sketch.uri); + if (!Sketch.Extensions.CODE_FILES.includes(path.ext)) { + root = root.resolve('data'); + } + return { uri: root.resolve(filename), filename: filename }; + } } export namespace AddFile { diff --git a/arduino-ide-extension/src/common/protocol/sketches-service.ts b/arduino-ide-extension/src/common/protocol/sketches-service.ts index ed93d9fb7..6a3fd370e 100644 --- a/arduino-ide-extension/src/common/protocol/sketches-service.ts +++ b/arduino-ide-extension/src/common/protocol/sketches-service.ts @@ -162,18 +162,9 @@ export namespace Sketch { } export namespace Extensions { export const MAIN = ['.ino', '.pde']; - export const SOURCE = ['.c', '.cpp', '.s']; - export const ADDITIONAL = [ - '.h', - '.c', - '.hpp', - '.hh', - '.cpp', - '.S', - '.json', - '.md', - '.adoc', - ]; + export const SOURCE = ['.c', '.cpp', '.S']; + export const CODE_FILES = [...MAIN, ...SOURCE, '.h', '.hh', '.hpp']; + export const ADDITIONAL = [...CODE_FILES, '.json', '.md', '.adoc']; export const ALL = Array.from(new Set([...MAIN, ...SOURCE, ...ADDITIONAL])); } export function isInSketch(uri: string | URI, sketch: Sketch): boolean {