Skip to content

Commit 498e5fc

Browse files
committed
feat: new command resume to restore last session (resolves #79)
1 parent cdf1ec8 commit 498e5fc

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ Triggered after the window is closed
190190
- `:Glance definitions` show definitions of the word under the cursor from the LSP server
191191
- `:Glance type_definitions` show type definitions of the word under the cursor from the LSP server
192192
- `:Glance implementations` show implementations of the word under the cursor from the LSP server
193+
- `:Glance resume` resume last closed session
193194

194195
### Example keybindings
195196

lua/glance/init.lua

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local Glance = {}
77
local glance = {}
88
Glance.__index = Glance
99
local initialized = false
10+
local last_session = nil
1011

1112
---@param opts? GlanceOpts
1213
function Glance.setup(opts)
@@ -336,12 +337,10 @@ Glance.actions = {
336337
---@param method GlanceMethod
337338
---@param opts? { hooks: GlanceHooks }
338339
open = function(method, opts)
340+
local commands = vim.tbl_keys(require('glance.lsp').methods)
341+
table.insert(commands, 'resume')
339342
vim.validate({
340-
method = utils.valid_enum(
341-
method,
342-
vim.tbl_keys(require('glance.lsp').methods),
343-
false
344-
),
343+
method = utils.valid_enum(method, commands, false),
345344
})
346345
-- Manually call the setup in case user hasn't initialized the plugin
347346
-- It will only run once
@@ -380,6 +379,23 @@ Glance.actions = {
380379
close_fold = function()
381380
glance:toggle_fold(false)
382381
end,
382+
resume = function()
383+
if not last_session then
384+
return utils.info('No previous Glance session to resume')
385+
end
386+
387+
-- Create new Glance instance with stored state
388+
create(
389+
last_session.results,
390+
vim.api.nvim_get_current_buf(),
391+
vim.api.nvim_get_current_win(),
392+
vim.lsp.util.make_position_params(),
393+
last_session.method,
394+
last_session.offset_encoding
395+
)
396+
397+
-- TODO: Restore cursor position
398+
end,
383399
}
384400

385401
function Glance:create(opts)
@@ -403,6 +419,14 @@ function Glance:create(opts)
403419
preview_bufnr = list:get_current_item().bufnr,
404420
})
405421

422+
-- Used for restoring the previous session
423+
last_session = {
424+
results = opts.results,
425+
position_params = opts.params,
426+
method = opts.method,
427+
offset_encoding = opts.offset_encoding,
428+
}
429+
406430
local scope = {
407431
list = list,
408432
preview = preview,
@@ -535,6 +559,7 @@ function Glance:close()
535559
end
536560

537561
vim.api.nvim_del_augroup_by_name('Glance')
562+
538563
self.list:close()
539564
self.preview:close()
540565

plugin/glance.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
vim.api.nvim_create_user_command('Glance', function(event)
2-
require('glance').open(event.args)
2+
if event.args == 'resume' then
3+
require('glance').actions.resume()
4+
else
5+
require('glance').open(event.args)
6+
end
37
end, {
48
nargs = 1,
59
complete = function(arg)
610
local list = vim.tbl_keys(require('glance.lsp').methods)
11+
table.insert(list, 'resume')
12+
713
return vim.tbl_filter(function(s)
814
return string.match(s, '^' .. arg)
915
end, list)

0 commit comments

Comments
 (0)