mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Add initial support for jump to declaration
This commit is contained in:
@@ -124,3 +124,18 @@ describe "OutlineView", ->
|
||||
generator = new TagGenerator(path, callback)
|
||||
generator.generate().done ->
|
||||
expect(tags.length).toBe 0
|
||||
|
||||
describe "jump to declaration", ->
|
||||
it "doesn't move the cursor when no declaration is found", ->
|
||||
rootView.open("tagged.js")
|
||||
editor = rootView.getActiveEditor()
|
||||
editor.setCursorBufferPosition([0,12])
|
||||
editor.trigger 'outline-view:jump-to-declaration'
|
||||
expect(editor.getCursorBufferPosition()).toEqual [0,12]
|
||||
|
||||
it "moves the cursor to the declaration", ->
|
||||
rootView.open("tagged.js")
|
||||
editor = rootView.getActiveEditor()
|
||||
editor.setCursorBufferPosition([6,24])
|
||||
editor.trigger 'outline-view:jump-to-declaration'
|
||||
expect(editor.getCursorBufferPosition()).toEqual [2,0]
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
window.keymap.bindKeys '.editor'
|
||||
'meta-j': 'outline-view:toggle'
|
||||
'f3': 'outline-view:jump-to-declaration'
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
SelectList = require 'select-list'
|
||||
Editor = require 'editor'
|
||||
TagGenerator = require 'outline-view/src/tag-generator'
|
||||
TagReader = require 'outline-view/src/tag-reader'
|
||||
Point = require 'point'
|
||||
|
||||
module.exports =
|
||||
class OutlineView extends SelectList
|
||||
@@ -11,6 +13,7 @@ class OutlineView extends SelectList
|
||||
requireStylesheet 'outline-view/src/outline-view.css'
|
||||
@instance = new OutlineView(rootView)
|
||||
rootView.command 'outline-view:toggle', => @instance.toggle()
|
||||
rootView.command 'outline-view:jump-to-declaration', => @instance.jumpToDeclaration()
|
||||
|
||||
@viewClass: -> "#{super} outline-view"
|
||||
|
||||
@@ -50,6 +53,9 @@ class OutlineView extends SelectList
|
||||
|
||||
confirmed : ({position, name}) ->
|
||||
@cancel()
|
||||
@moveToPosition(position)
|
||||
|
||||
moveToPosition: (position) ->
|
||||
editor = @rootView.getActiveEditor()
|
||||
editor.scrollToBufferPosition(position, center: true)
|
||||
editor.setCursorBufferPosition(position)
|
||||
@@ -62,3 +68,18 @@ class OutlineView extends SelectList
|
||||
attach: ->
|
||||
@rootView.append(this)
|
||||
@miniEditor.focus()
|
||||
|
||||
jumpToDeclaration: ->
|
||||
editor = @rootView.getActiveEditor()
|
||||
matches = TagReader.find(editor)
|
||||
return unless matches.length is 1
|
||||
|
||||
tag = matches[0]
|
||||
return unless tag.pattern
|
||||
pattern = tag.pattern.replace(/(^^\/\^)|(\$\/$)/g, '') # Remove leading /^ and trailing $/
|
||||
if pattern and @rootView.openInExistingEditor(tag.file, true, true)
|
||||
buffer = editor.getBuffer()
|
||||
for row in [0...buffer.getLineCount()]
|
||||
continue unless pattern is buffer.lineForRow(row)
|
||||
@moveToPosition(new Point(row, 0))
|
||||
break
|
||||
|
||||
13
src/extensions/outline-view/src/tag-reader.coffee
Normal file
13
src/extensions/outline-view/src/tag-reader.coffee
Normal file
@@ -0,0 +1,13 @@
|
||||
fs = require 'fs'
|
||||
|
||||
module.exports =
|
||||
|
||||
find: (editor) ->
|
||||
word = editor.getTextInRange(editor.getCursor().getCurrentWordBufferRange())
|
||||
return [] unless word.length > 0
|
||||
|
||||
project = editor.rootView().project
|
||||
tagsFile = project.resolve("tags") or project.resolve("TAGS")
|
||||
return [] unless fs.isFile(tagsFile)
|
||||
|
||||
$tags.find(tagsFile, word) or []
|
||||
Reference in New Issue
Block a user