1
1
import { MenuModelRegistry } from '@theia/core' ;
2
- import {
3
- PreferenceScope ,
4
- PreferenceService ,
5
- } from '@theia/core/lib/browser/preferences/preference-service' ;
6
2
import { CompositeTreeNode } from '@theia/core/lib/browser/tree' ;
7
3
import { DisposableCollection } from '@theia/core/lib/common/disposable' ;
8
4
import { nls } from '@theia/core/lib/common/nls' ;
9
5
import { inject , injectable } from '@theia/core/shared/inversify' ;
10
6
import { MainMenuManager } from '../../common/main-menu-manager' ;
11
7
import type { AuthenticationSession } from '../../node/auth/types' ;
12
- import { CloudSketchOpenStrategy } from '../arduino-preferences' ;
13
8
import { AuthenticationClientService } from '../auth/authentication-client-service' ;
14
9
import { CreateApi } from '../create/create-api' ;
15
10
import { CreateUri } from '../create/create-uri' ;
@@ -34,13 +29,10 @@ export class NewCloudSketch extends Contribution {
34
29
private readonly authenticationService : AuthenticationClientService ;
35
30
@inject ( MainMenuManager )
36
31
private readonly mainMenuManager : MainMenuManager ;
37
- @inject ( PreferenceService )
38
- private readonly preferenceService : PreferenceService ;
39
32
40
33
private readonly toDispose = new DisposableCollection ( ) ;
41
34
private _session : AuthenticationSession | undefined ;
42
35
private _enabled : boolean ;
43
- private _strategy : CloudSketchOpenStrategy ;
44
36
45
37
override onReady ( ) : void {
46
38
this . toDispose . pushAll ( [
@@ -52,23 +44,15 @@ export class NewCloudSketch extends Contribution {
52
44
}
53
45
} ) ,
54
46
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 ( ) ;
67
52
}
68
53
}
69
54
} ) ,
70
55
] ) ;
71
- this . _strategy = this . preferences [ 'arduino.cloud.sketchOpenStrategy' ] ;
72
56
this . _enabled = this . preferences [ 'arduino.cloud.enabled' ] ;
73
57
this . _session = this . authenticationService . session ;
74
58
if ( this . _session ) {
@@ -136,77 +120,48 @@ export class NewCloudSketch extends Contribution {
136
120
}
137
121
138
122
if ( result ) {
139
- return this . maybeOpen ( treeModel , result ) ;
123
+ return this . open ( treeModel , result ) ;
140
124
}
141
125
return undefined ;
142
126
}
143
127
144
- private async maybeOpen (
128
+ private async open (
145
129
treeModel : CloudSketchbookTreeModel ,
146
130
newSketch : Create . Sketch
147
131
) : 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
+ ) ;
150
138
}
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 } .`
164
142
) ;
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
+ )
173
156
) ;
174
157
return undefined ;
175
- } else if ( answer === always ) {
176
- await this . preferenceService . set (
177
- 'arduino.cloud.sketchOpenStrategy' ,
178
- 'Always' ,
179
- PreferenceScope . User
180
- ) ;
181
158
}
159
+ throw err ;
182
160
}
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
+ ) ;
210
165
}
211
166
212
167
private treeModelFrom (
0 commit comments