diff --git a/src/packages/tree-view/lib/tree-view.coffee b/src/packages/tree-view/lib/tree-view.coffee index bf1df2b2e..5a6ea480f 100644 --- a/src/packages/tree-view/lib/tree-view.coffee +++ b/src/packages/tree-view/lib/tree-view.coffee @@ -127,8 +127,10 @@ class TreeView extends ScrollView @root = null selectActiveFile: -> - activeFilePath = rootView.getActiveView()?.getPath() - @selectEntryForPath(activeFilePath) if activeFilePath + if activeFilePath = rootView.getActiveView()?.getPath?() + @selectEntryForPath(activeFilePath) + else + @deselect() revealActiveFile: -> @attach() @@ -290,9 +292,12 @@ class TreeView extends ScrollView return false unless entry.get(0) entry = entry.view() unless entry instanceof View @selectedPath = entry.getPath() - @treeViewList.find('.selected').removeClass('selected') + @deselect() entry.addClass('selected') + deselect: -> + @treeViewList.find('.selected').removeClass('selected') + scrollTop: (top) -> if top @treeViewList.scrollTop(top) diff --git a/src/packages/tree-view/spec/tree-view-spec.coffee b/src/packages/tree-view/spec/tree-view-spec.coffee index fc2f9fe81..2bd5ac3d6 100644 --- a/src/packages/tree-view/spec/tree-view-spec.coffee +++ b/src/packages/tree-view/spec/tree-view-spec.coffee @@ -1,4 +1,5 @@ $ = require 'jquery' +{$$} = require 'space-pen' _ = require 'underscore' TreeView = require 'tree-view/lib/tree-view' RootView = require 'root-view' @@ -301,18 +302,25 @@ describe "TreeView", -> expect(subdir).toHaveClass 'expanded' expect(rootView.getActiveView().isFocused).toBeFalsy() - describe "when a new file is opened in the active editor", -> - it "selects the file in the tree view if the file's entry visible", -> - sampleJs.click() - rootView.open(require.resolve('fixtures/tree-view/tree-view.txt')) + describe "when the active item changes on the active pane", -> + describe "when the item has a path", -> + it "selects the entry with that path in the tree view if it is visible", -> + sampleJs.click() + rootView.open(require.resolve('fixtures/tree-view/tree-view.txt')) - expect(sampleTxt).toHaveClass 'selected' - expect(treeView.find('.selected').length).toBe 1 + expect(sampleTxt).toHaveClass 'selected' + expect(treeView.find('.selected').length).toBe 1 - it "selects the file's parent dir if the file's entry is not visible", -> - rootView.open('dir1/sub-dir1/sub-file1') - dirView = treeView.root.find('.directory:contains(dir1)').view() - expect(dirView).toHaveClass 'selected' + it "selects the path's parent dir if its entry is not visible", -> + rootView.open('dir1/sub-dir1/sub-file1') + dirView = treeView.root.find('.directory:contains(dir1)').view() + expect(dirView).toHaveClass 'selected' + + describe "when the item has no path", -> + it "deselects the previously selected entry", -> + sampleJs.click() + rootView.getActivePane().showItem($$ -> @div('hello')) + expect(rootView.find('.selected')).not.toExist() describe "when a different editor becomes active", -> it "selects the file in that is open in that editor", ->