Skip to content

Commit 2493c66

Browse files
Fixed renaming behavior for pasted file
1 parent a09d8d1 commit 2493c66

File tree

5 files changed

+37
-33
lines changed

5 files changed

+37
-33
lines changed

CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ final class CEWorkspaceFile: Codable, Comparable, Hashable, Identifiable, Editor
116116

117117
/// Returns a boolean that is true if the resource represented by this object is a directory.
118118
lazy var isFolder: Bool = {
119-
isPhantom ? resolvedURL.hasDirectoryPath : resolvedURL.isFolder
119+
phantomFile != nil ? resolvedURL.hasDirectoryPath : resolvedURL.isFolder
120120
}()
121121

122122
/// Returns a boolean that is true if the contents of the directory at this path are
@@ -164,8 +164,8 @@ final class CEWorkspaceFile: Codable, Comparable, Hashable, Identifiable, Editor
164164
FileIcon.iconColor(fileType: type)
165165
}
166166

167-
/// Indicates whether the file is phantom (not yet created on disk)
168-
var isPhantom: Bool = false
167+
/// Holds information about the phantom file
168+
var phantomFile: PhantomFile?
169169

170170
init(
171171
id: String,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// PhantomFile.swift
3+
// CodeEdit
4+
//
5+
// Created by Abe Malla on 7/25/25.
6+
//
7+
8+
/// Represents a file that doesn't exist on disk
9+
enum PhantomFile {
10+
case empty
11+
case pasteboardContent
12+
}

CodeEdit/Features/NavigatorArea/ProjectNavigator/OutlineView/ProjectNavigatorMenuActions.swift

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,14 @@ extension ProjectNavigatorMenu {
107107
/// Action that creates a new file with clipboard content
108108
@objc
109109
func newFileFromClipboard() {
110-
guard let item else { return }
111-
do {
112-
let clipBoardContent = NSPasteboard.general.string(forType: .string)?.data(using: .utf8)
113-
if let clipBoardContent, !clipBoardContent.isEmpty, let newFile = try workspace?
114-
.workspaceFileManager?
115-
.addFile(
116-
fileName: "untitled",
117-
toFile: item,
118-
contents: clipBoardContent
119-
) {
120-
workspace?.listenerModel.highlightedFileItem = newFile
121-
workspace?.editorManager?.openTab(item: newFile)
122-
self.renameFile()
123-
}
124-
} catch {
125-
let alert = NSAlert(error: error)
126-
alert.addButton(withTitle: "Dismiss")
127-
alert.runModal()
110+
guard item != nil else { return }
111+
let clipBoardContent = NSPasteboard.general.string(forType: .string)?.data(using: .utf8)
112+
113+
guard let clipBoardContent, !clipBoardContent.isEmpty else {
114+
return
128115
}
116+
117+
createAndAddPhantomFile(isFolder: false, usePasteboardContent: true)
129118
}
130119

131120
/// Action that creates a new untitled folder
@@ -263,9 +252,9 @@ extension ProjectNavigatorMenu {
263252
NSPasteboard.general.setString(paths, forType: .string)
264253
}
265254

266-
private func createAndAddPhantomFile(isFolder: Bool) {
255+
private func createAndAddPhantomFile(isFolder: Bool, usePasteboardContent: Bool = false) {
267256
guard let item else { return }
268-
let phantomFile = CEWorkspaceFile(
257+
let file = CEWorkspaceFile(
269258
id: UUID().uuidString,
270259
url: item.url
271260
.appending(
@@ -275,21 +264,21 @@ extension ProjectNavigatorMenu {
275264
changeType: nil,
276265
staged: false
277266
)
278-
phantomFile.isPhantom = true
279-
phantomFile.parent = item
267+
file.phantomFile = usePasteboardContent ? .pasteboardContent : .empty
268+
file.parent = item
280269

281270
// Add phantom file to parent's children temporarily for display
282271
if let workspace = workspace,
283272
let fileManager = workspace.workspaceFileManager {
284273
_ = fileManager.childrenOfFile(item)
285-
fileManager.flattenedFileItems[phantomFile.id] = phantomFile
274+
fileManager.flattenedFileItems[file.id] = file
286275
if fileManager.childrenMap[item.id] == nil {
287276
fileManager.childrenMap[item.id] = []
288277
}
289-
fileManager.childrenMap[item.id]?.append(phantomFile.id)
278+
fileManager.childrenMap[item.id]?.append(file.id)
290279
}
291280

292-
workspace?.listenerModel.highlightedFileItem = phantomFile
281+
workspace?.listenerModel.highlightedFileItem = file
293282
sender.outlineView.reloadData()
294283
self.renameFile()
295284
}

CodeEdit/Features/NavigatorArea/ProjectNavigator/OutlineView/ProjectNavigatorTableViewCell.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ final class ProjectNavigatorTableViewCell: FileSystemTableViewCell {
5757
override func controlTextDidEndEditing(_ obj: Notification) {
5858
guard let fileItem else { return }
5959

60-
if fileItem.isPhantom {
60+
if fileItem.phantomFile != nil {
6161
DispatchQueue.main.async { [weak fileItem, weak self] in
6262
guard let fileItem, let self = self else { return }
6363
self.handlePhantomFileCompletion(fileItem: fileItem, wasCancelled: false)
@@ -100,7 +100,10 @@ final class ProjectNavigatorTableViewCell: FileSystemTableViewCell {
100100
} else {
101101
let newFile = try workspaceFileManager.addFile(
102102
fileName: newName,
103-
toFile: parent
103+
toFile: parent,
104+
contents: fileItem.phantomFile == PhantomFile.pasteboardContent
105+
? NSPasteboard.general.string(forType: .string)?.data(using: .utf8)
106+
: nil
104107
)
105108
workspace.workspace?.listenerModel.highlightedFileItem = newFile
106109
workspace.workspace?.editorManager?.openTab(item: newFile)
@@ -140,7 +143,7 @@ final class ProjectNavigatorTableViewCell: FileSystemTableViewCell {
140143
textView: NSTextView,
141144
doCommandBy commandSelector: Selector
142145
) -> Bool {
143-
guard let fileItem, fileItem.isPhantom else { return false }
146+
guard let fileItem, fileItem.phantomFile != nil else { return false }
144147

145148
if commandSelector == #selector(NSResponder.cancelOperation(_:)) {
146149
DispatchQueue.main.async { [weak fileItem, weak self] in

CodeEdit/Features/NavigatorArea/ProjectNavigator/OutlineView/ProjectNavigatorViewController+NSOutlineViewDelegate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ extension ProjectNavigatorViewController: NSOutlineViewDelegate {
4444

4545
guard let item = outlineView.item(atRow: selectedIndex) as? CEWorkspaceFile else { return }
4646

47-
if !item.isFolder && !item.isPhantom && shouldSendSelectionUpdate {
47+
if !item.isFolder && item.phantomFile == nil && shouldSendSelectionUpdate {
4848
shouldSendSelectionUpdate = false
4949
if workspace?.editorManager?.activeEditor.selectedTab?.file != item {
5050
workspace?.editorManager?.activeEditor.openTab(file: item, asTemporary: true)
@@ -131,7 +131,7 @@ extension ProjectNavigatorViewController: NSOutlineViewDelegate {
131131
outlineView.selectRowIndexes(.init(integer: row), byExtendingSelection: false)
132132
shouldSendSelectionUpdate = true
133133

134-
if fileItem.isPhantom {
134+
if fileItem.phantomFile != nil {
135135
return
136136
}
137137

0 commit comments

Comments
 (0)