-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtextobject.go
More file actions
111 lines (94 loc) · 1.8 KB
/
textobject.go
File metadata and controls
111 lines (94 loc) · 1.8 KB
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
package wig
func TextObjectWord(ctx Context, bigword bool) (start, end int) {
cur := ContextCursorGet(ctx)
start = cur.Char
end = start
line := CursorLine(ctx.Buf, cur)
cls := CursorChClass(ctx.Buf, cur)
if bigword {
for start > 0 {
if line.Value.IsEmpty() {
break
}
if getChClass(line.Value[start-1]) == cls {
start--
} else {
break
}
}
}
end = start
for i, r := range line.Value {
if i < start {
continue
}
if getChClass(r) == cls {
end = i
continue
}
break
}
return start, end
}
// Returns selection inside '(', '{', '[' as "Selection" range. This implementation is simple
// and does not check if open/close symbols are "balanced".
// TODO: rewrite
func TextObjectBlock(buf *Buffer, ch rune, include bool) (found bool, sel *Selection, cur Cursor) {
// defer func(c Cursor) {
// buf.Cursor = c
// }(buf.Cursor)
openClose := map[rune]rune{
'(': ')',
'{': '}',
'[': ']',
}
if _, ok := openClose[ch]; !ok {
for k, v := range openClose {
if v == ch {
ch = k
break
}
}
}
openCh := ch
closeCh := openClose[ch]
// move cursor back until 'openCh' is found
openChFound := false
for {
if CursorChar(buf, nil) == openCh {
openChFound = true
break
}
if !CursorDec(buf, nil) {
break
}
}
if !openChFound {
return
}
// TODO: fix
bufCursor := Cursor{}
start := bufCursor
// move cursor "left" till we find first "open" bracket
for {
if CursorChar(buf, nil) == closeCh {
end := bufCursor
if include == false {
// no selection. empty ().
if end.Char == start.Char+1 {
return true, nil, end
}
start.Char += 1
end.Char -= 1
}
return true, &Selection{
Start: start,
End: end,
}, bufCursor
}
if !CursorInc(buf, nil) {
break
}
}
return false, nil, bufCursor
}