-
Notifications
You must be signed in to change notification settings - Fork 8
/
insdel.go
200 lines (185 loc) · 4.29 KB
/
insdel.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
// 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 (
"strings"
"github.com/limetext/backend"
"github.com/limetext/text"
)
type (
// Insert Command inserts the given characters, at all
// of the current selection locations, possibly replacing
// text if the selection area covers one or more characters.
Insert struct {
backend.DefaultCommand
// The characters to insert
Characters string
}
// LeftDelete Command deletes characters to the left of the
// current selection or the current selection if it is not empty.
LeftDelete struct {
backend.DefaultCommand
}
// RightDelete Command deletes characters to the right of the
// current selection or the current selection if it is not empty.
RightDelete struct {
backend.DefaultCommand
}
// DeleteWord Command deletes one word to right or left
// depending on forward variable.
DeleteWord struct {
backend.DefaultCommand
Forward bool
}
)
// Run executes the Insert command.
func (c *Insert) Run(v *backend.View, e *backend.Edit) error {
sel := v.Sel()
for i := 0; i < sel.Len(); i++ {
r := sel.Get(i)
if r.Size() == 0 {
v.Insert(e, r.B, c.Characters)
} else {
v.Replace(e, r, c.Characters)
}
}
return nil
}
// Run executes the LeftDelete command.
func (c *LeftDelete) Run(v *backend.View, e *backend.Edit) error {
trimSpace := false
tabSize := 4
if t := v.Settings().Bool("translate_tabs_to_spaces", false); t {
if t = v.Settings().Bool("use_tab_stops", true); t {
trimSpace = true
tabSize = v.Settings().Int("tab_size", 4)
}
}
sel := v.Sel()
hasNonEmpty := sel.HasNonEmpty()
i := 0
for {
l := sel.Len()
if i >= l {
break
}
r := sel.Get(i)
if r.A == r.B && !hasNonEmpty {
if trimSpace {
_, col := v.RowCol(r.A)
prevCol := r.A - (col - (col-tabSize+(tabSize-1))&^(tabSize-1))
if prevCol < 0 {
prevCol = 0
}
d := v.SubstrR(text.Region{A: prevCol, B: r.A})
i := len(d) - 1
for r.A > prevCol && i >= 0 && d[i] == ' ' {
r.A--
i--
}
}
if r.A == r.B {
r.A--
}
}
v.Erase(e, r)
if sel.Len() != l {
continue
}
i++
}
return nil
}
// Run executes the RightDelete command.
func (c *RightDelete) Run(v *backend.View, e *backend.Edit) error {
sel := v.Sel()
hasNonEmpty := sel.HasNonEmpty()
i := 0
for {
l := sel.Len()
if i >= l {
break
}
r := sel.Get(i)
if r.A == r.B && !hasNonEmpty {
r.B++
}
v.Erase(e, r)
if sel.Len() != l {
continue
}
i++
}
return nil
}
// Run executes the DeleteWord command.
func (c *DeleteWord) Run(v *backend.View, e *backend.Edit) error {
var class int
if c.Forward {
class = backend.CLASS_WORD_END | backend.CLASS_PUNCTUATION_END | backend.CLASS_LINE_START
} else {
class = backend.CLASS_WORD_START | backend.CLASS_PUNCTUATION_START | backend.CLASS_LINE_END | backend.CLASS_LINE_START
}
sel := v.Sel()
var rs []text.Region
for i := 0; i < sel.Len(); i++ {
r := sel.Get(i)
if r.Empty() {
p := c.findByClass(r.A, class, v)
if c.Forward {
r = text.Region{A: r.A, B: p}
} else {
r = text.Region{A: p, B: r.A}
}
}
rs = append(rs, r)
}
sel.Clear()
sel.AddAll(rs)
if c.Forward {
backend.GetEditor().CommandHandler().RunTextCommand(v, "right_delete", nil)
} else {
backend.GetEditor().CommandHandler().RunTextCommand(v, "left_delete", nil)
}
return nil
}
func (c *DeleteWord) findByClass(point int, class int, v *backend.View) int {
var end, d int
if c.Forward {
d = 1
end = v.Size()
if point > end {
point = end
}
s := v.Substr(text.Region{A: point, B: point + 2})
if strings.Contains(s, "\t") && strings.Contains(s, " ") {
class = backend.CLASS_WORD_START | backend.CLASS_PUNCTUATION_START | backend.CLASS_LINE_END
}
} else {
d = -1
end = 0
if point < end {
point = end
}
s := v.Substr(text.Region{A: point - 2, B: point})
if strings.Contains(s, "\t") && strings.Contains(s, " ") {
class = backend.CLASS_WORD_END | backend.CLASS_PUNCTUATION_END | backend.CLASS_LINE_START
}
}
point += d
for ; point != end; point += d {
if v.Classify(point)&class != 0 {
return point
}
}
return point
}
func init() {
register([]backend.Command{
&Insert{},
&LeftDelete{},
&RightDelete{},
&DeleteWord{},
})
}