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