-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (148 loc) · 3.52 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
package main
import (
"bufio"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/spf13/cobra"
"go.lsp.dev/jsonrpc2"
"go.lsp.dev/protocol"
"github.com/mattn/go-isatty"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type SymbolInfo struct {
protocol.SymbolInformation
RelativePath string
Description string
Embedding []float64
}
func main() {
rootCmd := &cobra.Command{
Use: "sage",
}
rootCmd.AddCommand(IndexCmd, LanguageServerCmd, CompletionCmd, DevCmds, CtxCmd)
if isatty.IsTerminal(os.Stdout.Fd()) {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
err := rootCmd.Execute()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
func startLsp(lsp *LanguageServerConfig, clientConn *protocol.Client, params *protocol.InitializeParams) (*ChildLanguageServer, error) {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
cmd := exec.Command(*lsp.Command, lsp.Args...)
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
workspaceDir := getWorkspaceDir(wd)
stdoutFileName := filepath.Join(workspaceDir, "lsp_stdout.log")
stdoutFile, err := os.Create(stdoutFileName)
if err != nil {
return nil, err
}
stdinFileName := filepath.Join(workspaceDir, "lsp_stdin.log")
stdinFile, err := os.Create(stdinFileName)
if err != nil {
return nil, err
}
stderrFileName := filepath.Join(workspaceDir, "lsp_stderr.log")
stderrFile, err := os.Create(stderrFileName)
if err != nil {
return nil, err
}
go func() {
stderrScanner := bufio.NewScanner(stderr)
for stderrScanner.Scan() {
_, err := stderrFile.Write(stderrScanner.Bytes())
if err != nil {
panic(err)
}
stderrFile.Sync()
}
err := stderrFile.Close()
if err != nil {
panic(err)
}
}()
if err := cmd.Start(); err != nil {
return nil, err
}
jsonRpcLogFile := filepath.Join(workspaceDir, "json_rpc.log")
jsonRpcLog, err := os.Create(jsonRpcLogFile)
if err != nil {
return nil, err
}
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.TimeKey = "timestamp"
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
core := zapcore.NewCore(
zapcore.NewJSONEncoder(encoderConfig), // Encoder configuration
zapcore.AddSync(jsonRpcLog), // Log file
zap.DebugLevel, // Log level
)
// Create the logger
logger := zap.New(core)
rwc := &CombinedReadWriteCloser{
Reader: stdout,
Writer: stdin,
Closer: func() error {
err2 := stdin.Close()
err1 := stdout.Close()
if err1 != nil || err2 != nil {
return fmt.Errorf("Stdout:(%w) Stdin:(%w)", err1, err2)
}
return cmd.Process.Kill()
},
readFile: stdoutFile,
writeFile: stdinFile,
}
stream := jsonrpc2.NewStream(rwc)
conn := jsonrpc2.NewConn(stream)
if clientConn == nil {
lspClient := protocol.ClientDispatcher(conn, logger)
clientConn = &lspClient
}
ctx, conn, server := protocol.NewClient(context.Background(), *clientConn, stream, zap.L())
result, err := server.Initialize(ctx, params)
if err != nil {
return nil, err
}
childLs := &ChildLanguageServer{
Conn: conn,
Cmd: cmd,
Server: server,
Context: ctx,
Close: func() {
logger.Sync()
jsonRpcLog.Close()
stdoutFile.Close()
stdinFile.Close()
stderrFile.Close()
conn.Close()
},
InitResult: result,
}
go func() {
cmd.Wait()
}()
return childLs, nil
}