11import { MenuModelRegistry } from '@theia/core' ;
2- import {
3- PreferenceScope ,
4- PreferenceService ,
5- } from '@theia/core/lib/browser/preferences/preference-service' ;
62import { CompositeTreeNode } from '@theia/core/lib/browser/tree' ;
73import { DisposableCollection } from '@theia/core/lib/common/disposable' ;
84import { nls } from '@theia/core/lib/common/nls' ;
95import { inject , injectable } from '@theia/core/shared/inversify' ;
106import { MainMenuManager } from '../../common/main-menu-manager' ;
117import type { AuthenticationSession } from '../../node/auth/types' ;
12- import { CloudSketchOpenStrategy } from '../arduino-preferences' ;
138import { AuthenticationClientService } from '../auth/authentication-client-service' ;
149import { CreateApi } from '../create/create-api' ;
1510import { CreateUri } from '../create/create-uri' ;
@@ -34,13 +29,10 @@ export class NewCloudSketch extends Contribution {
3429 private readonly authenticationService : AuthenticationClientService ;
3530 @inject ( MainMenuManager )
3631 private readonly mainMenuManager : MainMenuManager ;
37- @inject ( PreferenceService )
38- private readonly preferenceService : PreferenceService ;
3932
4033 private readonly toDispose = new DisposableCollection ( ) ;
4134 private _session : AuthenticationSession | undefined ;
4235 private _enabled : boolean ;
43- private _strategy : CloudSketchOpenStrategy ;
4436
4537 override onReady ( ) : void {
4638 this . toDispose . pushAll ( [
@@ -52,23 +44,15 @@ export class NewCloudSketch extends Contribution {
5244 }
5345 } ) ,
5446 this . preferences . onPreferenceChanged ( ( { preferenceName, newValue } ) => {
55- switch ( preferenceName ) {
56- case 'arduino.cloud.sketchOpenStrategy' : {
57- this . _strategy = newValue as CloudSketchOpenStrategy ;
58- break ;
59- }
60- case 'arduino.cloud.enabled' : {
61- const oldEnabled = this . _enabled ;
62- this . _enabled = Boolean ( newValue ) ;
63- if ( this . _enabled !== oldEnabled ) {
64- this . mainMenuManager . update ( ) ;
65- }
66- break ;
47+ if ( preferenceName === 'arduino.cloud.enabled' ) {
48+ const oldEnabled = this . _enabled ;
49+ this . _enabled = Boolean ( newValue ) ;
50+ if ( this . _enabled !== oldEnabled ) {
51+ this . mainMenuManager . update ( ) ;
6752 }
6853 }
6954 } ) ,
7055 ] ) ;
71- this . _strategy = this . preferences [ 'arduino.cloud.sketchOpenStrategy' ] ;
7256 this . _enabled = this . preferences [ 'arduino.cloud.enabled' ] ;
7357 this . _session = this . authenticationService . session ;
7458 if ( this . _session ) {
@@ -136,77 +120,48 @@ export class NewCloudSketch extends Contribution {
136120 }
137121
138122 if ( result ) {
139- return this . maybeOpen ( treeModel , result ) ;
123+ return this . open ( treeModel , result ) ;
140124 }
141125 return undefined ;
142126 }
143127
144- private async maybeOpen (
128+ private async open (
145129 treeModel : CloudSketchbookTreeModel ,
146130 newSketch : Create . Sketch
147131 ) : Promise < URI | undefined > {
148- if ( this . _strategy === 'Never' ) {
149- return undefined ;
132+ const id = CreateUri . toUri ( newSketch ) . path . toString ( ) ;
133+ const node = treeModel . getNode ( id ) ;
134+ if ( ! node ) {
135+ throw new Error (
136+ `Could not find remote sketchbook tree node with Tree node ID: ${ id } .`
137+ ) ;
150138 }
151- if ( this . _strategy === 'Ask' ) {
152- const yes = nls . localize ( 'vscode/extensionsUtils/yes' , 'Yes' ) ;
153- const never = CloudSketchOpenStrategy . labelOf ( 'Never' ) ;
154- const always = CloudSketchOpenStrategy . labelOf ( 'Always' ) ;
155- const answer = await this . messageService . info (
156- nls . localize (
157- 'arduino/newCloudSketch/openNewSketch' ,
158- "Do you want to pull the new remote sketch '{0}' and open it in a new window?" ,
159- newSketch . name
160- ) ,
161- never ,
162- yes ,
163- always
139+ if ( ! CloudSketchbookTree . CloudSketchDirNode . is ( node ) ) {
140+ throw new Error (
141+ `Remote sketchbook tree node expected to represent a directory but it did not. Tree node ID: ${ id } .`
164142 ) ;
165- if ( ! answer ) {
166- return undefined ;
167- }
168- if ( answer === never ) {
169- await this . preferenceService . set (
170- 'arduino.cloud.sketchOpenStrategy' ,
171- 'Never' ,
172- PreferenceScope . User
143+ }
144+ try {
145+ await treeModel . sketchbookTree ( ) . pull ( { node } ) ;
146+ } catch ( err ) {
147+ if ( isNotFound ( err ) ) {
148+ await treeModel . updateRoot ( ) ;
149+ await treeModel . refresh ( ) ;
150+ this . messageService . error (
151+ nls . localize (
152+ 'arduino/newCloudSketch/notFound' ,
153+ "Could not pull the remote sketch '{0}'. It does not exist." ,
154+ newSketch . name
155+ )
173156 ) ;
174157 return undefined ;
175- } else if ( answer === always ) {
176- await this . preferenceService . set (
177- 'arduino.cloud.sketchOpenStrategy' ,
178- 'Always' ,
179- PreferenceScope . User
180- ) ;
181158 }
159+ throw err ;
182160 }
183- const node = treeModel . getNode ( CreateUri . toUri ( newSketch ) . path . toString ( ) ) ;
184- if ( ! node ) {
185- return undefined ;
186- }
187- if ( CloudSketchbookTree . CloudSketchDirNode . is ( node ) ) {
188- try {
189- await treeModel . sketchbookTree ( ) . pull ( { node } ) ;
190- } catch ( err ) {
191- if ( isNotFound ( err ) ) {
192- await treeModel . updateRoot ( ) ;
193- await treeModel . refresh ( ) ;
194- this . messageService . error (
195- nls . localize (
196- 'arduino/newCloudSketch/notFound' ,
197- "Could not pull the remote sketch '{0}'. It does not exist." ,
198- newSketch . name
199- )
200- ) ;
201- return undefined ;
202- }
203- throw err ;
204- }
205- return this . commandService . executeCommand (
206- SketchbookCommands . OPEN_NEW_WINDOW . id ,
207- { node }
208- ) ;
209- }
161+ return this . commandService . executeCommand (
162+ SketchbookCommands . OPEN_NEW_WINDOW . id ,
163+ { node }
164+ ) ;
210165 }
211166
212167 private treeModelFrom (
0 commit comments