-
Notifications
You must be signed in to change notification settings - Fork 8
/
move.go
474 lines (431 loc) · 10.7 KB
/
move.go
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Copyright 2013 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package commands
import (
"fmt"
"strings"
"github.com/limetext/backend"
"github.com/limetext/text"
"github.com/limetext/util"
)
type (
// Move Command moves the current selection.
Move struct {
backend.DefaultCommand
// Specifies the type of "move" operation.
By MoveByType
// Whether the current selection should be extended or not.
Extend bool
// Whether to move forward or backwards.
Forward bool
// Used together with By=Stops, extends "word_separators" defined by settings.
Separators string
// Used together with By=Stops, go to word begin.
WordBegin bool
// Used together with By=Stops, go to word end.
WordEnd bool
// Used together with By=Stops, go to punctuation begin.
PunctBegin bool
// Used together with By=Stops, go to punctuation end.
PunctEnd bool
// Used together with By=Stops, go to an empty line.
EmptyLine bool
// Used together with By=Stops, TODO: ???
ClipToLine bool
}
// MoveByType Specifies the type of "move" operation.
MoveByType int
// MoveToType Specifies the type of "move_to" operation to perform.
MoveToType int
// MoveTo Command moves or extends the current selection to the specified location.
MoveTo struct {
backend.DefaultCommand
// The type of "move_to" operation to perform.
To MoveToType
// Whether the current selection should be extended or not.
Extend bool
}
// ScrollLines Command moves the viewpoint "Amount" lines from the current location.
ScrollLines struct {
backend.BypassUndoCommand
// The number of lines to scroll (positive or negative direction).
Amount int
}
)
const (
// BOL is Beginning of line.
BOL MoveToType = iota
// EOL is End of line
EOL
//BOF is Beginning of file.
BOF
// EOF is End of file.
EOF
// Brackets >-> Current level close bracket.
Brackets
)
const (
// Characters by Characters.
Characters MoveByType = iota
// Stops will move by Stops (TODO(.): what exactly is a stop?).
Stops
// Lines will move by Lines.
Lines
// Words will move by Words.
Words
// WordEnds will move by Word Ends.
WordEnds
// SubWords will move by Sub Words.
SubWords
// SubWordEnds will Move by Sub Word Ends.
SubWordEnds
// Pages will move by Page.
Pages
)
func moveAction(v *backend.View, extend, scroll bool, transform func(r text.Region) int) {
fe := backend.GetEditor().Frontend()
sel := v.Sel()
rs := sel.Regions()
bs := v.Size()
for i := range rs {
row1, _ := v.RowCol(rs[i].B)
rs[i].B = transform(rs[i])
if rs[i].B < 0 {
rs[i].B = 0
} else if rs[i].B > bs {
// Yes > the size, and not size-1 because the cursor being at "size"
// is the position it will be at when we are appending
// to the buffer.
rs[i].B = bs
}
row2, _ := v.RowCol(rs[i].B)
if scroll && row2 != row1 && !fe.VisibleRegion(v).Contains(rs[i].B) {
scrollLine(v, row1-row2)
}
if !extend {
rs[i].A = rs[i].B
}
}
sel.Clear()
sel.AddAll(rs)
}
// Set will define the type of move
func (mt *MoveToType) Set(v interface{}) error {
switch to := v.(string); to {
case "eol":
*mt = EOL
case "bol":
*mt = BOL
case "bof":
*mt = BOF
case "eof":
*mt = EOF
case "brackets":
*mt = Brackets
default:
return fmt.Errorf("move_to: Unimplemented 'to' type: %s", to)
}
return nil
}
// Run executes the MoveTo command.
func (c *MoveTo) Run(v *backend.View, e *backend.Edit) error {
switch c.To {
case EOL:
moveAction(v, c.Extend, true, func(r text.Region) int {
line := v.Line(r.B)
return line.B
})
case BOL:
moveAction(v, c.Extend, true, func(r text.Region) int {
line := v.Line(r.B)
return line.A
})
case BOF:
moveAction(v, c.Extend, true, func(r text.Region) int {
return 0
})
case EOF:
moveAction(v, c.Extend, true, func(r text.Region) int {
return v.Size()
})
case Brackets:
moveAction(v, c.Extend, true, func(r text.Region) (pos int) {
var (
of int
co = 1
str, br, rv string
opening = "([{"
closing = ")]}"
)
pos = r.B
// next and before character
n := v.Substr(text.Region{r.B, r.B + 1})
b := v.Substr(text.Region{r.B, r.B - 1})
if strings.ContainsAny(n, opening) {
// TODO: Maybe it's better to use sth like view.FindByClass or even
// view.FindByClass() function itself instead of getting whole text
// and looping through it. With using view.FindByClass() function
// backward we won't need to reverse the text anymore
str = v.Substr(text.Region{r.B + 1, v.Size()})
br = n
rv = revert(n)
of = 2
} else if strings.ContainsAny(b, closing) {
// TODO: same as above
str = v.Substr(text.Region{0, r.B - 1})
br = b
rv = revert(b)
str = reverse(str)
co = -1
of = -2
} else if strings.ContainsAny(n, closing) {
// TODO: same as above
str = v.Substr(text.Region{0, r.B - 1})
br = n
rv = revert(n)
str = reverse(str)
co = -1
of = -1
} else {
// TODO: same as above
str = v.Substr(text.Region{r.B, v.Size()})
bef := v.Substr(text.Region{0, r.B})
if p := strings.LastIndexAny(bef, opening); p == -1 {
return
} else {
br = string(bef[p])
rv = revert(br)
}
}
count := 1
for i, c := range str {
if ch := string(c); ch == br {
count++
} else if ch == rv {
count--
}
if count == 0 {
return i*co + r.B + of
}
}
return
})
default:
return fmt.Errorf("move_to: Unimplemented 'to' action: %d", c.To)
}
return nil
}
// Set the type of move.
func (m *MoveByType) Set(v interface{}) error {
switch by := v.(string); by {
case "lines":
*m = Lines
case "characters":
*m = Characters
case "stops":
*m = Stops
case "words":
*m = Words
case "word_ends":
*m = WordEnds
case "subwords":
*m = SubWords
case "subword_ends":
*m = SubWordEnds
case "pages":
*m = Pages
default:
return fmt.Errorf("move: Unimplemented 'by' action: %s", by)
}
return nil
}
// Run executes the Move command.
func (c *Move) Run(v *backend.View, e *backend.Edit) error {
p := util.Prof.Enter("move.run.action")
defer p.Exit()
switch c.By {
case Characters:
moveAction(v, c.Extend, true, func(r text.Region) int {
dir := 1
if !c.Forward {
dir = -1
}
return r.B + dir
})
case Stops:
moveAction(v, c.Extend, true, func(in text.Region) int {
tmp := v.Settings().String("word_separators", backend.DEFAULT_SEPARATORS)
defer v.Settings().Set("word_separators", tmp)
v.Settings().Set("word_separators", c.Separators)
classes := 0
if c.WordBegin {
classes |= backend.CLASS_WORD_START
}
if c.WordEnd {
classes |= backend.CLASS_WORD_END
}
if c.PunctBegin {
classes |= backend.CLASS_PUNCTUATION_START
}
if c.PunctEnd {
classes |= backend.CLASS_PUNCTUATION_END
}
if c.EmptyLine {
classes |= backend.CLASS_EMPTY_LINE
}
return v.FindByClass(in.B, c.Forward, classes)
})
case Lines:
moveAction(v, c.Extend, true, func(in text.Region) int {
r, col := v.RowCol(in.B)
fromLine := v.Line(v.TextPoint(r, 0))
if !c.Forward {
r--
} else {
r++
}
// the line we are moving to
toLine := v.Line(v.TextPoint(r, 0))
col = findCol(v, in, fromLine, toLine, col)
return v.TextPoint(r, col)
})
case Words:
moveAction(v, c.Extend, true, func(in text.Region) int {
return v.FindByClass(in.B, c.Forward, backend.CLASS_WORD_START|
backend.CLASS_LINE_END|backend.CLASS_LINE_START)
})
case WordEnds:
moveAction(v, c.Extend, true, func(in text.Region) int {
return v.FindByClass(in.B, c.Forward, backend.CLASS_WORD_END|
backend.CLASS_LINE_END|backend.CLASS_LINE_START)
})
case SubWords:
moveAction(v, c.Extend, true, func(in text.Region) int {
return v.FindByClass(in.B, c.Forward, backend.CLASS_SUB_WORD_START|
backend.CLASS_WORD_START|backend.CLASS_PUNCTUATION_START|
backend.CLASS_LINE_END|backend.CLASS_LINE_START)
})
case SubWordEnds:
moveAction(v, c.Extend, true, func(in text.Region) int {
return v.FindByClass(in.B, c.Forward, backend.CLASS_SUB_WORD_END|
backend.CLASS_WORD_END|backend.CLASS_PUNCTUATION_END|
backend.CLASS_LINE_END|backend.CLASS_LINE_START)
})
case Pages:
fe := backend.GetEditor().Frontend()
vr := fe.VisibleRegion(v)
ls := v.Lines(vr)
l := len(ls)
moveAction(v, c.Extend, false, func(in text.Region) int {
row, col := v.RowCol(in.B)
fromLine := v.Line(v.TextPoint(row, 0))
dir := -1
if c.Forward {
dir = 1
}
row += dir * (l - 1)
toLine := v.Line(v.TextPoint(row, 0))
col = findCol(v, in, fromLine, toLine, col)
return v.TextPoint(row, col)
})
dir := -1
if c.Forward {
dir = 1
}
row, _ := v.RowCol(ls[0].Begin())
row += dir * (l - 1)
if row < 0 {
row = 0
}
if end, _ := v.RowCol(v.Size()); row+l-1 > end {
row = end - l + 1
}
end := row + l - 1
a, b := v.TextPoint(row, 0), v.TextPoint(end, 0)
r := v.Line(b)
b = r.End()
fe.Show(v, text.Region{a, b})
}
return nil
}
// If there is tabs in the fromLine, buffer counts them as 1 character but we
// need to count them as tab_size from settings and find the column we should
// move to in toLine based on that
func findCol(v *backend.View, in, fromLine, toLine text.Region, col int) int {
size := v.Settings().Int("tab_size", 4)
fromTabs := strings.Count(v.Substr(text.Region{fromLine.Begin(), in.B}), "\t")
col += fromTabs * (size - 1)
tab := strings.Repeat("\t", size)
toText := strings.Replace(v.Substr(toLine), "\t", tab, -1)
if col > len(toText) {
col = len(toText)
}
toText = toText[:col]
toTabs := strings.Count(toText, "\t")
col -= (size - 1) * (toTabs / size)
mod := toTabs % size
col -= mod
if mod > size/2 {
col += 1
}
if col > toLine.Size() {
col = toLine.Size()
}
return col
}
// Default returns the default seprators.
func (c *Move) Default(key string) interface{} {
if key == "separators" {
return backend.DEFAULT_SEPARATORS
}
return nil
}
func revert(c string) string {
switch c {
case "(":
return ")"
case ")":
return "("
case "[":
return "]"
case "]":
return "["
case "{":
return "}"
case "}":
return "{"
}
return ""
}
func reverse(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
func (c *ScrollLines) Run(v *backend.View, e *backend.Edit) error {
scrollLine(v, c.Amount)
return nil
}
func scrollLine(v *backend.View, amount int) {
fe := backend.GetEditor().Frontend()
vr := fe.VisibleRegion(v)
r1, _ := v.RowCol(vr.Begin())
r2, _ := v.RowCol(vr.End())
r1 -= amount
r2 -= amount
a := v.TextPoint(r1, 0)
b := v.TextPoint(r2, 0)
r := v.Line(b)
b = r.End()
fe.Show(v, text.Region{a, b})
}
func init() {
register([]backend.Command{
&Move{},
&MoveTo{},
&ScrollLines{},
})
}