@@ -98,25 +98,27 @@ export async function selectToolchain() {
98
98
}
99
99
100
100
/** A {@link vscode.QuickPickItem} that contains the path to an installed Swift toolchain */
101
- type SwiftToolchainItem = PublicSwiftToolchainItem | XcodeToolchainItem ;
101
+ type SwiftToolchainItem = PublicSwiftToolchainItem | XcodeToolchainItem | SwiftlyToolchainItem ;
102
102
103
103
/** Common properties for a {@link vscode.QuickPickItem} that represents a Swift toolchain */
104
104
interface BaseSwiftToolchainItem extends vscode . QuickPickItem {
105
105
type : "toolchain" ;
106
- toolchainPath : string ;
107
- swiftFolderPath : string ;
108
106
onDidSelect ?( ) : Promise < void > ;
109
107
}
110
108
111
109
/** A {@link vscode.QuickPickItem} for a Swift toolchain that has been installed manually */
112
110
interface PublicSwiftToolchainItem extends BaseSwiftToolchainItem {
113
- category : "public" | "swiftly" ;
111
+ category : "public" ;
112
+ toolchainPath : string ;
113
+ swiftFolderPath : string ;
114
114
}
115
115
116
116
/** A {@link vscode.QuickPickItem} for a Swift toolchain provided by an installed Xcode application */
117
117
interface XcodeToolchainItem extends BaseSwiftToolchainItem {
118
118
category : "xcode" ;
119
119
xcodePath : string ;
120
+ toolchainPath : string ;
121
+ swiftFolderPath : string ;
120
122
}
121
123
122
124
/** A {@link vscode.QuickPickItem} that performs an action for the user */
@@ -125,6 +127,11 @@ interface ActionItem extends vscode.QuickPickItem {
125
127
run ( ) : Promise < void > ;
126
128
}
127
129
130
+ interface SwiftlyToolchainItem extends BaseSwiftToolchainItem {
131
+ category : "swiftly" ;
132
+ version : string ;
133
+ }
134
+
128
135
/** A {@link vscode.QuickPickItem} that separates items in the UI */
129
136
class SeparatorItem implements vscode . QuickPickItem {
130
137
readonly type = "separator" ;
@@ -146,7 +153,8 @@ type SelectToolchainItem = SwiftToolchainItem | ActionItem | SeparatorItem;
146
153
* @returns an array of {@link SelectToolchainItem}
147
154
*/
148
155
async function getQuickPickItems (
149
- activeToolchain : SwiftToolchain | undefined
156
+ activeToolchain : SwiftToolchain | undefined ,
157
+ cwd ?: vscode . Uri
150
158
) : Promise < SelectToolchainItem [ ] > {
151
159
// Find any Xcode installations on the system
152
160
const xcodes = ( await SwiftToolchain . findXcodeInstalls ( ) )
@@ -195,18 +203,43 @@ async function getQuickPickItems(
195
203
// Find any Swift toolchains installed via Swiftly
196
204
const swiftlyToolchains = ( await Swiftly . listAvailableToolchains ( ) )
197
205
. reverse ( )
198
- . map < SwiftToolchainItem > ( toolchainPath => ( {
206
+ . map < SwiftlyToolchainItem > ( toolchainPath => ( {
199
207
type : "toolchain" ,
200
- category : "swiftly" ,
201
208
label : path . basename ( toolchainPath ) ,
202
- detail : toolchainPath ,
203
- toolchainPath : path . join ( toolchainPath , "usr" ) ,
204
- swiftFolderPath : path . join ( toolchainPath , "usr" , "bin" ) ,
209
+ category : "swiftly" ,
210
+ version : path . basename ( toolchainPath ) ,
211
+ onDidSelect : async ( ) => {
212
+ try {
213
+ await Swiftly . use ( toolchainPath ) ;
214
+ void showReloadExtensionNotification (
215
+ "Changing the Swift path requires Visual Studio Code be reloaded."
216
+ ) ;
217
+ } catch ( error ) {
218
+ void vscode . window . showErrorMessage (
219
+ `Failed to switch Swiftly toolchain: ${ error } `
220
+ ) ;
221
+ }
222
+ } ,
205
223
} ) ) ;
206
224
// Mark which toolchain is being actively used
207
225
if ( activeToolchain ) {
226
+ const currentSwiftlyVersion = activeToolchain . isSwiftlyManaged
227
+ ? await Swiftly . inUseVersion ( "swiftly" , cwd )
228
+ : undefined ;
208
229
const toolchainInUse = [ ...xcodes , ...toolchains , ...swiftlyToolchains ] . find ( toolchain => {
209
- return toolchain . toolchainPath === activeToolchain . toolchainPath ;
230
+ if ( currentSwiftlyVersion ) {
231
+ if ( toolchain . category !== "swiftly" ) {
232
+ return false ;
233
+ }
234
+
235
+ // For Swiftly toolchains, check if the label matches the active toolchain version
236
+ return currentSwiftlyVersion === toolchain . label ;
237
+ }
238
+ // For non-Swiftly toolchains, check if the toolchain path matches
239
+ return (
240
+ ( toolchain as PublicSwiftToolchainItem | XcodeToolchainItem ) . toolchainPath ===
241
+ activeToolchain . toolchainPath
242
+ ) ;
210
243
} ) ;
211
244
if ( toolchainInUse ) {
212
245
toolchainInUse . description = "$(check) in use" ;
@@ -262,10 +295,13 @@ async function getQuickPickItems(
262
295
*
263
296
* @param activeToolchain the {@link WorkspaceContext}
264
297
*/
265
- export async function showToolchainSelectionQuickPick ( activeToolchain : SwiftToolchain | undefined ) {
298
+ export async function showToolchainSelectionQuickPick (
299
+ activeToolchain : SwiftToolchain | undefined ,
300
+ cwd ?: vscode . Uri
301
+ ) {
266
302
let xcodePaths : string [ ] = [ ] ;
267
303
const selected = await vscode . window . showQuickPick < SelectToolchainItem > (
268
- getQuickPickItems ( activeToolchain ) . then ( result => {
304
+ getQuickPickItems ( activeToolchain , cwd ) . then ( result => {
269
305
xcodePaths = result
270
306
. filter ( ( i ) : i is XcodeToolchainItem => "category" in i && i . category === "xcode" )
271
307
. map ( xcode => xcode . xcodePath ) ;
@@ -303,8 +339,23 @@ export async function showToolchainSelectionQuickPick(activeToolchain: SwiftTool
303
339
} ) ;
304
340
}
305
341
}
306
- // Update the toolchain path
307
- const isUpdated = await setToolchainPath ( selected . swiftFolderPath , developerDir ) ;
342
+ // Update the toolchain path`
343
+ let swiftPath : string | undefined ;
344
+
345
+ // Handle Swiftly toolchains specially
346
+ if ( selected . category === "swiftly" ) {
347
+ try {
348
+ swiftPath = undefined ;
349
+ } catch ( error ) {
350
+ void vscode . window . showErrorMessage ( `Failed to switch Swiftly toolchain: ${ error } ` ) ;
351
+ return ;
352
+ }
353
+ } else {
354
+ // For non-Swiftly toolchains, use the swiftFolderPath
355
+ swiftPath = selected . swiftFolderPath ;
356
+ }
357
+
358
+ const isUpdated = await setToolchainPath ( swiftPath , developerDir ) ;
308
359
if ( isUpdated && selected . onDidSelect ) {
309
360
await selected . onDidSelect ( ) ;
310
361
}
0 commit comments