@@ -3,7 +3,21 @@ import { ExtensionContext, commands, Uri, window, workspace } from 'vscode';
33import { state } from './extension' ;
44
55export function setupCommands ( context : ExtensionContext ) {
6+ const config = workspace . getConfiguration ( 'processing' ) ;
7+
68 const runSketch = commands . registerCommand ( 'processing.sketch.run' , ( resource : Uri ) => {
9+ const autosave = config . get < boolean > ( 'autosave' ) ;
10+ if ( autosave === true ) {
11+ // Save all files before running the sketch
12+ commands . executeCommand ( 'workbench.action.files.saveAll' ) ;
13+ }
14+ if ( resource == undefined ) {
15+ const editor = window . activeTextEditor ;
16+ if ( editor ) {
17+ resource = editor . document . uri ;
18+ }
19+ }
20+
721 // TODO: If the command is run from a keyboard shortcut, find the current file
822 if ( ! resource ) {
923 return ;
@@ -53,29 +67,16 @@ export function setupCommands(context: ExtensionContext) {
5367 return ;
5468 }
5569
56- workspace . updateWorkspaceFolders (
57- workspace . workspaceFolders ? workspace . workspaceFolders . length : 0 ,
58- null ,
59- { uri : Uri . file ( folder ) }
60- ) ;
61- await commands . executeCommand ( 'workbench.view.explorer' ) ;
62- const folderName = folder . split ( '/' ) . pop ( ) ;
63- const sketchFile = Uri . file ( `${ folder } /${ folderName } .pde` ) ;
64- try {
65- if ( ! workspace . fs . stat ( sketchFile ) ) {
66- return ;
67- }
6870
69- const doc = await workspace . openTextDocument ( sketchFile ) ;
70- if ( ! doc ) {
71- window . showErrorMessage ( `Could not open sketch file: ${ sketchFile . fsPath } ` ) ;
72- return ;
73- }
74- await window . showTextDocument ( doc , { preview : false } ) ;
75- window . activeTextEditor ?. revealRange ( doc . lineAt ( 0 ) . range ) ;
76- } catch ( error ) {
77- window . showErrorMessage ( `Could not open sketch file: ${ sketchFile . fsPath } ` ) ;
78- console . error ( error ) ;
71+ const newWindow = config . get < boolean > ( 'newWindow' ) ;
72+ if ( newWindow === true ) {
73+ await commands . executeCommand ( 'vscode.openFolder' , Uri . file ( folder ) , true ) ;
74+ } else {
75+ workspace . updateWorkspaceFolders (
76+ workspace . workspaceFolders ? workspace . workspaceFolders . length : 0 ,
77+ null ,
78+ { uri : Uri . file ( folder ) }
79+ ) ;
7980 }
8081 } ) ;
8182
@@ -85,7 +86,9 @@ export function setupCommands(context: ExtensionContext) {
8586 canSelectFolders : true ,
8687 canSelectMany : false ,
8788 title : "Select a folder to create a new sketch in" ,
88- defaultUri : workspace . workspaceFolders ? workspace . workspaceFolders [ 0 ] . uri : undefined ,
89+ defaultUri : workspace . workspaceFolders && workspace . workspaceFolders [ 0 ]
90+ ? Uri . joinPath ( workspace . workspaceFolders [ 0 ] . uri , '..' )
91+ : undefined ,
8992 } ) ;
9093 if ( ! folder || folder . length === 0 ) {
9194 return ;
0 commit comments