summaryrefslogtreecommitdiff
path: root/internal/lsp/handlers_codeaction.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-02 13:48:47 +0200
committerPaul Buetow <paul@buetow.org>2026-03-02 13:48:47 +0200
commit8285f000444be9a301e06be47cb5ff2c858dc732 (patch)
treee2f15ca004fadda100b18d516368a61f4d80cc4b /internal/lsp/handlers_codeaction.go
parenta065de381f0343b580db47509a8c0f46384c7ae8 (diff)
lsp: handle code action payload marshal errors (task 406)
Diffstat (limited to 'internal/lsp/handlers_codeaction.go')
-rw-r--r--internal/lsp/handlers_codeaction.go44
1 files changed, 37 insertions, 7 deletions
diff --git a/internal/lsp/handlers_codeaction.go b/internal/lsp/handlers_codeaction.go
index 2a1d7fa..f61b79f 100644
--- a/internal/lsp/handlers_codeaction.go
+++ b/internal/lsp/handlers_codeaction.go
@@ -120,7 +120,10 @@ func (s *Server) appendCustomActions(actions *[]CodeAction, p CodeActionParams,
Selection string `json:"selection"`
Diagnostics []Diagnostic `json:"diagnostics"`
}{Type: "custom", ID: ca.ID, URI: p.TextDocument.URI, Range: p.Range, Selection: sel, Diagnostics: diags}
- raw, _ := json.Marshal(payload)
+ raw, ok := s.marshalCodeActionData(payload)
+ if !ok {
+ continue
+ }
kind := ca.Kind
if strings.TrimSpace(kind) == "" {
kind = "quickfix"
@@ -139,7 +142,10 @@ func (s *Server) appendCustomActions(actions *[]CodeAction, p CodeActionParams,
Range Range `json:"range"`
Selection string `json:"selection"`
}{Type: "custom", ID: ca.ID, URI: p.TextDocument.URI, Range: p.Range, Selection: sel}
- raw, _ := json.Marshal(payload)
+ raw, ok := s.marshalCodeActionData(payload)
+ if !ok {
+ continue
+ }
kind := ca.Kind
if strings.TrimSpace(kind) == "" {
kind = "refactor"
@@ -285,7 +291,10 @@ func (s *Server) buildSimplifyCodeAction(p CodeActionParams, sel string) *CodeAc
Range Range `json:"range"`
Selection string `json:"selection"`
}{Type: "simplify", URI: p.TextDocument.URI, Range: p.Range, Selection: sel}
- raw, _ := json.Marshal(payload)
+ raw, ok := s.marshalCodeActionData(payload)
+ if !ok {
+ return nil
+ }
ca := CodeAction{Title: "Hexai: simplify and improve", Kind: "refactor", Data: raw}
return &ca
}
@@ -299,7 +308,10 @@ func (s *Server) buildRewriteCodeAction(p CodeActionParams, sel string) *CodeAct
Instruction string `json:"instruction"`
Selection string `json:"selection"`
}{Type: "rewrite", URI: p.TextDocument.URI, Range: p.Range, Instruction: instr, Selection: cleaned}
- raw, _ := json.Marshal(payload)
+ raw, ok := s.marshalCodeActionData(payload)
+ if !ok {
+ return nil
+ }
ca := CodeAction{Title: "Hexai: rewrite selection", Kind: "refactor.rewrite", Data: raw}
return &ca
}
@@ -318,7 +330,10 @@ func (s *Server) buildDiagnosticsCodeAction(p CodeActionParams, sel string) *Cod
Selection string `json:"selection"`
Diagnostics []Diagnostic `json:"diagnostics"`
}{Type: "diagnostics", URI: p.TextDocument.URI, Range: p.Range, Selection: sel, Diagnostics: diags}
- raw, _ := json.Marshal(payload)
+ raw, ok := s.marshalCodeActionData(payload)
+ if !ok {
+ return nil
+ }
ca := CodeAction{Title: "Hexai: resolve diagnostics", Kind: "quickfix", Data: raw}
return &ca
}
@@ -408,6 +423,15 @@ func (s *Server) handleCodeActionResolve(req Request) {
s.reply(req.ID, ca, nil)
}
+func (s *Server) marshalCodeActionData(payload any) ([]byte, bool) {
+ raw, err := json.Marshal(payload)
+ if err != nil {
+ logging.Logf("lsp ", "code action payload marshal error: %v", err)
+ return nil, false
+ }
+ return raw, true
+}
+
// diagnosticsInRange parses the CodeAction context and returns diagnostics
// that overlap the given selection range. If the context is missing or does
// not contain diagnostics, returns an empty slice.
@@ -486,7 +510,10 @@ func (s *Server) buildGoUnitTestCodeAction(p CodeActionParams) *CodeAction {
URI string `json:"uri"`
Range Range `json:"range"`
}{Type: "go_test", URI: uri, Range: p.Range}
- raw, _ := json.Marshal(payload)
+ raw, ok := s.marshalCodeActionData(payload)
+ if !ok {
+ return nil
+ }
ca := CodeAction{Title: "Hexai: implement unit test", Kind: "quickfix", Data: raw}
return &ca
}
@@ -505,7 +532,10 @@ func (s *Server) buildDocumentCodeAction(p CodeActionParams, sel string) *CodeAc
Range Range `json:"range"`
Selection string `json:"selection"`
}{Type: "document", URI: p.TextDocument.URI, Range: p.Range, Selection: sel}
- raw, _ := json.Marshal(payload)
+ raw, ok := s.marshalCodeActionData(payload)
+ if !ok {
+ return nil
+ }
ca := CodeAction{Title: "Hexai: document code", Kind: "refactor.rewrite", Data: raw}
return &ca
}