Skip to content

Commit 548fd1a

Browse files
committed
refactor(getDiagnostics): tweak file scheme handling, assume URI input
1 parent 5cab3d3 commit 548fd1a

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

lua/claudecode/tools/get_diagnostics.lua

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,9 @@ local function handler(params)
4444
logger.debug("Getting diagnostics for all open buffers")
4545
diagnostics = vim.diagnostic.get(nil)
4646
else
47-
-- Remove file:// prefix if present
4847
local uri = params.uri
49-
local filepath = uri
50-
if uri:sub(1, 7) == "file://" then
51-
filepath = uri:sub(8) -- Remove "file://" prefix
52-
end
48+
-- Strips the file:// scheme
49+
local filepath = vim.uri_to_fname(uri)
5350

5451
-- Get buffer number for the specific file
5552
local bufnr = vim.fn.bufnr(filepath)

tests/unit/tools/get_diagnostics_spec.lua

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ describe("Tool: get_diagnostics", function()
4040
end
4141
return -1 -- File not open
4242
end)
43+
_G.vim.uri_to_fname = spy.new(function(uri)
44+
-- Realistic mock that matches vim.uri_to_fname behavior
45+
if uri:sub(1, 7) == "file://" then
46+
return uri:sub(8)
47+
end
48+
-- Real vim.uri_to_fname throws an error for URIs without proper scheme
49+
error("URI must contain a scheme: " .. uri)
50+
end)
4351
end)
4452

4553
after_each(function()
@@ -49,6 +57,7 @@ describe("Tool: get_diagnostics", function()
4957
_G.vim.api.nvim_buf_get_name = nil
5058
_G.vim.json.encode = nil
5159
_G.vim.fn.bufnr = nil
60+
_G.vim.uri_to_fname = nil
5261
-- Note: We don't nullify _G.vim.lsp or _G.vim.diagnostic entirely
5362
-- as they are checked for existence.
5463
end)
@@ -175,7 +184,8 @@ describe("Tool: get_diagnostics", function()
175184
expect(success).to_be_true()
176185
expect(#result.content).to_be(1)
177186

178-
-- Should have called vim.diagnostic.get with specific buffer
187+
-- Should have used vim.uri_to_fname to convert URI to file path
188+
assert.spy(_G.vim.uri_to_fname).was_called_with("file:///test/file.lua")
179189
assert.spy(_G.vim.diagnostic.get).was_called_with(1)
180190
assert.spy(_G.vim.fn.bufnr).was_called_with("/test/file.lua")
181191
end)
@@ -192,27 +202,9 @@ describe("Tool: get_diagnostics", function()
192202
expect(err.message).to_be("File not open in buffer")
193203
assert_contains(err.data, "File must be open in Neovim to retrieve diagnostics: /unknown/file.lua")
194204

195-
-- Should have checked for buffer but not called vim.diagnostic.get
205+
-- Should have used vim.uri_to_fname and checked for buffer but not called vim.diagnostic.get
206+
assert.spy(_G.vim.uri_to_fname).was_called_with("file:///unknown/file.lua")
196207
assert.spy(_G.vim.fn.bufnr).was_called_with("/unknown/file.lua")
197208
assert.spy(_G.vim.diagnostic.get).was_not_called()
198209
end)
199-
200-
it("should handle URI without file:// prefix", function()
201-
_G.vim.fn.bufnr = spy.new(function(filepath)
202-
if filepath == "/test/file.lua" then
203-
return 1
204-
end
205-
return -1
206-
end)
207-
_G.vim.diagnostic.get = spy.new(function()
208-
return {}
209-
end)
210-
211-
local success, result = pcall(get_diagnostics_handler, { uri = "/test/file.lua" })
212-
expect(success).to_be_true()
213-
214-
-- Should have used the path directly
215-
assert.spy(_G.vim.fn.bufnr).was_called_with("/test/file.lua")
216-
assert.spy(_G.vim.diagnostic.get).was_called_with(1)
217-
end)
218210
end)

0 commit comments

Comments
 (0)