Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5981f47
Fire events when creating, moving or deleting items
Alhadis Sep 7, 2016
c088550
Add TreeView methods to attach file-event handlers
Alhadis Sep 7, 2016
1488a66
Add specs for 5981f47 and c088550
Alhadis Sep 7, 2016
74b1b7e
Fix typos and polish punctuation of spec titles
Alhadis Sep 7, 2016
34a5e1d
Merge branch 'master' into file-events
Alhadis Oct 28, 2016
75bb6b6
Merge branch 'master' into file-events
Alhadis Oct 29, 2016
b5c2755
Merge branch 'master' into file-events
Alhadis Jan 4, 2017
fab5074
Resolve conflicts with v0.213.1
Alhadis Jan 6, 2017
2ffc575
Resolve conflicts with v0.214.0
Alhadis Feb 6, 2017
271949c
Resolve conflicts with v0.215.1
Alhadis Mar 3, 2017
6d07f77
Update editor paths when containing directory is renamed via move dialog
kuychaco Mar 10, 2017
081ead9
Update editor paths when containing directory is renamed via drag'n'drop
kuychaco Mar 10, 2017
49c6893
Extract helper method updateEditorsForPath
kuychaco Mar 10, 2017
06d227f
Close editors when containing directory is removed
kuychaco Mar 10, 2017
95fb486
Merge branch 'file-events' of https://github.com/Cutlery-Drawer/tree-…
Mar 25, 2017
15f87c7
Update Move and CopyDialog to take callbacks
Mar 25, 2017
2642b82
Standardize event params
Mar 25, 2017
b86a328
Update tests
Mar 25, 2017
425f396
:fire: console.log
Mar 25, 2017
9a5e04b
Betterer names
Mar 25, 2017
b17769f
Merge ku-mkt-udpate-editors-after-folder-operations, using new strategy
Mar 25, 2017
b5d228d
Use new hooks for updating editors
Mar 25, 2017
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
6 changes: 4 additions & 2 deletions lib/copy-dialog.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Dialog = require './dialog'

module.exports =
class CopyDialog extends Dialog
constructor: (@initialPath) ->
constructor: (@initialPath, {@onCopy}) ->
super
prompt: 'Enter the new path for the duplicate.'
initialPath: atom.project.relativize(@initialPath)
Expand All @@ -32,8 +32,10 @@ class CopyDialog extends Dialog
try
if fs.isDirectorySync(@initialPath)
fs.copySync(@initialPath, newPath)
@onCopy?({initialPath: @initialPath, newPath: newPath})
else
fs.copy @initialPath, newPath, ->
fs.copy @initialPath, newPath, =>
@onCopy?({initialPath: @initialPath, newPath: newPath})
atom.workspace.open newPath,
activatePane: true
initialLine: activeEditor?.getLastCursor().getBufferRow()
Expand Down
7 changes: 7 additions & 0 deletions lib/helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ module.exports =
fullExtension = extension + fullExtension
filePath = path.basename(filePath, extension)
fullExtension

updateEditorsForPath: (oldPath, newPath) ->
editors = atom.workspace.getTextEditors()
for editor in editors
filePath = editor.getPath()
if filePath?.startsWith(oldPath)
editor.getBuffer().setPath(filePath.replace(oldPath, newPath))
3 changes: 2 additions & 1 deletion lib/move-dialog.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Dialog = require './dialog'

module.exports =
class MoveDialog extends Dialog
constructor: (@initialPath) ->
constructor: (@initialPath, {@onMove}) ->
if fs.isDirectorySync(@initialPath)
prompt = 'Enter the new path for the directory.'
else
Expand Down Expand Up @@ -36,6 +36,7 @@ class MoveDialog extends Dialog
try
fs.makeTreeSync(directoryPath) unless fs.existsSync(directoryPath)
fs.moveSync(@initialPath, newPath)
@onMove?(initialPath: @initialPath, newPath: newPath)
if repo = repoForPath(newPath)
repo.getPathStatus(@initialPath)
repo.getPathStatus(newPath)
Expand Down
57 changes: 46 additions & 11 deletions lib/tree-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ path = require 'path'
{shell} = require 'electron'

_ = require 'underscore-plus'
{BufferedProcess, CompositeDisposable} = require 'atom'
{repoForPath, getStyleObject, getFullExtension} = require "./helpers"
{BufferedProcess, CompositeDisposable, Emitter} = require 'atom'
{repoForPath, getStyleObject, getFullExtension, updateEditorsForPath} = require "./helpers"
fs = require 'fs-plus'

AddDialog = require './add-dialog'
Expand Down Expand Up @@ -41,6 +41,7 @@ class TreeView
@element.appendChild(@resizeHandle)

@disposables = new CompositeDisposable
@emitter = new Emitter
@focusAfterAttach = false
@roots = []
@scrollLeftAfterAttach = -1
Expand Down Expand Up @@ -73,6 +74,14 @@ class TreeView
@element.style.width = "#{state.width}px" if state.width > 0
@attach() if state.attached

@disposables.add @onEntryMoved ({initialPath, newPath}) ->
updateEditorsForPath(initialPath, newPath)

@disposables.add @onEntryDeleted ({path}) ->
for editor in atom.workspace.getTextEditors()
if editor?.getPath()?.startsWith(path)
editor.destroy()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not destroy them if they have unsaved changes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this code path didn't change (it was only moved). I'll double check again but I was pretty sure that it left the editor open with unsaved changes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you're right - it definitely does get destroyed. Sounds like a bug to me! I'll check for another issue and if one doesn't exist, create one. Thanks.


serialize: ->
directoryExpansionStates: new ((roots) ->
@[root.directory.path] = root.directory.serializeExpansionState() for root in roots
Expand All @@ -90,6 +99,21 @@ class TreeView
@rootDragAndDrop.dispose()
@detach() if @panel?

onDirectoryCreated: (callback) ->
@emitter.on('directory-created', callback)

onEntryCopied: (callback) ->
@emitter.on('entry-copied', callback)

onEntryDeleted: (callback) ->
@emitter.on('entry-deleted', callback)

onEntryMoved: (callback) ->
@emitter.on('entry-moved', callback)

onFileCreated: (callback) ->
@emitter.on('file-created', callback)

handleEvents: ->
@resizeHandle.addEventListener 'dblclick', => @resizeToFitContent()
@resizeHandle.addEventListener 'mousedown', (e) => @resizeStarted(e)
Expand Down Expand Up @@ -493,7 +517,9 @@ class TreeView
oldPath = @getActivePath()

if oldPath
dialog = new MoveDialog(oldPath)
dialog = new MoveDialog oldPath,
onMove: ({initialPath, newPath}) =>
@emitter.emit 'entry-moved', {initialPath, newPath}
dialog.attach()

# Get the outline of a system call to the current platform's file manager.
Expand Down Expand Up @@ -580,7 +606,9 @@ class TreeView
oldPath = @getActivePath()
return unless oldPath

dialog = new CopyDialog(oldPath)
dialog = new CopyDialog oldPath,
onCopy: ({initialPath, newPath}) =>
@emitter.emit 'entry-copied', {initialPath, newPath}
dialog.attach()

removeSelectedEntries: ->
Expand All @@ -606,9 +634,7 @@ class TreeView
failedDeletions = []
for selectedPath in selectedPaths
if shell.moveItemToTrash(selectedPath)
for editor in atom.workspace.getTextEditors()
if editor?.getPath() is selectedPath
editor.destroy()
@emitter.emit 'entry-deleted', {path: selectedPath}
else
failedDeletions.push "#{selectedPath}"
if repo = repoForPath(selectedPath)
Expand Down Expand Up @@ -698,15 +724,21 @@ class TreeView

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)
@emitter.emit 'entry-copied', {initialPath, 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))
@emitter.emit 'entry-copied', {initialPath, newPath}
else if cutPaths
# Only move the target if the cut target doesn't exists and if the newPath
# Only move the target if the cut target doesn't exist 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)
@emitter.emit 'entry-moved', {initialPath, newPath}

add: (isCreatingFile) ->
selectedEntry = @selectedEntry() ? @roots[0]
Expand All @@ -717,9 +749,11 @@ class TreeView
@entryForPath(createdPath)?.reload()
@selectEntryForPath(createdPath)
@updateRoots() if atom.config.get('tree-view.squashDirectoryNames')
@emitter.emit 'directory-created', {path: createdPath}
dialog.onDidCreateFile (createdPath) =>
atom.workspace.open(createdPath)
@updateRoots() if atom.config.get('tree-view.squashDirectoryNames')
@emitter.emit 'file-created', {path: createdPath}
dialog.attach()

removeProjectFolder: (e) ->
Expand Down Expand Up @@ -795,6 +829,7 @@ class TreeView
try
fs.makeTreeSync(newDirectoryPath) unless fs.existsSync(newDirectoryPath)
fs.moveSync(initialPath, newPath)
@emitter.emit 'entry-moved', {initialPath, newPath}

if repo = repoForPath(newPath)
repo.getPathStatus(initialPath)
Expand Down
Loading