15
15
import * as fs from "fs/promises" ;
16
16
import * as path from "path" ;
17
17
import * as vscode from "vscode" ;
18
- import { fileExists , pathExists } from "../utilities/filesystem" ;
19
18
20
19
const extension = "swift" ;
21
20
const defaultFileName = `Untitled.${ extension } ` ;
@@ -29,17 +28,16 @@ export async function newSwiftFile(
29
28
if ( uri ) {
30
29
// Attempt to create the file at the given directory.
31
30
const dir = ( await isDirectory ( uri ) ) ? uri . fsPath : path . dirname ( uri . fsPath ) ;
32
- const defaultName = path . join ( dir , defaultFileName ) ;
33
- const givenPath = await vscode . window . showInputBox ( {
34
- value : defaultName ,
35
- valueSelection : [ dir . length + 1 , defaultName . length - extension . length - 1 ] ,
36
- prompt : "Enter a file path to be created" ,
37
- validateInput : validatePathValid ,
31
+ const defaultName = vscode . Uri . file ( path . join ( dir , defaultFileName ) ) ;
32
+ const targetUri = await vscode . window . showSaveDialog ( {
33
+ defaultUri : defaultName ,
34
+ title : "Enter a file path to be created" ,
38
35
} ) ;
39
- if ( ! givenPath ) {
36
+
37
+ if ( ! targetUri ) {
40
38
return ;
41
39
}
42
- const targetUri = vscode . Uri . file ( givenPath ) ;
40
+
43
41
try {
44
42
await fs . writeFile ( targetUri . fsPath , "" , "utf-8" ) ;
45
43
const document = await vscode . workspace . openTextDocument ( targetUri ) ;
@@ -56,19 +54,3 @@ export async function newSwiftFile(
56
54
await vscode . window . showTextDocument ( document ) ;
57
55
}
58
56
}
59
-
60
- async function validatePathValid ( input : string ) {
61
- const inputPath = vscode . Uri . file ( input ) . fsPath ;
62
- const filePathExists = await fileExists ( inputPath ) ;
63
- if ( filePathExists ) {
64
- return `Supplied path ${ inputPath } already exists` ;
65
- }
66
-
67
- const inputDir = path . dirname ( inputPath ) ;
68
- const dirExists = await pathExists ( inputDir ) ;
69
- if ( ! dirExists ) {
70
- return `Supplied directory ${ inputDir } doesn't exist` ;
71
- }
72
-
73
- return undefined ;
74
- }
0 commit comments