]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/vim-mode/lib/operators/put-operator.coffee
Update atoms
[rbdr/dotfiles] / atom / packages / vim-mode / lib / operators / put-operator.coffee
1 _ = require 'underscore-plus'
2 {Operator} = require './general-operators'
3 settings = require '../settings'
4
5 module.exports =
6 #
7 # It pastes everything contained within the specifed register
8 #
9 class Put extends Operator
10 register: null
11
12 constructor: (@editor, @vimState, {@location}={}) ->
13 @location ?= 'after'
14 @complete = true
15 @register = settings.defaultRegister()
16
17 # Public: Pastes the text in the given register.
18 #
19 # count - The number of times to execute.
20 #
21 # Returns nothing.
22 execute: (count=1) ->
23 {text, type} = @vimState.getRegister(@register) or {}
24 return unless text
25
26 textToInsert = _.times(count, -> text).join('')
27
28 selection = @editor.getSelectedBufferRange()
29 if selection.isEmpty()
30 # Clean up some corner cases on the last line of the file
31 if type is 'linewise'
32 textToInsert = textToInsert.replace(/\n$/, '')
33 if @location is 'after' and @onLastRow()
34 textToInsert = "\n#{textToInsert}"
35 else
36 textToInsert = "#{textToInsert}\n"
37
38 if @location is 'after'
39 if type is 'linewise'
40 if @onLastRow()
41 @editor.moveToEndOfLine()
42
43 originalPosition = @editor.getCursorScreenPosition()
44 originalPosition.row += 1
45 else
46 @editor.moveDown()
47 else
48 unless @onLastColumn()
49 @editor.moveRight()
50
51 if type is 'linewise' and not originalPosition?
52 @editor.moveToBeginningOfLine()
53 originalPosition = @editor.getCursorScreenPosition()
54
55 @editor.insertText(textToInsert)
56
57 if originalPosition?
58 @editor.setCursorScreenPosition(originalPosition)
59 @editor.moveToFirstCharacterOfLine()
60
61 if type isnt 'linewise'
62 @editor.moveLeft()
63 @vimState.activateNormalMode()
64
65 # Private: Helper to determine if the editor is currently on the last row.
66 #
67 # Returns true on the last row and false otherwise.
68 onLastRow: ->
69 {row, column} = @editor.getCursorBufferPosition()
70 row is @editor.getBuffer().getLastRow()
71
72 onLastColumn: ->
73 @editor.getLastCursor().isAtEndOfLine()