Skip to content

Creating a new file with clipboard contents #1995

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -222,8 +222,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/lukepistrol/SwiftLintPlugin",
"state" : {
"revision" : "3825ebf8d55bb877c91bc897e8e3d0c001f16fba",
"version" : "0.58.2"
"revision" : "3780efccceaa87f17ec39638a9d263d0e742b71c",
"version" : "0.59.1"
}
},
{
Original file line number Diff line number Diff line change
@@ -62,7 +62,8 @@ extension CEWorkspaceFileManager {
func addFile(
fileName: String,
toFile file: CEWorkspaceFile,
useExtension: String? = nil
useExtension: String? = nil,
contents: Data? = nil
) throws -> CEWorkspaceFile {
// check the folder for other files, and see what the most common file extension is
do {
@@ -95,7 +96,7 @@ extension CEWorkspaceFileManager {
// Create the file
guard fileManager.createFile(
atPath: fileUrl.path,
contents: nil,
contents: contents,
attributes: [FileAttributeKey.creationDate: Date()]
) else {
throw CocoaError.error(.fileWriteUnknown, url: fileUrl)
Original file line number Diff line number Diff line change
@@ -61,6 +61,12 @@ final class ProjectNavigatorMenu: NSMenu {
let showFileInspector = menuItem("Show File Inspector", action: nil)

let newFile = menuItem("New File...", action: #selector(newFile))
let newFileFromClipboard = menuItem(
"New File from Clipboard",
action: #selector(newFileFromClipboard),
key: "v"
)
newFileFromClipboard.keyEquivalentModifierMask = [.command, .control]
let newFolder = menuItem("New Folder", action: #selector(newFolder))

let rename = menuItem("Rename", action: #selector(renameFile))
@@ -100,6 +106,7 @@ final class ProjectNavigatorMenu: NSMenu {
showFileInspector,
NSMenuItem.separator(),
newFile,
newFileFromClipboard,
newFolder
]

Original file line number Diff line number Diff line change
@@ -98,6 +98,51 @@ extension ProjectNavigatorMenu {
}
}

/// Opens the rename file dialogue on the cell this was presented from.
@objc
func renameFile() {
guard let newFile = workspace?.listenerModel.highlightedFileItem else { return }
let row = sender.outlineView.row(forItem: newFile)
guard row > 0,
let cell = sender.outlineView.view(
atColumn: 0,
row: row,
makeIfNecessary: false
) as? ProjectNavigatorTableViewCell else {
return
}
sender.outlineView.window?.makeFirstResponder(cell.textField)
}

// TODO: Automatically identified the file type
/// Action that creates a new file with clipboard content
@objc
func newFileFromClipboard() {
guard let item else { return }
do {
let clipBoardContent = NSPasteboard.general.string(forType: .string)?.data(using: .utf8)
if let newFile = try workspace?
.workspaceFileManager?
.addFile(
fileName: "untitled",
toFile: item,
useExtension: "",
contents: clipBoardContent
) {
workspace?.listenerModel.highlightedFileItem = newFile
workspace?.editorManager?.openTab(item: newFile)
}
/// To resolve racing condition
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.renameFile()
}
} catch {
let alert = NSAlert(error: error)
alert.addButton(withTitle: "Dismiss")
alert.runModal()
}
}

// TODO: allow custom folder names
/// Action that creates a new untitled folder
@objc
@@ -143,21 +188,6 @@ extension ProjectNavigatorMenu {
reloadData()
}

/// Opens the rename file dialogue on the cell this was presented from.
@objc
func renameFile() {
let row = sender.outlineView.row(forItem: item)
guard row > 0,
let cell = sender.outlineView.view(
atColumn: 0,
row: row,
makeIfNecessary: false
) as? ProjectNavigatorTableViewCell else {
return
}
sender.outlineView.window?.makeFirstResponder(cell.textField)
}

/// Action that moves the item to trash.
@objc
func trash() {