1package lsp
2
3import (
4 "encoding/json"
5 "log"
6
7 "github.com/kujtimiihoxha/termai/internal/lsp/protocol"
8 "github.com/kujtimiihoxha/termai/internal/lsp/util"
9)
10
11// Requests
12
13func HandleWorkspaceConfiguration(params json.RawMessage) (any, error) {
14 return []map[string]any{{}}, nil
15}
16
17func HandleRegisterCapability(params json.RawMessage) (any, error) {
18 var registerParams protocol.RegistrationParams
19 if err := json.Unmarshal(params, ®isterParams); err != nil {
20 log.Printf("Error unmarshaling registration params: %v", err)
21 return nil, err
22 }
23
24 for _, reg := range registerParams.Registrations {
25 switch reg.Method {
26 case "workspace/didChangeWatchedFiles":
27 // Parse the registration options
28 optionsJSON, err := json.Marshal(reg.RegisterOptions)
29 if err != nil {
30 log.Printf("Error marshaling registration options: %v", err)
31 continue
32 }
33
34 var options protocol.DidChangeWatchedFilesRegistrationOptions
35 if err := json.Unmarshal(optionsJSON, &options); err != nil {
36 log.Printf("Error unmarshaling registration options: %v", err)
37 continue
38 }
39
40 // Store the file watchers registrations
41 notifyFileWatchRegistration(reg.ID, options.Watchers)
42 }
43 }
44
45 return nil, nil
46}
47
48func HandleApplyEdit(params json.RawMessage) (any, error) {
49 var edit protocol.ApplyWorkspaceEditParams
50 if err := json.Unmarshal(params, &edit); err != nil {
51 return nil, err
52 }
53
54 err := util.ApplyWorkspaceEdit(edit.Edit)
55 if err != nil {
56 log.Printf("Error applying workspace edit: %v", err)
57 return protocol.ApplyWorkspaceEditResult{Applied: false, FailureReason: err.Error()}, nil
58 }
59
60 return protocol.ApplyWorkspaceEditResult{Applied: true}, nil
61}
62
63// FileWatchRegistrationHandler is a function that will be called when file watch registrations are received
64type FileWatchRegistrationHandler func(id string, watchers []protocol.FileSystemWatcher)
65
66// fileWatchHandler holds the current handler for file watch registrations
67var fileWatchHandler FileWatchRegistrationHandler
68
69// RegisterFileWatchHandler sets the handler for file watch registrations
70func RegisterFileWatchHandler(handler FileWatchRegistrationHandler) {
71 fileWatchHandler = handler
72}
73
74// notifyFileWatchRegistration notifies the handler about new file watch registrations
75func notifyFileWatchRegistration(id string, watchers []protocol.FileSystemWatcher) {
76 if fileWatchHandler != nil {
77 fileWatchHandler(id, watchers)
78 }
79}
80
81// Notifications
82
83func HandleServerMessage(params json.RawMessage) {
84 var msg struct {
85 Type int `json:"type"`
86 Message string `json:"message"`
87 }
88 if err := json.Unmarshal(params, &msg); err == nil {
89 log.Printf("Server message: %s\n", msg.Message)
90 }
91}
92
93func HandleDiagnostics(client *Client, params json.RawMessage) {
94 var diagParams protocol.PublishDiagnosticsParams
95 if err := json.Unmarshal(params, &diagParams); err != nil {
96 log.Printf("Error unmarshaling diagnostic params: %v", err)
97 return
98 }
99
100 client.diagnosticsMu.Lock()
101 defer client.diagnosticsMu.Unlock()
102
103 client.diagnostics[diagParams.URI] = diagParams.Diagnostics
104}