-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
205 lines (166 loc) · 3.91 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/jmespath-community/go-jmespath"
"github.com/neovim/go-client/nvim"
"github.com/neovim/go-client/nvim/plugin"
)
var resultBufs *resultBuffers = &resultBuffers{buffers: make(map[nvim.Buffer]nvim.Buffer)}
type resultBuffers struct {
buffers map[nvim.Buffer]nvim.Buffer
}
func (r *resultBuffers) buffer(v *nvim.Nvim, k nvim.Buffer) (buf nvim.Buffer, err error) {
if err = r.clean(v); err != nil {
return buf, err
}
buf, ok := r.buffers[k]
if !ok {
// create scratch buffer
buf, err = v.CreateBuffer(false, true)
if err != nil {
return buf, err
}
if err = v.SetBufferOption(buf, "filetype", "json"); err != nil {
return buf, err
}
r.buffers[k] = buf
}
return buf, nil
}
func (r *resultBuffers) deleteBuffer(v *nvim.Nvim, k nvim.Buffer) error {
if err := v.DeleteBuffer(r.buffers[k], map[string]bool{"force": true, "unload": false}); err != nil {
return err
}
delete(r.buffers, k)
return nil
}
func (r *resultBuffers) clean(v *nvim.Nvim) error {
for k, b := range r.buffers {
valid, err := v.IsBufferValid(b)
if err != nil {
return err
}
// the buffer is wiped out
if !valid {
delete(r.buffers, k)
continue
}
loaded, err := v.IsBufferLoaded(b)
if err != nil {
return err
}
// the buffer may be removed from the buffer list
if !loaded {
if err := r.deleteBuffer(v, k); err != nil {
return err
}
continue
}
// If the buffer is hidden and the key of that is invalid, delete the item.
hidden, err := isBufferHidden(v, b)
if err != nil {
return err
}
if hidden {
valid, err := v.IsBufferValid(k)
if err != nil {
return err
}
if !valid {
if err := r.deleteBuffer(v, k); err != nil {
return err
}
}
}
}
return nil
}
type encoder struct {
*json.Encoder
buf *bytes.Buffer
}
func newEncoder() *encoder {
buf := &bytes.Buffer{}
e := json.NewEncoder(buf)
e.SetEscapeHTML(false)
e.SetIndent("", " ")
return &encoder{
Encoder: e,
buf: buf,
}
}
func jsonUmarshalBuffer(v *nvim.Nvim, b nvim.Buffer, data any) error {
lines, err := v.BufferLines(b, 0, -1, false)
if err != nil {
return err
}
input := bytes.Join(lines, []byte("\n"))
return json.Unmarshal(input, data)
}
func jsonMarshal(data any) ([]byte, error) {
e := newEncoder()
if err := e.Encode(data); err != nil {
return nil, err
}
return bytes.TrimSuffix(e.buf.Bytes(), []byte("\n")), nil
}
func isBufferHidden(v *nvim.Nvim, b nvim.Buffer) (bool, error) {
res, err := v.Exec(fmt.Sprintf("echo empty(win_findbuf(%d))", b), map[string]interface{}{"output": true})
if err != nil {
return false, err
}
output, isStr := res["output"].(string)
if !isStr {
return false, errors.New("Unexpected output")
}
return output == "1", nil
}
func displayResult(v *nvim.Nvim, curBuf nvim.Buffer, data any) error {
bs, err := jsonMarshal(data)
if err != nil {
return err
}
buf, err := resultBufs.buffer(v, curBuf)
if err != nil {
return err
}
if hidden, err := isBufferHidden(v, buf); err == nil && hidden {
curWin, err := v.CurrentWindow()
if err != nil {
return err
}
// open the result buffer
if _, err = v.Exec(fmt.Sprintf("bo vs | b %d", buf), map[string]interface{}{"output": false}); err != nil {
return err
}
if err = v.SetCurrentWindow(curWin); err != nil {
return err
}
}
return v.SetBufferLines(buf, 0, -1, true, bytes.Split(bs, []byte("\n")))
}
func search(v *nvim.Nvim, args []string) error {
query := args[0]
curBuf, err := v.CurrentBuffer()
if err != nil {
return err
}
var data any
if err := jsonUmarshalBuffer(v, curBuf, &data); err != nil {
return err
}
res, err := jmespath.Search(query, data)
if err != nil {
return err
}
return displayResult(v, curBuf, res)
}
func main() {
plugin.Main(func(p *plugin.Plugin) error {
p.HandleCommand(&plugin.CommandOptions{Name: "JP", NArgs: "1"}, search)
return nil
})
}