-
Notifications
You must be signed in to change notification settings - Fork 74
Add New Swift File command #1018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the VS Code Swift open source project | ||
// | ||
// Copyright (c) 2021-2024 the VS Code Swift project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import * as fs from "fs/promises"; | ||
import * as path from "path"; | ||
import * as vscode from "vscode"; | ||
|
||
const extension = "swift"; | ||
const defaultFileName = `Untitled.${extension}`; | ||
|
||
export async function newSwiftFile( | ||
uri?: vscode.Uri, | ||
isDirectory: (uri: vscode.Uri) => Promise<boolean> = async uri => { | ||
return (await vscode.workspace.fs.stat(uri)).type === vscode.FileType.Directory; | ||
} | ||
) { | ||
if (uri) { | ||
// Attempt to create the file at the given directory. | ||
const dir = (await isDirectory(uri)) ? uri.fsPath : path.dirname(uri.fsPath); | ||
const defaultName = vscode.Uri.file(path.join(dir, defaultFileName)); | ||
const targetUri = await vscode.window.showSaveDialog({ | ||
defaultUri: defaultName, | ||
title: "Enter a file path to be created", | ||
}); | ||
|
||
if (!targetUri) { | ||
return; | ||
} | ||
|
||
try { | ||
await fs.writeFile(targetUri.fsPath, "", "utf-8"); | ||
const document = await vscode.workspace.openTextDocument(targetUri); | ||
await vscode.languages.setTextDocumentLanguage(document, "swift"); | ||
await vscode.window.showTextDocument(document); | ||
} catch (err) { | ||
vscode.window.showErrorMessage(`Failed to create ${targetUri.fsPath}`); | ||
} | ||
} else { | ||
// If no path is supplied then open an untitled editor w/ Swift language type | ||
const document = await vscode.workspace.openTextDocument({ | ||
language: "swift", | ||
}); | ||
await vscode.window.showTextDocument(document); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the VS Code Swift open source project | ||
// | ||
// Copyright (c) 2024 the VS Code Swift project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// import * as assert from "assert"; | ||
import * as vscode from "vscode"; | ||
import * as assert from "assert"; | ||
import * as path from "path"; | ||
import { anything, deepEqual, verify, when } from "ts-mockito"; | ||
import { newSwiftFile } from "../../../src/commands/newFile"; | ||
import { mockNamespace } from "../../unit-tests/MockUtils"; | ||
import { TemporaryFolder } from "../../../src/utilities/tempFolder"; | ||
import { fileExists } from "../../../src/utilities/filesystem"; | ||
|
||
suite("NewFile Command Test Suite", () => { | ||
const workspaceMock = mockNamespace(vscode, "workspace"); | ||
const windowMock = mockNamespace(vscode, "window"); | ||
const languagesMock = mockNamespace(vscode, "languages"); | ||
|
||
test("Creates a blank file if no URI is provided", async () => { | ||
await newSwiftFile(undefined); | ||
|
||
verify(workspaceMock.openTextDocument(deepEqual({ language: "swift" }))).once(); | ||
verify(windowMock.showTextDocument(anything())).once(); | ||
}); | ||
|
||
test("Creates file at provided directory", async () => { | ||
const folder = await TemporaryFolder.create(); | ||
const file = path.join(folder.path, "MyFile.swift"); | ||
|
||
when(windowMock.showSaveDialog(anything())).thenReturn( | ||
Promise.resolve(vscode.Uri.file(file)) | ||
); | ||
|
||
await newSwiftFile(vscode.Uri.file(folder.path), () => Promise.resolve(true)); | ||
|
||
assert.ok(await fileExists(file)); | ||
|
||
verify(workspaceMock.openTextDocument(anything())).once(); | ||
verify(languagesMock.setTextDocumentLanguage(anything(), "swift")).once(); | ||
verify(windowMock.showTextDocument(anything())).once(); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.