Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Select files after paste operation #631

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 30 additions & 4 deletions lib/tree-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,12 @@ class TreeView extends View
cutPaths = if LocalStorage['tree-view:cutPath'] then JSON.parse(LocalStorage['tree-view:cutPath']) else null
copiedPaths = if LocalStorage['tree-view:copyPath'] then JSON.parse(LocalStorage['tree-view:copyPath']) else null
initialPaths = copiedPaths or cutPaths
newPaths = []

catchAndShowFileErrors = (operation) ->
catchAndShowFileErrors = (operation, newPath) ->
try
operation()
newPaths.push(newPath)
catch error
atom.notifications.addWarning("Unable to paste paths: #{initialPaths}", detail: error.message)

Expand All @@ -594,15 +596,39 @@ class TreeView extends View

if fs.isDirectorySync(initialPath)
# use fs.copy to copy directories since read/write will fail for directories
catchAndShowFileErrors -> fs.copySync(initialPath, newPath)
catchAndShowFileErrors( (-> fs.copySync(initialPath, newPath)), newPath)
else
# read the old file and write a new one at target location
catchAndShowFileErrors -> fs.writeFileSync(newPath, fs.readFileSync(initialPath))
catchAndShowFileErrors( (-> fs.writeFileSync(newPath, fs.readFileSync(initialPath))), newPath)
else if cutPaths
# Only move the target if the cut target doesn't exists and if the newPath
# is not within the initial path
unless fs.existsSync(newPath) or newPath.startsWith(initialPath)
catchAndShowFileErrors -> fs.moveSync(initialPath, newPath)
catchAndShowFileErrors( (-> fs.moveSync(initialPath, newPath)), newPath)
if repo = repoForPath(initialPath)
repo.getPathStatus(initialPath) # for side effect; cut may result in status change of parent dir
if newPaths.length > 0
@deselect(@getSelectedEntries())
@selectOnCreate(newPaths)

# For new paths that have been created, select each entry if it exists or
# set up a subscription to select it when it is added.
selectOnCreate: (newPaths) ->
subscribedDirectories = []
for p in newPaths
entry = @entryForPath(p)
if entry.isPathEqual(p)
entry.classList.add('selected')
continue
if entry instanceof DirectoryView
continue if subscribedDirectories.indexOf(entry.directory.name) isnt -1
subscribedDirectories.push(entry.directory.name)
subscription = entry.directory.onDidAddEntries (addedEntries) =>
for e in addedEntries
@entryForPath(e.path)?.classList.add('selected') if newPaths.indexOf(e.path) isnt -1
if repo = repoForPath(e.path)
repo.getPathStatus(e.path)
subscription.dispose()

add: (isCreatingFile) ->
selectedEntry = @selectedEntry() ? @roots[0]
Expand Down
14 changes: 13 additions & 1 deletion spec/tree-view-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ describe "TreeView", ->

describe "when a file has been copied", ->
describe "when a file is selected", ->
it "creates a copy of the original file in the selected file's parent directory", ->
it "creates a copy of the original file in the selected file's parent directory and selects it", ->
LocalStorage['tree-view:copyPath'] = JSON.stringify([filePath])

fileView2.click()
Expand All @@ -1281,12 +1281,23 @@ describe "TreeView", ->
expect(fs.existsSync(path.join(dirPath2, path.basename(filePath)))).toBeTruthy()
expect(fs.existsSync(filePath)).toBeTruthy()

waitsFor (done) ->
disposable = dirView2[0].directory.onDidAddEntries ->
disposable.dispose()
done()

runs ->
newFileView = dirView2.find('.file:contains(test-file.txt)')
expect(newFileView).not.toBeNull()
expect(newFileView).toHaveClass('selected')

describe 'when target already exists', ->
it 'appends a number to the destination name', ->
LocalStorage['tree-view:copyPath'] = JSON.stringify([filePath])

fileView.click()
atom.commands.dispatch(treeView.element, "tree-view:paste")
fileView.click()
atom.commands.dispatch(treeView.element, "tree-view:paste")

fileArr = filePath.split(path.sep).pop().split('.')
Expand All @@ -1312,6 +1323,7 @@ describe "TreeView", ->

dirView.click()
atom.commands.dispatch(treeView.element, "tree-view:paste")
dirView.click()
atom.commands.dispatch(treeView.element, "tree-view:paste")

fileArr = filePath.split(path.sep).pop().split('.')
Expand Down