]> git.r.bdr.sh - rbdr/dotfiles/blame - atom/packages/vim-mode/lib/operators/replace-operator.coffee
Adds atom
[rbdr/dotfiles] / atom / packages / vim-mode / lib / operators / replace-operator.coffee
CommitLineData
24c7594d
BB
1_ = require 'underscore-plus'
2{OperatorWithInput} = require './general-operators'
3{ViewModel} = require '../view-models/view-model'
4{Range} = require 'atom'
5
6module.exports =
7class Replace extends OperatorWithInput
8 constructor: (@editor, @vimState, {@selectOptions}={}) ->
9 super(@editor, @vimState)
10 @viewModel = new ViewModel(this, class: 'replace', hidden: true, singleChar: true, defaultText: '\n')
11
12 execute: (count=1) ->
13 if @input.characters is ""
14 # replace canceled
15
16 if @vimState.mode is "visual"
17 @vimState.resetVisualMode()
18 else
19 @vimState.activateCommandMode()
20
21 return
22
23 @editor.transact =>
24 if @motion?
25 if _.contains(@motion.select(), true)
26 @editor.replaceSelectedText null, (text) =>
27 text.replace(/./g, @input.characters)
28 for selection in @editor.getSelections()
29 point = selection.getBufferRange().start
30 selection.setBufferRange(Range.fromPointWithDelta(point, 0, 0))
31 else
32 for cursor in @editor.getCursors()
33 pos = cursor.getBufferPosition()
34 currentRowLength = @editor.lineTextForBufferRow(pos.row).length
35 continue unless currentRowLength - pos.column >= count
36
37 _.times count, =>
38 point = cursor.getBufferPosition()
39 @editor.setTextInBufferRange(Range.fromPointWithDelta(point, 0, 1), @input.characters)
40 cursor.moveRight()
41 cursor.setBufferPosition(pos)
42
43 # Special case: when replaced with a newline move to the start of the
44 # next row.
45 if @input.characters is "\n"
46 _.times count, =>
47 @editor.moveDown()
48 @editor.moveToFirstCharacterOfLine()
49
50 @vimState.activateCommandMode()