1package lsp
2
3import (
4 "encoding/json"
5
6 "github.com/kujtimiihoxha/termai/internal/config"
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 logger.Error("Error unmarshaling registration params", "error", 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 logger.Error("Error marshaling registration options", "error", err)
31 continue
32 }
33
34 var options protocol.DidChangeWatchedFilesRegistrationOptions
35 if err := json.Unmarshal(optionsJSON, &options); err != nil {
36 logger.Error("Error unmarshaling registration options", "error", 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 logger.Error("Error applying workspace edit", "error", 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 cnf := config.Get()
85 var msg struct {
86 Type int `json:"type"`
87 Message string `json:"message"`
88 }
89 if err := json.Unmarshal(params, &msg); err == nil {
90 if cnf.Debug {
91 logger.Debug("Server message", "type", msg.Type, "message", msg.Message)
92 }
93 }
94}
95
96func HandleDiagnostics(client *Client, params json.RawMessage) {
97 var diagParams protocol.PublishDiagnosticsParams
98 if err := json.Unmarshal(params, &diagParams); err != nil {
99 logger.Error("Error unmarshaling diagnostics params", "error", err)
100 return
101 }
102
103 client.diagnosticsMu.Lock()
104 defer client.diagnosticsMu.Unlock()
105
106 client.diagnostics[diagParams.URI] = diagParams.Diagnostics
107}