Skip to content

Commit 17591e7

Browse files
authored
Resolve #1029: Support files and folders to be dragged in externally … (#1040)
* Resolve #1029: Support files and folders to be dragged in externally into project navigator * Support holding option while dropping to copy * feat: show + when dragging with option clicked
1 parent 5ae20af commit 17591e7

5 files changed

Lines changed: 46 additions & 20 deletions

File tree

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@
339339
D7012EE827E757850001E1EF /* FindNavigatorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7012EE727E757850001E1EF /* FindNavigatorView.swift */; };
340340
D7211D4327E066CE008F2ED7 /* Localized+Ex.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7211D4227E066CE008F2ED7 /* Localized+Ex.swift */; };
341341
D7211D4727E06BFE008F2ED7 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = D7211D4927E06BFE008F2ED7 /* Localizable.strings */; };
342+
D7DC4B76298FFBE900D6C83D /* OutlintViewController+OutlineTableViewCellDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7DC4B75298FFBE900D6C83D /* OutlintViewController+OutlineTableViewCellDelegate.swift */; };
342343
D7E201AE27E8B3C000CB86D0 /* String+Ranges.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7E201AD27E8B3C000CB86D0 /* String+Ranges.swift */; };
343344
D7E201B027E8C07300CB86D0 /* FindNavigatorSearchBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7E201AF27E8C07300CB86D0 /* FindNavigatorSearchBar.swift */; };
344345
D7E201B227E8D50000CB86D0 /* FindNavigatorModeSelector.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7E201B127E8D50000CB86D0 /* FindNavigatorModeSelector.swift */; };
@@ -733,6 +734,7 @@
733734
D7012EE727E757850001E1EF /* FindNavigatorView.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = FindNavigatorView.swift; sourceTree = "<group>"; tabWidth = 4; };
734735
D7211D4227E066CE008F2ED7 /* Localized+Ex.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Localized+Ex.swift"; sourceTree = "<group>"; };
735736
D7211D4827E06BFE008F2ED7 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
737+
D7DC4B75298FFBE900D6C83D /* OutlintViewController+OutlineTableViewCellDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OutlintViewController+OutlineTableViewCellDelegate.swift"; sourceTree = "<group>"; };
736738
D7E201AD27E8B3C000CB86D0 /* String+Ranges.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Ranges.swift"; sourceTree = "<group>"; };
737739
D7E201AF27E8C07300CB86D0 /* FindNavigatorSearchBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindNavigatorSearchBar.swift; sourceTree = "<group>"; };
738740
D7E201B127E8D50000CB86D0 /* FindNavigatorModeSelector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindNavigatorModeSelector.swift; sourceTree = "<group>"; };
@@ -959,6 +961,7 @@
959961
285FEC6D27FE4B4A00E57D53 /* OutlineViewController.swift */,
960962
285FEC6F27FE4B9800E57D53 /* OutlineTableViewCell.swift */,
961963
285FEC7127FE4EEF00E57D53 /* OutlineMenu.swift */,
964+
D7DC4B75298FFBE900D6C83D /* OutlintViewController+OutlineTableViewCellDelegate.swift */,
962965
);
963966
path = OutlineView;
964967
sourceTree = "<group>";
@@ -2601,6 +2604,7 @@
26012604
58822534292C280D00E83CDE /* CursorLocation.swift in Sources */,
26022605
201169E52837B40300F92B46 /* SourceControlNavigatorRepositoriesView.swift in Sources */,
26032606
587B9E6A29301D8F00AC7927 /* GitLabPermissions.swift in Sources */,
2607+
D7DC4B76298FFBE900D6C83D /* OutlintViewController+OutlineTableViewCellDelegate.swift in Sources */,
26042608
587B9E9229301D8F00AC7927 /* BitBucketAccount.swift in Sources */,
26052609
58F2EAFE292FB2B0004A9BDE /* GitHubEnterpriseLoginView.swift in Sources */,
26062610
DE513F52281B672D002260B9 /* TabBarAccessory.swift in Sources */,

CodeEdit/Features/NavigatorSidebar/ProjectNavigator/OutlineView/OutlineTableViewCell.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import SwiftUI
99

1010
protocol OutlineTableViewCellDelegate: AnyObject {
1111
func moveFile(file: WorkspaceClient.FileItem, to destination: URL)
12+
func copyFile(file: WorkspaceClient.FileItem, to destination: URL)
1213
}
1314

1415
/// A `NSTableCellView` showing an ``icon`` and a ``label``

CodeEdit/Features/NavigatorSidebar/ProjectNavigator/OutlineView/OutlineViewController.swift

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ extension OutlineViewController: NSOutlineViewDataSource {
173173
if !fileItem.isFolder {
174174
outlineView.setDropItem(fileItem.parent, dropChildIndex: index)
175175
}
176-
return .move
176+
return info.draggingSourceOperationMask == .copy ? .copy : .move
177177
}
178178
return []
179179
}
@@ -199,7 +199,13 @@ extension OutlineViewController: NSOutlineViewDataSource {
199199
}
200200

201201
// Needs to come before call to .removeItem or else race condition occurs
202-
guard let srcFileItem = try? workspace?.workspaceClient?.getFileItem(fileItemURL.relativePath) else {
202+
var srcFileItem: WorkspaceClient.FileItem? = try? workspace?.workspaceClient?.getFileItem(fileItemURL.path)
203+
// If srcFileItem is nil, fileItemUrl is an external file url.
204+
if srcFileItem == nil {
205+
srcFileItem = WorkspaceClient.FileItem(url: URL(fileURLWithPath: fileItemURL.path))
206+
}
207+
208+
guard let srcFileItem else {
203209
return false
204210
}
205211

@@ -214,8 +220,11 @@ extension OutlineViewController: NSOutlineViewDataSource {
214220
fatalError(error.localizedDescription)
215221
}
216222
}
217-
218-
self.moveFile(file: srcFileItem, to: destURL)
223+
if info.draggingSourceOperationMask == .copy {
224+
self.copyFile(file: srcFileItem, to: destURL)
225+
} else {
226+
self.moveFile(file: srcFileItem, to: destURL)
227+
}
219228
}
220229
return true
221230
}
@@ -375,17 +384,3 @@ extension OutlineViewController: NSMenuDelegate {
375384
menu.update()
376385
}
377386
}
378-
379-
// MARK: - OutlineTableViewCellDelegate
380-
381-
extension OutlineViewController: OutlineTableViewCellDelegate {
382-
func moveFile(file: Item, to destination: URL) {
383-
if !file.isFolder {
384-
workspace?.closeTab(item: .codeEditor(file.id))
385-
}
386-
file.move(to: destination)
387-
if !file.isFolder {
388-
workspace?.openTab(item: file)
389-
}
390-
}
391-
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// OutlintViewController+OutlineTableViewCellDelegate.swift
3+
// CodeEdit
4+
//
5+
// Created by Ziyuan Zhao on 2023/2/5.
6+
//
7+
8+
import Foundation
9+
10+
// MARK: - OutlineTableViewCellDelegate
11+
12+
extension OutlineViewController: OutlineTableViewCellDelegate {
13+
func moveFile(file: Item, to destination: URL) {
14+
if !file.isFolder {
15+
workspace?.closeTab(item: .codeEditor(file.id))
16+
}
17+
file.move(to: destination)
18+
if !file.isFolder {
19+
workspace?.openTab(item: file)
20+
}
21+
}
22+
23+
func copyFile(file: WorkspaceClient.FileItem, to destination: URL) {
24+
file.duplicate(to: destination)
25+
}
26+
}

CodeEdit/Utils/WorkspaceClient/Model/FileItem.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ extension WorkspaceClient {
257257
}
258258

259259
/// This function duplicates the item or folder
260-
func duplicate() {
260+
func duplicate(to destination: URL? = nil) {
261+
var fileUrl = destination == nil ? self.url : destination!
261262
// if a file/folder with the same name exists, add "copy" to the end
262-
var fileUrl = self.url
263263
while FileItem.fileManger.fileExists(atPath: fileUrl.path) {
264264
let previousName = fileUrl.deletingPathExtension().lastPathComponent
265265
let filextension = fileUrl.pathExtension

0 commit comments

Comments
 (0)