-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmulti-cursor-plus.coffee
More file actions
288 lines (237 loc) · 8.74 KB
/
Copy pathmulti-cursor-plus.coffee
File metadata and controls
288 lines (237 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
{CompositeDisposable, Point, Range} = require 'atom'
_ = require 'underscore-plus'
module.exports =
subscriptions: null
#
# Activate the package.
#
activate: ->
@subscriptions = new CompositeDisposable
# Add commands
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:mark': =>
@mark()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:move-up': =>
@moveUp()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:move-down': =>
@moveDown()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:move-left': =>
@moveLeft()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:move-right': =>
@moveRight()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:move-to-beginning-of-word': =>
@moveToBeginningOfWord()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:move-to-end-of-word': =>
@moveToEndOfWord()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:move-to-first-character-of-line': =>
@moveToFirstCharacterOfLine()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:move-to-end-of-line': =>
@moveToEndOfLine()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:move-to-top': =>
@moveToTop()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:move-to-bottom': =>
@moveToBottom()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:select-up': =>
@selectUp()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:select-down': =>
@selectDown()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:select-left': =>
@selectLeft()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:select-right': =>
@selectRight()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:select-to-beginning-of-word': =>
@selectToBeginningOfWord()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:select-to-end-of-word': =>
@selectToEndOfWord()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:select-to-first-character-of-line': =>
@selectToFirstCharacterOfLine()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:select-to-end-of-line': =>
@selectToEndOfLine()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:select-to-top': =>
@selectToTop()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:select-to-bottom': =>
@selectToBottom()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:mark-previous-like-this': =>
@markPreviousLikeThis()
@subscriptions.add atom.commands.add 'atom-text-editor',
'multi-cursor-plus:mark-next-like-this': =>
@markNextLikeThis()
markPreviousLikeThis: ->
editor = atom.workspace.getActiveTextEditor()
selections = editor?.getSelections()
bufferRange = editor?.getBuffer()?.getRange()
return unless selections?
if selections.length == 0
return
firstSelection = selections[0]
start = (selection) -> selection.getBufferRange().start
for selection in selections
if start(selection).isLessThan(start(firstSelection))
firstSelection = selection
currentText = firstSelection.getText()
return unless currentText?.length
selectionRange = firstSelection.getBufferRange()
searchRange = (new Range(bufferRange.start, selectionRange.start))
.translate(new Point(0, 0), new Point(0, -1))
editor.backwardsScanInBufferRange new RegExp(_.escapeRegExp(currentText)),
searchRange,
({match, range, stop}) =>
editor.addSelectionForBufferRange range
stop()
markNextLikeThis: ->
editor = atom.workspace.getActiveTextEditor()
selections = editor?.getSelections()
bufferRange = editor?.getBuffer()?.getRange()
return unless selections?
if selections.length == 0
return
lastSelection = selections[0]
end = (selection) -> selection.getBufferRange().end
for selection in selections
if end(lastSelection).isLessThan(end(selection))
lastSelection = selection
currentText = lastSelection.getText()
return unless currentText?.length
selectionRange = lastSelection.getBufferRange()
searchRange = (new Range(selectionRange.end, bufferRange.end))
.translate new Point(0, 1)
editor.scanInBufferRange new RegExp(_.escapeRegExp(currentText)),
searchRange,
({match, range, stop}) =>
editor.addSelectionForBufferRange range
stop()
#
# Deactivate the package.
#
deactivate: ->
@subscriptions.dispose()
#
# Get the last cursor.
#
getCursor: ->
atom.workspace.getActiveTextEditor()?.getLastCursor()
#
# Get the last selection.
#
getSelection: ->
atom.workspace.getActiveTextEditor()?.getLastSelection()
#
# Create a mark where the current cursor is (or destroy existing mark).
#
mark: ->
editor = atom.workspace.getActiveTextEditor()
lastSelection = @getSelection()
lastCursor = @getCursor()
if not editor or not lastSelection or not lastCursor
return
# Get the last selection's original range
range = lastSelection.getBufferRange()
# Get the last cursor's position
row = lastCursor.getBufferRow()
column = lastCursor.getBufferColumn()
destroyedSelection = false
beginningHasCursor = false
# Iterate over existing selections
for selection in editor.getSelections()
selectionRange = selection.getBufferRange()
if selectionRange.start.row is 0 and selectionRange.start.column is 0
# There is a cursor at the beginning of the buffer
beginningHasCursor = true
if selection is lastSelection
# Ignore current cursor
continue
else if selection.intersectsWith(lastSelection)
# Destroy the existing selection
selection.destroy()
destroyedSelection = true
# Return if one or more selections were destroyed (toggled off)
if destroyedSelection
return
# "Re-mark" the current buffer range (in order to set invalidate: 'never')
editor.markBufferRange(
[[range.start.row, range.start.column], [range.end.row, range.end.column]],
{
invalidate: 'never'
}
)
# FIXME: Cannot currently mark the beginning *and* the end of the buffer
if beginningHasCursor
# Last row and column in buffer
lastRow = editor.getLastBufferRow()
lastColumn = editor.lineTextForBufferRow(lastRow).length
# Create new cursor at the end of the buffer
# (can't be added at the current position, or it will be invalidated)
editor.addCursorAtBufferPosition([lastRow, lastColumn])
else
# Create new cursor at the beginning of the buffer
# (can't be added at the current position, or it will be invalidated)
editor.addCursorAtBufferPosition([0, 0])
# Move the new cursor to the correct position
editor.getLastSelection().cursor.setBufferPosition([row, column])
#
# Move the current cursor.
#
moveUp: ->
@getCursor()?.moveUp()
moveDown: ->
@getCursor()?.moveDown()
moveLeft: ->
@getCursor()?.moveLeft()
moveRight: ->
@getCursor()?.moveRight()
moveToBeginningOfWord: ->
@getCursor()?.moveToBeginningOfWord()
moveToEndOfWord: ->
@getCursor()?.moveToEndOfWord()
moveToFirstCharacterOfLine: ->
@getCursor()?.moveToFirstCharacterOfLine()
moveToEndOfLine: ->
@getCursor()?.moveToEndOfLine()
moveToTop: ->
@getCursor()?.moveToTop()
moveToBottom: ->
@getCursor()?.moveToBottom()
#
# Select with the current cursor.
#
selectUp: ->
@getSelection()?.selectUp()
selectDown: ->
@getSelection()?.selectDown()
selectLeft: ->
@getSelection()?.selectLeft()
selectRight: ->
@getSelection()?.selectRight()
selectToBeginningOfWord: ->
@getSelection()?.selectToBeginningOfWord()
selectToEndOfWord: ->
@getSelection()?.selectToEndOfWord()
selectToFirstCharacterOfLine: ->
@getSelection()?.selectToFirstCharacterOfLine()
selectToEndOfLine: ->
@getSelection()?.selectToEndOfLine()
selectToTop: ->
@getSelection()?.selectToTop()
selectToBottom: ->
@getSelection()?.selectToBottom()