Skip to content

Commit 20f15e5

Browse files
committed
feat: keep view after update issue
1 parent 3ca40c9 commit 20f15e5

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

lua/jira/board/helper.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,35 @@ M.get_cache_key = function(project_key, view_name)
2020
return key
2121
end
2222

23+
function M.with_valid_win(fn)
24+
if state.win and vim.api.nvim_win_is_valid(state.win) then
25+
return vim.api.nvim_win_call(state.win, fn)
26+
end
27+
end
28+
29+
function M.save_view_if_same(view_name)
30+
if state.current_view ~= view_name then
31+
return nil
32+
end
33+
34+
return M.with_valid_win(function()
35+
return vim.fn.winsaveview()
36+
end)
37+
end
38+
39+
function M.restore_view(view)
40+
if not view then
41+
return
42+
end
43+
44+
M.with_valid_win(function()
45+
local line_count = vim.api.nvim_buf_line_count(state.buf)
46+
if view.lnum > line_count then
47+
view.lnum = line_count
48+
end
49+
vim.fn.winrestview(view)
50+
end)
51+
end
52+
2353
return M
2454
-- vim: set ts=2 sts=2 sw=2 et ai si sta:

lua/jira/board/init.lua

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ local sprint = require("jira.jira-api.sprint")
1010
local common_ui = require("jira.common.ui")
1111
local board_ui = require("jira.board.ui")
1212

13+
local function ensure_window()
14+
if not state.win or not vim.api.nvim_win_is_valid(state.win) then
15+
board_ui.create_window()
16+
util.setup_static_highlights()
17+
end
18+
end
19+
1320
function M.refresh_view()
1421
local cache_key = helper.get_cache_key(state.project_key, state.current_view)
1522
state.cache[cache_key] = nil
@@ -105,7 +112,8 @@ function M.setup_keymaps()
105112

106113
-- Clear existing buffer keymaps
107114
local keys_to_clear =
108-
{ "o", "S", "B", "J", "H", "K", "m", "gx", "r", "q", "gs", "ga", "gw", "gb", "go", "<Esc>", "s", "a", "t", "co", "i", "c" }
115+
{ "o", "S", "B", "J", "H", "K", "m", "gx", "r", "q", "gs", "ga", "gw", "gb", "go", "<Esc>", "s", "a", "t", "co", "i",
116+
"c" }
109117
for _, k in ipairs(keys_to_clear) do
110118
pcall(vim.api.nvim_buf_del_keymap, state.buf, "n", k)
111119
end
@@ -178,36 +186,28 @@ function M.setup_keymaps()
178186
end
179187

180188
function M.load_view(project_key, view_name)
181-
local old_cursor = nil
182-
if state.win and api.nvim_win_is_valid(state.win) and state.current_view == view_name then
183-
old_cursor = api.nvim_win_get_cursor(state.win)
184-
end
189+
local old_view = helper.save_view_if_same(view_name)
185190

186191
state.project_key = project_key
187192
state.current_view = view_name
188193

194+
-- HELP VIEW
189195
if view_name == "Help" then
190196
vim.schedule(function()
191-
if not state.win or not api.nvim_win_is_valid(state.win) then
192-
board_ui.create_window()
193-
util.setup_static_highlights()
194-
end
197+
ensure_window()
198+
195199
state.tree = {}
196200
state.line_map = {}
197201
render.clear(state.buf)
198202
render.render_help(view_name)
199-
if old_cursor and state.win and api.nvim_win_is_valid(state.win) then
200-
local line_count = api.nvim_buf_line_count(state.buf)
201-
if old_cursor[1] > line_count then
202-
old_cursor[1] = line_count
203-
end
204-
api.nvim_win_set_cursor(state.win, old_cursor)
205-
end
203+
204+
helper.restore_view(old_view)
206205
M.setup_keymaps()
207206
end)
208207
return
209208
end
210209

210+
-- JQL INIT
211211
if view_name == "JQL" and not state.current_query then
212212
local query_names = M.get_query_names()
213213
if #query_names > 0 then
@@ -225,35 +225,23 @@ function M.load_view(project_key, view_name)
225225
local function process_issues(issues)
226226
vim.schedule(function()
227227
common_ui.stop_loading()
228+
ensure_window()
228229

229-
-- Setup UI if not already created
230-
if not state.win or not api.nvim_win_is_valid(state.win) then
231-
board_ui.create_window()
232-
util.setup_static_highlights()
233-
end
230+
render.clear(state.buf)
234231

235232
if not issues or #issues == 0 then
236233
state.tree = {}
237-
render.clear(state.buf)
238234
render.render_issue_tree(state.tree, state.current_view)
239235
vim.notify("No issues found in " .. view_name .. ".", vim.log.levels.WARN)
240236
else
241237
state.tree = util.build_issue_tree(issues)
242-
render.clear(state.buf)
243238
render.render_issue_tree(state.tree, state.current_view)
244239
if not cached_issues then
245240
vim.notify("Loaded " .. view_name .. " for " .. project_key, vim.log.levels.INFO)
246241
end
247242
end
248243

249-
if old_cursor and state.win and api.nvim_win_is_valid(state.win) then
250-
local line_count = api.nvim_buf_line_count(state.buf)
251-
if old_cursor[1] > line_count then
252-
old_cursor[1] = line_count
253-
end
254-
api.nvim_win_set_cursor(state.win, old_cursor)
255-
end
256-
244+
helper.restore_view(old_view)
257245
M.setup_keymaps()
258246
end)
259247
end

0 commit comments

Comments
 (0)