Skip to content

Commit 5965e83

Browse files
authored
feat: Enhanced diff highlighting. (#70)
1 parent 87ec959 commit 5965e83

File tree

9 files changed

+104
-31
lines changed

9 files changed

+104
-31
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ local cb = require'diffview.config'.diffview_callback
4242
require'diffview'.setup {
4343
diff_binaries = false, -- Show diffs for binaries
4444
use_icons = true, -- Requires nvim-web-devicons
45+
enhanced_diff_hl = false, -- See ':h diffview-config-enhanced_diff_hl'
4546
signs = {
4647
fold_closed = "",
4748
fold_open = "",

doc/diffview.txt

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ Author: Sindre T. Strøm
77

88
==============================================================================
99

10-
INTRODUCTION *diffview-nvim-introduction*
10+
INTRODUCTION *diffview-introduction*
1111

1212
Vim's diff mode is pretty good, but there is no convenient way to quickly bring
1313
up all modified files in a diffsplit. This plugin aims to provide a simple,
1414
unified, single tabpage interface that lets you easily review all changed files
1515
for any git rev.
1616

17-
USAGE *diffview-nvim-usage*
17+
USAGE *diffview-usage*
1818

1919
Quick-start: `:DiffviewOpen` to open a Diffview that compares against the
2020
index.
@@ -30,15 +30,16 @@ Diffviews are automatically updated:
3030

3131
Updates are not run unless the current tabpage is a Diffview.
3232

33-
CONFIGURATION *diffview-nvim-configuration*
33+
CONFIGURATION *diffview-config*
3434

3535
Example configuration with default settings:
3636
>
3737
-- Lua
3838
local cb = require'diffview.config'.diffview_callback
3939
require'diffview'.setup {
4040
diff_binaries = false, -- Show diffs for binaries
41-
use_icons = true -- Requires nvim-web-devicons
41+
use_icons = true, -- Requires nvim-web-devicons
42+
enhanced_diff_hl = false, -- See |diffview-config-enhanced_diff_hl|
4243
signs = {
4344
fold_closed = "",
4445
fold_open = "",
@@ -123,13 +124,20 @@ Example configuration with default settings:
123124
}
124125
<
125126

126-
LAYOUT *diffview-nvim-layout*
127+
enhanced_diff_hl *diffview-config-enhanced_diff_hl*
128+
Type: `boolean`, Default: `false`
129+
130+
Enable/disable enhanced diff highlighting. When enabled, |hl-DiffAdd|
131+
in the left diff buffer will be highlighted as |hl-DiffDelete|, and
132+
the delete fill-chars will be highlighted more subtly.
133+
134+
LAYOUT *diffview-layout*
127135

128136
The diff windows can be aligned either with a horizontal split or a vertical
129137
split. To change the alignment add either `horizontal` or `vertical` to your
130138
'diffopt'.
131139

132-
COMMANDS *diffview-nvim-commands*
140+
COMMANDS *diffview-commands*
133141

134142
*:DiffviewOpen*
135143
:DiffviewOpen [git-rev] [args] [ -- {paths...}]
@@ -194,7 +202,7 @@ COMMANDS *diffview-nvim-commands*
194202
:DiffviewRefresh Update stats and entries in the file list of the
195203
current Diffview.
196204

197-
MAPS *diffview-nvim-maps*
205+
MAPS *diffview-maps*
198206

199207
All listed maps are the defaults, but all mappings can be configured. Most of
200208
the file panel mappings should also work from the view if they are added to
@@ -205,7 +213,7 @@ really make sense specifically in the file panel, such as `next_entry`,
205213
these will target the file currently open in the view rather than the file
206214
under the cursor in the file panel.
207215

208-
*diffview-nvim-file-inference*
216+
*diffview-file-inference*
209217
File inference~
210218

211219
Mappings that target a file will infer the target file according to a set of
@@ -216,77 +224,77 @@ simple rules:
216224
- If the cursor is in a file panel, the target file will be determined by
217225
the entry under the cursor.
218226

219-
*diffview-nvim-maps-view*
227+
*diffview-maps-view*
220228
View maps~
221229

222230
These maps are available in the diff buffers while a Diffview is the current
223231
tabpage.
224232

225-
*diffview-nvim-maps-select_next_entry*
233+
*diffview-maps-select_next_entry*
226234
<Tab> Open the diff for the next file.
227235

228-
*diffview-nvim-maps-select_prev_entry*
236+
*diffview-maps-select_prev_entry*
229237
<S-Tab> Open the diff for the previous file.
230238

231-
*diffview-nvim-maps-goto_file*
239+
*diffview-maps-goto_file*
232240
gf Open the local version of the file in a new split in a
233241
different tabpage. This will target your previous
234242
(last accessed) tabpage first. If you have no
235243
non-diffview tabpages open, the file will open in a
236244
new tabpage. See |diffview-nvim-file-inference| for
237245
details on how the file target is determined.
238246

239-
*diffview-nvim-maps-goto_file_split*
247+
*diffview-maps-goto_file_split*
240248
<C-w><C-f> Open the local version of the file in a new split. See
241249
|diffview-nvim-file-inference| for details on how the
242250
file target is determined.
243251

244-
*diffview-nvim-maps-goto_file_tab*
252+
*diffview-maps-goto_file_tab*
245253
<C-w>gf Open the local version of the file in a new tabpage.
246254
See |diffview-nvim-file-inference| for details on how
247255
the file target is determined.
248256

249-
*diffview-nvim-maps-toggle_files*
257+
*diffview-maps-toggle_files*
250258
<leader>b Toggle the file panel.
251259

252-
*diffview-nvim-maps-focus_files*
260+
*diffview-maps-focus_files*
253261
<leader>e Bring focus to the file panel.
254262

255-
*diffview-nvim-maps-file-panel*
263+
*diffview-maps-file-panel*
256264
File panel maps~
257265

258266
These maps are available in the file panel buffer.
259267

260-
*diffview-nvim-maps-next_entry*
268+
*diffview-maps-next_entry*
261269
j Bring the cursor to the next file entry
262270
<Down>
263271

264-
*diffview-nvim-maps-prev_entry*
272+
*diffview-maps-prev_entry*
265273
k Bring the cursor to the previous file entry
266274
<Up>
267275

268-
*diffview-nvim-maps-select_entry*
276+
*diffview-maps-select_entry*
269277
o Open the diff for the selected file entry.
270278
<CR>
271279
<2-LeftMouse>
272280

273-
*diffview-nvim-maps-toggle_stage_entry*
281+
*diffview-maps-toggle_stage_entry*
274282
- Stage/unstage the selected file entry.
275283

276-
*diffview-nvim-maps-stage_all*
284+
*diffview-maps-stage_all*
277285
S Stage all entries.
278286

279-
*diffview-nvim-maps-unstage_all*
287+
*diffview-maps-unstage_all*
280288
U Unstage all entries.
281289

282-
*diffview-nvim-maps-restore_entry*
290+
*diffview-maps-restore_entry*
283291
X Revert the selected file entry to the state from the
284292
left side of the diff. This only works if the right
285293
side of the diff is showing the local state of the
286294
file. A command is echoed that shows how to undo the
287295
change. Check `:messages` to see it again.
288296

289-
*diffview-nvim-maps-refresh_files*
297+
*diffview-maps-refresh_files*
290298
R Update the stats and entries in the file list.
291299

292300
<Tab> Open the diff for the next file.
@@ -299,23 +307,23 @@ R Update the stats and entries in the file list.
299307

300308
<leader>e Bring focus to the file panel.
301309

302-
*diffview-nvim-maps-file-history-panel*
310+
*diffview-maps-file-history-panel*
303311
File history panel maps~
304312

305313
These mappings are available in the file history panel buffer (the panel
306314
listing the commits).
307315

308-
*diffview-nvim-maps-options*
316+
*diffview-maps-options*
309317
g! Open the options panel.
310318

311-
*diffview-nvim-maps-open_in_diffview*
319+
*diffview-maps-open_in_diffview*
312320
<C-d> Open the commit entry under the cursor in a Diffview.
313321

314-
*diffview-nvim-maps-open_all_folds*
322+
*diffview-maps-open_all_folds*
315323
zR Open the fold on all commit entries (only when showing
316324
history for multiple files).
317325

318-
*diffview-nvim-maps-close_all_folds*
326+
*diffview-maps-close_all_folds*
319327
zM Close the fold on all commit entries (only when showing
320328
history for multiple files).
321329

@@ -339,7 +347,7 @@ o Open the diff for the selected item.
339347

340348
<leader>e Bring focus to the file history panel.
341349

342-
*diffview-nvim-maps-file-history-option-panel*
350+
*diffview-maps-file-history-option-panel*
343351
File history option panel maps~
344352

345353
These mappings are available from the file history options panel. The option

lua/diffview/api/views/diff/diff_view.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function CDiffView:init(opt)
3434
self.layout_mode = CDiffView.get_layout_mode()
3535
self.nulled = false
3636
self.ready = false
37+
self.winopts = { left = {}, right = {} }
3738
self.git_root = opt.git_root
3839
self.git_dir = git.git_dir(opt.git_root)
3940
self.rev_arg = opt.rev_arg

lua/diffview/colors.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,17 @@ M.hl_links = {
7979
StatusIgnored = "Comment",
8080
}
8181

82+
function M.update_diff_hl()
83+
local fg = M.get_fg("DiffDelete") or "NONE"
84+
local bg = M.get_bg("DiffDelete") or "red"
85+
local gui = M.get_gui("DiffDelete") or "NONE"
86+
vim.cmd(string.format("hi def DiffviewDiffAddAsDelete guifg=%s guibg=%s gui=%s", fg, bg, gui))
87+
vim.cmd("hi def link DiffviewDiffDelete Comment")
88+
end
89+
8290
function M.setup()
91+
M.update_diff_hl()
92+
8393
for name, v in pairs(M.get_hl_groups()) do
8494
local fg = v.fg and " guifg=" .. v.fg or ""
8595
local bg = v.bg and " guibg=" .. v.bg or ""

lua/diffview/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local cb = M.diffview_callback
1111
M.defaults = {
1212
diff_binaries = false,
1313
use_icons = true,
14+
enhanced_diff_hl = false,
1415
signs = {
1516
fold_closed = "",
1617
fold_open = "",

lua/diffview/utils.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,12 @@ function M.tbl_indexof(t, v)
413413
return -1
414414
end
415415

416+
function M.tbl_clear(t)
417+
for k, _ in pairs(t) do
418+
t[k] = nil
419+
end
420+
end
421+
416422
function M.find_named_buffer(name)
417423
for _, v in ipairs(api.nvim_list_bufs()) do
418424
if vim.fn.bufname(v) == name then

lua/diffview/views/diff/diff_view.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ function DiffView:init(opt)
3838
self.layout_mode = DiffView.get_layout_mode()
3939
self.ready = false
4040
self.nulled = false
41+
self.winopts = { left = {}, right = {} }
4142
self.git_root = opt.git_root
4243
self.git_dir = git.git_dir(opt.git_root)
4344
self.rev_arg = opt.rev_arg
@@ -101,6 +102,7 @@ function DiffView:next_file()
101102
vim.cmd("diffoff!")
102103
cur = self.files[self.file_idx]
103104
cur:load_buffers(self.git_root, self.left_winid, self.right_winid)
105+
self:update_windows()
104106
self.panel:highlight_file(self:cur_file())
105107
self.nulled = false
106108

@@ -123,6 +125,7 @@ function DiffView:prev_file()
123125
vim.cmd("diffoff!")
124126
cur = self.files[self.file_idx]
125127
cur:load_buffers(self.git_root, self.left_winid, self.right_winid)
128+
self:update_windows()
126129
self.panel:highlight_file(self:cur_file())
127130
self.nulled = false
128131

@@ -145,6 +148,7 @@ function DiffView:set_file(file, focus)
145148
self.file_idx = i
146149
vim.cmd("diffoff!")
147150
self.files[self.file_idx]:load_buffers(self.git_root, self.left_winid, self.right_winid)
151+
self:update_windows()
148152
self.panel:highlight_file(self:cur_file())
149153
self.nulled = false
150154

@@ -281,12 +285,14 @@ function DiffView:recover_layout(state)
281285
vim.cmd("aboveleft " .. split_cmd)
282286
self.left_winid = api.nvim_get_current_win()
283287
self.panel:open()
288+
self:post_layout()
284289
self:set_file(self:cur_file())
285290
elseif not state.right_win then
286291
api.nvim_set_current_win(self.left_winid)
287292
vim.cmd("belowright " .. split_cmd)
288293
self.right_winid = api.nvim_get_current_win()
289294
self.panel:open()
295+
self:post_layout()
290296
self:set_file(self:cur_file())
291297
end
292298

lua/diffview/views/file_history/file_history_view.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function FileHistoryView:init(opt)
2525
self.layout_mode = FileHistoryView.get_layout_mode()
2626
self.ready = false
2727
self.nulled = false
28+
self.winopts = { left = {}, right = {} }
2829
self.git_root = opt.git_root
2930
self.git_dir = git.git_dir(self.git_root)
3031
self.path_args = opt.path_args
@@ -69,6 +70,7 @@ function FileHistoryView:next_item()
6970
cur = self.panel:next_file()
7071
if cur then
7172
cur:load_buffers(self.git_root, self.left_winid, self.right_winid)
73+
self:update_windows()
7274
self.panel:highlight_item(cur)
7375
self.nulled = false
7476

@@ -92,6 +94,7 @@ function FileHistoryView:prev_item()
9294
cur = self.panel:prev_file()
9395
if cur then
9496
cur:load_buffers(self.git_root, self.left_winid, self.right_winid)
97+
self:update_windows()
9598
self.panel:highlight_item(cur)
9699
self.nulled = false
97100

@@ -114,6 +117,7 @@ function FileHistoryView:set_file(file, focus)
114117
end
115118
vim.cmd("diffoff!")
116119
file:load_buffers(self.git_root, self.left_winid, self.right_winid)
120+
self:update_windows()
117121
self.panel.cur_item = { entry, file }
118122
self.panel:highlight_item(file)
119123
self.nulled = false
@@ -150,12 +154,14 @@ function FileHistoryView:recover_layout(state)
150154
vim.cmd("aboveleft " .. split_cmd)
151155
self.left_winid = api.nvim_get_current_win()
152156
self.panel:open()
157+
self:post_layout()
153158
self:set_file(self.panel.cur_item[2])
154159
elseif not state.right_win then
155160
api.nvim_set_current_win(self.left_winid)
156161
vim.cmd("belowright " .. split_cmd)
157162
self.right_winid = api.nvim_get_current_win()
158163
self.panel:open()
164+
self:post_layout()
159165
self:set_file(self.panel.cur_item[2])
160166
end
161167

0 commit comments

Comments
 (0)