-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.go
38 lines (33 loc) · 857 Bytes
/
action.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
package main
import (
"strings"
)
// toggleComment toggles line comment.
// It will comment lines when none of the lines are commented.
// It will uncomment lines when at least one of the lines are commented.
func toggleComment(comment string, t *Text, c *Cursor, sel *Selection) []*Action {
lns := make([]int, 0)
if sel.on {
lns = sel.Lines()
} else {
lns = append(lns, c.l)
}
commentedLns := make([]int, 0)
for _, l := range lns {
if strings.HasPrefix(t.lines[l].data, comment+" ") {
commentedLns = append(commentedLns, l)
break
}
}
if len(commentedLns) > 0 {
for _, l := range commentedLns {
t.lines[l].data = strings.Replace(t.lines[l].data, comment+" ", "", 1)
}
} else {
for _, l := range lns {
t.lines[l].data = comment + " " + t.lines[l].data
}
}
// TODO: return actions instead modify it.
return nil
}