Repository navigation
Expand file tree
/
Copy pathclient.go
More file actions
133 lines (115 loc) · 3.25 KB
/
Copy pathclient.go
File metadata and controls
133 lines (115 loc) · 3.25 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package client
import (
"context"
"fmt"
"io"
"net"
"os/exec"
"github.com/sourcegraph/go-langserver/pkg/lsp"
"github.com/sourcegraph/jsonrpc2"
)
// LspConn wraps a jsonrpc2.Conn for communiting with an
// LSP-based language server over stdio or tcp sockets.
type LspConn struct {
*jsonrpc2.Conn
}
// Initialize makes an `initialize` LSP request.
func (c *LspConn) Initialize(ctx context.Context, rootPath string) error {
return c.Call(ctx, "initialize", &lsp.InitializeParams{RootPath: rootPath}, nil)
}
// Definition makes an `textDocument/definition` LSP request.
func (c *LspConn) Definition(ctx context.Context, p *lsp.TextDocumentPositionParams) (*[]lsp.Location, error) {
var locResp []lsp.Location
err := c.Call(ctx, "textDocument/definition", p, &locResp)
if err != nil {
return nil, err
}
return &locResp, nil
}
// Hover makes an `textDocument/hover` LSP request.
func (c *LspConn) Hover(ctx context.Context, p *lsp.TextDocumentPositionParams) (*lsp.Hover, error) {
var hoverResp lsp.Hover
err := c.Call(ctx, "textDocument/hover", p, &hoverResp)
if err != nil {
return nil, err
}
return &hoverResp, nil
}
// References makes an `textDocument/references` LSP request.
func (c *LspConn) References(ctx context.Context, p *lsp.TextDocumentPositionParams) (*[]lsp.Location, error) {
rp := lsp.ReferenceParams{
TextDocumentPositionParams: *p,
Context: lsp.ReferenceContext{
IncludeDeclaration: true,
},
}
var refsResp []lsp.Location
err := c.Call(ctx, "textDocument/references", rp, &refsResp)
if err != nil {
return nil, err
}
return &refsResp, nil
}
func (c *LspConn) Symbol(ctx context.Context, p *lsp.WorkspaceSymbolParams) (*[]lsp.SymbolInformation, error) {
var symResp []lsp.SymbolInformation
err := c.Call(ctx, "workspace/symbol", p, &symResp)
if err != nil {
return nil, err
}
return &symResp, nil
}
// Shutdown makes an `shutdown` LSP request.
func (c *LspConn) Shutdown(ctx context.Context) error {
return c.Call(ctx, "shutdown", nil, nil)
}
// NewTCPClient returns a new LspConn connected to a tcp socket at address.
func NewTCPClient(ctx context.Context, addr string) (*LspConn, error) {
if addr == "" {
return nil, fmt.Errorf("provide a command and addr to create a lang server tcp client")
}
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
c := LspConn{}
c.Conn = jsonrpc2.NewConn(ctx, conn, nil)
return &c, nil
}
type stdrwc struct {
Stdin io.WriteCloser
Stdout io.ReadCloser
}
func (v stdrwc) Read(p []byte) (int, error) {
return v.Stdout.Read(p)
}
func (v stdrwc) Write(p []byte) (int, error) {
return v.Stdin.Write(p)
}
func (v stdrwc) Close() error {
if err := v.Stdin.Close(); err != nil {
return err
}
return v.Stdout.Close()
}
// NewStdioClient returns a new LspConn connected to a process over stdio.
func NewStdioClient(ctx context.Context, cmd string) (*LspConn, *exec.Cmd, error) {
subProcess := exec.Command(cmd)
stdin, err := subProcess.StdinPipe()
if err != nil {
return nil, nil, err
}
stdout, err := subProcess.StdoutPipe()
if err != nil {
return nil, nil, err
}
if err = subProcess.Start(); err != nil {
return nil, nil, err
}
s := stdrwc{
Stdin: stdin,
Stdout: stdout,
}
c := LspConn{}
c.Conn = jsonrpc2.NewConn(ctx, s, nil)
return &c, subProcess, nil
}