| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package lsp
- import (
- "encoding/json"
- "github.com/sst/opencode/internal/config"
- "github.com/sst/opencode/internal/lsp/protocol"
- "github.com/sst/opencode/internal/lsp/util"
- "log/slog"
- )
- // Requests
- func HandleWorkspaceConfiguration(params json.RawMessage) (any, error) {
- return []map[string]any{{}}, nil
- }
- func HandleRegisterCapability(params json.RawMessage) (any, error) {
- var registerParams protocol.RegistrationParams
- if err := json.Unmarshal(params, ®isterParams); err != nil {
- slog.Error("Error unmarshaling registration params", "error", err)
- return nil, err
- }
- for _, reg := range registerParams.Registrations {
- switch reg.Method {
- case "workspace/didChangeWatchedFiles":
- // Parse the registration options
- optionsJSON, err := json.Marshal(reg.RegisterOptions)
- if err != nil {
- slog.Error("Error marshaling registration options", "error", err)
- continue
- }
- var options protocol.DidChangeWatchedFilesRegistrationOptions
- if err := json.Unmarshal(optionsJSON, &options); err != nil {
- slog.Error("Error unmarshaling registration options", "error", err)
- continue
- }
- // Store the file watchers registrations
- notifyFileWatchRegistration(reg.ID, options.Watchers)
- }
- }
- return nil, nil
- }
- func HandleApplyEdit(params json.RawMessage) (any, error) {
- var edit protocol.ApplyWorkspaceEditParams
- if err := json.Unmarshal(params, &edit); err != nil {
- return nil, err
- }
- err := util.ApplyWorkspaceEdit(edit.Edit)
- if err != nil {
- slog.Error("Error applying workspace edit", "error", err)
- return protocol.ApplyWorkspaceEditResult{Applied: false, FailureReason: err.Error()}, nil
- }
- return protocol.ApplyWorkspaceEditResult{Applied: true}, nil
- }
- // FileWatchRegistrationHandler is a function that will be called when file watch registrations are received
- type FileWatchRegistrationHandler func(id string, watchers []protocol.FileSystemWatcher)
- // fileWatchHandler holds the current handler for file watch registrations
- var fileWatchHandler FileWatchRegistrationHandler
- // RegisterFileWatchHandler sets the handler for file watch registrations
- func RegisterFileWatchHandler(handler FileWatchRegistrationHandler) {
- fileWatchHandler = handler
- }
- // notifyFileWatchRegistration notifies the handler about new file watch registrations
- func notifyFileWatchRegistration(id string, watchers []protocol.FileSystemWatcher) {
- if fileWatchHandler != nil {
- fileWatchHandler(id, watchers)
- }
- }
- // Notifications
- func HandleServerMessage(params json.RawMessage) {
- cnf := config.Get()
- var msg struct {
- Type int `json:"type"`
- Message string `json:"message"`
- }
- if err := json.Unmarshal(params, &msg); err == nil {
- if cnf.DebugLSP {
- slog.Debug("Server message", "type", msg.Type, "message", msg.Message)
- }
- }
- }
- func HandleDiagnostics(client *Client, params json.RawMessage) {
- var diagParams protocol.PublishDiagnosticsParams
- if err := json.Unmarshal(params, &diagParams); err != nil {
- slog.Error("Error unmarshaling diagnostics params", "error", err)
- return
- }
- client.diagnosticsMu.Lock()
- defer client.diagnosticsMu.Unlock()
- client.diagnostics[diagParams.URI] = diagParams.Diagnostics
- }
|