Skip to content

Fix missing syntax highlighting in proposed diff view #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
# Default target
all: format check test

# Detect if we are already inside a Nix shell
ifeq (,$(IN_NIX_SHELL))
NIX_PREFIX := nix develop .#ci -c
else
NIX_PREFIX :=
endif

# Check for syntax errors
check:
@echo "Checking Lua files for syntax errors..."
nix develop .#ci -c find lua -name "*.lua" -type f -exec lua -e "assert(loadfile('{}'))" \;
$(NIX_PREFIX) find lua -name "*.lua" -type f -exec lua -e "assert(loadfile('{}'))" \;
@echo "Running luacheck..."
nix develop .#ci -c luacheck lua/ tests/ --no-unused-args --no-max-line-length
$(NIX_PREFIX) luacheck lua/ tests/ --no-unused-args --no-max-line-length

# Format all files
format:
Expand Down
60 changes: 59 additions & 1 deletion lua/claudecode/diff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,57 @@ function M._cleanup_diff_layout(tab_name, target_win, new_win)
logger.debug("diff", "[CLEANUP] Layout cleanup completed for:", tab_name)
end

-- Detect filetype from a path or existing buffer (best-effort)
local function _detect_filetype(path, buf)
-- 1) Try Neovim's builtin matcher if available (>=0.10)
if vim.filetype and type(vim.filetype.match) == "function" then
local ok, ft = pcall(vim.filetype.match, { filename = path })
if ok and ft and ft ~= "" then
return ft
end
end

-- 2) Try reading from existing buffer
if buf and vim.api.nvim_buf_is_valid(buf) then
local ft = vim.api.nvim_buf_get_option(buf, "filetype")
if ft and ft ~= "" then
return ft
end
end

-- 3) Fallback to simple extension mapping
local ext = path:match("%.([%w_%-]+)$") or ""
local simple_map = {
lua = "lua",
ts = "typescript",
js = "javascript",
jsx = "javascriptreact",
tsx = "typescriptreact",
py = "python",
go = "go",
rs = "rust",
c = "c",
h = "c",
cpp = "cpp",
hpp = "cpp",
md = "markdown",
sh = "sh",
zsh = "zsh",
bash = "bash",
json = "json",
yaml = "yaml",
yml = "yaml",
toml = "toml",
}
Comment on lines +248 to +269
Copy link
Preview

Copilot AI Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider extracting the simple_map table to a module-level constant so it isn’t recreated on every call, improving performance and clarity.

Suggested change
local simple_map = {
lua = "lua",
ts = "typescript",
js = "javascript",
jsx = "javascriptreact",
tsx = "typescriptreact",
py = "python",
go = "go",
rs = "rust",
c = "c",
h = "c",
cpp = "cpp",
hpp = "cpp",
md = "markdown",
sh = "sh",
zsh = "zsh",
bash = "bash",
json = "json",
yaml = "yaml",
yml = "yaml",
toml = "toml",
}

Copilot uses AI. Check for mistakes.

return simple_map[ext]
end
--- Open diff using native Neovim functionality
-- @param old_file_path string Path to the original file
-- @param new_file_path string Path to the new file (used for naming)
-- @param new_file_contents string Contents of the new file
-- @param tab_name string Name for the diff tab/view
-- @return table Result with provider, tab_name, and success status

function M._open_native_diff(old_file_path, new_file_path, new_file_contents, tab_name)
local new_filename = vim.fn.fnamemodify(new_file_path, ":t") .. ".new"
local tmp_file, err = M._create_temp_file(new_file_contents, new_filename)
Expand Down Expand Up @@ -259,9 +304,16 @@ function M._open_native_diff(old_file_path, new_file_path, new_file_contents, ta
vim.cmd("edit " .. vim.fn.fnameescape(tmp_file))
vim.api.nvim_buf_set_name(0, new_file_path .. " (New)")

-- Propagate filetype to the proposed buffer for proper syntax highlighting (#20)
local proposed_buf = vim.api.nvim_get_current_buf()
local old_filetype = _detect_filetype(old_file_path)
if old_filetype and old_filetype ~= "" then
vim.api.nvim_set_option_value("filetype", old_filetype, { buf = proposed_buf })
end

vim.cmd("wincmd =")

local new_buf = vim.api.nvim_get_current_buf()
local new_buf = proposed_buf
vim.api.nvim_set_option_value("buftype", "nofile", { buf = new_buf })
vim.api.nvim_set_option_value("bufhidden", "wipe", { buf = new_buf })
vim.api.nvim_set_option_value("swapfile", false, { buf = new_buf })
Expand Down Expand Up @@ -665,6 +717,12 @@ function M._create_diff_view_from_window(target_window, old_file_path, new_buffe
vim.cmd("vsplit")
local new_win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(new_win, new_buffer)

-- Ensure new buffer inherits filetype from original for syntax highlighting (#20)
local original_ft = _detect_filetype(old_file_path, original_buffer)
if original_ft and original_ft ~= "" then
vim.api.nvim_set_option_value("filetype", original_ft, { buf = new_buffer })
end
vim.cmd("diffthis")
logger.debug("diff", "Created split window", new_win, "with new buffer", new_buffer)

Expand Down
34 changes: 34 additions & 0 deletions tests/unit/diff_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,40 @@ describe("Diff Module", function()
end)
end)

describe("Filetype Propagation", function()
it("should propagate original filetype to proposed buffer", function()
diff.setup({})

-- Spy on nvim_set_option_value
spy.on(_G.vim.api, "nvim_set_option_value")
Copy link
Preview

Copilot AI Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] After spying on nvim_set_option_value, consider restoring or removing the spy in an after_each block to avoid side effects on other tests.

Copilot uses AI. Check for mistakes.


local mock_file = {
write = function() end,
close = function() end,
}
local old_io_open = io.open
rawset(io, "open", function()
return mock_file
end)

local result = diff._open_native_diff("/tmp/test.ts", "/tmp/test.ts", "-- new", "Propagate FT")
expect(result.success).to_be_true()

-- Verify spy called with filetype typescript
local calls = _G.vim.api.nvim_set_option_value.calls or {}
local found = false
for _, c in ipairs(calls) do
if c.vals[1] == "filetype" and c.vals[2] == "typescript" then
found = true
break
end
end
expect(found).to_be_true()

rawset(io, "open", old_io_open)
end)
end)

describe("Open Diff Function", function()
it("should use native provider", function()
diff.setup({})
Expand Down