interface.go 3.3 KB

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