This repository was archived by the owner on Mar 27, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
195 lines (162 loc) · 3.47 KB
/
Copy pathmain.go
File metadata and controls
195 lines (162 loc) · 3.47 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
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
package main
import (
"bufio"
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"github.com/sourcegraph/langserver/client"
"github.com/sourcegraph/go-langserver/pkg/lsp"
)
var (
mode = flag.String("mode", "stdio", "communication mode (stdio|tcp)")
addr = flag.String("addr", ":2088", "server listen address (tcp)")
rootPath = flag.String("root", "", "workspace root path")
cmd = flag.String("cmd", "", "langserver executable command (for stdio mode)")
ctx = context.Background()
stdin = bufio.NewReader(os.Stdin)
)
func main() {
flag.Parse()
log.SetFlags(0)
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
if *rootPath == "" {
return fmt.Errorf("specify a workspace root")
}
if *mode == "stdio" && *cmd == "" {
return fmt.Errorf("specify a command to run to start a language server")
}
var c *client.LspConn
var e *exec.Cmd
var err error
switch *mode {
case "tcp":
c, err = client.NewTCPClient(ctx, *addr)
case "stdio":
c, e, err = client.NewStdioClient(ctx, *cmd)
default:
return fmt.Errorf("invalid mode (tcp|stdio): %s", *mode)
}
if err != nil {
return err
}
defer c.Close()
if e != nil {
defer e.Process.Kill()
}
err = c.Initialize(ctx, *rootPath)
if err != nil {
return err
}
for {
method := promptMethod()
if method == "" {
continue
}
if method == "shutdown" {
break
}
var (
p lsp.TextDocumentPositionParams
q lsp.WorkspaceSymbolParams
)
if method == "symbol" {
query := promptQuery()
q = lsp.WorkspaceSymbolParams{
Query: query,
Limit: 10,
}
} else {
file := promptFile()
if file == "" || filepath.IsAbs(file) {
continue
}
line := promptLine()
char := promptCharacter()
p = lsp.TextDocumentPositionParams{
TextDocument: lsp.TextDocumentIdentifier{URI: file},
Position: lsp.Position{Line: line, Character: char},
}
}
printResponse := func(resp interface{}, err error) {
if err != nil {
fmt.Println("ERROR: ", err)
}
j, _ := json.MarshalIndent(resp, "", "\t")
fmt.Printf("\n\n%v\n\n\n", string(j))
}
switch method {
case "hover":
resp, err := c.Hover(ctx, &p)
printResponse(resp, err)
case "definition":
resp, err := c.Definition(ctx, &p)
printResponse(resp, err)
case "references":
resp, err := c.References(ctx, &p)
printResponse(resp, err)
case "symbol":
resp, err := c.Symbol(ctx, &q)
printResponse(resp, err)
default:
continue
}
}
err = c.Shutdown(ctx)
if err != nil {
return err
}
return nil
}
func promptString(prompt string) string {
fmt.Print(prompt)
var text string
fmt.Scanln(&text)
return text
}
func promptInt(prompt string) int {
for {
i, err := strconv.Atoi(promptString(prompt))
if err == nil {
return i
}
}
}
func promptMethod() string {
switch promptString("Choose (1) hover, (2) definition, (3) references, (4) symbol, (5) shutdown: ") {
case "1":
return "hover"
case "2":
return "definition"
case "3":
return "references"
case "4":
return "symbol"
case "5":
return "shutdown"
default:
return ""
}
}
func promptFile() string {
return promptString("Choose a file path relative to root: ")
}
func promptQuery() string {
return promptString("Enter a query: ")
}
func promptLine() int {
return promptInt("Choose a 0-indexed line: ")
}
func promptCharacter() int {
return promptInt("Choose a 0-indexed character offset for that line: ")
}