]> git.r.bdr.sh - rbdr/dotfiles/blame - atom/packages/vim-mode/lib/operators/increase-operators.coffee
Update vim plugins
[rbdr/dotfiles] / atom / packages / vim-mode / lib / operators / increase-operators.coffee
CommitLineData
24c7594d
BB
1{Operator} = require './general-operators'
2{Range} = require 'atom'
3settings = require '../settings'
4
5#
6# It increases or decreases the next number on the line
7#
8class Increase extends Operator
9 step: 1
10
11 constructor: ->
12 super
13 @complete = true
14 @numberRegex = new RegExp(settings.numberRegex())
15
16 execute: (count=1) ->
17 @editor.transact =>
18 increased = false
19 for cursor in @editor.getCursors()
20 if @increaseNumber(count, cursor) then increased = true
21 atom.beep() unless increased
22
23 increaseNumber: (count, cursor) ->
24 # find position of current number, adapted from from SearchCurrentWord
25 cursorPosition = cursor.getBufferPosition()
26 numEnd = cursor.getEndOfCurrentWordBufferPosition(wordRegex: @numberRegex, allowNext: false)
27
28 if numEnd.column is cursorPosition.column
29 # either we don't have a current number, or it ends on cursor, i.e. precedes it, so look for the next one
30 numEnd = cursor.getEndOfCurrentWordBufferPosition(wordRegex: @numberRegex, allowNext: true)
31 return if numEnd.row isnt cursorPosition.row # don't look beyond the current line
32 return if numEnd.column is cursorPosition.column # no number after cursor
33
34 cursor.setBufferPosition numEnd
35 numStart = cursor.getBeginningOfCurrentWordBufferPosition(wordRegex: @numberRegex, allowPrevious: false)
36
37 range = new Range(numStart, numEnd)
38
39 # parse number, increase/decrease
40 number = parseInt(@editor.getTextInBufferRange(range), 10)
41 if isNaN(number)
42 cursor.setBufferPosition(cursorPosition)
43 return
44
45 number += @step*count
46
47 # replace current number with new
48 newValue = String(number)
49 @editor.setTextInBufferRange(range, newValue, normalizeLineEndings: false)
50
51 cursor.setBufferPosition(row: numStart.row, column: numStart.column-1+newValue.length)
52 return true
53
54class Decrease extends Increase
55 step: -1
56
57module.exports = {Increase, Decrease}