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

Commit

Permalink
Merge pull request #1049 from atom/mkt-events-and-editor-updates
Browse files Browse the repository at this point in the history
Update editors when containing directory is renamed or removed (redux)
  • Loading branch information
Michelle Tilley authored Mar 27, 2017
2 parents d90a0c2 + b5d228d commit 9993b8f
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 46 deletions.
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()

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

0 comments on commit 9993b8f

Please sign in to comment.