-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathprovider.go
192 lines (170 loc) · 4.96 KB
/
provider.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package dotnet
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"github.com/swaggest/openapi-go/openapi3"
"github.com/go-logr/logr"
"github.com/konveyor/analyzer-lsp/provider"
"go.lsp.dev/jsonrpc2"
"go.lsp.dev/protocol"
"go.lsp.dev/uri"
)
type dotnetProvider struct{
Log logr.Logger
}
var _ provider.BaseClient = &dotnetProvider{}
func NewDotnetProvider(log logr.Logger) *dotnetProvider {
return &dotnetProvider{
Log: log,
}
}
type stdioRWCloser struct {
io.Reader
io.Writer
}
type dotnetCondition struct {
Referenced referenceCondition `yaml:"referenced"`
}
// Example:
// dotnet.referenced:
// namespace: System.Web.Mvc
// pattern: HttpNotFound
type referenceCondition struct {
Namespace string `yaml:"namespace"`
Pattern string `yaml:"pattern"`
}
func (r *stdioRWCloser) Close() error {
return nil
}
func (p *dotnetProvider) Capabilities() []provider.Capability {
r := openapi3.NewReflector()
caps := []provider.Capability{}
refCap, err := provider.ToProviderCap(r, p.Log, dotnetCondition{}, "referenced")
if err != nil {
p.Log.Error(err, "failed to registery capability")
} else {
caps = append(caps, refCap)
}
return caps
}
func (p *dotnetProvider) Init(ctx context.Context, log logr.Logger, config provider.InitConfig) (provider.ServiceClient, error) {
var mode provider.AnalysisMode = provider.AnalysisMode(config.AnalysisMode)
if mode != provider.FullAnalysisMode {
return nil, fmt.Errorf("only full analysis is supported")
}
// handle proxy settings
for k, v := range config.Proxy.ToEnvVars() {
os.Setenv(k, v)
}
codePath, err := filepath.Abs(config.Location)
if err != nil {
log.Error(err, "unable to get path to analyze")
return nil, err
}
ctx, cancelFunc := context.WithCancel(ctx)
log = log.WithValues("provider", "dotnet")
sentLog := &sent{l: log.WithValues("stdio", "sent")}
recvLog := &received{l: log.WithValues("stdio", "recv")}
handlerLog := log.WithValues("stdio", "replyHandler")
lspServerPath, ok := config.ProviderSpecificConfig[provider.LspServerPathConfigKey].(string)
if !ok || lspServerPath == "" {
cancelFunc()
return nil, fmt.Errorf("invalid lspServerPath provided, unable to init dotnet provider")
}
cmd := exec.CommandContext(ctx, lspServerPath)
cmd.Dir = codePath // At a minimum, 'csharp-ls' doesn't respect URI @initialization
stdin, err := cmd.StdinPipe()
if err != nil {
cancelFunc()
return nil, err
}
clientWriter := io.MultiWriter(stdin, sentLog)
stdout, err := cmd.StdoutPipe()
if err != nil {
cancelFunc()
return nil, err
}
clientReader := io.TeeReader(stdout, recvLog)
if err := cmd.Start(); err != nil {
log.Error(err, "failed to start language server process")
cancelFunc()
return nil, err
}
log.V(2).Info("language server started")
// Unlike the golang-external-provider, we need to be able to respond
// to requests from the server. This requires us to startup a server
// to handle those requests.
serverChannel := make(chan int)
h := &handler{
log: &handlerLog,
ch: serverChannel,
}
conn := jsonrpc2.NewConn(jsonrpc2.NewStream(&stdioRWCloser{
Reader: clientReader,
Writer: clientWriter,
}))
go func() {
err := jsonrpc2.HandlerServer(jsonrpc2.ReplyHandler(h.replyHandler)).ServeStream(ctx, conn)
if err != nil {
if errors.Is(err, io.EOF) {
handlerLog.Info("received eof", "canceled", errors.Is(ctx.Err(), context.Canceled))
return
}
handlerLog.Error(err, "something bad happened to our client side server")
return
}
}()
log.V(2).Info("language server connection established")
log.V(2).Info("initializing language server")
var initializeResult protocol.InitializeResult
for {
if _, err := conn.Call(ctx, protocol.MethodInitialize, &protocol.InitializeParams{
RootURI: uri.File(codePath),
Capabilities: protocol.ClientCapabilities{
TextDocument: &protocol.TextDocumentClientCapabilities{
DocumentSymbol: &protocol.DocumentSymbolClientCapabilities{
HierarchicalDocumentSymbolSupport: true,
},
},
Workspace: &protocol.WorkspaceClientCapabilities{
DidChangeWatchedFiles: &protocol.DidChangeWatchedFilesWorkspaceClientCapabilities{
DynamicRegistration: false,
},
// WorkspaceFolders: true,
},
},
// WorkspaceFolders: []protocol.WorkspaceFolder{
// protocol.WorkspaceFolder{
// URI: "/opt/app-root/src",
// Name: "workspace",
// },
// },
}, &initializeResult); err != nil {
log.Error(err, "initialize failed, will try again")
continue
}
break
}
log.V(2).Info("language server initialized")
if err := conn.Notify(ctx, protocol.MethodInitialized, &protocol.InitializedParams{}); err != nil {
log.Error(err, "initialized notification failed")
cancelFunc()
return nil, err
}
log.Info("waiting for language server to load the project")
<-serverChannel
log.Info("project loaded")
return &dotnetServiceClient{
rpc: conn,
ctx: ctx,
cancelFunc: cancelFunc,
cmd: cmd,
log: log,
config: config,
}, nil
}