@@ -98,25 +98,27 @@ export async function selectToolchain() {
9898}
9999
100100/** A {@link vscode.QuickPickItem} that contains the path to an installed Swift toolchain */
101- type SwiftToolchainItem = PublicSwiftToolchainItem | XcodeToolchainItem ;
101+ type SwiftToolchainItem = PublicSwiftToolchainItem | XcodeToolchainItem | SwiftlyToolchainItem ;
102102
103103/** Common properties for a {@link vscode.QuickPickItem} that represents a Swift toolchain */
104104interface BaseSwiftToolchainItem extends vscode . QuickPickItem {
105105 type : "toolchain" ;
106- toolchainPath : string ;
107- swiftFolderPath : string ;
108106 onDidSelect ?( ) : Promise < void > ;
109107}
110108
111109/** A {@link vscode.QuickPickItem} for a Swift toolchain that has been installed manually */
112110interface PublicSwiftToolchainItem extends BaseSwiftToolchainItem {
113- category : "public" | "swiftly" ;
111+ category : "public" ;
112+ toolchainPath : string ;
113+ swiftFolderPath : string ;
114114}
115115
116116/** A {@link vscode.QuickPickItem} for a Swift toolchain provided by an installed Xcode application */
117117interface XcodeToolchainItem extends BaseSwiftToolchainItem {
118118 category : "xcode" ;
119119 xcodePath : string ;
120+ toolchainPath : string ;
121+ swiftFolderPath : string ;
120122}
121123
122124/** A {@link vscode.QuickPickItem} that performs an action for the user */
@@ -125,6 +127,11 @@ interface ActionItem extends vscode.QuickPickItem {
125127 run ( ) : Promise < void > ;
126128}
127129
130+ interface SwiftlyToolchainItem extends BaseSwiftToolchainItem {
131+ category : "swiftly" ;
132+ version : string ;
133+ }
134+
128135/** A {@link vscode.QuickPickItem} that separates items in the UI */
129136class SeparatorItem implements vscode . QuickPickItem {
130137 readonly type = "separator" ;
@@ -146,7 +153,8 @@ type SelectToolchainItem = SwiftToolchainItem | ActionItem | SeparatorItem;
146153 * @returns an array of {@link SelectToolchainItem}
147154 */
148155async function getQuickPickItems (
149- activeToolchain : SwiftToolchain | undefined
156+ activeToolchain : SwiftToolchain | undefined ,
157+ cwd ?: vscode . Uri
150158) : Promise < SelectToolchainItem [ ] > {
151159 // Find any Xcode installations on the system
152160 const xcodes = ( await SwiftToolchain . findXcodeInstalls ( ) )
@@ -195,18 +203,43 @@ async function getQuickPickItems(
195203 // Find any Swift toolchains installed via Swiftly
196204 const swiftlyToolchains = ( await Swiftly . listAvailableToolchains ( ) )
197205 . reverse ( )
198- . map < SwiftToolchainItem > ( toolchainPath => ( {
206+ . map < SwiftlyToolchainItem > ( toolchainPath => ( {
199207 type : "toolchain" ,
200- category : "swiftly" ,
201208 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+ } ,
205223 } ) ) ;
206224 // Mark which toolchain is being actively used
207225 if ( activeToolchain ) {
226+ const currentSwiftlyVersion = activeToolchain . isSwiftlyManaged
227+ ? await Swiftly . inUseVersion ( "swiftly" , cwd )
228+ : undefined ;
208229 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+ ) ;
210243 } ) ;
211244 if ( toolchainInUse ) {
212245 toolchainInUse . description = "$(check) in use" ;
@@ -262,10 +295,13 @@ async function getQuickPickItems(
262295 *
263296 * @param activeToolchain the {@link WorkspaceContext}
264297 */
265- export async function showToolchainSelectionQuickPick ( activeToolchain : SwiftToolchain | undefined ) {
298+ export async function showToolchainSelectionQuickPick (
299+ activeToolchain : SwiftToolchain | undefined ,
300+ cwd ?: vscode . Uri
301+ ) {
266302 let xcodePaths : string [ ] = [ ] ;
267303 const selected = await vscode . window . showQuickPick < SelectToolchainItem > (
268- getQuickPickItems ( activeToolchain ) . then ( result => {
304+ getQuickPickItems ( activeToolchain , cwd ) . then ( result => {
269305 xcodePaths = result
270306 . filter ( ( i ) : i is XcodeToolchainItem => "category" in i && i . category === "xcode" )
271307 . map ( xcode => xcode . xcodePath ) ;
@@ -303,8 +339,23 @@ export async function showToolchainSelectionQuickPick(activeToolchain: SwiftTool
303339 } ) ;
304340 }
305341 }
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 ) ;
308359 if ( isUpdated && selected . onDidSelect ) {
309360 await selected . onDidSelect ( ) ;
310361 }
0 commit comments