Debian Codegraph @

powernap/pkg/lsp/protocol/interface.go

raw
1 // Package protocol provides types and functions for the Language Server
2 // Protocol (LSP).
3 package protocol
4
5 import "fmt"
6
7 // WorkspaceSymbolResult is an interface for types that represent workspace symbols
8 type WorkspaceSymbolResult interface {
9 GetName() string
10 GetLocation() Location
11 isWorkspaceSymbol() // marker method
12 }
13
14 func (ws *WorkspaceSymbol) GetName() string { return ws.Name }
15 func (ws *WorkspaceSymbol) GetLocation() Location {
16 switch v := ws.Location.Value.(type) {
17 case Location:
18 return v
19 case LocationUriOnly:
20 return Location{URI: v.URI}
21 }
22 return Location{}
23 }
24 func (ws *WorkspaceSymbol) isWorkspaceSymbol() {}
25
26 func (si *SymbolInformation) GetName() string { return si.Name }
27 func (si *SymbolInformation) GetLocation() Location { return si.Location }
28 func (si *SymbolInformation) isWorkspaceSymbol() {}
29
30 // Results converts the Value to a slice of WorkspaceSymbolResult
31 func (r Or_Result_workspace_symbol) Results() ([]WorkspaceSymbolResult, error) {
32 if r.Value == nil {
33 return make([]WorkspaceSymbolResult, 0), nil
34 }
35 switch v := r.Value.(type) {
36 case []WorkspaceSymbol:
37 results := make([]WorkspaceSymbolResult, len(v))
38 for i := range v {
39 results[i] = &v[i]
40 }
41 return results, nil
42 case []SymbolInformation:
43 results := make([]WorkspaceSymbolResult, len(v))
44 for i := range v {
45 results[i] = &v[i]
46 }
47 return results, nil
48 default:
49 return nil, fmt.Errorf("unknown symbol type: %T", r.Value)
50 }
51 }
52
53 // DocumentSymbolResult is an interface for types that represent document symbols
54 type DocumentSymbolResult interface {
55 GetRange() Range
56 GetName() string
57 isDocumentSymbol() // marker method
58 }
59
60 func (ds *DocumentSymbol) GetRange() Range { return ds.Range }
61 func (ds *DocumentSymbol) GetName() string { return ds.Name }
62 func (ds *DocumentSymbol) isDocumentSymbol() {}
63
64 func (si *SymbolInformation) GetRange() Range { return si.Location.Range }
65
66 // Note: SymbolInformation already has GetName() implemented above
67 func (si *SymbolInformation) isDocumentSymbol() {}
68
69 // Results converts the Value to a slice of DocumentSymbolResult
70 func (r Or_Result_textDocument_documentSymbol) Results() ([]DocumentSymbolResult, error) {
71 if r.Value == nil {
72 return make([]DocumentSymbolResult, 0), nil
73 }
74 switch v := r.Value.(type) {
75 case []DocumentSymbol:
76 results := make([]DocumentSymbolResult, len(v))
77 for i := range v {
78 results[i] = &v[i]
79 }
80 return results, nil
81 case []SymbolInformation:
82 results := make([]DocumentSymbolResult, len(v))
83 for i := range v {
84 results[i] = &v[i]
85 }
86 return results, nil
87 default:
88 return nil, fmt.Errorf("unknown document symbol type: %T", v)
89 }
90 }
91
92 // TextEditResult is an interface for types that can be used as text edits
93 type TextEditResult interface {
94 GetRange() Range
95 GetNewText() string
96 isTextEdit() // marker method
97 }
98
99 func (te *TextEdit) GetRange() Range { return te.Range }
100 func (te *TextEdit) GetNewText() string { return te.NewText }
101 func (te *TextEdit) isTextEdit() {}
102
103 // AsTextEdit converts Or_TextDocumentEdit_edits_Elem to TextEdit
104 func (e Or_TextDocumentEdit_edits_Elem) AsTextEdit() (TextEdit, error) {
105 if e.Value == nil {
106 return TextEdit{}, fmt.Errorf("nil text edit")
107 }
108 switch v := e.Value.(type) {
109 case TextEdit:
110 return v, nil
111 case AnnotatedTextEdit:
112 return TextEdit{
113 Range: v.Range,
114 NewText: v.NewText,
115 }, nil
116 default:
117 return TextEdit{}, fmt.Errorf("unknown text edit type: %T", e.Value)
118 }
119 }