]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/vim-mode/lib/motions/find-motion.coffee
Adds atom
[rbdr/dotfiles] / atom / packages / vim-mode / lib / motions / find-motion.coffee
1 {MotionWithInput} = require './general-motions'
2 {ViewModel} = require '../view-models/view-model'
3 {Point, Range} = require 'atom'
4
5 class Find extends MotionWithInput
6 constructor: (@editor, @vimState) ->
7 super(@editor, @vimState)
8 @vimState.currentFind = this
9 @viewModel = new ViewModel(this, class: 'find', singleChar: true, hidden: true)
10 @backwards = false
11 @repeatReversed = false
12 @offset = 0
13 @repeated = false
14
15 match: (cursor, count) ->
16 currentPosition = cursor.getBufferPosition()
17 line = @editor.lineTextForBufferRow(currentPosition.row)
18 if @backwards
19 index = currentPosition.column
20 for i in [0..count-1]
21 return if index <= 0 # we can't move backwards any further, quick return
22 index = line.lastIndexOf(@input.characters, index-1-(@offset*@repeated))
23 if index >= 0
24 new Point(currentPosition.row, index + @offset)
25 else
26 index = currentPosition.column
27 for i in [0..count-1]
28 index = line.indexOf(@input.characters, index+1+(@offset*@repeated))
29 return if index < 0 # no match found
30 if index >= 0
31 new Point(currentPosition.row, index - @offset)
32
33 reverse: ->
34 @backwards = not @backwards
35 this
36
37 moveCursor: (cursor, count=1) ->
38 if (match = @match(cursor, count))?
39 cursor.setBufferPosition(match)
40
41 repeat: (opts={}) ->
42 opts.reverse = !!opts.reverse
43 @repeated = true
44 if opts.reverse isnt @repeatReversed
45 @reverse()
46 @repeatReversed = opts.reverse
47 this
48
49 class Till extends Find
50 constructor: (@editor, @vimState) ->
51 super(@editor, @vimState)
52 @offset = 1
53
54 module.exports = {Find, Till}