@@ -30,6 +30,7 @@ import { debugLaunchConfig, getLaunchConfiguration } from "./debugger/launch";
30
30
import { execFile } from "./utilities/utilities" ;
31
31
import { SwiftExecOperation , TaskOperation } from "./tasks/TaskQueue" ;
32
32
import { SwiftProjectTemplate } from "./toolchain/toolchain" ;
33
+ import { showToolchainSelectionQuickPick , showToolchainError } from "./ui/ToolchainSelection" ;
33
34
34
35
/**
35
36
* References:
@@ -40,6 +41,8 @@ import { SwiftProjectTemplate } from "./toolchain/toolchain";
40
41
* https://code.visualstudio.com/api/extension-guides/command
41
42
*/
42
43
44
+ export type WorkspaceContextWithToolchain = WorkspaceContext & { toolchain : SwiftToolchain } ;
45
+
43
46
/**
44
47
* Executes a {@link vscode.Task task} to resolve this package's dependencies.
45
48
*/
@@ -97,19 +100,27 @@ export async function updateDependencies(ctx: WorkspaceContext) {
97
100
* Prompts the user to input project details and then executes `swift package init`
98
101
* to create the project.
99
102
*/
100
- export async function createNewProject ( ctx : WorkspaceContext ) : Promise < void > {
103
+ export async function createNewProject ( toolchain : SwiftToolchain | undefined ) : Promise < void > {
104
+ // It is possible for this command to be run without a valid toolchain because it can be
105
+ // run before the Swift extension is activated. Show the toolchain error notification in
106
+ // this case.
107
+ if ( ! toolchain ) {
108
+ showToolchainError ( ) ;
109
+ return ;
110
+ }
111
+
101
112
// The context key `swift.createNewProjectAvailable` only works if the extension has been
102
113
// activated. As such, we also have to allow this command to run when no workspace is
103
114
// active. Show an error to the user if the command is unavailable.
104
- if ( ! ctx . toolchain . swiftVersion . isGreaterThanOrEqual ( new Version ( 5 , 8 , 0 ) ) ) {
115
+ if ( ! toolchain . swiftVersion . isGreaterThanOrEqual ( new Version ( 5 , 8 , 0 ) ) ) {
105
116
vscode . window . showErrorMessage (
106
117
"Creating a new swift project is only available starting in swift version 5.8.0."
107
118
) ;
108
119
return ;
109
120
}
110
121
111
122
// Prompt the user for the type of project they would like to create
112
- const availableProjectTemplates = await ctx . toolchain . getProjectTemplates ( ) ;
123
+ const availableProjectTemplates = await toolchain . getProjectTemplates ( ) ;
113
124
const selectedProjectTemplate = await vscode . window . showQuickPick <
114
125
vscode . QuickPickItem & { type : SwiftProjectTemplate }
115
126
> (
@@ -186,7 +197,7 @@ export async function createNewProject(ctx: WorkspaceContext): Promise<void> {
186
197
async ( ) => {
187
198
await execSwift (
188
199
[ "package" , "init" , "--type" , projectType , "--name" , projectName ] ,
189
- ctx . toolchain ,
200
+ toolchain ,
190
201
{
191
202
cwd : projectUri . fsPath ,
192
203
}
@@ -630,7 +641,7 @@ async function openPackage(workspaceContext: WorkspaceContext) {
630
641
}
631
642
}
632
643
633
- function insertFunctionComment ( workspaceContext : WorkspaceContext ) {
644
+ async function insertFunctionComment ( workspaceContext : WorkspaceContext ) : Promise < void > {
634
645
const activeEditor = vscode . window . activeTextEditor ;
635
646
if ( ! activeEditor ) {
636
647
return ;
@@ -640,8 +651,8 @@ function insertFunctionComment(workspaceContext: WorkspaceContext) {
640
651
}
641
652
642
653
/** Restart the SourceKit-LSP server */
643
- function restartLSPServer ( workspaceContext : WorkspaceContext ) {
644
- workspaceContext . languageClientManager . restart ( ) ;
654
+ function restartLSPServer ( workspaceContext : WorkspaceContext ) : Promise < void > {
655
+ return workspaceContext . languageClientManager . restart ( ) ;
645
656
}
646
657
647
658
/** Execute task and show UI while running */
@@ -726,61 +737,9 @@ async function switchPlatform() {
726
737
) ;
727
738
}
728
739
729
- /**
730
- * Choose DEVELOPER_DIR
731
- * @param workspaceContext
732
- */
733
- async function selectXcodeDeveloperDir ( ) {
734
- const defaultXcode = await SwiftToolchain . getXcodeDeveloperDir ( ) ;
735
- const selectedXcode = configuration . swiftEnvironmentVariables . DEVELOPER_DIR ;
736
- const xcodes = await SwiftToolchain . getXcodeInstalls ( ) ;
737
- await withQuickPick (
738
- selectedXcode ?? defaultXcode ,
739
- xcodes . map ( xcode => {
740
- const developerDir = `${ xcode } /Contents/Developer` ;
741
- return {
742
- label : developerDir === defaultXcode ? `${ xcode } (default)` : xcode ,
743
- folder : developerDir === defaultXcode ? undefined : developerDir ,
744
- } ;
745
- } ) ,
746
- async selected => {
747
- let swiftEnv = configuration . swiftEnvironmentVariables ;
748
- const previousDeveloperDir = swiftEnv . DEVELOPER_DIR ?? defaultXcode ;
749
- if ( selected . folder ) {
750
- swiftEnv . DEVELOPER_DIR = selected . folder ;
751
- } else if ( swiftEnv . DEVELOPER_DIR ) {
752
- // if DEVELOPER_DIR was set and the new folder is the default then
753
- // delete variable
754
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
755
- const { DEVELOPER_DIR , ...rest } = swiftEnv ;
756
- swiftEnv = rest ;
757
- }
758
- configuration . swiftEnvironmentVariables = swiftEnv ;
759
- // if SDK is inside previous DEVELOPER_DIR then move to new DEVELOPER_DIR
760
- if (
761
- configuration . sdk . length > 0 &&
762
- configuration . sdk . startsWith ( previousDeveloperDir )
763
- ) {
764
- configuration . sdk = configuration . sdk . replace (
765
- previousDeveloperDir ,
766
- selected . folder ?? defaultXcode
767
- ) ;
768
- }
769
- vscode . window
770
- . showInformationMessage (
771
- "Changing the Xcode Developer Directory requires the project be reloaded." ,
772
- "Ok"
773
- )
774
- . then ( ( ) => {
775
- vscode . commands . executeCommand ( "workbench.action.reloadWindow" ) ;
776
- } ) ;
777
- }
778
- ) ;
779
- }
780
-
781
- async function attachDebugger ( workspaceContext : WorkspaceContext ) {
740
+ async function attachDebugger ( ctx : WorkspaceContext ) {
782
741
// use LLDB to get list of processes
783
- const lldb = workspaceContext . toolchain . getLLDB ( ) ;
742
+ const lldb = await ctx . toolchain . getLLDB ( ) ;
784
743
try {
785
744
const { stdout } = await execFile ( lldb , [
786
745
"--batch" ,
@@ -823,12 +782,24 @@ function updateAfterError(result: boolean, folderContext: FolderContext) {
823
782
}
824
783
}
825
784
785
+ export function registerToolchainCommands (
786
+ toolchain : SwiftToolchain | undefined
787
+ ) : vscode . Disposable [ ] {
788
+ return [
789
+ vscode . commands . registerCommand ( "swift.createNewProject" , ( ) =>
790
+ createNewProject ( toolchain )
791
+ ) ,
792
+ vscode . commands . registerCommand ( "swift.selectToolchain" , ( ) =>
793
+ showToolchainSelectionQuickPick ( toolchain )
794
+ ) ,
795
+ ] ;
796
+ }
797
+
826
798
/**
827
799
* Registers this extension's commands in the given {@link vscode.ExtensionContext context}.
828
800
*/
829
- export function register ( ctx : WorkspaceContext ) {
830
- ctx . subscriptions . push (
831
- vscode . commands . registerCommand ( "swift.createNewProject" , ( ) => createNewProject ( ctx ) ) ,
801
+ export function register ( ctx : WorkspaceContext ) : vscode . Disposable [ ] {
802
+ return [
832
803
vscode . commands . registerCommand ( "swift.resolveDependencies" , ( ) =>
833
804
resolveDependencies ( ctx )
834
805
) ,
@@ -873,9 +844,6 @@ export function register(ctx: WorkspaceContext) {
873
844
openInExternalEditor ( item ) ;
874
845
}
875
846
} ) ,
876
- vscode . commands . registerCommand ( "swift.selectXcodeDeveloperDir" , ( ) =>
877
- selectXcodeDeveloperDir ( )
878
- ) ,
879
- vscode . commands . registerCommand ( "swift.attachDebugger" , ( ) => attachDebugger ( ctx ) )
880
- ) ;
847
+ vscode . commands . registerCommand ( "swift.attachDebugger" , ( ) => attachDebugger ( ctx ) ) ,
848
+ ] ;
881
849
}
0 commit comments