feat: add neogit support (#117)
## TODO 1. docs (vimdoc + readme) - this is a non-trivial feature 2. push luarocks version ## Problem diffs.nvim only activates on `fugitive`, `git`, and `gitcommit` filetypes. Neogit uses its own custom filetypes (`NeogitStatus`, `NeogitCommitView`, `NeogitDiffView`) and doesn't set `b:git_dir`, so the plugin never attaches and repo root resolution fails for filetype detection within diff hunks. ## Solution Two changes: 1. **`lua/diffs/init.lua`** — Add the three Neogit filetypes to the default `filetypes` list. The `FileType` autocmd in `plugin/diffs.lua` already handles them correctly since the `is_fugitive_buffer` guard only applies to the `git` filetype. 2. **`lua/diffs/parser.lua`** — Add a CWD-based fallback in `get_repo_root()`. After the existing `b:diffs_repo_root` and `b:git_dir` checks, fall back to `vim.fn.getcwd()` via `git.get_repo_root()` (already cached). Without this, the parser can't resolve filetypes for files in Neogit buffers. Neogit's expanded diffs use standard unified diff format, so the parser handles them without modification. Closes #110.
This commit is contained in:
parent
5d3bbc3631
commit
3d640c207b
8 changed files with 314 additions and 56 deletions
|
|
@ -287,7 +287,7 @@ describe('highlight', function()
|
|||
local extmarks = get_extmarks(bufnr)
|
||||
local has_diff_add = false
|
||||
for _, mark in ipairs(extmarks) do
|
||||
if mark[4] and mark[4].hl_group == 'DiffsAdd' then
|
||||
if mark[4] and mark[4].line_hl_group == 'DiffsAdd' then
|
||||
has_diff_add = true
|
||||
break
|
||||
end
|
||||
|
|
@ -320,7 +320,7 @@ describe('highlight', function()
|
|||
local extmarks = get_extmarks(bufnr)
|
||||
local has_diff_delete = false
|
||||
for _, mark in ipairs(extmarks) do
|
||||
if mark[4] and mark[4].hl_group == 'DiffsDelete' then
|
||||
if mark[4] and mark[4].line_hl_group == 'DiffsDelete' then
|
||||
has_diff_delete = true
|
||||
break
|
||||
end
|
||||
|
|
@ -386,7 +386,7 @@ describe('highlight', function()
|
|||
local extmarks = get_extmarks(bufnr)
|
||||
local has_diff_add = false
|
||||
for _, mark in ipairs(extmarks) do
|
||||
if mark[4] and mark[4].hl_group == 'DiffsAdd' then
|
||||
if mark[4] and mark[4].line_hl_group == 'DiffsAdd' then
|
||||
has_diff_add = true
|
||||
break
|
||||
end
|
||||
|
|
@ -500,7 +500,7 @@ describe('highlight', function()
|
|||
local extmarks = get_extmarks(bufnr)
|
||||
local has_diff_add = false
|
||||
for _, mark in ipairs(extmarks) do
|
||||
if mark[4] and mark[4].hl_group == 'DiffsAdd' then
|
||||
if mark[4] and mark[4].line_hl_group == 'DiffsAdd' then
|
||||
has_diff_add = true
|
||||
break
|
||||
end
|
||||
|
|
@ -560,7 +560,7 @@ describe('highlight', function()
|
|||
delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('uses hl_group not line_hl_group for line backgrounds', function()
|
||||
it('uses line_hl_group for line backgrounds', function()
|
||||
local bufnr = create_buffer({
|
||||
'@@ -1,2 +1,1 @@',
|
||||
'-local x = 1',
|
||||
|
|
@ -582,17 +582,19 @@ describe('highlight', function()
|
|||
)
|
||||
|
||||
local extmarks = get_extmarks(bufnr)
|
||||
local found = false
|
||||
for _, mark in ipairs(extmarks) do
|
||||
local d = mark[4]
|
||||
if d and (d.hl_group == 'DiffsAdd' or d.hl_group == 'DiffsDelete') then
|
||||
assert.is_true(d.hl_eol == true)
|
||||
assert.is_nil(d.line_hl_group)
|
||||
if d and (d.line_hl_group == 'DiffsAdd' or d.line_hl_group == 'DiffsDelete') then
|
||||
found = true
|
||||
assert.is_nil(d.hl_eol)
|
||||
end
|
||||
end
|
||||
assert.is_true(found)
|
||||
delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('hl_eol background extmarks are multiline so hl_eol takes effect', function()
|
||||
it('line_hl_group background extmarks are single-line', function()
|
||||
local bufnr = create_buffer({
|
||||
'@@ -1,2 +1,1 @@',
|
||||
'-local x = 1',
|
||||
|
|
@ -616,8 +618,8 @@ describe('highlight', function()
|
|||
local extmarks = get_extmarks(bufnr)
|
||||
for _, mark in ipairs(extmarks) do
|
||||
local d = mark[4]
|
||||
if d and (d.hl_group == 'DiffsAdd' or d.hl_group == 'DiffsDelete') then
|
||||
assert.is_true(d.end_row > mark[2])
|
||||
if d and (d.line_hl_group == 'DiffsAdd' or d.line_hl_group == 'DiffsDelete') then
|
||||
assert.is_nil(d.end_row)
|
||||
end
|
||||
end
|
||||
delete_buffer(bufnr)
|
||||
|
|
@ -771,7 +773,7 @@ describe('highlight', function()
|
|||
if d then
|
||||
if d.hl_group == 'DiffsClear' then
|
||||
table.insert(priorities.clear, d.priority)
|
||||
elseif d.hl_group == 'DiffsAdd' or d.hl_group == 'DiffsDelete' then
|
||||
elseif d.line_hl_group == 'DiffsAdd' or d.line_hl_group == 'DiffsDelete' then
|
||||
table.insert(priorities.line_bg, d.priority)
|
||||
elseif d.hl_group == 'DiffsAddText' or d.hl_group == 'DiffsDeleteText' then
|
||||
table.insert(priorities.char_bg, d.priority)
|
||||
|
|
@ -871,8 +873,8 @@ describe('highlight', function()
|
|||
local extmarks = get_extmarks(bufnr)
|
||||
local line_bgs = {}
|
||||
for _, mark in ipairs(extmarks) do
|
||||
if mark[4] and mark[4].hl_eol then
|
||||
line_bgs[mark[2]] = mark[4].hl_group
|
||||
if mark[4] and mark[4].line_hl_group then
|
||||
line_bgs[mark[2]] = mark[4].line_hl_group
|
||||
end
|
||||
end
|
||||
assert.is_nil(line_bgs[1])
|
||||
|
|
@ -1063,8 +1065,8 @@ describe('highlight', function()
|
|||
local marker_text = {}
|
||||
for _, mark in ipairs(extmarks) do
|
||||
local d = mark[4]
|
||||
if d and d.hl_eol then
|
||||
line_bgs[mark[2]] = d.hl_group
|
||||
if d and d.line_hl_group then
|
||||
line_bgs[mark[2]] = d.line_hl_group
|
||||
end
|
||||
if d and d.number_hl_group then
|
||||
gutter_hls[mark[2]] = d.number_hl_group
|
||||
|
|
|
|||
|
|
@ -587,5 +587,86 @@ describe('parser', function()
|
|||
assert.are.equal('/tmp/test-repo', hunks[1].repo_root)
|
||||
delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('detects neogit modified prefix', function()
|
||||
local bufnr = create_buffer({
|
||||
'modified hello.lua',
|
||||
'@@ -1,2 +1,3 @@',
|
||||
' local M = {}',
|
||||
'+local x = 1',
|
||||
' return M',
|
||||
})
|
||||
local hunks = parser.parse_buffer(bufnr)
|
||||
|
||||
assert.are.equal(1, #hunks)
|
||||
assert.are.equal('hello.lua', hunks[1].filename)
|
||||
assert.are.equal('lua', hunks[1].ft)
|
||||
assert.are.equal(3, #hunks[1].lines)
|
||||
delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('detects neogit new file prefix', function()
|
||||
local bufnr = create_buffer({
|
||||
'new file hello.lua',
|
||||
'@@ -0,0 +1,2 @@',
|
||||
'+local M = {}',
|
||||
'+return M',
|
||||
})
|
||||
local hunks = parser.parse_buffer(bufnr)
|
||||
|
||||
assert.are.equal(1, #hunks)
|
||||
assert.are.equal('hello.lua', hunks[1].filename)
|
||||
assert.are.equal('lua', hunks[1].ft)
|
||||
assert.are.equal(2, #hunks[1].lines)
|
||||
delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('detects neogit deleted prefix', function()
|
||||
local bufnr = create_buffer({
|
||||
'deleted hello.lua',
|
||||
'@@ -1,2 +0,0 @@',
|
||||
'-local M = {}',
|
||||
'-return M',
|
||||
})
|
||||
local hunks = parser.parse_buffer(bufnr)
|
||||
|
||||
assert.are.equal(1, #hunks)
|
||||
assert.are.equal('hello.lua', hunks[1].filename)
|
||||
assert.are.equal('lua', hunks[1].ft)
|
||||
assert.are.equal(2, #hunks[1].lines)
|
||||
delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('detects bare filename for untracked files', function()
|
||||
local bufnr = create_buffer({
|
||||
'newfile.rs',
|
||||
'@@ -0,0 +1,3 @@',
|
||||
'+fn main() {',
|
||||
'+ println!("hello");',
|
||||
'+}',
|
||||
})
|
||||
local hunks = parser.parse_buffer(bufnr)
|
||||
|
||||
assert.are.equal(1, #hunks)
|
||||
assert.are.equal('newfile.rs', hunks[1].filename)
|
||||
assert.are.equal(3, #hunks[1].lines)
|
||||
delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('does not match section headers as bare filenames', function()
|
||||
local bufnr = create_buffer({
|
||||
'Untracked files (1)',
|
||||
'newfile.rs',
|
||||
'@@ -0,0 +1,3 @@',
|
||||
'+fn main() {',
|
||||
'+ println!("hello");',
|
||||
'+}',
|
||||
})
|
||||
local hunks = parser.parse_buffer(bufnr)
|
||||
|
||||
assert.are.equal(1, #hunks)
|
||||
assert.are.equal('newfile.rs', hunks[1].filename)
|
||||
delete_buffer(bufnr)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue