Ctrl-meta-1 reveals the active file in the tree view

This commit is contained in:
Nathan Sobo
2012-07-26 14:02:10 -06:00
parent 8b743b90a2
commit 334ba6ba82
3 changed files with 52 additions and 2 deletions

View File

@@ -139,6 +139,34 @@ describe "TreeView", ->
expect(treeView.hasParent()).toBeTruthy()
expect(treeView).toMatchSelector(':focus')
describe "when tree-view:reveal-current-file is triggered on the root view", ->
beforeEach ->
treeView.detach()
spyOn(treeView, 'focus')
describe "if the current file has a path", ->
it "shows and focuses the tree view and selects the file", ->
rootView.open('dir/a')
rootView.trigger 'tree-view:reveal-active-file'
expect(treeView.hasParent()).toBeTruthy()
expect(treeView.focus).toHaveBeenCalled()
expect(treeView.selectedEntry().getPath()).toMatch /dir\/a$/
describe "if the current file has no path", ->
it "shows and focuses the tree view, but does not attempt to select a specific file", ->
rootView.open()
expect(rootView.getActiveEditSession().getPath()).toBeUndefined()
rootView.trigger 'tree-view:reveal-active-file'
expect(treeView.hasParent()).toBeTruthy()
expect(treeView.focus).toHaveBeenCalled()
describe "if there is no editor open", ->
it "shows and focuses the tree view, but does not attempt to select a specific file", ->
expect(rootView.getActiveEditSession()).toBeUndefined()
rootView.trigger 'tree-view:reveal-active-file'
expect(treeView.hasParent()).toBeTruthy()
expect(treeView.focus).toHaveBeenCalled()
describe "when tree-view:unfocus is triggered on the tree view", ->
it "surrenders focus to the root view but remains open", ->
rootView.attachToDom()

View File

@@ -1,5 +1,6 @@
window.keymap.bindKeys '#root-view'
'ctrl-1': 'tree-view:toggle'
'ctrl-meta-1': 'tree-view:reveal-active-file'
window.keymap.bindKeys '.tree-view'
'escape': 'tree-view:unfocus'

View File

@@ -54,6 +54,7 @@ class TreeView extends View
@on 'tree-view:directory-modified', => @selectActiveFile()
@on 'tree-view:unfocus', => @rootView.focus()
@rootView.on 'tree-view:toggle', => @toggle()
@rootView.on 'tree-view:reveal-active-file', => @revealActiveFile()
@rootView.on 'active-editor-path-change', => @selectActiveFile()
@rootView.project.on 'path-change', => @updateRoot()
@@ -105,7 +106,24 @@ class TreeView extends View
activeFilePath = @rootView.getActiveEditor()?.getPath()
@selectEntryForPath(activeFilePath)
selectEntryForPath: (path) ->
revealActiveFile: ->
@attach()
@focus()
return unless activeFilePath = @rootView.getActiveEditor()?.getPath()
project = @rootView.project
activePathComponents = project.relativize(activeFilePath).split('/')
currentPath = project.getPath().replace(/\/$/, '')
for pathComponent in activePathComponents
currentPath += '/' + pathComponent
entry = @entryForPath(currentPath)
if entry.hasClass('directory')
entry.expand()
else
@selectEntry(entry)
entryForPath: (path) ->
fn = (bestMatchEntry, element) ->
entry = $(element).view()
regex = new RegExp("^" + _.escapeRegExp(entry.getPath()))
@@ -114,7 +132,10 @@ class TreeView extends View
else
bestMatchEntry
@selectEntry(@find(".entry").toArray().reduce(fn, @root))
@find(".entry").toArray().reduce(fn, @root)
selectEntryForPath: (path) ->
@selectEntry(@entryForPath(path))
moveDown: ->
selectedEntry = @selectedEntry()