Skip to content

Commit

Permalink
Fix #13: Fix Windows path handling.
Browse files Browse the repository at this point in the history
Also removes support for creating multiple files at once; refactoring
the code to support Windows paths created some bugs that involved fixing
the multiple-file code, which I didn't think we needed anyway.
  • Loading branch information
Michael Kelly committed Aug 17, 2015
1 parent 0dac298 commit 51e18e6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 44 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
0.9.0 (Pending)
===============

* Fix handling of Windows paths by replace all forward-slash instances
with path.sep, and detecting the root directory for a path better.
* Removed support for creating multiple paths at once, as it doesn't
really fit with what advanced-open-file is about and made the code
harder to understand.


0.8.2 (August 3 2015)
=====================

Expand Down
82 changes: 38 additions & 44 deletions lib/advanced-open-file-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ blockEvent = (ev) ->
ev.stopPropagation()


# Find filesystem root for the given path by calling path.dirname
# until it returns the same value as its input.
getRoot = (inputPath) ->
lastPath = null
while inputPath != lastPath
lastPath = inputPath
inputPath = path.dirname(inputPath)
return inputPath

isRoot = (inputPath) ->
return path.dirname(inputPath) is inputPath


class DirectoryListView extends ScrollView
@content: ->
@ul class: "list-group", outlet: "directoryList"
Expand All @@ -37,8 +50,6 @@ class DirectoryListView extends ScrollView

module.exports =
class AdvancedFileView extends View
PATH_SEPARATOR: ","
FS_ROOT: if os.platform == "win32" then process.cwd().split(path.sep)[0] else "/"
advancedFileView: null
keyUpListener: null

Expand Down Expand Up @@ -93,7 +104,7 @@ class AdvancedFileView extends View
selectItem: (listItem) ->
if listItem.hasClass "parent-directory"
newPath = path.dirname(@inputPath())
@updatePath newPath + (if newPath != @FS_ROOT then path.sep else "")
@updatePath newPath + path.sep
else
newPath = path.join @inputPath(), listItem.text()
if not listItem.hasClass "directory"
Expand All @@ -107,31 +118,19 @@ class AdvancedFileView extends View
atom.project.addPath(folderPath)
@detach()

# Retrieves the reference directory for the relative paths
referenceDir: () ->
atom.project.getPaths()[0] or osenv.home()
"/"

# Resolves the path being inputted in the dialog, up to the last slash
inputPath: () ->
input = @getLastSearchedFile()
path.join @referenceDir(), input.substr(0, input.lastIndexOf(path.sep))

inputFullPath: () ->
input = @getLastSearchedFile()
path.join @referenceDir(), input

getLastSearchedFile: () ->
input = @miniEditor.getText()
commonIndex = input.lastIndexOf(@PATH_SEPARATOR) + 1
input.substring(commonIndex, input.length)
if input.endsWith(path.sep)
return input
else
return path.dirname(input)


# Returns the list of directories matching the current input (path and autocomplete fragment)
getFileList: (callback) ->
input = @getLastSearchedFile()
input = @miniEditor.getText()
fs.stat @inputPath(), (err, stat) =>

if err?.code is "ENOENT"
return []

Expand Down Expand Up @@ -178,7 +177,7 @@ class AdvancedFileView extends View
longestPrefix = @longestCommonPrefix((file.name for file in files))
newPath = path.join(@inputPath(), longestPrefix)

if (newPath.length > @inputFullPath().length)
if (newPath.length > @inputPath().length)
@updatePath(newPath)
else
atom.beep()
Expand All @@ -187,7 +186,7 @@ class AdvancedFileView extends View

updatePath: (newPath, oldPath=null) ->
@pathHistory.push oldPath or @miniEditor.getText()
@miniEditor.setText newPath
@miniEditor.setText path.normalize(newPath)

update: ->
if @detaching
Expand All @@ -196,7 +195,7 @@ class AdvancedFileView extends View
if atom.config.get "advanced-open-file.helmDirSwitch"
text = @miniEditor.getText()
if text.endsWith path.sep + path.sep
@updatePath @FS_ROOT, text[...-1]
@updatePath getRoot(text), text[...-1]
else if text.endsWith path.sep + "~" + path.sep
try # Make sure ~ doesn't exist in the current directory.
fs.statSync @inputPath()
Expand All @@ -206,7 +205,7 @@ class AdvancedFileView extends View
@getFileList (files) ->
@renderAutocompleteList files

if /\/$/.test @miniEditor.getText()
if @miniEditor.getText().endsWith(path.sep)
@setMessage "file-directory-create"
else
@setMessage "file-add"
Expand All @@ -228,8 +227,8 @@ class AdvancedFileView extends View
isProjectFolder = inputPath in atom.project.getPaths()
showOpenAsProjectFolder = not withinProjectFolder and not isProjectFolder

input = @getLastSearchedFile()
@directoryListView.renderFiles files, input and input != @FS_ROOT, showOpenAsProjectFolder
input = @inputPath()
@directoryListView.renderFiles files, input and not isRoot(input), showOpenAsProjectFolder

confirm: (pathToConfirm) ->
inputPath = pathToConfirm or @miniEditor.getText()
Expand All @@ -240,23 +239,18 @@ class AdvancedFileView extends View
else
atom.beep()
else
relativePaths = inputPath.split(@PATH_SEPARATOR)

for relativePath in relativePaths
pathToCreate = path.join(@referenceDir(), relativePath)
createWithin = path.dirname(pathToCreate)
try
if /\/$/.test(pathToCreate)
mkdirp pathToCreate
else
if atom.config.get "advanced-open-file.createFileInstantly"
mkdirp createWithin unless fs.existsSync(createWithin) and fs.statSync(createWithin)
touch pathToCreate
atom.workspace.open pathToCreate
catch error
@setMessage "alert", error.message

@detach()
createWithin = path.dirname(inputPath)
try
if inputPath.endsWith(path.sep)
mkdirp inputPath
else
if atom.config.get "advanced-open-file.createFileInstantly"
mkdirp createWithin unless fs.existsSync(createWithin) and fs.statSync(createWithin)
touch inputPath
atom.workspace.open inputPath
@detach()
catch error
@setMessage "alert", error.message

detach: ->
$("html").off("click", @outsideClickHandler) unless not @outsideClickHandler
Expand Down Expand Up @@ -337,7 +331,7 @@ class AdvancedFileView extends View
if ev.keyCode is 9
ev.preventDefault()
ev.stopPropagation()
pathToComplete = @getLastSearchedFile()
pathToComplete = @inputPath()
@autocomplete pathToComplete

@miniEditor.focus()
Expand Down

0 comments on commit 51e18e6

Please sign in to comment.