1
- import { CompositeTreeNode } from '@theia/core/lib/browser/tree ' ;
1
+ import { CompositeTreeNode } from '@theia/core/lib/browser' ;
2
2
import { Progress } from '@theia/core/lib/common/message-service-protocol' ;
3
3
import { nls } from '@theia/core/lib/common/nls' ;
4
4
import { injectable } from '@theia/core/shared/inversify' ;
@@ -12,10 +12,12 @@ import {
12
12
import { WorkspaceInputDialogWithProgress } from '../theia/workspace/workspace-input-dialog' ;
13
13
import { CloudSketchbookTree } from '../widgets/cloud-sketchbook/cloud-sketchbook-tree' ;
14
14
import { CloudSketchbookTreeModel } from '../widgets/cloud-sketchbook/cloud-sketchbook-tree-model' ;
15
- import { SketchbookTree } from '../widgets/sketchbook/sketchbook-tree' ;
16
15
import { Command , CommandRegistry , Sketch , URI } from './contribution' ;
17
- import { CloudSketchContribution } from './create-contribution' ;
18
- import { DeleteSketch , DeleteSketchParams } from './delete-sketch' ;
16
+ import {
17
+ CloudSketchContribution ,
18
+ pushingSketch ,
19
+ sketchAlreadyExists ,
20
+ } from './create-contribution' ;
19
21
20
22
export interface RenameCloudSketchParams {
21
23
readonly cloudUri : URI ;
@@ -33,109 +35,121 @@ export class RenameCloudSketch extends CloudSketchContribution {
33
35
private async renameSketch (
34
36
params : RenameCloudSketchParams ,
35
37
initValue : string = params . sketch . name
36
- ) : Promise < URI | undefined > {
38
+ ) : Promise < string | undefined > {
37
39
const treeModel = await this . treeModel ( ) ;
38
40
if ( treeModel ) {
39
41
const posixPath = params . cloudUri . path . toString ( ) ;
40
42
const node = treeModel . getNode ( posixPath ) ;
41
- if ( SketchbookTree . SketchDirNode . is ( node ) ) {
42
- const result = await this . openWizard (
43
- params ,
44
- node ,
45
- treeModel . root ,
46
- treeModel ,
47
- initValue
48
- ) ;
49
- if ( result ) {
50
- console . log ( result ) ;
51
- }
43
+ const parentNode = node ?. parent ;
44
+ if (
45
+ CloudSketchbookTree . CloudSketchDirNode . is ( node ) &&
46
+ CompositeTreeNode . is ( parentNode )
47
+ ) {
48
+ return this . openWizard ( params , node , parentNode , treeModel , initValue ) ;
52
49
}
53
50
}
54
51
return undefined ;
55
52
}
56
53
57
54
private async openWizard (
58
55
params : RenameCloudSketchParams ,
59
- node : SketchbookTree . SketchDirNode ,
60
- rootNode : CompositeTreeNode ,
56
+ node : CloudSketchbookTree . CloudSketchDirNode ,
57
+ parentNode : CompositeTreeNode ,
61
58
treeModel : CloudSketchbookTreeModel ,
62
59
initialValue ?: string | undefined
63
60
) : Promise < string | undefined > {
64
- const existingNames = rootNode . children
61
+ const parentUri = CloudSketchbookTree . CloudSketchDirNode . is ( parentNode )
62
+ ? parentNode . uri
63
+ : CreateUri . root ;
64
+ const existingNames = parentNode . children
65
65
. filter ( CloudSketchbookTree . CloudSketchDirNode . is )
66
66
. map ( ( { fileStat } ) => fileStat . name ) ;
67
- return new WorkspaceInputDialogWithProgress (
67
+ const value = await new WorkspaceInputDialogWithProgress (
68
68
{
69
69
title : nls . localize (
70
70
'arduino/renameCloudSketch/renameSketchTitle' ,
71
- 'New name of the Remote Sketch'
71
+ 'New name of the Cloud Sketch'
72
72
) ,
73
- parentUri : CreateUri . root ,
73
+ parentUri,
74
74
initialValue,
75
75
validate : ( input ) => {
76
76
if ( existingNames . includes ( input ) ) {
77
- return nls . localize (
78
- 'arduino/newCloudSketch/sketchAlreadyExists' ,
79
- "Remote sketch '{0}' already exists." ,
80
- input
81
- ) ;
77
+ return sketchAlreadyExists ( input ) ;
82
78
}
83
79
return Sketch . validateCloudSketchFolderName ( input ) ?? '' ;
84
80
} ,
85
81
} ,
86
82
this . labelProvider ,
87
- ( value ) => this . renameSketchWithProgress ( params , value , treeModel )
83
+ ( value ) => {
84
+ return this . renameSketchWithProgress ( params , node , treeModel , value ) ;
85
+ }
88
86
) . open ( ) ;
87
+ // The input dialog resolves with the <input> value not the URI.
88
+ // IDE2 must remap the value string to the target URI string in the local cache.
89
+ if ( value ) {
90
+ return new URI ( params . sketch . uri ) . parent . resolve ( value ) . toString ( ) ;
91
+ }
92
+ return undefined ;
89
93
}
90
94
91
95
private renameSketchWithProgress (
92
96
params : RenameCloudSketchParams ,
93
- value : string ,
94
- treeModel : CloudSketchbookTreeModel
97
+ node : CloudSketchbookTree . CloudSketchDirNode ,
98
+ treeModel : CloudSketchbookTreeModel ,
99
+ value : string
95
100
) : ( progress : Progress ) => Promise < unknown > {
96
101
return async ( progress : Progress ) => {
97
- let result : Create . Sketch | undefined | ConflictError ;
102
+ const fromPosixPath = params . cloudUri . path . toString ( ) ;
103
+ const toPosixPath = params . cloudUri . parent . resolve ( value ) . path . toString ( ) ;
104
+ let result : Create . Sketch | ConflictError | undefined ;
98
105
try {
106
+ // push
107
+ progress . report ( { message : pushingSketch ( params . sketch . name ) } ) ;
108
+ await treeModel . sketchbookTree ( ) . push ( node ) ;
109
+
110
+ // rename
99
111
progress . report ( {
100
112
message : nls . localize (
101
113
'arduino/cloudSketch/renaming' ,
102
- "Renaming remote sketch '{0}'..." ,
114
+ "Renaming cloud sketch '{0}'..." ,
103
115
value
104
116
) ,
105
117
} ) ;
106
- result = await this . createApi . createSketch ( value ) ;
118
+ await this . createApi . rename ( fromPosixPath , toPosixPath ) ;
119
+
120
+ // sync
121
+ progress . report ( {
122
+ message : nls . localize (
123
+ 'arduino/cloudSketch/renaming' ,
124
+ "Renaming cloud sketch '{0}'..." ,
125
+ value
126
+ ) ,
127
+ } ) ;
128
+ this . createApi . sketchCache . init ( ) ; // invalidate the cache
129
+ await this . createApi . sketches ( ) ; // IDE2 must pull all sketches to find the new one
130
+ const newSketch = this . createApi . sketchCache . getSketch ( toPosixPath ) ;
131
+ if ( ! newSketch ) {
132
+ return undefined ;
133
+ }
134
+
135
+ // pull
136
+ await treeModel . refresh ( ) ;
137
+ const pulledNode = await this . pull ( newSketch ) ;
138
+ if ( pulledNode ) {
139
+ result = newSketch ;
140
+ }
107
141
} catch ( err ) {
108
142
if ( isConflict ( err ) ) {
109
143
result = err ;
110
144
} else {
111
145
throw err ;
112
146
}
113
- } finally {
114
- if ( result ) {
115
- progress . report ( {
116
- message : nls . localize (
117
- 'arduino/cloudSketch/synchronizing' ,
118
- "Synchronizing sketchbook, pulling '{0}'..." ,
119
- value
120
- ) ,
121
- } ) ;
122
- await treeModel . refresh ( ) ;
123
- }
124
147
}
125
148
if ( result instanceof CreateError ) {
126
149
return this . renameSketch ( params , value ) ;
127
150
}
128
151
if ( result ) {
129
- setTimeout ( ( ) => {
130
- this . commandService . executeCommand (
131
- DeleteSketch . Commands . DELETE_SKETCH . id ,
132
- < DeleteSketchParams > {
133
- toDelete : params . sketch ,
134
- willNavigateAway : 'force' ,
135
- }
136
- ) ;
137
- } , 0 ) ;
138
- return this . openInNewWindow ( result ) ;
152
+ return CreateUri . toUri ( result ) . toString ( ) ;
139
153
}
140
154
return undefined ;
141
155
} ;
0 commit comments