From f9d36ed12fc9ef2b5357e6ccd2f3664e12ad94f4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C5=82awomir=20Laskowski?= <laskowski.box@gmail.com>
Date: Fri, 6 Nov 2015 03:56:53 +0100
Subject: [PATCH] Current opened files on top of list

---
 lib/project-view.coffee       | 31 +++++++++++--------------------
 spec/fuzzy-finder-spec.coffee |  4 +++-
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/lib/project-view.coffee b/lib/project-view.coffee
index 16c7009a..a79376c9 100644
--- a/lib/project-view.coffee
+++ b/lib/project-view.coffee
@@ -87,32 +87,23 @@ class ProjectView extends FuzzyFinderView
           @loadingBadge.text(humanize.intComma(pathsFound))
 
   projectRelativePathsForFilePaths: ->
-    projectRelativePaths = super
+    @getLastOpenedPaths().concat super
 
-    if lastOpenedPath = @getLastOpenedPath()
-      for {filePath}, index in projectRelativePaths
-        if filePath is lastOpenedPath
-          [entry] = projectRelativePaths.splice(index, 1)
-          projectRelativePaths.unshift(entry)
-          break
-
-    projectRelativePaths
-
-  getLastOpenedPath: ->
+  getLastOpenedPaths: ->
     activePath = atom.workspace.getActivePaneItem()?.getPath?()
+    editors = atom.workspace.getTextEditors()
 
-    lastOpenedEditor = null
+    recentEditors = editors.filter (editor) -> activePath isnt editor.getPath()
 
-    for editor in atom.workspace.getTextEditors()
-      filePath = editor.getPath()
-      continue unless filePath
-      continue if activePath is filePath
+    recentEditors.sort (editorA, editorB) ->
+      editorB.lastOpened - editorA.lastOpened
 
-      lastOpenedEditor ?= editor
-      if editor.lastOpened > lastOpenedEditor.lastOpened
-        lastOpenedEditor = editor
+    paths = recentEditors.map (editor) ->
+      filePath = editor.getPath()
+      [rootPath, projectRelativePath] = atom.project.relativizePath(filePath)
+      {filePath, projectRelativePath}
 
-    lastOpenedEditor?.getPath()
+    paths
 
   destroy: ->
     @loadPathsTask?.terminate()
diff --git a/spec/fuzzy-finder-spec.coffee b/spec/fuzzy-finder-spec.coffee
index 4e0464d2..0cb40c51 100644
--- a/spec/fuzzy-finder-spec.coffee
+++ b/spec/fuzzy-finder-spec.coffee
@@ -130,6 +130,7 @@ describe 'FuzzyFinder', ->
           expect(PathLoader.startTask.callCount).toBe 1
 
         it "puts the last active path first", ->
+          waitsForPromise -> atom.workspace.open 'dir/a'
           waitsForPromise -> atom.workspace.open 'sample.txt'
           waitsForPromise -> atom.workspace.open 'sample.js'
 
@@ -139,7 +140,8 @@ describe 'FuzzyFinder', ->
 
           runs ->
             expect(projectView.list.find("li:eq(0)").text()).toContain('sample.txt')
-            expect(projectView.list.find("li:eq(1)").text()).toContain('sample.html')
+            expect(projectView.list.find("li:eq(1)").text()).toContain('dir/a')
+            expect(projectView.list.find("li:eq(2)").text()).toContain('sample.html')
 
         describe "symlinks on #darwin or #linux", ->
           [junkDirPath, junkFilePath] = []