Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

paramsEx #16

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions gopls/goxls/lsview/lsview.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"log"
"os"
"reflect"
"sort"
"time"

"golang.org/x/tools/internal/fakenet"
Expand Down Expand Up @@ -52,17 +53,17 @@ func Main(app, goxls string) {
id := req.ID()
log.Printf("[%v] %s:\n%s", id, req.Method(), params(req.Params()))
reqChan <- id
resp := respFetch(respChan)
resp, ret := respFetch(respChan)
if resp != nil {
log.Printf("[%v] %s ret:\n%s", id, app, resp)
}
if goxls != "" {
select { // allow send request failed
case <-time.After(time.Second):
case reqChan2 <- id:
if resp2 := respFetch(respChan2); resp2 != nil {
if resp2, ret2 := respFetch(respChan2); resp2 != nil {
log.Printf("[%v] %s ret:\n%s", id, goxls, resp2)
if !reflect.DeepEqual(resp, resp2) {
if !reflect.DeepEqual(ret, ret2) {
logd.Printf("[%v] %s:\n%s", id, req.Method(), params(req.Params()))
logd.Printf("[%v] %s ret:\n%s", id, app, resp)
logd.Printf("[%v] %s ret:\n%s", id, goxls, resp2)
Expand All @@ -82,19 +83,17 @@ func Main(app, goxls string) {
select {}
}

func respFetch(respChan chan *jsonrpc2.Response) any {
func respFetch(respChan chan *jsonrpc2.Response) (any, any) {
select {
case <-time.After(time.Second):
case resp := <-respChan:
ret := any(resp.Err())
if ret == nil {
ret = params(resp.Result())
} else {
ret = fmt.Sprintf("%serror: %v\n", indent, ret)
return paramsEx(resp.Result())
}
return ret
return fmt.Sprintf("%serror: %v\n", indent, ret), nil
}
return nil
return nil, nil
}

func respLoop(app string, respChan chan *jsonrpc2.Response, reqChan chan jsonrpc2.ID) {
Expand Down Expand Up @@ -143,16 +142,27 @@ func params(raw json.RawMessage) []byte {
if err != nil {
return raw
}
return paramsEx(ret, indent)
return paramsFmt(ret, indent)
}

func paramsEx(raw json.RawMessage) ([]byte, any) {
var ret any
err := json.Unmarshal(raw, &ret)
if err != nil {
return raw, ret
}
return paramsFmt(ret, indent), ret
}

func paramsEx(ret any, prefix string) []byte {
func paramsFmt(ret any, prefix string) []byte {
var b bytes.Buffer
switch val := ret.(type) {
case mapt:
for k, v := range val {
keys := keys(val)
for _, k := range keys {
v := val[k]
if isComplex(v) {
fmt.Fprintf(&b, "%s%s:\n%s", prefix, k, paramsEx(v, prefix+indent))
fmt.Fprintf(&b, "%s%s:\n%s", prefix, k, paramsFmt(v, prefix+indent))
} else {
fmt.Fprintf(&b, "%s%s: %v\n", prefix, k, v)
}
Expand Down Expand Up @@ -187,6 +197,15 @@ func isComplex(v any) bool {
return ok
}

func keys(v mapt) []string {
ret := make([]string, 0, len(v))
for key := range v {
ret = append(ret, key)
}
sort.Strings(ret)
return ret
}

func check(err error) {
if err != nil {
log.Panicln(err)
Expand Down