Skip to content

Commit 8ec7549

Browse files
author
a
committed
LSP symbol search
1 parent d493a3f commit 8ec7549

File tree

5 files changed

+150
-17
lines changed

5 files changed

+150
-17
lines changed

exec.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,7 @@ Lsp refs
15871587
return
15881588
}
15891589

1590-
v := strings.Split(arg, " ")
1590+
v := strings.SplitN(arg, " ", 2)
15911591
arg = v[0]
15921592
var rest string
15931593
if len(v) > 1 {
@@ -1618,6 +1618,8 @@ Lsp refs
16181618
srv.ExecCodeAction(lspb, rest, executeLspTextEdits, func(tolook string) {
16191619
Load(ec, 0, false, []rune(tolook))
16201620
})
1621+
case "symbol":
1622+
Warn(srv.WorkspaceSymbol(rest))
16211623
default:
16221624
Warn("wrong argument")
16231625
}

extra/LookFile/lookfile.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func readEvents(buf *util.BufferConn, searchChan chan<- string, moveChan chan<-
146146
}
147147
}
148148

149-
func searcher(buf *util.BufferConn, cwd string, searchChan <-chan string, moveChan <-chan int, openChan <-chan struct{}) {
149+
func searcher(p9clnt *clnt.Clnt, buf *util.BufferConn, cwd string, searchChan <-chan string, moveChan <-chan int, openChan <-chan struct{}) {
150150
resultChan := make(chan *lookFileResult, 1)
151151
var searchDone chan struct{}
152152
curNeedle := ""
@@ -185,8 +185,12 @@ func searcher(buf *util.BufferConn, cwd string, searchChan <-chan string, moveCh
185185
if needle != "" {
186186
resultList = resultList[0:0]
187187
searchDone = make(chan struct{})
188-
go fileSystemSearch(cwd, resultChan, searchDone, needle, exact, MAX_RESULTS)
189-
go tagsSearch(resultChan, searchDone, needle, exact, MAX_RESULTS)
188+
if needle[0] == '@' {
189+
go lspSymbolSearch(p9clnt, cwd, resultChan, searchDone, needle[1:])
190+
} else {
191+
go fileSystemSearch(cwd, resultChan, searchDone, needle, exact, MAX_RESULTS)
192+
go tagsSearch(resultChan, searchDone, needle, exact, MAX_RESULTS)
193+
}
190194
} else {
191195
displayResults(buf, curSelected, resultList)
192196
}
@@ -255,7 +259,11 @@ func displayResults(buf *util.BufferConn, curSelected int, resultList []*lookFil
255259
result.start = len(t)
256260
t = append(t, []rune(result.show)...)
257261
//t = append(t, []rune(fmt.Sprintf(" (%d)", result.score))...)
258-
result.end = len(t)
262+
if result.loadEnd > 0 {
263+
result.end = result.loadEnd + result.start
264+
} else {
265+
result.end = len(t)
266+
}
259267
t = append(t, []rune("\n")...)
260268
for i := range result.mpos {
261269
color[result.mpos[i]+s] = 0x03
@@ -316,5 +324,5 @@ func main() {
316324
moveChan := make(chan int, 1)
317325

318326
go readEvents(buf, searchChan, moveChan, openChan)
319-
searcher(buf, cwd, searchChan, moveChan, openChan)
327+
searcher(p9clnt, buf, cwd, searchChan, moveChan, openChan)
320328
}

extra/LookFile/search.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package main
22

33
import (
4+
"fmt"
5+
"io/ioutil"
46
"os"
57
"path/filepath"
68
"strings"
79
"unicode"
810
"unicode/utf8"
911

1012
"github.com/aarzilli/yacco/regexp"
13+
"github.com/aarzilli/yacco/util"
14+
"github.com/lionkov/go9p/p"
15+
"github.com/lionkov/go9p/p/clnt"
1116
)
1217

1318
var Extensions []string
@@ -23,6 +28,7 @@ type lookFileResult struct {
2328
mpos []int
2429
needle string
2530

31+
loadEnd int
2632
start, end int
2733
}
2834

@@ -138,13 +144,6 @@ func fileSystemSearch(edDir string, resultChan chan<- *lookFileResult, searchDon
138144
continue
139145
}
140146

141-
/*
142-
off := utf8.RuneCountInString(relPath) - utf8.RuneCountInString(fi[i].Name())
143-
144-
if !strings.HasSuffix(relPath, fi[i].Name()) {
145-
off = -1
146-
}*/
147-
148147
d := depth
149148
if fi[i].IsDir() {
150149
relPath += "/"
@@ -209,7 +208,7 @@ func fileSystemSearchMatch(exact bool, needlerx []*regexp.Regex, relPath, needle
209208
}
210209

211210
select {
212-
case resultChan <- &lookFileResult{score, relPath, mpos, needle, 0, 0}:
211+
case resultChan <- &lookFileResult{score, relPath, mpos, needle, 0, 0, 0}:
213212
case <-searchDone:
214213
return -1
215214
}
@@ -347,7 +346,7 @@ func tagsSearch(resultChan chan<- *lookFileResult, searchDone chan struct{}, nee
347346
}
348347

349348
select {
350-
case resultChan <- &lookFileResult{score, x, []int{}, needle, 0, 0}:
349+
case resultChan <- &lookFileResult{score, x, []int{}, needle, 0, 0, 0}:
351350
case <-searchDone:
352351
return
353352
}
@@ -358,3 +357,31 @@ func tagsSearch(resultChan chan<- *lookFileResult, searchDone chan struct{}, nee
358357
}
359358
}
360359
}
360+
361+
func lspSymbolSearch(p9clnt *clnt.Clnt, cwd string, resultChan chan<- *lookFileResult, searchDone chan struct{}, needle string) {
362+
lspfd, err := p9clnt.FOpen(fmt.Sprintf("/%s/lsp", os.Getenv("winid")), p.OWRITE)
363+
util.Allergic(debug, err)
364+
fmt.Fprintf(lspfd, "symbol %s", needle)
365+
lspfd.Close()
366+
367+
lspfd, err = p9clnt.FOpen(fmt.Sprintf("/%s/lsp", os.Getenv("winid")), p.OREAD)
368+
util.Allergic(debug, err)
369+
resp, err := ioutil.ReadAll(lspfd)
370+
util.Allergic(debug, err)
371+
lspfd.Close()
372+
373+
for i, line := range strings.Split(string(resp), "\n") {
374+
v := strings.SplitN(line, " ", 3)
375+
if len(v) != 3 {
376+
continue
377+
}
378+
if v[1] == "Field" {
379+
continue
380+
}
381+
rel, _ := filepath.Rel(cwd, v[0])
382+
if rel != "" {
383+
v[0] = rel
384+
}
385+
resultChan <- &lookFileResult{show: v[0] + " " + strings.ToLower(v[1]) + " " + v[2], loadEnd: len([]rune(v[0])), score: i, needle: "@" + needle}
386+
}
387+
}

fs.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/aarzilli/yacco/config"
1818
"github.com/aarzilli/yacco/edit"
1919
"github.com/aarzilli/yacco/hl"
20+
"github.com/aarzilli/yacco/lsp"
2021
"github.com/aarzilli/yacco/util"
2122

2223
"github.com/lionkov/go9p/p"
@@ -223,6 +224,8 @@ func FsAddEditor(n int) {
223224
prop.Add(bufdir, "prop", user, nil, 0660, prop)
224225
jumps := &ReadOnlyP9{srv.File{}, bwr(jumpFileFn)}
225226
jumps.Add(bufdir, "jumps", user, nil, 0440, jumps)
227+
lsp := &ReadWriteP9{srv.File{}, bwr(readLspFn), bww(writeLspFn)}
228+
lsp.Add(bufdir, "lsp", user, nil, 0660, lsp)
226229
}
227230

228231
func FsRemoveEditor(n int) {
@@ -1222,3 +1225,50 @@ func clunkLogFileFn(conn string) error {
12221225
}
12231226
return nil
12241227
}
1228+
1229+
var lspout = make(map[int]string)
1230+
1231+
func readLspFn(i int, off int64) ([]byte, syscall.Errno) {
1232+
if off > 0 {
1233+
return []byte{}, 0
1234+
}
1235+
ec := bufferExecContext(i)
1236+
if ec == nil {
1237+
return nil, syscall.ENOENT
1238+
}
1239+
return []byte(lspout[i]), 0
1240+
}
1241+
1242+
func writeLspFn(i int, data []byte, off int64) syscall.Errno {
1243+
ec := bufferExecContext(i)
1244+
if ec == nil {
1245+
return syscall.ENOENT
1246+
}
1247+
srv, lspb := lsp.BufferToLsp(Wnd.tagbuf.Dir, ec.ed.bodybuf, ec.ed.sfr.Fr.Sel, true, Warn, defaultLookForLsp)
1248+
if srv == nil {
1249+
return syscall.EIO
1250+
}
1251+
1252+
v := strings.SplitN(string(data), " ", 2)
1253+
arg := v[0]
1254+
var rest string
1255+
if len(v) > 1 {
1256+
rest = v[1]
1257+
}
1258+
1259+
switch arg {
1260+
case "refs":
1261+
lspout[i] = srv.References(lspb)
1262+
case "ca":
1263+
lspout[i] = ""
1264+
srv.ExecCodeAction(lspb, rest, executeLspTextEdits, func(tolook string) {
1265+
Load(*ec, 0, false, []rune(tolook))
1266+
})
1267+
case "symbol":
1268+
lspout[i] = srv.WorkspaceSymbol(rest)
1269+
default:
1270+
return syscall.EIO
1271+
}
1272+
1273+
return 0
1274+
}

lsp/lsp.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package lsp
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
67
"fmt"
@@ -296,8 +297,9 @@ func (srv *LspSrv) References(a LspBufferPos) string {
296297
return appendLocs("", locs, a.Path, a.Ln)
297298
}
298299

300+
const sillyURI = "file://"
301+
299302
func appendLocs(s string, defs []locationAndKind, curPath string, ln int) string {
300-
const sillyURI = "file://"
301303
if len(defs) <= 0 {
302304
return s
303305
}
@@ -468,7 +470,51 @@ func (srv *LspSrv) ExecCodeAction(a LspBufferPos, rest string, execEdit func([]T
468470
srv.look = oldlook
469471
}()
470472
var out any
471-
srv.conn.Call(context.Background(), "workspace/executeCommand", &ExecuteCommandParams{Command: cmd.Command, Arguments: cmd.Arguments}, out)
473+
srv.conn.Call(context.Background(), "workspace/executeCommand", &ExecuteCommandParams{Command: cmd.Command, Arguments: cmd.Arguments}, &out)
474+
}
475+
476+
var kindToString = map[SymbolKind]string{
477+
1: "File",
478+
2: "Module",
479+
3: "Namespace",
480+
4: "Package",
481+
5: "Class",
482+
6: "Method",
483+
7: "Property",
484+
8: "Field",
485+
9: "Constructor",
486+
10: "Enum",
487+
11: "Interface",
488+
12: "Function",
489+
13: "Variable",
490+
14: "Constant",
491+
15: "String",
492+
16: "Number",
493+
17: "Boolean",
494+
18: "Array",
495+
19: "Object",
496+
20: "Key",
497+
21: "Null",
498+
22: "EnumMember",
499+
23: "Struct",
500+
24: "Event",
501+
25: "Operator",
502+
26: "TypeParameter",
503+
}
504+
505+
func (srv *LspSrv) WorkspaceSymbol(query string) string {
506+
var out []SymbolInformation
507+
err := srv.conn.Call(context.Background(), "workspace/symbol", &WorkspaceSymbolParams{Query: query}, &out)
508+
lspLog(fmt.Sprintf("workspace/symbol %q %v\n", query, err))
509+
var buf bytes.Buffer
510+
for i := range out {
511+
uri := out[i].Location.URI
512+
if strings.HasPrefix(uri, sillyURI) {
513+
uri = uri[len(sillyURI):]
514+
}
515+
fmt.Fprintf(&buf, "%s:%d %s %s\n", uri, out[i].Location.Range.Start.Line+1, kindToString[out[i].Kind], out[i].Name)
516+
}
517+
return buf.String()
472518
}
473519

474520
func issfx(a, b []uint16) bool {

0 commit comments

Comments
 (0)