forked from limetext/commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline.go
226 lines (199 loc) · 5.22 KB
/
line.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
// 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 (
// Duplicate copies all of the current selections,
// inserting each copy before the original. If any
// of the selections are empty, instead, the entire
// line containing that cursor is copied and inserted
// before the original.
DuplicateLine struct {
backend.DefaultCommand
}
// JoinLines removes every new line in the
// selections and the first new line after.
JoinLines struct {
backend.DefaultCommand
}
// SelectLines makes the selection fill the lines
// covered by it currently.
SelectLines struct {
backend.DefaultCommand
Forward bool
}
// SwapLineUp swaps the currently selected lines with the ones above.
SwapLineUp struct {
backend.DefaultCommand
}
// SwapLineDown swaps the currently selected
// lines with the ones below.
SwapLineDown struct {
backend.DefaultCommand
}
// SplitSelectionIntoLines will split the current
// selection into lines.
SplitSelectionIntoLines struct {
backend.DefaultCommand
}
)
// Run executes the DuplicateLine command.
func (c *DuplicateLine) Run(v *backend.View, e *backend.Edit) error {
// v.Sel() is the RegionSet, a collection of Regions representing
// the current selections
sel := v.Sel()
// when we have multiple cursors in the document,
// we will interate through each cursor
for i := 0; i < sel.Len(); i++ {
// Selection of a cursor
r := sel.Get(i)
suffix := ""
// If the selection is empty
// then we want to duplicate the entire line
if r.Empty() {
r = v.Line(r.End())
suffix = "\n"
}
t := v.Substr(r) + suffix
v.Insert(e, r.Begin(), t)
}
return nil
}
// Run executes the JoinLines command.
func (c *JoinLines) Run(v *backend.View, e *backend.Edit) error {
sel := v.Sel()
for i := 0; i < sel.Len(); i++ {
r := sel.Get(i)
// Removing new line and triming in the selection
t := v.Substr(r)
t = strings.Replace(t, "\r", "\n", -1)
slice := strings.Split(t, "\n")
t = ""
for j, s := range slice {
if j == 0 {
t += s
continue
}
t += " " + strings.TrimLeft(s, " \t")
}
v.Replace(e, r, t)
// Removing the first new line after selection
liner := v.FullLine(r.End())
line := v.Substr(liner)
line = strings.Replace(line, "\n", "", -1)
line = strings.Replace(line, "\r", "", -1)
line = strings.TrimRight(line, " \t")
// Triming the line after
nextline := liner.End() + 1
nextliner := v.FullLine(nextline)
nline := v.Substr(nextliner)
if nline != "" {
v.Replace(e, nextliner, " "+strings.TrimLeft(nline, " \t"))
}
v.Replace(e, liner, line)
}
return nil
}
// Run executes the SwapLineUp command.
func (c *SwapLineUp) Run(v *backend.View, e *backend.Edit) error {
sel := v.Sel()
for i := 0; i < sel.Len(); i++ {
r := sel.Get(i)
// Expand to all lines under selection
fline := v.Line(r.Begin())
lline := v.Line(r.End())
r = text.Region{fline.Begin(), lline.End()}
t := v.Substr(r)
// Select line before region
bline := v.Line(r.Begin() - 1)
bt := v.Substr(bline)
v.Replace(e, r, bt)
v.Replace(e, bline, t)
}
return nil
}
// Run executes the SwapLineDown command.
func (c *SwapLineDown) Run(v *backend.View, e *backend.Edit) error {
sel := v.Sel()
for i := 0; i < sel.Len(); i++ {
r := sel.Get(i)
// Expand to all lines under selection
fline := v.Line(r.Begin())
lline := v.Line(r.End())
r = text.Region{fline.Begin(), lline.End()}
t := v.Substr(r)
// Select line before region
nline := v.Line(r.End() + 1)
nt := v.Substr(nline)
v.Replace(e, nline, t)
v.Replace(e, r, nt)
}
return nil
}
// Run executes the SelectLines command.
func (c *SelectLines) Run(v *backend.View, e *backend.Edit) error {
var (
rs []text.Region
line, l text.Region
d int
)
sel := v.Sel()
for i := 0; i < sel.Len(); i++ {
r := sel.Get(i)
// Get the distance of the selection to the begining of line
if c.Forward {
line = v.FullLine(r.End())
l = v.Line(line.End() + 1)
d = r.End() - line.Begin()
} else {
line = v.FullLine(r.Begin())
l = v.Line(line.Begin() - 1)
d = r.Begin() - line.Begin()
}
// If the next line length is more than the calculated distance
// Put new region at the exact distance
// If not put region at the end of the next|before line
if l.Size() < d {
rs = append(rs, text.Region{l.End(), l.End()})
} else {
rs = append(rs, text.Region{l.Begin() + d, l.Begin() + d})
}
}
v.Sel().AddAll(rs)
return nil
}
// Run executes the SplitSelectionIntoLines command
func (c *SplitSelectionIntoLines) Run(v *backend.View, e *backend.Edit) error {
var rs []text.Region
sel := v.Sel()
for i := 0; i < sel.Len(); i++ {
r := sel.Get(i)
lines := v.Lines(r)
for i := 0; i < len(lines); i++ {
if i != 0 {
// Remove line endings
r2 := v.FullLine(lines[i-1].End())
lines[i] = lines[i].Clip(r2)
}
rs = append(rs, lines[i].Intersection(r))
}
}
v.Sel().Clear()
v.Sel().AddAll(rs)
return nil
}
func init() {
register([]backend.Command{
&JoinLines{},
&SelectLines{},
&SwapLineUp{},
&SwapLineDown{},
&SplitSelectionIntoLines{},
&DuplicateLine{},
})
}