Skip to content

Commit b05ab84

Browse files
committed
Use gitsigns.nvim to display inline diffs in the "diff1_inline" layout.
1 parent 85903aa commit b05ab84

File tree

8 files changed

+160
-82
lines changed

8 files changed

+160
-82
lines changed

lua/diffview/actions.lua

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local utils = lazy.require("diffview.utils") ---@module "diffview.utils"
99
local vcs_utils = lazy.require("diffview.vcs.utils") ---@module "diffview.vcs.utils"
1010

1111
local Diff1 = lazy.access("diffview.scene.layouts.diff_1", "Diff1") ---@type Diff1|LazyModule
12+
local Diff1Inline = lazy.access("diffview.scene.layouts.diff_1_inline", "Diff1Inline") ---@type Diff1Inline|LazyModule
1213
local Diff2Hor = lazy.access("diffview.scene.layouts.diff_2_hor", "Diff2Hor") ---@type Diff2Hor|LazyModule
1314
local Diff2Ver = lazy.access("diffview.scene.layouts.diff_2_ver", "Diff2Ver") ---@type Diff2Ver|LazyModule
1415
local Diff3 = lazy.access("diffview.scene.layouts.diff_3", "Diff3") ---@type Diff3|LazyModule
@@ -406,6 +407,7 @@ function M.cycle_layout()
406407
standard = {
407408
Diff2Hor.__get(),
408409
Diff2Ver.__get(),
410+
Diff1Inline.__get(),
409411
},
410412
merge_tool = {
411413
Diff3Hor.__get(),

lua/diffview/config.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local actions = require("diffview.actions")
44
local lazy = require("diffview.lazy")
55

66
local Diff1 = lazy.access("diffview.scene.layouts.diff_1", "Diff1") ---@type Diff1|LazyModule
7+
local Diff1Inline = lazy.access("diffview.scene.layouts.diff_1_inline", "Diff1Inline") ---@type Diff1Inline|LazyModule
78
local Diff2 = lazy.access("diffview.scene.layouts.diff_2", "Diff2") ---@type Diff2|LazyModule
89
local Diff2Hor = lazy.access("diffview.scene.layouts.diff_2_hor", "Diff2Hor") ---@type Diff2Hor|LazyModule
910
local Diff2Ver = lazy.access("diffview.scene.layouts.diff_2_ver", "Diff2Ver") ---@type Diff2Ver|LazyModule
@@ -293,6 +294,7 @@ function M.get_log_options(single_file, t)
293294
end
294295

295296
---@alias LayoutName "diff1_plain"
297+
--- | "diff1_inline"
296298
--- | "diff2_horizontal"
297299
--- | "diff2_vertical"
298300
--- | "diff3_horizontal"
@@ -302,6 +304,7 @@ end
302304

303305
local layout_map = {
304306
diff1_plain = Diff1,
307+
diff_1_inline = Diff1Inline,
305308
diff2_horizontal = Diff2Hor,
306309
diff2_vertical = Diff2Ver,
307310
diff3_horizontal = Diff3Hor,
@@ -480,7 +483,7 @@ function M.setup(user_config)
480483
do
481484
-- Validate layouts
482485
local view = M._config.view
483-
local standard_layouts = { "diff2_horizontal", "diff2_vertical", -1 }
486+
local standard_layouts = { "diff2_horizontal", "diff2_vertical", "diff1_inline", -1 }
484487
local merge_layuots = {
485488
"diff1_plain",
486489
"diff3_horizontal",

lua/diffview/scene/file_entry.lua

+12-7
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ function FileEntry:convert_layout(target_layout)
120120
end
121121

122122
self.layout = target_layout({
123+
parent = self,
123124
a = utils.tbl_access(self.layout, "a.file") or create_file(self.revs.a, "a"),
124125
b = utils.tbl_access(self.layout, "b.file") or create_file(self.revs.b, "b"),
125126
c = utils.tbl_access(self.layout, "c.file") or create_file(self.revs.c, "c"),
@@ -203,7 +204,7 @@ function FileEntry.with_layout(layout_class, opt)
203204
}) --[[@as vcs.File ]]
204205
end
205206

206-
return FileEntry({
207+
local entry = FileEntry({
207208
adapter = opt.adapter,
208209
path = opt.path,
209210
oldpath = opt.oldpath,
@@ -212,13 +213,17 @@ function FileEntry.with_layout(layout_class, opt)
212213
kind = opt.kind,
213214
commit = opt.commit,
214215
revs = opt.revs,
215-
layout = layout_class({
216-
a = create_file(opt.revs.a, "a"),
217-
b = create_file(opt.revs.b, "b"),
218-
c = create_file(opt.revs.c, "c"),
219-
d = create_file(opt.revs.d, "d"),
220-
}),
221216
})
217+
218+
entry.layout = layout_class({
219+
parent = entry,
220+
a = create_file(opt.revs.a, "a"),
221+
b = create_file(opt.revs.b, "b"),
222+
c = create_file(opt.revs.c, "c"),
223+
d = create_file(opt.revs.d, "d"),
224+
})
225+
226+
return entry
222227
end
223228

224229
M.FileEntry = FileEntry

lua/diffview/scene/layout.lua

+2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ local api = vim.api
88
local M = {}
99

1010
---@class Layout : diffview.Object
11+
---@field parent FileEntry
1112
---@field windows Window[]
1213
---@field emitter EventEmitter
1314
---@field pivot_producer fun(): integer?
1415
local Layout = oop.create_class("Layout")
1516

1617
function Layout:init(opt)
1718
opt = opt or {}
19+
self.parent = opt.parent
1820
self.windows = opt.windows or {}
1921
self.emitter = opt.emitter or EventEmitter()
2022

lua/diffview/scene/layouts/diff_1.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
local lazy = require("diffview.lazy")
21
local Layout = require("diffview.scene.layout").Layout
2+
local lazy = require("diffview.lazy")
33
local oop = require("diffview.oop")
44

55
local Diff3 = lazy.access("diffview.scene.layouts.diff_3", "Diff3") ---@type Diff3|LazyModule
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local Diff1 = require("diffview.scene.layouts.diff_1").Diff1
2+
local lazy = require("diffview.lazy")
3+
local oop = require("diffview.oop")
4+
5+
local M = {}
6+
7+
---@class Diff1Inline : Diff1
8+
local Diff1Inline = oop.create_class("Diff1Inline", Diff1)
9+
10+
---@param opt Diff1.init.Opt
11+
function Diff1Inline:init(opt)
12+
Diff1Inline:super().init(self, opt)
13+
end
14+
15+
M.Diff1Inline = Diff1Inline
16+
return M

lua/diffview/scene/window.lua

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
local lazy = require("diffview.lazy")
22
local oop = require("diffview.oop")
33

4+
local Diff1 = lazy.access("diffview.scene.layouts.diff_1", "Diff1") ---@type Diff1|LazyModule
45
local File = lazy.access("diffview.vcs.file", "File") ---@type vcs.File|LazyModule
6+
local GitAdapter = lazy.access("diffview.vcs.adapters.git", "GitAdapter") ---@type GitAdapter|LazyModule
57
local RevType = lazy.access("diffview.vcs.rev", "RevType") ---@type RevType|LazyModule
68
local config = lazy.require("diffview.config") ---@module "diffview.config"
79
local lib = lazy.require("diffview.lib") ---@module "diffview.lib"
@@ -62,6 +64,12 @@ function Window:is_focused()
6264
return self:is_valid() and api.nvim_get_current_win() == self.id
6365
end
6466

67+
function Window:is_file_open()
68+
return self:is_valid()
69+
and self.file:is_valid()
70+
and api.nvim_win_get_buf(self.id) == self.file.bufnr
71+
end
72+
6573
---@param callback fun(file: vcs.File)
6674
function Window:load_file(callback)
6775
assert(self.file)
@@ -87,11 +95,25 @@ function Window:open_file(callback)
8795
self:_save_winopts()
8896
end
8997

90-
self:apply_file_winopts()
98+
local winopt_overrides
99+
local base_rev = utils.tbl_access(self, "parent.parent.revs.a") --[[@as Rev? ]]
100+
local use_inline_diff = self.file.kind ~= "conflicting"
101+
and self.parent:instanceof(Diff1.__get())
102+
and self.file.adapter:instanceof(GitAdapter.__get())
103+
104+
if use_inline_diff then
105+
winopt_overrides = { foldmethod = "manual", diff = false }
106+
end
107+
108+
self:apply_file_winopts(winopt_overrides)
91109
self.file:attach_buffer(false, {
92110
keymaps = config.get_layout_keymaps(self.parent),
93111
disable_diagnostics = self.file.kind == "conflicting"
94112
and config.get_config().view.merge_tool.disable_diagnostics,
113+
inline_diff = {
114+
enabled = use_inline_diff,
115+
base = base_rev and base_rev:object_name(),
116+
}
95117
})
96118

97119
api.nvim_win_call(self.id, function()
@@ -163,11 +185,16 @@ function Window:_restore_winopts()
163185
end
164186
end
165187

166-
function Window:apply_file_winopts()
188+
---@param overrides WindowOptions?
189+
function Window:apply_file_winopts(overrides)
167190
assert(self.file)
168191
if self.file.winopts then
192+
if overrides then
193+
utils.set_local(self.id, vim.tbl_extend("force", self.file.winopts, overrides))
194+
else
169195
utils.set_local(self.id, self.file.winopts)
170196
end
197+
end
171198
end
172199

173200
function Window:apply_null_winopts()

0 commit comments

Comments
 (0)