1 {Operator} = require './general-operators'
2 {Range} = require 'atom'
3 settings = require '../settings'
6 # It increases or decreases the next number on the line
8 class Increase extends Operator
14 @numberRegex = new RegExp(settings.numberRegex())
19 for cursor in @editor.getCursors()
20 if @increaseNumber(count, cursor) then increased = true
21 atom.beep() unless increased
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)
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
34 cursor.setBufferPosition numEnd
35 numStart = cursor.getBeginningOfCurrentWordBufferPosition(wordRegex: @numberRegex, allowPrevious: false)
37 range = new Range(numStart, numEnd)
39 # parse number, increase/decrease
40 number = parseInt(@editor.getTextInBufferRange(range), 10)
42 cursor.setBufferPosition(cursorPosition)
47 # replace current number with new
48 newValue = String(number)
49 @editor.setTextInBufferRange(range, newValue, normalizeLineEndings: false)
51 cursor.setBufferPosition(row: numStart.row, column: numStart.column-1+newValue.length)
54 class Decrease extends Increase
57 module.exports = {Increase, Decrease}