diff --git a/lib/tree-view.coffee b/lib/tree-view.coffee index e18e14ad..d2d32abb 100644 --- a/lib/tree-view.coffee +++ b/lib/tree-view.coffee @@ -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) @@ -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] diff --git a/spec/tree-view-spec.coffee b/spec/tree-view-spec.coffee index 5051a90a..5ff9148c 100644 --- a/spec/tree-view-spec.coffee +++ b/spec/tree-view-spec.coffee @@ -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() @@ -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('.') @@ -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('.')