Skip to content
This repository was archived by the owner on Apr 6, 2018. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions keymaps/vim-mode.cson
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
'm': 'vim-mode:mark'
'`': 'vim-mode:move-to-mark-literal'
'\'': 'vim-mode:move-to-mark'
'g D': 'vim-mode:move-to-definition'

'f': 'vim-mode:find'
'F': 'vim-mode:find-backwards'
Expand Down
2 changes: 2 additions & 0 deletions lib/motions/index.coffee
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
Motions = require './general-motions'
{Search, SearchCurrentWord, BracketMatchingMotion, RepeatSearch} = require './search-motion'
MoveToMark = require './move-to-mark-motion'
MoveToDefinition = require './move-to-definition-motion'
{Find, Till} = require './find-motion'

Motions.Search = Search
Motions.SearchCurrentWord = SearchCurrentWord
Motions.BracketMatchingMotion = BracketMatchingMotion
Motions.RepeatSearch = RepeatSearch
Motions.MoveToMark = MoveToMark
Motions.MoveToDefinition = MoveToDefinition
Motions.Find = Find
Motions.Till = Till

Expand Down
7 changes: 7 additions & 0 deletions lib/motions/move-to-definition-motion.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{SearchCurrentWord} = require './search-motion'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe remove lines 2, 3, 5? They seem unused.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@irrationalistic ☝️ ? 😎

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry! Currently traveling, will do this asap!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Awaiting CI...


module.exports =
class MoveToDefinition extends SearchCurrentWord

# Scan using a fake cursor that reports its position at the top of the doc
scan: (cursor) -> super {getBufferPosition: -> [0, 0]}, true
11 changes: 8 additions & 3 deletions lib/motions/search-motion.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class SearchBase extends MotionWithInput
else
atom.beep()

scan: (cursor) ->
scan: (cursor, returnFirstResult = false) ->
return [] if @input.characters is ""

currentPosition = cursor.getBufferPosition()

[rangesBefore, rangesAfter] = [[], []]
@editor.scan @getSearchTerm(@input.characters), ({range}) =>
@editor.scan @getSearchTerm(@input.characters), (iteration) =>
range = iteration.range
isBefore = if @reverse
range.start.compare(currentPosition) < 0
else
Expand All @@ -43,6 +43,8 @@ class SearchBase extends MotionWithInput
else
rangesAfter.push(range)

iteration.stop() if returnFirstResult

if @reverse
rangesAfter.concat(rangesBefore).reverse()
else
Expand All @@ -60,6 +62,9 @@ class SearchBase extends MotionWithInput

modFlags = Object.keys(modifiers).join('')

# Escape the term for use in regex
term = term.replace /([$])/g, '\\$1'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would prevent the use of $ to mean the end of the line in normal searches.
I would actually suggest to override getSearchTerm in SearchCurrentWord so that it simply returns new RegExp(_.escapeRegExp(term), 'g') - or with 'gi' instead of 'g' if case insensitivity is desired for SearchCurrentWord and its kin.

try
new RegExp(term, modFlags)
catch
Expand Down
1 change: 1 addition & 0 deletions lib/vim-state.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class VimState
'repeat-search-backwards': (e) => new Motions.RepeatSearch(@editor, this).reversed()
'move-to-mark': (e) => new Motions.MoveToMark(@editor, this)
'move-to-mark-literal': (e) => new Motions.MoveToMark(@editor, this, false)
'move-to-definition': (e) => new Motions.MoveToDefinition(@editor, this)
'mark': (e) => new Operators.Mark(@editor, this)
'find': (e) => new Motions.Find(@editor, this)
'find-backwards': (e) => new Motions.Find(@editor, this).reverse()
Expand Down
40 changes: 40 additions & 0 deletions spec/motions-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,46 @@ describe "Motions", ->
normalModeInputKeydown('`')
expect(editor.getCursorBufferPosition()).toEqual [1, 5]

describe 'the gD keybindings', ->

it 'moves to the definition of a js variable', ->
editor.setText('var myVar;\nmyVar')
editor.setCursorBufferPosition([1, 1])
keydown('g')
keydown('D', shift: true)
expect(editor.getCursorBufferPosition()).toEqual [0, 4]

it 'moves to the definition of a php variable', ->
# a user may choose to include `$` as part of names:
atom.config.set('vim-mode.iskeyword', '[$@a-zA-Z0-9_\-]+')

editor.setText('$myVar = 20;\n$myVar')
editor.setCursorBufferPosition([1, 1])
keydown('g')
keydown('D', shift: true)
expect(editor.getCursorBufferPosition()).toEqual [0, 0]

it 'moves to the definition of a class', ->
editor.setText('class myClass {}\nmyClass')
editor.setCursorBufferPosition([1, 1])
keydown('g')
keydown('D', shift: true)
expect(editor.getCursorBufferPosition()).toEqual [0, 6]

it 'moves to the definition of a global variable', ->
editor.setText('bar = 10\nbar')
editor.setCursorBufferPosition([1, 1])
keydown('g')
keydown('D', shift: true)
expect(editor.getCursorBufferPosition()).toEqual [0, 0]

it 'moves to the definition of a variable with many keywords', ->
editor.setText('public static int bar = 10\nbar')
editor.setCursorBufferPosition([1, 1])
keydown('g')
keydown('D', shift: true)
expect(editor.getCursorBufferPosition()).toEqual [0, 18]

describe 'the f/F keybindings', ->
beforeEach ->
editor.setText("abcabcabcabc\n")
Expand Down