handlers.go 3.1 KB

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