When the editor buffer changes and the path is not visible in the TreeView, It selects the closes ancestor entry of that file.

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-05-11 15:39:36 -07:00
parent 2f86a3a6e5
commit d51b229d56
4 changed files with 58 additions and 7 deletions

View File

@@ -204,13 +204,24 @@ describe "TreeView", ->
expect(rootView.activeEditor().isFocused).toBeFalsy()
describe "when a new file is opened in the active editor", ->
it "is selected in the tree view if visible", ->
it "is selected in the tree view if the file's entry visible", ->
sampleJs.click()
rootView.open(require.resolve('fixtures/sample.txt'))
expect(sampleTxt).toHaveClass 'selected'
expect(treeView.find('.selected').length).toBe 1
it "selected a file's parent dir if the file's entry is not visible", ->
# treeView.attachToDom()
rootView.open(require.resolve('fixtures/dir/a-dir/oh-git'))
dirView = treeView.root.find('.directory:contains(dir)').view()
expect(dirView).toHaveClass 'selected'
# dirView.expand()
# aDirView = dirView.find('.directory:contains(a-dir)').view()
# expect(aDirView).toHaveClass 'selected'
describe "when a different editor becomes active", ->
it "selects the file in that is open in that editor", ->
sampleJs.click()

View File

@@ -15,7 +15,7 @@ beforeEach ->
directoriesWithSubscriptions = []
afterEach ->
$('#jasmine-content').empty()
# $('#jasmine-content').empty()
document.title = defaultTitle
ensureNoDirectorySubscriptions()

View File

@@ -104,11 +104,15 @@ class TreeView extends View
@selectEntryForPath(activeFilePath)
selectEntryForPath: (path) ->
for element in @find(".entry")
view = $(element).view()
if view.getPath() == path
@selectEntry(view)
return
fn = (bestMatchEntry, element) ->
entry = $(element).view()
regex = new RegExp("^" + _.escapeRegExp(entry.getPath()))
if regex.test(path) and entry.getPath().length > bestMatchEntry.getPath().length
entry
else
bestMatchEntry
@selectEntry(@find(".entry").toArray().reduce(fn, @root))
moveDown: ->
selectedEntry = @selectedEntry()

View File

@@ -9,3 +9,39 @@ _.mixin
sum = 0
sum += elt for elt in array
sum
escapeRegExp: (string) ->
# Referring to the table here:
# https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/regexp
# these characters should be escaped
# \ ^ $ * + ? . ( ) | { } [ ]
# These characters only have special meaning inside of brackets
# they do not need to be escaped, but they MAY be escaped
# without any adverse effects (to the best of my knowledge and casual testing)
# : ! , =
# my test "~!@#$%^&*(){}[]`/=?+\|-_;:'\",<.>".match(/[\#]/g)
specials = [
# order matters for these
"-"
"["
"]"
# order doesn't matter for any of these
"/"
"{"
"}"
"("
")"
"*"
"+"
"?"
"."
"\\"
"^"
"$"
"|"]
# I choose to escape every character with '\'
# even though only some strictly require it when inside of []
regex = RegExp('[' + specials.join('\\') + ']', 'g')
string.replace(regex, "\\$&");