Skip to content

Commit 5cab3d3

Browse files
committed
refactor(getDiagnostics): use alternate json encode
We're also passing nil explicitly to get diagnostics from all buffers
1 parent c9cbe55 commit 5cab3d3

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

lua/claudecode/tools/get_diagnostics.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ local function handler(params)
4141

4242
if not params.uri then
4343
-- Get diagnostics for all buffers
44-
diagnostics = vim.diagnostic.get()
4544
logger.debug("Getting diagnostics for all open buffers")
45+
diagnostics = vim.diagnostic.get(nil)
4646
else
4747
-- Remove file:// prefix if present
4848
local uri = params.uri
@@ -76,7 +76,7 @@ local function handler(params)
7676
table.insert(formatted_diagnostics, {
7777
type = "text",
7878
-- json encode this
79-
text = vim.fn.json_encode({
79+
text = vim.json.encode({
8080
-- Use the file path and diagnostic information
8181
filePath = file_path,
8282
-- Convert line and column to 1-indexed

tests/unit/tools/get_diagnostics_spec.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe("Tool: get_diagnostics", function()
3030
_G.vim.api.nvim_buf_get_name = spy.new(function(bufnr)
3131
return "/path/to/file_for_buf_" .. tostring(bufnr) .. ".lua"
3232
end)
33-
_G.vim.fn.json_encode = spy.new(function(obj)
33+
_G.vim.json.encode = spy.new(function(obj)
3434
return vim.inspect(obj) -- Use vim.inspect as a simple serialization
3535
end)
3636
_G.vim.fn.bufnr = spy.new(function(filepath)
@@ -47,7 +47,7 @@ describe("Tool: get_diagnostics", function()
4747
package.loaded["claudecode.logger"] = nil
4848
_G.vim.diagnostic.get = nil
4949
_G.vim.api.nvim_buf_get_name = nil
50-
_G.vim.fn.json_encode = nil
50+
_G.vim.json.encode = nil
5151
_G.vim.fn.bufnr = nil
5252
-- Note: We don't nullify _G.vim.lsp or _G.vim.diagnostic entirely
5353
-- as they are checked for existence.
@@ -59,7 +59,7 @@ describe("Tool: get_diagnostics", function()
5959
expect(result).to_be_table()
6060
expect(result.content).to_be_table()
6161
expect(#result.content).to_be(0)
62-
assert.spy(_G.vim.diagnostic.get).was_called_with()
62+
assert.spy(_G.vim.diagnostic.get).was_called_with(nil)
6363
end)
6464

6565
it("should return formatted diagnostics if available", function()
@@ -81,10 +81,10 @@ describe("Tool: get_diagnostics", function()
8181
expect(result.content[2].type).to_be("text")
8282

8383
-- Verify JSON encoding was called with correct structure
84-
assert.spy(_G.vim.fn.json_encode).was_called(2)
84+
assert.spy(_G.vim.json.encode).was_called(2)
8585

8686
-- Check the first diagnostic was encoded with 1-indexed values
87-
local first_call_args = _G.vim.fn.json_encode.calls[1].vals[1]
87+
local first_call_args = _G.vim.json.encode.calls[1].vals[1]
8888
expect(first_call_args.filePath).to_be("/path/to/file_for_buf_1.lua")
8989
expect(first_call_args.line).to_be(11) -- 10 + 1 for 1-indexing
9090
expect(first_call_args.character).to_be(6) -- 5 + 1 for 1-indexing
@@ -119,8 +119,8 @@ describe("Tool: get_diagnostics", function()
119119
expect(#result.content).to_be(1)
120120

121121
-- Verify only the diagnostic with a file path was included
122-
assert.spy(_G.vim.fn.json_encode).was_called(1)
123-
local encoded_args = _G.vim.fn.json_encode.calls[1].vals[1]
122+
assert.spy(_G.vim.json.encode).was_called(1)
123+
local encoded_args = _G.vim.json.encode.calls[1].vals[1]
124124
expect(encoded_args.filePath).to_be("/path/to/file1.lua")
125125
end)
126126

0 commit comments

Comments
 (0)