diff --git a/src/packages/fuzzy-finder/lib/fuzzy-finder-view.coffee b/src/packages/fuzzy-finder/lib/fuzzy-finder-view.coffee index 8b5228571..3de3b9d49 100644 --- a/src/packages/fuzzy-finder/lib/fuzzy-finder-view.coffee +++ b/src/packages/fuzzy-finder/lib/fuzzy-finder-view.coffee @@ -22,6 +22,10 @@ class FuzzyFinderView extends SelectList @subscribe $(window), 'focus', => @reloadProjectPaths = true @observeConfig 'fuzzy-finder.ignoredNames', => @reloadProjectPaths = true + rootView.eachEditor (editor) -> + editor.activeEditSession.lastOpened = (new Date) - 1 + editor.on 'editor:active-edit-session-changed', (e, editSession, index) -> + editSession.lastOpened = (new Date) - 1 @miniEditor.command 'editor:split-left', => @splitOpenPath (editor, session) -> editor.splitLeft(session) @@ -143,10 +147,25 @@ class FuzzyFinderView extends SelectList @loadPathsTask.start() populateOpenBufferPaths: -> - @paths = rootView.getOpenBufferPaths().map (path) => - rootView.project.relativize(path) + editSessions = rootView.project.getEditSessions() + + editSessions = _.sortBy editSessions, (editSession) => + if editSession is rootView.getActiveEditSession() + 0 + else + -(editSession.lastOpened or 1) + + @paths = _.map editSessions, (editSession) => + rootView.project.relativize editSession.getPath() + @setArray(@paths) + getOpenedPaths: -> + paths = {} + for editSession in rootView.project.getEditSessions() + paths[editSession.getPath()] = editSession.lastOpened + paths + detach: -> super diff --git a/src/packages/fuzzy-finder/lib/fuzzy-finder.coffee b/src/packages/fuzzy-finder/lib/fuzzy-finder.coffee index 2bdd340a1..a117a5735 100644 --- a/src/packages/fuzzy-finder/lib/fuzzy-finder.coffee +++ b/src/packages/fuzzy-finder/lib/fuzzy-finder.coffee @@ -1,8 +1,10 @@ +_ = require 'underscore' + module.exports = projectPaths: null fuzzyFinderView: null - activate: -> + activate: (state) -> rootView.command 'fuzzy-finder:toggle-file-finder', => @createView().toggleFileFinder() rootView.command 'fuzzy-finder:toggle-buffer-finder', => @@ -15,6 +17,11 @@ module.exports = @loadPathsTask = new LoadPathsTask((paths) => @projectPaths = paths) @loadPathsTask.start() + for path, lastOpened of state + session = _.detect rootView.project.getEditSessions(), (editSession) -> + editSession.getPath() is path + session?.lastOpened = lastOpened + deactivate: -> @loadPathsTask?.terminate() @fuzzyFinderView?.cancel() @@ -22,6 +29,9 @@ module.exports = @projectPaths = null @fuzzyFinderView = null + serialize: -> + @fuzzyFinderView?.getOpenedPaths() + createView: -> unless @fuzzyFinderView FuzzyFinderView = require 'fuzzy-finder/lib/fuzzy-finder-view' diff --git a/src/packages/fuzzy-finder/spec/fuzzy-finder-spec.coffee b/src/packages/fuzzy-finder/spec/fuzzy-finder-spec.coffee index b044cdcef..cb9cc03f0 100644 --- a/src/packages/fuzzy-finder/spec/fuzzy-finder-spec.coffee +++ b/src/packages/fuzzy-finder/spec/fuzzy-finder-spec.coffee @@ -1,6 +1,7 @@ RootView = require 'root-view' FuzzyFinder = require 'fuzzy-finder/lib/fuzzy-finder-view' LoadPathsTask = require 'fuzzy-finder/lib/load-paths-task' +_ = require 'underscore' $ = require 'jquery' {$$} = require 'space-pen' fs = require 'fs' @@ -123,13 +124,43 @@ describe 'FuzzyFinder', -> rootView.trigger 'fuzzy-finder:toggle-buffer-finder' expect(finderView.miniEditor.getText()).toBe '' - it "lists the paths of the current open buffers", -> + it "lists the paths of the current open buffers by most recently modified", -> + rootView.attachToDom() + rootView.open 'sample-with-tabs.coffee' rootView.trigger 'fuzzy-finder:toggle-buffer-finder' - expect(finderView.list.children('li').length).toBe 2 + children = finderView.list.children('li') + expect(children.get(0).outerText).toBe "sample.txt" + expect(children.get(1).outerText).toBe "sample.js" + expect(children.get(2).outerText).toBe "sample-with-tabs.coffee" + + rootView.open 'sample.txt' + rootView.trigger 'fuzzy-finder:toggle-buffer-finder' + children = finderView.list.children('li') + expect(children.get(0).outerText).toBe "sample-with-tabs.coffee" + expect(children.get(1).outerText).toBe "sample.js" + expect(children.get(2).outerText).toBe "sample.txt" + + expect(finderView.list.children('li').length).toBe 3 expect(finderView.list.find("li:contains(sample.js)")).toExist() expect(finderView.list.find("li:contains(sample.txt)")).toExist() + expect(finderView.list.find("li:contains(sample-with-tabs.coffee)")).toExist() expect(finderView.list.children().first()).toHaveClass 'selected' + it "serializes the list of paths and their last opened time", -> + rootView.open 'sample-with-tabs.coffee' + rootView.trigger 'fuzzy-finder:toggle-buffer-finder' + rootView.open 'sample.js' + rootView.trigger 'fuzzy-finder:toggle-buffer-finder' + + states = rootView.serialize().packageStates + states = _.map states['fuzzy-finder'], (path, time) -> [ path, time ] + states = _.sortBy states, (path, time) -> -time + + paths = [ 'sample-with-tabs.coffee', 'sample.txt', 'sample.js' ] + for [time, path] in states + expect(_.last path.split '/').toBe paths.shift() + expect(time).toBeGreaterThan 50000 + describe "when the active editor only contains edit sessions for anonymous buffers", -> it "does not open", -> editor = rootView.getActiveEditor()