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

@@ -288,6 +288,7 @@ class Buffer
matchesInCharacterRange: (regex, startIndex, endIndex) ->
text = @getText()
matches = []
regex.lastIndex = startIndex

View File

@@ -150,7 +150,7 @@ class Cursor
previousLinesRange = [[previousNonBlankRow, 0], currentBufferPosition]
beginningOfWordPosition = currentBufferPosition
@editSession.backwardsScanInRange @wordRegex, previousLinesRange, (match, matchRange, { stop }) =>
@editSession.backwardsScanInRange (options.wordRegex || @wordRegex), previousLinesRange, (match, matchRange, { stop }) =>
if matchRange.end.isGreaterThanOrEqual(currentBufferPosition) or allowPrevious
beginningOfWordPosition = matchRange.start
stop()
@@ -162,7 +162,7 @@ class Cursor
range = [currentBufferPosition, @editSession.getEofBufferPosition()]
endOfWordPosition = null
@editSession.scanInRange @wordRegex, range, (match, matchRange, { stop }) =>
@editSession.scanInRange (options.wordRegex || @wordRegex), range, (match, matchRange, { stop }) =>
endOfWordPosition = matchRange.end
if not allowNext and matchRange.start.isGreaterThan(currentBufferPosition)
endOfWordPosition = currentBufferPosition
@@ -195,6 +195,14 @@ class Cursor
getCurrentWordPrefix: ->
@editSession.getTextInBufferRange([@getBeginningOfCurrentWordBufferPosition(), @getBufferPosition()])
getCurrentWord: (options = {}) ->
match = @editSession.getTextInBufferRange([@getBeginningOfCurrentWordBufferPosition(options),
@getEndOfCurrentWordBufferPosition(options)])
return match if options.includeDelimiter?
if match?.length > 2
match.substring(1, match.length-1)
isAtBeginningOfLine: ->
@getBufferPosition().column == 0

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) =>