gitstack

grindlemire/go-tui code browser

7.4 KB Go 242 lines 2026-01-31 · 3130a1f raw
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
package lsp

import (
	"encoding/json"

	"github.com/grindlemire/go-tui/internal/lsp/log"
)

// Router dispatches LSP method requests to the appropriate handler.
// All language feature methods are dispatched through providers in the Registry.
// Lifecycle and document sync methods are handled directly by the Server.
type Router struct {
	server   *Server
	registry *Registry
}

// NewRouter creates a new Router with the given server and optional provider registry.
func NewRouter(server *Server, registry *Registry) *Router {
	return &Router{
		server:   server,
		registry: registry,
	}
}

// Route dispatches a request to the appropriate handler.
// This replaces the old Server.route method, adding support for provider-based dispatch.
func (r *Router) Route(req Request) (any, *Error) {
	switch req.Method {
	// Lifecycle
	case "initialize":
		return r.server.handleInitialize(req.Params)
	case "initialized":
		return r.server.handleInitialized()
	case "shutdown":
		return r.server.handleShutdown()
	case "exit":
		r.server.handleExit()
		return nil, nil

	// Document synchronization
	case "textDocument/didOpen":
		return r.server.handleDidOpen(req.Params)
	case "textDocument/didChange":
		return r.server.handleDidChange(req.Params)
	case "textDocument/didClose":
		return r.server.handleDidClose(req.Params)
	case "textDocument/didSave":
		return r.server.handleDidSave(req.Params)

	// Language features - all dispatched to providers
	case "textDocument/hover":
		return r.handleHover(req.Params)
	case "textDocument/completion":
		return r.handleCompletion(req.Params)
	case "textDocument/definition":
		return r.handleDefinition(req.Params)
	case "textDocument/references":
		return r.handleReferences(req.Params)
	case "textDocument/documentSymbol":
		return r.handleDocumentSymbol(req.Params)
	case "workspace/symbol":
		return r.handleWorkspaceSymbol(req.Params)
	case "textDocument/formatting":
		return r.handleFormatting(req.Params)
	case "textDocument/semanticTokens/full":
		return r.handleSemanticTokensFull(req.Params)

	default:
		log.Server("Unknown method: %s", req.Method)
		return nil, &Error{Code: CodeMethodNotFound, Message: "Method not found: " + req.Method}
	}
}

// --- Provider-aware dispatch methods ---
// Each method checks if a provider is registered; if so, it resolves a CursorContext
// and dispatches to the provider. Otherwise, it falls back to the legacy handler.

func (r *Router) handleHover(params json.RawMessage) (any, *Error) {
	if r.registry != nil && r.registry.Hover != nil {
		return r.dispatchPositional(params, func(ctx *CursorContext) (any, error) {
			return r.registry.Hover.Hover(ctx)
		})
	}
	log.Server("WARN: hover provider not registered")
	return nil, nil
}

func (r *Router) handleCompletion(params json.RawMessage) (any, *Error) {
	if r.registry != nil && r.registry.Completion != nil {
		return r.dispatchPositional(params, func(ctx *CursorContext) (any, error) {
			return r.registry.Completion.Complete(ctx)
		})
	}
	log.Server("WARN: completion provider not registered")
	return nil, nil
}

func (r *Router) handleDefinition(params json.RawMessage) (any, *Error) {
	if r.registry != nil && r.registry.Definition != nil {
		return r.dispatchPositional(params, func(ctx *CursorContext) (any, error) {
			return r.registry.Definition.Definition(ctx)
		})
	}
	log.Server("WARN: definition provider not registered")
	return nil, nil
}

func (r *Router) handleReferences(params json.RawMessage) (any, *Error) {
	if r.registry != nil && r.registry.References != nil {
		// References has an extra includeDeclaration param
		var p struct {
			TextDocument TextDocumentIdentifier `json:"textDocument"`
			Position     Position               `json:"position"`
			Context      struct {
				IncludeDeclaration bool `json:"includeDeclaration"`
			} `json:"context"`
		}
		if err := json.Unmarshal(params, &p); err != nil {
			return nil, &Error{Code: CodeInvalidParams, Message: err.Error()}
		}

		doc := r.server.docs.Get(p.TextDocument.URI)
		if doc == nil {
			return nil, nil
		}

		ctx := ResolveCursorContext(doc, p.Position)
		result, err := r.registry.References.References(ctx, p.Context.IncludeDeclaration)
		if err != nil {
			return nil, &Error{Code: CodeInternalError, Message: err.Error()}
		}
		return result, nil
	}
	log.Server("WARN: references provider not registered")
	return nil, nil
}

func (r *Router) handleDocumentSymbol(params json.RawMessage) (any, *Error) {
	if r.registry != nil && r.registry.DocumentSymbol != nil {
		var p DocumentSymbolParams
		if err := json.Unmarshal(params, &p); err != nil {
			return nil, &Error{Code: CodeInvalidParams, Message: err.Error()}
		}

		doc := r.server.docs.Get(p.TextDocument.URI)
		if doc == nil {
			return nil, nil
		}

		result, err := r.registry.DocumentSymbol.DocumentSymbols(doc)
		if err != nil {
			return nil, &Error{Code: CodeInternalError, Message: err.Error()}
		}
		return result, nil
	}
	log.Server("WARN: documentSymbol provider not registered")
	return nil, nil
}

func (r *Router) handleWorkspaceSymbol(params json.RawMessage) (any, *Error) {
	if r.registry != nil && r.registry.WorkspaceSymbol != nil {
		var p WorkspaceSymbolParams
		if err := json.Unmarshal(params, &p); err != nil {
			return nil, &Error{Code: CodeInvalidParams, Message: err.Error()}
		}

		result, err := r.registry.WorkspaceSymbol.WorkspaceSymbols(p.Query)
		if err != nil {
			return nil, &Error{Code: CodeInternalError, Message: err.Error()}
		}
		return result, nil
	}
	log.Server("WARN: workspaceSymbol provider not registered")
	return nil, nil
}

func (r *Router) handleFormatting(params json.RawMessage) (any, *Error) {
	if r.registry != nil && r.registry.Formatting != nil {
		var p DocumentFormattingParams
		if err := json.Unmarshal(params, &p); err != nil {
			return nil, &Error{Code: CodeInvalidParams, Message: err.Error()}
		}

		doc := r.server.docs.Get(p.TextDocument.URI)
		if doc == nil {
			return nil, nil
		}

		result, err := r.registry.Formatting.Format(doc, p.Options)
		if err != nil {
			return nil, &Error{Code: CodeInternalError, Message: err.Error()}
		}
		return result, nil
	}
	log.Server("WARN: formatting provider not registered")
	return nil, nil
}

func (r *Router) handleSemanticTokensFull(params json.RawMessage) (any, *Error) {
	if r.registry != nil && r.registry.SemanticTokens != nil {
		var p SemanticTokensParams
		if err := json.Unmarshal(params, &p); err != nil {
			return nil, &Error{Code: CodeInvalidParams, Message: err.Error()}
		}

		doc := r.server.docs.Get(p.TextDocument.URI)
		if doc == nil {
			return nil, nil
		}

		result, err := r.registry.SemanticTokens.SemanticTokensFull(doc)
		if err != nil {
			return nil, &Error{Code: CodeInternalError, Message: err.Error()}
		}
		return result, nil
	}
	return &SemanticTokens{Data: []int{}}, nil
}

// dispatchPositional is a helper that parses a textDocument/position request,
// resolves a CursorContext, and dispatches to a provider function.
func (r *Router) dispatchPositional(params json.RawMessage, fn func(ctx *CursorContext) (any, error)) (any, *Error) {
	var p struct {
		TextDocument TextDocumentIdentifier `json:"textDocument"`
		Position     Position               `json:"position"`
	}
	if err := json.Unmarshal(params, &p); err != nil {
		return nil, &Error{Code: CodeInvalidParams, Message: err.Error()}
	}

	doc := r.server.docs.Get(p.TextDocument.URI)
	if doc == nil {
		return nil, nil
	}

	ctx := ResolveCursorContext(doc, p.Position)
	result, err := fn(ctx)
	if err != nil {
		return nil, &Error{Code: CodeInternalError, Message: err.Error()}
	}
	return result, nil
}