forked from konveyor/analyzer-lsp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_client.go
More file actions
254 lines (218 loc) · 6.7 KB
/
Copy pathservice_client.go
File metadata and controls
254 lines (218 loc) · 6.7 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package nodejs
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"strings"
"github.com/go-logr/logr"
base "github.com/konveyor/analyzer-lsp/lsp/base_service_client"
"github.com/konveyor/analyzer-lsp/lsp/protocol"
"github.com/konveyor/analyzer-lsp/provider"
"github.com/swaggest/openapi-go/openapi3"
"go.lsp.dev/uri"
"gopkg.in/yaml.v2"
)
type NodeServiceClientConfig struct {
base.LSPServiceClientConfig `yaml:",inline"`
blah int `yaml:",inline"`
}
// Tidy aliases
type serviceClientFn = base.LSPServiceClientFunc[*NodeServiceClient]
type NodeServiceClient struct {
*base.LSPServiceClientBase
*base.LSPServiceClientEvaluator[*NodeServiceClient]
Config NodeServiceClientConfig
}
type NodeServiceClientBuilder struct{}
func (n *NodeServiceClientBuilder) Init(ctx context.Context, log logr.Logger, c provider.InitConfig) (provider.ServiceClient, error) {
sc := &NodeServiceClient{}
// Unmarshal the config
b, _ := yaml.Marshal(c.ProviderSpecificConfig)
err := yaml.Unmarshal(b, &sc.Config)
if err != nil {
return nil, err
}
params := protocol.InitializeParams{}
// treat location as the only workspace folder
if c.Location != "" {
if !strings.HasPrefix(c.Location, "file://") {
c.Location = "file://" + c.Location
}
sc.Config.WorkspaceFolders = []string{c.Location}
}
if c.ProviderSpecificConfig == nil {
c.ProviderSpecificConfig = map[string]interface{}{}
}
c.ProviderSpecificConfig["workspaceFolders"] = sc.Config.WorkspaceFolders
if len(sc.Config.WorkspaceFolders) == 0 {
params.RootURI = ""
} else {
params.RootURI = sc.Config.WorkspaceFolders[0]
}
var workspaceFolders []protocol.WorkspaceFolder
seen := make(map[string]bool)
for _, f := range sc.Config.WorkspaceFolders {
if seen[f] {
continue
}
seen[f] = true
workspaceFolders = append(workspaceFolders, protocol.WorkspaceFolder{
URI: f,
Name: filepath.Base(strings.ReplaceAll(f, "file://", "")),
})
}
params.WorkspaceFolders = workspaceFolders
params.Capabilities = protocol.ClientCapabilities{
Workspace: &protocol.WorkspaceClientCapabilities{
WorkspaceFolders: true,
Diagnostics: &protocol.DiagnosticWorkspaceClientCapabilities{
RefreshSupport: true,
},
},
TextDocument: &protocol.TextDocumentClientCapabilities{
Definition: &protocol.DefinitionClientCapabilities{
LinkSupport: true,
},
DocumentSymbol: &protocol.DocumentSymbolClientCapabilities{
HierarchicalDocumentSymbolSupport: true,
},
},
}
var InitializationOptions map[string]any
err = json.Unmarshal([]byte(sc.Config.LspServerInitializationOptions), &InitializationOptions)
if err != nil {
// fmt.Printf("Could not unmarshal into map[string]any: %s\n", sc.Config.LspServerInitializationOptions)
params.InitializationOptions = map[string]any{}
} else {
params.InitializationOptions = InitializationOptions
}
// Initialize the base client
scBase, err := base.NewLSPServiceClientBase(
ctx, log, c,
base.LogHandler(log),
params,
NewNodejsSymbolCacheHelper(log, c),
)
if err != nil {
return nil, err
}
sc.LSPServiceClientBase = scBase
// Initialize the fancy evaluator (dynamic dispatch ftw)
eval, err := base.NewLspServiceClientEvaluator(sc, n.GetGenericServiceClientCapabilities(log))
if err != nil {
return nil, err
}
sc.LSPServiceClientEvaluator = eval
return sc, nil
}
func (n *NodeServiceClientBuilder) GetGenericServiceClientCapabilities(log logr.Logger) []base.LSPServiceClientCapability {
caps := []base.LSPServiceClientCapability{}
r := openapi3.NewReflector()
refCap, err := provider.ToProviderCap(r, log, referencedCondition{}, "referenced")
if err != nil {
log.Error(err, "unable to get referenced cap")
} else {
caps = append(caps, base.LSPServiceClientCapability{
Capability: refCap,
Fn: serviceClientFn((*NodeServiceClient).EvaluateReferenced),
})
}
return caps
}
type resp = provider.ProviderEvaluateResponse
// Example condition
type referencedCondition struct {
Referenced struct {
Pattern string `yaml:"pattern"`
} `yaml:"referenced"`
provider.ProviderContext `yaml:",inline"`
}
// Example evaluate
func (sc *NodeServiceClient) EvaluateReferenced(ctx context.Context, cap string, info []byte) (provider.ProviderEvaluateResponse, error) {
var cond referencedCondition
err := yaml.Unmarshal(info, &cond)
if err != nil {
return resp{}, fmt.Errorf("error unmarshaling query info")
}
query := cond.Referenced.Pattern
if query == "" {
return resp{}, fmt.Errorf("unable to get query info")
}
// Query symbols once after all files are indexed
symbols := sc.GetAllDeclarations(ctx, query, false)
incidentsMap, err := sc.EvaluateSymbols(ctx, symbols)
if err != nil {
return resp{}, err
}
incidents := []provider.IncidentContext{}
for _, incident := range incidentsMap {
incidents = append(incidents, incident)
}
if len(incidents) == 0 {
return resp{Matched: false}, nil
}
return resp{
Matched: true,
Incidents: incidents,
}, nil
}
func (sc *NodeServiceClient) EvaluateSymbols(ctx context.Context, symbols []protocol.WorkspaceSymbol) (map[string]provider.IncidentContext, error) {
incidentsMap := make(map[string]provider.IncidentContext)
for _, s := range symbols {
baseLocation, ok := s.Location.Value.(protocol.Location)
if !ok {
sc.Log.V(7).Info("unable to get base location", "symbol", s)
continue
}
// Look for things that are in the location loaded,
// Note may need to filter out vendor at some point
if len(sc.BaseConfig.WorkspaceFolders) < 1 || !strings.Contains(baseLocation.URI, sc.BaseConfig.WorkspaceFolders[0]) {
continue
}
skip := false
for _, substr := range sc.BaseConfig.DependencyFolders {
if substr == "" {
continue
}
if strings.Contains(baseLocation.URI, substr) {
skip = true
break
}
}
if skip {
continue
}
u, err := uri.Parse(baseLocation.URI)
if err != nil {
return nil, err
}
lineNumber := int(baseLocation.Range.Start.Line) + 1
incident := provider.IncidentContext{
FileURI: u,
LineNumber: &lineNumber,
Variables: map[string]interface{}{
"file": baseLocation.URI,
},
CodeLocation: &provider.Location{
StartPosition: provider.Position{
Line: float64(lineNumber),
Character: float64(baseLocation.Range.Start.Character),
},
EndPosition: provider.Position{
Line: float64(lineNumber),
Character: float64(baseLocation.Range.End.Character),
},
},
}
b, _ := json.Marshal(incident)
incidentsMap[string(b)] = incident
}
return incidentsMap, nil
}
func (sc *NodeServiceClient) GetDependencies(ctx context.Context) (map[uri.URI][]*provider.Dep, error) {
return nil, nil
}
func (sc *NodeServiceClient) GetDependenciesDAG(ctx context.Context) (map[uri.URI][]provider.DepDAGItem, error) {
return nil, nil
}