mirror of
https://github.com/atom/atom.git
synced 2026-01-22 21:38:10 -05:00
Support opening fuzzy finder to a specific line
Adding a line number after a colon such as ':8' to the end of the fuzzy filter input field will navigate to line 8 of the opened editor.
This commit is contained in:
@@ -79,10 +79,13 @@ class SelectList extends View
|
||||
@loading.text(message)
|
||||
@loadingArea.show()
|
||||
|
||||
getFilterQuery: ->
|
||||
@miniEditor.getText()
|
||||
|
||||
populateList: ->
|
||||
return unless @array?
|
||||
|
||||
filterQuery = @miniEditor.getText()
|
||||
filterQuery = @getFilterQuery()
|
||||
if filterQuery.length
|
||||
filteredArray = fuzzyFilter(@array, filterQuery, key: @filterKey)
|
||||
else
|
||||
|
||||
@@ -5,6 +5,7 @@ $ = require 'jquery'
|
||||
humanize = require 'humanize-plus'
|
||||
fsUtils = require 'fs-utils'
|
||||
LoadPathsTask = require './load-paths-task'
|
||||
Point = require 'point'
|
||||
|
||||
module.exports =
|
||||
class FuzzyFinderView extends SelectList
|
||||
@@ -64,22 +65,39 @@ class FuzzyFinderView extends SelectList
|
||||
@div fsUtils.base(path), class: "primary-line file #{typeClass}"
|
||||
@div projectRelativePath, class: 'secondary-line path'
|
||||
|
||||
openPath: (path) ->
|
||||
rootView.open(path, {@allowActiveEditorChange}) if path
|
||||
openPath: (path, lineNumber) ->
|
||||
return unless path
|
||||
|
||||
rootView.open(path, {@allowActiveEditorChange})
|
||||
@moveToLine(lineNumber)
|
||||
|
||||
moveToLine: (lineNumber=-1) ->
|
||||
return unless lineNumber >= 0
|
||||
|
||||
if editor = rootView.getActiveView()
|
||||
position = new Point(lineNumber)
|
||||
editor.scrollToBufferPosition(position, center: true)
|
||||
editor.setCursorBufferPosition(position)
|
||||
editor.moveCursorToFirstCharacterOfLine()
|
||||
|
||||
splitOpenPath: (fn) ->
|
||||
{path} = @getSelectedElement()
|
||||
return unless path
|
||||
|
||||
lineNumber = @getLineNumber()
|
||||
if pane = rootView.getActivePane()
|
||||
fn(pane, project.open(path))
|
||||
@moveToLine(lineNumber)
|
||||
else
|
||||
@openPath(path)
|
||||
@openPath(path, lineNumber)
|
||||
|
||||
confirmed : ({path}) ->
|
||||
return unless path.length
|
||||
|
||||
if fsUtils.isFile(path)
|
||||
lineNumber = @getLineNumber()
|
||||
@cancel()
|
||||
@openPath(path)
|
||||
@openPath(path, lineNumber)
|
||||
else
|
||||
@setError('Selected path does not exist')
|
||||
setTimeout((=> @setError()), 2000)
|
||||
@@ -133,6 +151,22 @@ class FuzzyFinderView extends SelectList
|
||||
@attach()
|
||||
@miniEditor.setText(currentWord)
|
||||
|
||||
getFilterQuery: ->
|
||||
query = super
|
||||
colon = query.indexOf(':')
|
||||
if colon is -1
|
||||
query
|
||||
else
|
||||
query[0...colon]
|
||||
|
||||
getLineNumber: ->
|
||||
query = @miniEditor.getText()
|
||||
colon = query.indexOf(':')
|
||||
if colon is -1
|
||||
-1
|
||||
else
|
||||
parseInt(query[colon+1..]) - 1
|
||||
|
||||
setArray: (paths) ->
|
||||
projectRelativePaths = paths.map (path) ->
|
||||
projectRelativePath = project.relativize(path)
|
||||
|
||||
@@ -528,3 +528,25 @@ describe 'FuzzyFinder', ->
|
||||
rootView.trigger 'fuzzy-finder:toggle-buffer-finder'
|
||||
expect(finderView.find('.status.new').length).toBe 1
|
||||
expect(finderView.find('.status.new').closest('li').find('.file').text()).toBe 'newsample.js'
|
||||
|
||||
describe "when the filter text contains a colon followed by a number", ->
|
||||
it "opens the selected path to that line number", ->
|
||||
rootView.attachToDom()
|
||||
expect(rootView.find('.fuzzy-finder')).not.toExist()
|
||||
[editor] = rootView.getEditors()
|
||||
expect(editor.getCursorBufferPosition()).toEqual [0, 0]
|
||||
|
||||
rootView.trigger 'fuzzy-finder:toggle-buffer-finder'
|
||||
expect(rootView.find('.fuzzy-finder')).toExist()
|
||||
finderView.miniEditor.insertText(':4')
|
||||
finderView.trigger 'core:confirm'
|
||||
|
||||
expect(editor.getCursorBufferPosition()).toEqual [3, 4]
|
||||
|
||||
rootView.trigger 'fuzzy-finder:toggle-buffer-finder'
|
||||
expect(rootView.find('.fuzzy-finder')).toExist()
|
||||
finderView.miniEditor.insertText(':10')
|
||||
finderView.miniEditor.trigger 'pane:split-left'
|
||||
|
||||
expect(rootView.getActiveView()).not.toBe editor
|
||||
expect(rootView.getActiveView().getCursorBufferPosition()).toEqual [9, 2]
|
||||
|
||||
Reference in New Issue
Block a user