forked from limetext/lime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.go
384 lines (360 loc) · 8.45 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
// 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"
. "github.com/limetext/lime/backend"
"github.com/limetext/lime/backend/util"
"github.com/limetext/text"
"strings"
)
const (
// Beginning of line
BOL MoveToType = iota
// End of line
EOL
// Beginning of file
BOF
// End of file
EOF
// Current level close bracket
Brackets
)
const (
// Move by Characters
Characters MoveByType = iota
// Move by Stops (TODO(.): what exactly is a stop?)
Stops
// Move by Lines
Lines
// Move by Words
Words
// Move by Word Ends
WordEnds
// Move by Sub Words
SubWords
// Move by Sub Word Ends
SubWordEnds
// Move by Page
Pages
)
type (
// The MoveCommand moves the current selection
MoveCommand struct {
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
}
// Specifies the type of "move" operation
MoveByType int
// Specifies the type of "move_to" operation to perform
MoveToType int
// The MoveToCommand moves or extends the current selection to the specified location
MoveToCommand struct {
DefaultCommand
// The type of "move_to" operation to perform
To MoveToType
// Whether the current selection should be extended or not
Extend bool
}
// The ScrollLinesCommand moves the viewpoint "Amount" lines from the current location
ScrollLinesCommand struct {
BypassUndoCommand
// The number of lines to scroll (positive or negative direction)
Amount int
}
)
func move_action(v *View, extend bool, transform func(r text.Region) int) {
sel := v.Sel()
r := sel.Regions()
bs := v.Buffer().Size()
for i := range r {
r[i].B = transform(r[i])
if r[i].B < 0 {
r[i].B = 0
} else if r[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
r[i].B = bs
}
if !extend {
r[i].A = r[i].B
}
}
sel.Clear()
sel.AddAll(r)
}
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
}
func (c *MoveToCommand) Run(v *View, e *Edit) error {
switch c.To {
case EOL:
move_action(v, c.Extend, func(r text.Region) int {
line := v.Buffer().Line(r.B)
return line.B
})
case BOL:
move_action(v, c.Extend, func(r text.Region) int {
line := v.Buffer().Line(r.B)
return line.A
})
case BOF:
move_action(v, c.Extend, func(r text.Region) int {
return 0
})
case EOF:
move_action(v, c.Extend, func(r text.Region) int {
return v.Buffer().Size()
})
case Brackets:
move_action(v, c.Extend, 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.Buffer().Substr(text.Region{r.B, r.B + 1})
b := v.Buffer().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.Buffer().Substr(text.Region{r.B + 1, v.Buffer().Size()})
br = n
rv = revert(n)
of = 2
} else if strings.ContainsAny(b, closing) {
// TODO: same as above
str = v.Buffer().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.Buffer().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.Buffer().Substr(text.Region{r.B, v.Buffer().Size()})
bef := v.Buffer().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
}
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
}
func (c *MoveCommand) Run(v *View, e *Edit) error {
p := util.Prof.Enter("move.run.init")
p.Exit()
p = util.Prof.Enter("move.run.action")
defer p.Exit()
switch c.By {
case Characters:
dir := 1
if !c.Forward {
dir = -1
}
move_action(v, c.Extend, func(r text.Region) int {
return r.B + dir
})
case Stops:
move_action(v, c.Extend, func(in text.Region) int {
tmp := v.Settings().Get("word_separators", DEFAULT_SEPARATORS).(string)
defer v.Settings().Set("word_separators", tmp)
v.Settings().Set("word_separators", c.Separators)
classes := 0
if c.WordBegin {
classes |= CLASS_WORD_START
}
if c.WordEnd {
classes |= CLASS_WORD_END
}
if c.PunctBegin {
classes |= CLASS_PUNCTUATION_START
}
if c.PunctEnd {
classes |= CLASS_PUNCTUATION_END
}
if c.EmptyLine {
classes |= CLASS_EMPTY_LINE
}
return v.FindByClass(in.B, c.Forward, classes)
})
case Lines:
move_action(v, c.Extend, func(in text.Region) int {
r, col := v.Buffer().RowCol(in.B)
_ = r
if !c.Forward {
r--
} else {
r++
}
return v.Buffer().TextPoint(r, col)
})
case Words:
move_action(v, c.Extend, func(in text.Region) int {
return v.FindByClass(in.B, c.Forward, CLASS_WORD_START|
CLASS_LINE_END|CLASS_LINE_START)
})
case WordEnds:
move_action(v, c.Extend, func(in text.Region) int {
return v.FindByClass(in.B, c.Forward, CLASS_WORD_END|
CLASS_LINE_END|CLASS_LINE_START)
})
case SubWords:
move_action(v, c.Extend, func(in text.Region) int {
return v.FindByClass(in.B, c.Forward, CLASS_SUB_WORD_START|
CLASS_WORD_START|CLASS_PUNCTUATION_START|CLASS_LINE_END|
CLASS_LINE_START)
})
case SubWordEnds:
move_action(v, c.Extend, func(in text.Region) int {
return v.FindByClass(in.B, c.Forward, CLASS_SUB_WORD_END|
CLASS_WORD_END|CLASS_PUNCTUATION_END|CLASS_LINE_END|
CLASS_LINE_START)
})
case Pages:
// TODO: Should know how many lines does the frontend show in one page
}
return nil
}
func (c *MoveCommand) Default(key string) interface{} {
if key == "separators" {
return 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 *ScrollLinesCommand) Run(v *View, e *Edit) error {
fe := GetEditor().Frontend()
vr := fe.VisibleRegion(v)
var r int
if c.Amount >= 0 {
r, _ = v.Buffer().RowCol(vr.Begin())
r -= c.Amount
} else {
r, _ = v.Buffer().RowCol(vr.End() - 1)
r -= c.Amount
}
r = v.Buffer().TextPoint(r, 0)
fe.Show(v, text.Region{A: r, B: r})
return nil
}
func init() {
register([]Command{
&MoveCommand{},
&MoveToCommand{},
&ScrollLinesCommand{},
})
}