From 2eb7d2a215bb0a846d6f229b5c500cc0f90aa75d Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Thu, 29 Dec 2011 12:23:04 -0800 Subject: [PATCH] FileFinder loads files from editor.url's directory --- spec/atom/editor-spec.coffee | 9 ++------- spec/atom/file-finder-spec.coffee | 10 ++++++++-- spec/atom/root-view-spec.coffee | 32 ++++++++++++++++++++----------- src/atom/editor.coffee | 5 ++--- src/atom/file-finder.coffee | 2 ++ src/atom/root-view.coffee | 9 +++++++-- src/atom/window.coffee | 1 + 7 files changed, 43 insertions(+), 25 deletions(-) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index ed0118669..5e03e9f72 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -20,9 +20,8 @@ describe "Editor", -> editor.destroy() describe "initialize", -> - it "opens the given url", -> - Editor.build(url: tempFilePath) - expect(Editor.prototype.viewProperties.open).toHaveBeenCalledWith(tempFilePath) + it "has a buffer", -> + expect(editor.buffer).toBeDefined() describe 'destroy', -> it 'destroys the ace editor', -> @@ -46,10 +45,6 @@ describe "Editor", -> editor.open('something.text') expect(editor.getAceSession().getMode().name).toBe 'text' - it "assigns the url on the $atomController global", -> - editor.open("/other/path") - expect($atomController.url.toString()).toEqual("/other/path") - describe "when called with null", -> it "loads an empty buffer with no url", -> editor.open() diff --git a/spec/atom/file-finder-spec.coffee b/spec/atom/file-finder-spec.coffee index 4478920dd..9de19f2ae 100644 --- a/spec/atom/file-finder-spec.coffee +++ b/spec/atom/file-finder-spec.coffee @@ -2,15 +2,18 @@ FileFinder = require 'file-finder' describe 'FileFinder', -> finder = null + urls = null beforeEach -> urls = ['app.coffee', 'buffer.coffee', 'atom/app.coffee', 'atom/buffer.coffee'] finder = FileFinder.build {urls} + describe "initialize", -> + it "populates the ol with all urls", -> + expect(finder.urlList.children('li').length).toBe urls.length + describe "when characters are typed into the input element", -> it "displays matching urls in the ol element", -> - expect(finder.urlList.find('li')).not.toExist() - finder.input.val('ap') finder.input.keyup() @@ -26,6 +29,9 @@ describe 'FileFinder', -> expect(finder.urlList.find('li:contains(atom/app.coffee)').length).toBe 1 describe "findMatches(queryString)", -> + it "returns all urls if queryString is empty", -> + expect(finder.findMatches('')).toEqual urls + it "returns urls sorted by score of match against the given query", -> expect(finder.findMatches('ap')).toEqual ["app.coffee", "atom/app.coffee"] expect(finder.findMatches('a/ap')).toEqual ["atom/app.coffee"] diff --git a/spec/atom/root-view-spec.coffee b/spec/atom/root-view-spec.coffee index f7bae6c47..48f963fce 100644 --- a/spec/atom/root-view-spec.coffee +++ b/spec/atom/root-view-spec.coffee @@ -11,17 +11,27 @@ describe "RootView", -> rootView.addPane $('
') expect(rootView.vertical.children().length).toBe 2 - xdescribe "toggleFileFinder", -> - it "shows the FileFinder when it is not on screen and hides it when it is", -> - #expect(rootView.find('.file-finder')).not.toExist() - # rootView.toggleFileFinder() - # expect(rootView.find('.file-finder')).toExist() - # rootView.toggleFileFinder() - # expect(rootView.find('.file-finder')).not.toExist() + describe "toggleFileFinder", -> + describe "when the editor has a url", -> + beforeEach -> + rootView.editor.open require.resolve('window.coffee') - it "shows urls for all files in the same directory as editor.url", -> - rootView.editor.open require.resolve('window.coffee') - rootView.toggleFileFinder() - expect(rootView.fileFinder.urlList.length).toBeGreaterThan 1 + it "shows the FileFinder when it is not on screen and hides it when it is", -> + expect(rootView.find('.file-finder')).not.toExist() + rootView.toggleFileFinder() + expect(rootView.find('.file-finder')).toExist() + rootView.toggleFileFinder() + expect(rootView.find('.file-finder')).not.toExist() + + it "shows urls for all files in the same directory as editor.url", -> + rootView.toggleFileFinder() + expect(rootView.fileFinder.urlList.children('li').length).toBeGreaterThan 1 + + describe "when the editor has no url", -> + it "does not open the FileFinder", -> + expect(rootView.editor.buffer.url).toBeUndefined() + expect(rootView.find('.file-finder')).not.toExist() + rootView.toggleFileFinder() + expect(rootView.find('.file-finder')).not.toExist() diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index 71b43ab1a..7d9a7a7dd 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -14,9 +14,9 @@ class Editor extends Template buffer: null editorElement: null - initialize: ({url}) -> + initialize: () -> @buildAceEditor() - @open(url) + @open() shutdown: -> @destroy() @@ -25,7 +25,6 @@ class Editor extends Template @aceEditor.destroy() open: (url) -> - $atomController.url = url @buffer = new Buffer(url) session = new EditSession(@buffer.aceDocument, @buffer.getMode()) @aceEditor.setSession(session) diff --git a/src/atom/file-finder.coffee b/src/atom/file-finder.coffee index 1de7c5108..ae2997df1 100644 --- a/src/atom/file-finder.coffee +++ b/src/atom/file-finder.coffee @@ -14,6 +14,7 @@ class FileFinder extends Template urls: null initialize: ({@urls}) -> + @populateUrlList() populateUrlList: -> @urlList.empty() @@ -21,6 +22,7 @@ class FileFinder extends Template @urlList.append $("
  • #{url}
  • ") findMatches: (query) -> + return @urls unless query scoredUrls = ({url, score: stringScore(url, query)} for url in @urls) sortedUrls = scoredUrls.sort (a, b) -> a.score > b.score urlAndScore.url for urlAndScore in sortedUrls when urlAndScore.score > 0 diff --git a/src/atom/root-view.coffee b/src/atom/root-view.coffee index 503897d09..e5b4c6997 100644 --- a/src/atom/root-view.coffee +++ b/src/atom/root-view.coffee @@ -1,4 +1,5 @@ $ = require 'jquery' +fs = require 'fs' Editor = require 'editor' FileFinder = require 'file-finder' @@ -16,7 +17,7 @@ class RootView extends Template @div id: 'app-horizontal', => @div id: 'app-vertical', outlet: 'vertical', => @div id: 'main', outlet: 'main', => - @subview 'editor', Editor.build(url: $atomController.url?.toString()) + @subview 'editor', Editor.build() viewProperties: addPane: (view) -> @@ -25,10 +26,14 @@ class RootView extends Template @main.after(pane) toggleFileFinder: -> + return unless @editor.buffer.url + if @fileFinder @fileFinder.remove() @fileFinder = null else - @fileFinder = FileFinder.build(urls: [@editor.buffer.url]) + directory = fs.directory @editor.buffer.url + urls = fs.list directory + @fileFinder = FileFinder.build({urls}) @addPane(@fileFinder) @fileFinder.input.focus() diff --git a/src/atom/window.coffee b/src/atom/window.coffee index 6ad594a36..0ab8dbf08 100644 --- a/src/atom/window.coffee +++ b/src/atom/window.coffee @@ -16,6 +16,7 @@ windowAdditions = @keyBindings = {} @menuItemActions = {} @rootView = RootView.attach() + @rootView.editor.open $atomController.url?.toString() @registerEventHandlers() @bindKeys() @bindMenuItems()