From 334ba6ba82fa1431bbba95a69e0bf2a1aad83a02 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 26 Jul 2012 14:02:10 -0600 Subject: [PATCH] Ctrl-meta-1 reveals the active file in the tree view --- spec/extensions/tree-view-spec.coffee | 28 +++++++++++++++++++++++ src/extensions/tree-view/keymap.coffee | 1 + src/extensions/tree-view/tree-view.coffee | 25 ++++++++++++++++++-- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/spec/extensions/tree-view-spec.coffee b/spec/extensions/tree-view-spec.coffee index 07bd4413b..724620158 100644 --- a/spec/extensions/tree-view-spec.coffee +++ b/spec/extensions/tree-view-spec.coffee @@ -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() diff --git a/src/extensions/tree-view/keymap.coffee b/src/extensions/tree-view/keymap.coffee index 9c5b1f060..b7aff8127 100644 --- a/src/extensions/tree-view/keymap.coffee +++ b/src/extensions/tree-view/keymap.coffee @@ -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' diff --git a/src/extensions/tree-view/tree-view.coffee b/src/extensions/tree-view/tree-view.coffee index 0732923b0..96df5adf7 100644 --- a/src/extensions/tree-view/tree-view.coffee +++ b/src/extensions/tree-view/tree-view.coffee @@ -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()