]> git.r.bdr.sh - rbdr/dotfiles/blame - atom/packages/vim-mode/lib/operators/general-operators.coffee
Remove vim-unimpaired plugin
[rbdr/dotfiles] / atom / packages / vim-mode / lib / operators / general-operators.coffee
CommitLineData
24c7594d
BB
1_ = require 'underscore-plus'
2{Point, Range} = require 'atom'
3{ViewModel} = require '../view-models/view-model'
4Utils = require '../utils'
5settings = require '../settings'
6
7class OperatorError
8 constructor: (@message) ->
9 @name = 'Operator Error'
10
11class Operator
12 vimState: null
13 motion: null
14 complete: null
24c7594d 15
455f099b 16 constructor: (@editor, @vimState) ->
24c7594d
BB
17 @complete = false
18
19 # Public: Determines when the command can be executed.
20 #
21 # Returns true if ready to execute and false otherwise.
22 isComplete: -> @complete
23
24 # Public: Determines if this command should be recorded in the command
25 # history for repeats.
26 #
27 # Returns true if this command should be recorded.
28 isRecordable: -> true
29
30 # Public: Marks this as ready to execute and saves the motion.
31 #
32 # motion - The motion used to select what to operate on.
33 #
34 # Returns nothing.
35 compose: (motion) ->
36 if not motion.select
37 throw new OperatorError('Must compose with a motion')
38
39 @motion = motion
40 @complete = true
41
42 canComposeWith: (operation) -> operation.select?
43
44 # Public: Preps text and sets the text register
45 #
46 # Returns nothing
47 setTextRegister: (register, text) ->
48 if @motion?.isLinewise?()
49 type = 'linewise'
50 if text[-1..] isnt '\n'
51 text += '\n'
52 else
53 type = Utils.copyType(text)
54 @vimState.setRegister(register, {text, type}) unless text is ''
55
56# Public: Generic class for an operator that requires extra input
57class OperatorWithInput extends Operator
58 constructor: (@editor, @vimState) ->
59 @editor = @editor
60 @complete = false
61
62 canComposeWith: (operation) -> operation.characters? or operation.select?
63
64 compose: (operation) ->
65 if operation.select?
66 @motion = operation
67 if operation.characters?
68 @input = operation
69 @complete = true
70
71#
72# It deletes everything selected by the following motion.
73#
74class Delete extends Operator
75 register: null
24c7594d 76
455f099b 77 constructor: (@editor, @vimState) ->
24c7594d 78 @complete = false
24c7594d
BB
79 @register = settings.defaultRegister()
80
81 # Public: Deletes the text selected by the given motion.
82 #
83 # count - The number of times to execute.
84 #
85 # Returns nothing.
86 execute: (count) ->
455f099b 87 if _.contains(@motion.select(count), true)
24c7594d 88 @setTextRegister(@register, @editor.getSelectedText())
455f099b
BB
89 @editor.transact =>
90 for selection in @editor.getSelections()
91 selection.deleteSelectedText()
24c7594d
BB
92 for cursor in @editor.getCursors()
93 if @motion.isLinewise?()
94 cursor.skipLeadingWhitespace()
95 else
96 cursor.moveLeft() if cursor.isAtEndOfLine() and not cursor.isAtBeginningOfLine()
97
455f099b 98 @vimState.activateNormalMode()
24c7594d
BB
99
100#
101# It toggles the case of everything selected by the following motion
102#
103class ToggleCase extends Operator
455f099b 104 constructor: (@editor, @vimState, {@complete}={}) ->
24c7594d 105
455f099b 106 execute: (count) ->
24c7594d 107 if @motion?
455f099b 108 if _.contains(@motion.select(count), true)
24c7594d
BB
109 @editor.replaceSelectedText {}, (text) ->
110 text.split('').map((char) ->
111 lower = char.toLowerCase()
112 if char is lower
113 char.toUpperCase()
114 else
115 lower
116 ).join('')
117 else
118 @editor.transact =>
119 for cursor in @editor.getCursors()
120 point = cursor.getBufferPosition()
121 lineLength = @editor.lineTextForBufferRow(point.row).length
455f099b 122 cursorCount = Math.min(count ? 1, lineLength - point.column)
24c7594d
BB
123
124 _.times cursorCount, =>
125 point = cursor.getBufferPosition()
126 range = Range.fromPointWithDelta(point, 0, 1)
127 char = @editor.getTextInBufferRange(range)
128
129 if char is char.toLowerCase()
130 @editor.setTextInBufferRange(range, char.toUpperCase())
131 else
132 @editor.setTextInBufferRange(range, char.toLowerCase())
133
134 cursor.moveRight() unless point.column >= lineLength - 1
135
455f099b 136 @vimState.activateNormalMode()
24c7594d
BB
137
138#
139# In visual mode or after `g` with a motion, it makes the selection uppercase
140#
141class UpperCase extends Operator
455f099b 142 constructor: (@editor, @vimState) ->
24c7594d
BB
143 @complete = false
144
455f099b
BB
145 execute: (count) ->
146 if _.contains(@motion.select(count), true)
24c7594d
BB
147 @editor.replaceSelectedText {}, (text) ->
148 text.toUpperCase()
149
455f099b 150 @vimState.activateNormalMode()
24c7594d
BB
151
152#
153# In visual mode or after `g` with a motion, it makes the selection lowercase
154#
155class LowerCase extends Operator
455f099b 156 constructor: (@editor, @vimState) ->
24c7594d
BB
157 @complete = false
158
455f099b
BB
159 execute: (count) ->
160 if _.contains(@motion.select(count), true)
24c7594d
BB
161 @editor.replaceSelectedText {}, (text) ->
162 text.toLowerCase()
163
455f099b 164 @vimState.activateNormalMode()
24c7594d
BB
165
166#
167# It copies everything selected by the following motion.
168#
169class Yank extends Operator
170 register: null
171
455f099b 172 constructor: (@editor, @vimState) ->
24c7594d
BB
173 @register = settings.defaultRegister()
174
175 # Public: Copies the text selected by the given motion.
176 #
177 # count - The number of times to execute.
178 #
179 # Returns nothing.
180 execute: (count) ->
455f099b
BB
181 oldTop = @editor.getScrollTop()
182 oldLeft = @editor.getScrollLeft()
183 oldLastCursorPosition = @editor.getCursorBufferPosition()
184
24c7594d
BB
185 originalPositions = @editor.getCursorBufferPositions()
186 if _.contains(@motion.select(count), true)
187 text = @editor.getSelectedText()
188 startPositions = _.pluck(@editor.getSelectedBufferRanges(), "start")
189 newPositions = for originalPosition, i in originalPositions
455f099b
BB
190 if startPositions[i]
191 position = Point.min(startPositions[i], originalPositions[i])
192 if @vimState.mode isnt 'visual' and @motion.isLinewise?()
193 position = new Point(position.row, originalPositions[i].column)
194 position
24c7594d
BB
195 else
196 originalPosition
197 else
198 text = ''
199 newPositions = originalPositions
200
201 @setTextRegister(@register, text)
202
203 @editor.setSelectedBufferRanges(newPositions.map (p) -> new Range(p, p))
455f099b
BB
204
205 if oldLastCursorPosition.isEqual(@editor.getCursorBufferPosition())
206 @editor.setScrollLeft(oldLeft)
207 @editor.setScrollTop(oldTop)
208
209 @vimState.activateNormalMode()
24c7594d
BB
210
211#
212# It combines the current line with the following line.
213#
214class Join extends Operator
455f099b 215 constructor: (@editor, @vimState) -> @complete = true
24c7594d
BB
216
217 # Public: Combines the current with the following lines
218 #
219 # count - The number of times to execute.
220 #
221 # Returns nothing.
222 execute: (count=1) ->
223 @editor.transact =>
224 _.times count, =>
225 @editor.joinLines()
455f099b 226 @vimState.activateNormalMode()
24c7594d
BB
227
228#
229# Repeat the last operation
230#
231class Repeat extends Operator
455f099b 232 constructor: (@editor, @vimState) -> @complete = true
24c7594d
BB
233
234 isRecordable: -> false
235
236 execute: (count=1) ->
237 @editor.transact =>
238 _.times count, =>
239 cmd = @vimState.history[0]
240 cmd?.execute()
241#
242# It creates a mark at the current cursor position
243#
244class Mark extends OperatorWithInput
455f099b 245 constructor: (@editor, @vimState) ->
24c7594d
BB
246 super(@editor, @vimState)
247 @viewModel = new ViewModel(this, class: 'mark', singleChar: true, hidden: true)
248
249 # Public: Creates the mark in the specified mark register (from user input)
250 # at the current position
251 #
252 # Returns nothing.
253 execute: ->
254 @vimState.setMark(@input.characters, @editor.getCursorBufferPosition())
455f099b 255 @vimState.activateNormalMode()
24c7594d
BB
256
257module.exports = {
258 Operator, OperatorWithInput, OperatorError, Delete, ToggleCase,
259 UpperCase, LowerCase, Yank, Join, Repeat, Mark
260}