@@ -18,6 +18,7 @@ import * as fs from "fs/promises";
18
18
import { createSwiftTask } from "../tasks/SwiftTaskProvider" ;
19
19
import { WorkspaceContext } from "../WorkspaceContext" ;
20
20
import { Version } from "../utilities/version" ;
21
+ import configuration from "../configuration" ;
21
22
22
23
/**
23
24
* Run the active document through the Swift REPL
@@ -40,50 +41,55 @@ export async function runSwiftScript(ctx: WorkspaceContext) {
40
41
return ;
41
42
}
42
43
43
- const picked = await vscode . window . showQuickPick (
44
- [
45
- // Potentially add more versions here
46
- { value : 5 , label : "Swift 5" } ,
47
- { value : 6 , label : "Swift 6" } ,
48
- ] ,
49
- {
50
- placeHolder : "Select a target Swift version" ,
51
- }
52
- ) ;
44
+ let target : number ;
53
45
54
- if ( ! picked ) {
55
- return ;
56
- }
57
-
58
- if ( picked ) {
59
- const target = picked . value ;
60
- let filename = document . fileName ;
61
- let isTempFile = false ;
62
- if ( document . isUntitled ) {
63
- // if document hasn't been saved, save it to a temporary file
64
- isTempFile = true ;
65
- filename = ctx . tempFolder . filename ( document . fileName , "swift" ) ;
66
- const text = document . getText ( ) ;
67
- await fs . writeFile ( filename , text ) ;
68
- } else {
69
- // otherwise save document
70
- await document . save ( ) ;
71
- }
72
- const runTask = createSwiftTask (
73
- [ "-swift-version" , target . toString ( ) , filename ] ,
74
- `Run ${ filename } ` ,
46
+ const defaultVersion = configuration . defaultSwiftVersion ;
47
+ if ( defaultVersion !== undefined ) {
48
+ target = defaultVersion ;
49
+ } else {
50
+ const picked = await vscode . window . showQuickPick (
51
+ [
52
+ // Potentially add more versions here
53
+ { value : 5 , label : "Swift 5" } ,
54
+ { value : 6 , label : "Swift 6" } ,
55
+ ] ,
75
56
{
76
- scope : vscode . TaskScope . Global ,
77
- cwd : vscode . Uri . file ( path . dirname ( filename ) ) ,
78
- presentationOptions : { reveal : vscode . TaskRevealKind . Always , clear : true } ,
79
- } ,
80
- ctx . toolchain
57
+ placeHolder : "Select a target Swift version" ,
58
+ }
81
59
) ;
82
- await ctx . tasks . executeTaskAndWait ( runTask ) ;
83
60
84
- // delete file after running swift
85
- if ( isTempFile ) {
86
- await fs . rm ( filename ) ;
61
+ if ( ! picked ) {
62
+ return ;
87
63
}
64
+ target = picked . value ;
65
+ }
66
+
67
+ let filename = document . fileName ;
68
+ let isTempFile = false ;
69
+ if ( document . isUntitled ) {
70
+ // if document hasn't been saved, save it to a temporary file
71
+ isTempFile = true ;
72
+ filename = ctx . tempFolder . filename ( document . fileName , "swift" ) ;
73
+ const text = document . getText ( ) ;
74
+ await fs . writeFile ( filename , text ) ;
75
+ } else {
76
+ // otherwise save document
77
+ await document . save ( ) ;
78
+ }
79
+ const runTask = createSwiftTask (
80
+ [ "-swift-version" , target . toString ( ) , filename ] ,
81
+ `Run ${ filename } ` ,
82
+ {
83
+ scope : vscode . TaskScope . Global ,
84
+ cwd : vscode . Uri . file ( path . dirname ( filename ) ) ,
85
+ presentationOptions : { reveal : vscode . TaskRevealKind . Always , clear : true } ,
86
+ } ,
87
+ ctx . toolchain
88
+ ) ;
89
+ await ctx . tasks . executeTaskAndWait ( runTask ) ;
90
+
91
+ // delete file after running swift
92
+ if ( isTempFile ) {
93
+ await fs . rm ( filename ) ;
88
94
}
89
95
}
0 commit comments