forked from sqls-server/sqls
/
hover.go
542 lines (502 loc) · 12.9 KB
/
hover.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package handler
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/mikeismert/sqls/ast"
"github.com/mikeismert/sqls/ast/astutil"
"github.com/mikeismert/sqls/internal/database"
"github.com/mikeismert/sqls/internal/lsp"
"github.com/mikeismert/sqls/parser"
"github.com/mikeismert/sqls/parser/parseutil"
"github.com/mikeismert/sqls/token"
"github.com/sourcegraph/jsonrpc2"
)
var ErrNoHover = errors.New("no hover infomation found")
func (s *Server) handleTextDocumentHover(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
if req.Params == nil {
return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeInvalidParams}
}
var params lsp.HoverParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
f, ok := s.files[params.TextDocument.URI]
if !ok {
return nil, fmt.Errorf("document not found: %s", params.TextDocument.URI)
}
res, err := hover(f.Text, params, s.worker.Cache())
if err != nil {
if errors.Is(ErrNoHover, err) {
return nil, nil
}
return nil, err
}
return res, nil
}
func hover(text string, params lsp.HoverParams, dbCache *database.DBCache) (*lsp.Hover, error) {
if dbCache == nil {
return nil, nil
}
pos := token.Pos{
Line: params.Position.Line,
Col: params.Position.Character + 1,
}
parsed, err := parser.Parse(text)
if err != nil {
return nil, err
}
// Find identifiers from focused statement
nodeWalker := parseutil.NewNodeWalker(parsed, pos)
hoverTargetMatcher := astutil.NodeMatcher{
NodeTypes: []ast.NodeType{
ast.TypeMemberIdentifer,
ast.TypeIdentifer,
},
}
focusedIdentNodes := nodeWalker.CurNodeMatches(hoverTargetMatcher)
if len(focusedIdentNodes) == 0 {
return nil, ErrNoHover
}
ident, memIdent := findIdent(focusedIdentNodes)
// Collect environment
hoverEnv, err := collectEnvirontment(parsed, pos)
if err != nil {
return nil, err
}
// Check hover type
ctx := getHoverTypes(nodeWalker, hoverEnv)
// Create hover contents
var hoverContent *lsp.MarkupContent
if ident != nil && memIdent != nil {
identName := ident.NoQuateString()
parentName := memIdent.ParentTok.NoQuateString()
childName := memIdent.ChildTok.NoQuateString()
if identName == parentName {
// The cursor is on the member identifier parent.
// example "w[o]rld.city"
hoverContent = hoverContentFromParentIdent(ctx, identName, dbCache, hoverEnv)
} else if identName == childName {
// The cursor is on the member identifier child.
// example "world.c[i]ty"
hoverContent = hoverContentFromChildIdent(ctx, identName, dbCache, hoverEnv)
} else {
// Invalid
hoverContent = nil
}
} else if ident == nil && memIdent != nil {
// The cursor is on the dot with the member identifier
// example "world[.]city"
hoverContent = hoverContentFromChildIdent(ctx, memIdent.ChildTok.NoQuateString(), dbCache, hoverEnv)
} else if ident != nil && memIdent == nil {
// The cursor is on the identifier
// example "c[i]ty"
hoverContent = hoverContentFromIdent(ctx, ident.NoQuateString(), dbCache, hoverEnv)
}
if hoverContent == nil {
return nil, ErrNoHover
}
var posIdent ast.Node
posIdent = ident
if ident == nil && memIdent != nil {
posIdent = memIdent
}
res := &lsp.Hover{
Contents: *hoverContent,
Range: lsp.Range{
Start: lsp.Position{
Line: posIdent.Pos().Line,
Character: posIdent.Pos().Col,
},
End: lsp.Position{
Line: posIdent.End().Line,
Character: posIdent.End().Col,
},
},
}
return res, nil
}
type hoverEnvironment struct {
aliases []ast.Node
tables []*parseutil.TableInfo
subQueries []*parseutil.SubQueryInfo
}
func (e *hoverEnvironment) getTableRealName(aliasName string) (string, bool) {
for _, table := range e.tables {
if table.Alias == aliasName {
return table.Name, true
}
}
return "", false
}
func (e *hoverEnvironment) getColumnRealName(aliasedName string) (string, bool) {
for _, v := range e.aliases {
alias, _ := v.(*ast.Aliased)
if alias.AliasedName.String() == aliasedName {
switch v := alias.RealName.(type) {
case *ast.Identifer:
return v.String(), true
case *ast.MemberIdentifer:
return v.Child.String(), true
}
}
}
return "", false
}
func (e *hoverEnvironment) getSubQueryView(name string) (*parseutil.SubQueryInfo, bool) {
for _, subQuery := range e.subQueries {
if subQuery.Name == name {
return subQuery, true
}
}
return nil, false
}
func (e *hoverEnvironment) getSubQueryViewOne() (*parseutil.SubQueryInfo, bool) {
if len(e.subQueries) == 1 {
return e.subQueries[0], true
}
return nil, false
}
func (e *hoverEnvironment) isSubQuery(name string) bool {
_, ok := e.getSubQueryView(name)
return ok
}
func collectEnvirontment(parsed ast.TokenList, pos token.Pos) (*hoverEnvironment, error) {
// Collect environment infomation
aliases := parseutil.ExtractAliasedIdentifer(parsed)
definedTables, err := parseutil.ExtractTable(parsed, pos)
if err != nil {
return nil, err
}
subQueries, err := parseutil.ExtractSubQueryViews(parsed, pos)
if err != nil {
return nil, err
}
environment := &hoverEnvironment{
aliases: aliases,
tables: definedTables,
subQueries: subQueries,
}
return environment, nil
}
func findIdent(nodes []ast.Node) (*ast.Identifer, *ast.MemberIdentifer) {
var (
ident *ast.Identifer
memIdent *ast.MemberIdentifer
)
for _, node := range nodes {
switch v := node.(type) {
case *ast.Identifer:
ident = v
case *ast.MemberIdentifer:
memIdent = v
}
}
return ident, memIdent
}
func hoverContentFromIdent(ctx *hoverContext, identName string, dbCache *database.DBCache, hoverEnv *hoverEnvironment) *lsp.MarkupContent {
if hoverTypeIs(ctx.types, hoverTypeColumn) {
columnName := identName
if realName, ok := hoverEnv.getColumnRealName(columnName); ok {
columnName = realName
}
hoverContents := []*lsp.MarkupContent{}
for _, table := range hoverEnv.tables {
colDesc, ok := dbCache.Column(table.Name, columnName)
if ok {
hoverContents = append(
hoverContents,
columnHoverInfo(table.Name, columnName, colDesc),
)
}
}
if len(hoverContents) >= 2 {
return nil
}
if len(hoverContents) == 1 {
return hoverContents[0]
}
}
if hoverTypeIs(ctx.types, hoverTypeTable) {
// translate table alias
tableName := identName
for _, table := range hoverEnv.tables {
if table.Alias == tableName {
tableName = table.Name
}
}
// find table
cols, ok := dbCache.ColumnDescs(tableName)
if ok {
return tableHoverInfo(tableName, cols)
}
}
if hoverTypeIs(ctx.types, hoverTypeSubQueryColumn) {
columnName := identName
subQueryView, ok := hoverEnv.getSubQueryViewOne()
if !ok {
return nil
}
return subqueryColumnHoverInfo(columnName, subQueryView, dbCache)
}
return nil
}
func hoverContentFromParentIdent(ctx *hoverContext, identName string, dbCache *database.DBCache, hoverEnv *hoverEnvironment) *lsp.MarkupContent {
switch ctx.parent.Type {
case parentTypeNone:
return nil
case parentTypeSchema:
case parentTypeTable:
tableName := identName
realName, ok := hoverEnv.getTableRealName(tableName)
if ok {
tableName = realName
}
columns, ok := dbCache.ColumnDescs(tableName)
if ok {
return tableHoverInfo(tableName, columns)
}
case parentTypeSubQuery:
subQueryName := identName
subQueryView, ok := hoverEnv.getSubQueryView(subQueryName)
if !ok {
return nil
}
return subqueryHoverInfo(subQueryView, dbCache)
}
return nil
}
func hoverContentFromChildIdent(ctx *hoverContext, identName string, dbCache *database.DBCache, hoverEnv *hoverEnvironment) *lsp.MarkupContent {
switch ctx.parent.Type {
case parentTypeNone:
return nil
case parentTypeSchema:
columns, ok := dbCache.ColumnDescs(identName)
if ok {
return tableHoverInfo(identName, columns)
}
case parentTypeTable:
tableName := ctx.parent.Name
realName, ok := hoverEnv.getTableRealName(tableName)
if ok {
tableName = realName
}
if colDesc, ok := dbCache.Column(tableName, identName); ok {
return columnHoverInfo(tableName, identName, colDesc)
}
return nil
case parentTypeSubQuery:
subQueryView, ok := hoverEnv.getSubQueryView(ctx.parent.Name)
if !ok {
return nil
}
return subqueryColumnHoverInfo(identName, subQueryView, dbCache)
}
return nil
}
func columnHoverInfo(tableName, colName string, colDesc *database.ColumnDesc) *lsp.MarkupContent {
return &lsp.MarkupContent{
Kind: lsp.Markdown,
Value: database.ColumnDoc(tableName, colDesc),
}
}
func tableHoverInfo(tableName string, cols []*database.ColumnDesc) *lsp.MarkupContent {
return &lsp.MarkupContent{
Kind: lsp.Markdown,
Value: database.TableDoc(tableName, cols),
}
}
func subqueryHoverInfo(subQuery *parseutil.SubQueryInfo, dbCache *database.DBCache) *lsp.MarkupContent {
return &lsp.MarkupContent{
Kind: lsp.Markdown,
Value: database.SubqueryDoc(subQuery.Name, subQuery.Views, dbCache),
}
}
func subqueryColumnHoverInfo(identName string, subQuery *parseutil.SubQueryInfo, dbCache *database.DBCache) *lsp.MarkupContent {
return &lsp.MarkupContent{
Kind: lsp.Markdown,
Value: database.SubqueryColumnDoc(identName, subQuery.Views, dbCache),
}
}
func hoverTypeIs(hoverTypes []hoverType, expect hoverType) bool {
for _, t := range hoverTypes {
if t == expect {
return true
}
}
return false
}
type hoverType int
const (
_ hoverType = iota
hoverTypeKeyword
hoverTypeFunction
hoverTypeAlias
hoverTypeColumn
hoverTypeTable
hoverTypeView
hoverTypeSubQueryView
hoverTypeSubQueryColumn
hoverTypeChange
hoverTypeUser
hoverTypeSchema
)
type parentType int
const (
parentTypeNone parentType = iota
parentTypeSchema
parentTypeTable
parentTypeSubQuery
)
type hoverParent struct {
Type parentType
Name string
}
var noneParent = &hoverParent{Type: parentTypeNone}
type hoverContext struct {
types []hoverType
parent *hoverParent
}
func getHoverTypes(nw *parseutil.NodeWalker, hoverEnv *hoverEnvironment) *hoverContext {
memberIdentifierMatcher := astutil.NodeMatcher{
NodeTypes: []ast.NodeType{ast.TypeMemberIdentifer},
}
syntaxPos := parseutil.CheckSyntaxPosition(nw)
t := []hoverType{hoverTypeKeyword}
p := noneParent
switch {
case syntaxPos == parseutil.ColName:
if nw.CurNodeIs(memberIdentifierMatcher) {
// has parent
mi := nw.CurNodeTopMatched(memberIdentifierMatcher).(*ast.MemberIdentifer)
t = []hoverType{
hoverTypeColumn,
hoverTypeSubQueryColumn,
hoverTypeView,
hoverTypeFunction,
}
name := mi.Parent.String()
p = &hoverParent{
Type: parentTypeTable,
Name: name,
}
if hoverEnv.isSubQuery(name) {
p = &hoverParent{
Type: parentTypeSubQuery,
Name: name,
}
}
} else {
t = []hoverType{
hoverTypeColumn,
hoverTypeTable,
hoverTypeSubQueryColumn,
hoverTypeSubQueryView,
hoverTypeAlias,
hoverTypeView,
hoverTypeFunction,
hoverTypeKeyword,
}
}
case syntaxPos == parseutil.AliasName:
// pass
case syntaxPos == parseutil.SelectExpr || syntaxPos == parseutil.CaseValue:
if nw.CurNodeIs(memberIdentifierMatcher) {
// has parent
mi := nw.CurNodeTopMatched(memberIdentifierMatcher).(*ast.MemberIdentifer)
t = []hoverType{
hoverTypeColumn,
hoverTypeView,
hoverTypeSubQueryColumn,
hoverTypeFunction,
}
name := mi.Parent.String()
p = &hoverParent{
Type: parentTypeTable,
Name: name,
}
if hoverEnv.isSubQuery(name) {
p = &hoverParent{
Type: parentTypeSubQuery,
Name: name,
}
}
} else {
t = []hoverType{
hoverTypeColumn,
hoverTypeTable,
hoverTypeAlias,
hoverTypeView,
hoverTypeSubQueryColumn,
hoverTypeSubQueryView,
hoverTypeFunction,
hoverTypeKeyword,
}
}
case syntaxPos == parseutil.TableReference:
if nw.CurNodeIs(memberIdentifierMatcher) {
// has parent
mi := nw.CurNodeTopMatched(memberIdentifierMatcher).(*ast.MemberIdentifer)
t = []hoverType{
hoverTypeTable,
hoverTypeView,
hoverTypeSubQueryColumn,
hoverTypeFunction,
}
p = &hoverParent{
Type: parentTypeSchema,
Name: mi.Parent.String(),
}
} else {
t = []hoverType{
hoverTypeColumn,
hoverTypeTable,
hoverTypeSchema,
hoverTypeView,
hoverTypeSubQueryColumn,
hoverTypeSubQueryView,
hoverTypeFunction,
hoverTypeKeyword,
}
}
case syntaxPos == parseutil.WhereCondition:
if nw.CurNodeIs(memberIdentifierMatcher) {
// has parent
mi := nw.CurNodeTopMatched(memberIdentifierMatcher).(*ast.MemberIdentifer)
t = []hoverType{
hoverTypeTable,
hoverTypeView,
hoverTypeSubQueryColumn,
hoverTypeFunction,
}
p = &hoverParent{
Type: parentTypeTable,
Name: mi.Parent.String(),
}
} else {
t = []hoverType{
hoverTypeColumn,
hoverTypeTable,
hoverTypeSchema,
hoverTypeView,
hoverTypeSubQueryColumn,
hoverTypeSubQueryView,
hoverTypeFunction,
hoverTypeKeyword,
}
}
case syntaxPos == parseutil.InsertColumn:
t = []hoverType{
hoverTypeColumn,
hoverTypeTable,
hoverTypeView,
}
default:
// pass
}
return &hoverContext{
types: t,
parent: p,
}
}