|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the VS Code Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021-2024 the VS Code Swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of VS Code Swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import * as fs from "fs/promises"; |
| 16 | +import * as path from "path"; |
| 17 | +import * as vscode from "vscode"; |
| 18 | +import { fileExists, pathExists } from "../utilities/filesystem"; |
| 19 | + |
| 20 | +const extension = "swift"; |
| 21 | +const defaultFileName = `Untitled.${extension}`; |
| 22 | + |
| 23 | +export async function newSwiftFile( |
| 24 | + uri?: vscode.Uri, |
| 25 | + isDirectory: (uri: vscode.Uri) => Promise<boolean> = async uri => { |
| 26 | + return (await vscode.workspace.fs.stat(uri)).type === vscode.FileType.Directory; |
| 27 | + } |
| 28 | +) { |
| 29 | + if (uri) { |
| 30 | + // Attempt to create the file at the given directory. |
| 31 | + 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, |
| 38 | + }); |
| 39 | + if (!givenPath) { |
| 40 | + return; |
| 41 | + } |
| 42 | + const targetUri = vscode.Uri.file(givenPath); |
| 43 | + try { |
| 44 | + await fs.writeFile(targetUri.fsPath, "", "utf-8"); |
| 45 | + const document = await vscode.workspace.openTextDocument(targetUri); |
| 46 | + await vscode.languages.setTextDocumentLanguage(document, "swift"); |
| 47 | + await vscode.window.showTextDocument(document); |
| 48 | + } catch (err) { |
| 49 | + vscode.window.showErrorMessage(`Failed to create ${targetUri.fsPath}`); |
| 50 | + } |
| 51 | + } else { |
| 52 | + // If no path is supplied then open an untitled editor w/ Swift language type |
| 53 | + const document = await vscode.workspace.openTextDocument({ |
| 54 | + language: "swift", |
| 55 | + }); |
| 56 | + await vscode.window.showTextDocument(document); |
| 57 | + } |
| 58 | +} |
| 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