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

Merged
Merged
Show file tree
Hide file tree
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
let newFolder = menuItem("New Folder", action: #selector(newFolder))

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,47 @@ 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 clipBoardContent, !clipBoardContent.isEmpty, let newFile = try workspace?
.workspaceFileManager?
.addFile(
fileName: "untitled",
toFile: item,
contents: clipBoardContent
) {
workspace?.listenerModel.highlightedFileItem = newFile
workspace?.editorManager?.openTab(item: newFile)
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
Expand Down Expand Up @@ -143,21 +184,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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// ProjectNavigatorNSOutlineView.swift
// CodeEdit
//
// Created by Khan Winter on 6/10/25.
//

import AppKit

final class ProjectNavigatorNSOutlineView: NSOutlineView, NSMenuItemValidation {
override func performKeyEquivalent(with event: NSEvent) -> Bool {
guard event.window === window && window?.firstResponder === self else {
return super.performKeyEquivalent(with: event)
}

if event.charactersIgnoringModifiers == "v"
&& event.modifierFlags.intersection(.deviceIndependentFlagsMask) == .command {
guard let menu = menu as? ProjectNavigatorMenu else {
return super.performKeyEquivalent(with: event)
}
menu.delegate?.menuNeedsUpdate?(menu)
for fileItem in selectedRowIndexes.compactMap({ item(atRow: $0) as? CEWorkspaceFile }) {
menu.item = fileItem
menu.newFileFromClipboard()
}
return true
}
return super.performKeyEquivalent(with: event)
}

func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
if menuItem.action == #selector(ProjectNavigatorMenu.newFileFromClipboard) {
return !selectedRowIndexes.isEmpty
}
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ extension ProjectNavigatorViewController: NSMenuDelegate {
let row = outlineView.clickedRow
guard let menu = menu as? ProjectNavigatorMenu else { return }

menu.workspace = workspace
if row == -1 {
menu.item = nil
} else {
if let item = outlineView.item(atRow: row) as? CEWorkspaceFile {
menu.item = item
menu.workspace = workspace
} else {
menu.item = nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ final class ProjectNavigatorViewController: NSViewController {
self.scrollView.hasVerticalScroller = true
self.view = scrollView

self.outlineView = NSOutlineView()
self.outlineView = ProjectNavigatorNSOutlineView()
self.outlineView.dataSource = self
self.outlineView.delegate = self
self.outlineView.autosaveExpandedItems = true
Expand Down