Skip to content

Commit 1eb2454

Browse files
authored
fix: replace non-existent Panel:get_winid() with winid field (sindrets#611) (#16)
1 parent a5e16ed commit 1eb2454

3 files changed

Lines changed: 75 additions & 4 deletions

File tree

lua/diffview/scene/views/diff/listeners.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ return function(view)
2323

2424
-- Restore panel cursor position.
2525
if view.panel_cursor and view.panel:is_open() then
26-
local winid = view.panel:get_winid()
26+
local winid = view.panel.winid
2727
if winid and api.nvim_win_is_valid(winid) then
2828
pcall(api.nvim_win_set_cursor, winid, view.panel_cursor)
2929
end
@@ -38,7 +38,7 @@ return function(view)
3838

3939
-- Save panel cursor position.
4040
if view.panel:is_open() then
41-
local winid = view.panel:get_winid()
41+
local winid = view.panel.winid
4242
if winid and api.nvim_win_is_valid(winid) then
4343
view.panel_cursor = api.nvim_win_get_cursor(winid)
4444
end

lua/diffview/scene/views/file_history/listeners.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ return function(view)
2222

2323
-- Restore panel cursor position.
2424
if view.panel_cursor and view.panel:is_open() then
25-
local winid = view.panel:get_winid()
25+
local winid = view.panel.winid
2626
if winid and api.nvim_win_is_valid(winid) then
2727
pcall(api.nvim_win_set_cursor, winid, view.panel_cursor)
2828
end
@@ -33,7 +33,7 @@ return function(view)
3333

3434
-- Save panel cursor position.
3535
if view.panel:is_open() then
36-
local winid = view.panel:get_winid()
36+
local winid = view.panel.winid
3737
if winid and api.nvim_win_is_valid(winid) then
3838
view.panel_cursor = api.nvim_win_get_cursor(winid)
3939
end
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
local helpers = require("diffview.tests.helpers")
2+
3+
local eq = helpers.eq
4+
5+
describe("diffview.ui.panel", function()
6+
local Panel = require("diffview.ui.panel").Panel
7+
8+
describe("interface contract", function()
9+
-- The tab_enter/tab_leave listeners in diff and file_history views
10+
-- depend on panel instances exposing a `winid` field and an `is_open`
11+
-- method. Verify these exist on the base class so that a future
12+
-- refactor cannot silently break the contract (see issue #611).
13+
14+
it("has a winid field after init", function()
15+
local panel = Panel({
16+
bufname = "TestPanel",
17+
config = Panel.default_config_split,
18+
})
19+
-- winid starts nil (panel not yet opened).
20+
eq(nil, panel.winid)
21+
end)
22+
23+
it("exposes is_open as a callable method", function()
24+
local panel = Panel({
25+
bufname = "TestPanel",
26+
config = Panel.default_config_split,
27+
})
28+
eq("function", type(panel.is_open))
29+
end)
30+
31+
it("is_open returns falsy when winid is nil", function()
32+
local panel = Panel({
33+
bufname = "TestPanel",
34+
config = Panel.default_config_split,
35+
})
36+
assert.falsy(panel:is_open())
37+
end)
38+
39+
it("does not expose a get_winid method", function()
40+
-- get_winid has never been part of the Panel API. Callers should
41+
-- access the winid field directly. This test guards against
42+
-- accidental re-introduction of calls to a non-existent method.
43+
local panel = Panel({
44+
bufname = "TestPanel",
45+
config = Panel.default_config_split,
46+
})
47+
eq(nil, panel.get_winid)
48+
end)
49+
end)
50+
51+
describe("subclass contracts", function()
52+
-- The actual panels used by the two view types must inherit the same
53+
-- interface.
54+
55+
local function assert_panel_interface(panel_class, name)
56+
it(name .. " inherits winid field", function()
57+
eq(nil, rawget(panel_class, "get_winid"))
58+
end)
59+
60+
it(name .. " inherits is_open method", function()
61+
eq("function", type(panel_class.is_open))
62+
end)
63+
end
64+
65+
local FilePanel = require("diffview.scene.views.diff.file_panel").FilePanel
66+
local FileHistoryPanel = require("diffview.scene.views.file_history.file_history_panel").FileHistoryPanel
67+
68+
assert_panel_interface(FilePanel, "FilePanel")
69+
assert_panel_interface(FileHistoryPanel, "FileHistoryPanel")
70+
end)
71+
end)

0 commit comments

Comments
 (0)