fuzzy find files using content under cursor

This commit is contained in:
Derek Greentree
2013-01-17 15:36:22 -08:00
parent 2b9c768273
commit b229dd21f4
5 changed files with 84 additions and 5 deletions

View File

@@ -1,3 +1,4 @@
'body':
'meta-t': 'fuzzy-finder:toggle-file-finder'
'meta-b': 'fuzzy-finder:toggle-buffer-finder'
'ctrl-.': 'fuzzy-finder:find-under-cursor'

View File

@@ -270,6 +270,40 @@ describe 'FuzzyFinder', ->
runs ->
expect(finder.list.find("li:contains(tree-view.js)")).not.toExist()
describe "fuzzy find by content under cursor", ->
beforeEach ->
rootView.attachToDom()
spyOn(rootView.project, "getFilePaths").andCallThrough()
it "opens the fuzzy finder window when there are multiple matches", ->
cursor = rootView.getActiveEditor().getCursor()
spyOn(cursor, "getCurrentWord").andReturn("sample")
rootView.trigger 'fuzzy-finder:find-under-cursor'
waitsFor ->
finder.list.children('li').length > 0
runs ->
expect(rootView.find('.fuzzy-finder')).toExist()
expect(rootView.find('.fuzzy-finder input:focus')).toExist()
it "opens a file directly when there is a single match", ->
cursor = rootView.getActiveEditor().getCursor()
spyOn(cursor, "getCurrentWord").andReturn("sample.txt")
openedPath = null
spyOn(rootView, "open").andCallFake (path) ->
openedPath = path
rootView.trigger 'fuzzy-finder:find-under-cursor'
waitsFor ->
openedPath != null
runs ->
expect(rootView.find('.fuzzy-finder')).not.toExist()
expect(openedPath).toBe "sample.txt"
describe "opening a path into a split", ->
beforeEach ->
rootView.attachToDom()

View File

@@ -6,10 +6,13 @@ fs = require 'fs'
module.exports =
class FuzzyFinder extends SelectList
filenameRegex: /([^\w\.\-\/\\])/
@activate: (rootView) ->
@instance = new FuzzyFinder(rootView)
rootView.command 'fuzzy-finder:toggle-file-finder', => @instance.toggleFileFinder()
rootView.command 'fuzzy-finder:toggle-buffer-finder', => @instance.toggleBufferFinder()
rootView.command 'fuzzy-finder:find-under-cursor', => @instance.findUnderCursor()
@viewClass: ->
[super, 'fuzzy-finder'].join(' ')
@@ -93,9 +96,32 @@ class FuzzyFinder extends SelectList
@populateOpenBufferPaths()
@attach() if @paths?.length
populateProjectPaths: ->
findUnderCursor: ->
if @hasParent()
@cancel()
else
return unless @rootView.project.getPath()?
@allowActiveEditorChange = false
theWord = @rootView.getActiveEditor()
.getCursor().getCurrentWord(wordRegex: @filenameRegex)
if theWord?
@populateProjectPaths(filter: theWord, done: (paths) =>
if paths?.length == 1
@rootView.open(paths[0])
else
@attach() if paths?.length
@miniEditor.setText(theWord))
populateProjectPaths: (options = {}) ->
if @projectPaths?.length > 0
@setArray(@projectPaths)
listedItems =
if options.filter?
@projectPaths.filter (path) ->
return path.indexOf(options.filter) >= 0
else
@projectPaths
@setArray(listedItems)
options.done(listedItems) if options.done?
else
@setLoading("Indexing...")
@@ -111,7 +137,16 @@ class FuzzyFinder extends SelectList
return true
@reloadProjectPaths = false
@setArray(@projectPaths)
listedItems =
if options.filter?
@projectPaths.filter (path) ->
return path.indexOf(options.filter) >= 0
else
@projectPaths
@setArray(listedItems)
debugger
options.done(listedItems) if options.done?
populateOpenBufferPaths: ->
@paths = @rootView.getOpenBufferPaths().map (path) =>