Skip to content

Commit c2fd4c3

Browse files
committed
fix(word_diff): "No newline at eof" should show once for single hunk
Problem: When use `require('gitsigns.diff_int').run_diff` I find "no_nl_at_eof=true" can be set for both removed/added hunk, this make "\ No newline at eof" appear multiple times. Solution: * Only show no_nl_at_eof when it changed, remove uselss +/- prefix. * Also insert no_nl_at_eof after insert word diff to hls array to avoid messup the hls[] array index.
1 parent 1ce96a4 commit c2fd4c3

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

lua/gitsigns/hunks.lua

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -575,15 +575,6 @@ function M.linespec_for_hunk(hunk, fileformat)
575575
}
576576
hls[#hls + 1] = { { spec.sym .. l, { mark } } }
577577
end
578-
if config.diff_opts.internal then
579-
if
580-
spec.lines == removed and hunk.removed.no_nl_at_eof
581-
or spec.lines == added and hunk.added.no_nl_at_eof
582-
then
583-
local mark = { start_row = 0, end_row = 1, hl_group = 'GitSignsNoEOLPreview' }
584-
hls[#hls + 1] = { { spec.sym .. '\\ No newline at end of file', { mark } } }
585-
end
586-
end
587578
end
588579

589580
if config.diff_opts.internal then
@@ -611,6 +602,17 @@ function M.linespec_for_hunk(hunk, fileformat)
611602
end_col = region[4],
612603
}
613604
end
605+
606+
local no_nl_at_eof ---@type integer?
607+
if hunk.removed.no_nl_at_eof and not hunk.added.no_nl_at_eof then
608+
no_nl_at_eof = hunk.removed.count + 1
609+
elseif not hunk.removed.no_nl_at_eof and hunk.added.no_nl_at_eof then
610+
no_nl_at_eof = hunk.removed.count + hunk.added.count + 1
611+
end
612+
if no_nl_at_eof then
613+
local mark = { start_row = 0, end_row = 1, hl_group = 'GitSignsNoEOLPreview' }
614+
table.insert(hls, no_nl_at_eof, { { '\\ No newline at end of file', { mark } } })
615+
end
614616
end
615617

616618
return hls

test/gitsigns_spec.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -978,20 +978,19 @@ describe('gitsigns (with screen)', function()
978978
it('shows "No newline at end of file" in preview popup', function()
979979
setup_test_repo({ test_file_text = { 'a' } })
980980
setup_gitsigns(config)
981-
screen:try_resize(30, 10)
981+
screen:try_resize(30, 5)
982982
edit(test_file)
983983
wait_for_attach()
984984

985985
-- Remove newline at end of file (`printf a >a`)
986-
local file_path = scratch .. '/dummy.txt'
987-
local f = assert(io.open(file_path, 'wb'))
986+
local f = assert(io.open(test_file, 'wb'))
988987
f:write('a') -- Write without trailing newline
989988
f:close()
990989

991990
command('checktime')
992991
helpers.sleep(50)
993992
feed('mhp')
994-
screen:expect({ any = [[No newline at end of file]] })
993+
screen:expect({ any = [[\ No newline at end of file]] })
995994
end)
996995
end)
997996

0 commit comments

Comments
 (0)