@@ -47,17 +47,18 @@ M.refresh = function(opts)
4747 -- not to resume or a different picker, i.e. hide files and open buffers
4848 or winobj and winobj :hidden ()
4949 then
50+ local mode = vim .api .nvim_get_mode ().mode
5051 ctx = {
51- mode = vim . api . nvim_get_mode (). mode ,
52+ mode = mode ,
5253 bufnr = vim .api .nvim_get_current_buf (),
5354 bname = vim .api .nvim_buf_get_name (0 ),
5455 winid = vim .api .nvim_get_current_win (),
5556 last_winid = vim .fn .win_getid (vim .fn .winnr (" #" )),
5657 alt_bufnr = vim .fn .bufnr (" #" ),
5758 tabnr = vim .fn .tabpagenr (),
5859 tabh = vim .api .nvim_win_get_tabpage (0 ),
59- cursor = vim . api . nvim_win_get_cursor ( 0 ),
60- line = vim . api . nvim_get_current_line ( ),
60+ cursor = M . get_cursor ( mode ),
61+ line = M . get_line ( mode ),
6162 curtab_wins = (function ()
6263 local ret = {}
6364 local wins = vim .api .nvim_tabpage_list_wins (0 )
@@ -120,4 +121,41 @@ M.set_info = function(x)
120121 info = x
121122end
122123
124+ -- modified from blink.cmp
125+ function M .get_mode (mode )
126+ mode = mode or vim .api .nvim_get_mode ().mode
127+ return (mode == " c" and " cmdline" )
128+ or (mode == " t" and " term" )
129+ or (vim .fn .getcmdwintype () ~= " " and " cmdwin" )
130+ or " default"
131+ end
132+
133+ function M .get_cursor (mode )
134+ mode = M .get_mode (mode )
135+ return mode == " cmdline" and { 1 , vim .fn .getcmdpos () - 1 } or vim .api .nvim_win_get_cursor (0 )
136+ end
137+
138+ function M .set_cursor (mode , cursor )
139+ mode = M .get_mode (mode )
140+ if vim .tbl_contains ({ " default" , " term" , " cmdwin" }, mode ) then
141+ return vim .api .nvim_win_set_cursor (0 , cursor )
142+ end
143+
144+ assert (mode == " cmdline" , " Unsupported mode for setting cursor: " .. mode )
145+ assert (cursor [1 ] == 1 , " Cursor must be on the first line in cmdline mode" )
146+ vim .fn .setcmdpos (cursor [2 ])
147+ end
148+
149+ function M .get_line (mode , num )
150+ if M .get_mode (mode ) == " cmdline" then
151+ assert (num == nil or num == 0 ,
152+ " Cannot get line number " .. tostring (num ) .. " in cmdline mode. Only 0 is supported" )
153+ return vim .fn .getcmdline ()
154+ end
155+
156+ -- This method works for normal buffers and the terminal prompt
157+ if num == nil then num = M .get_cursor ()[1 ] - 1 end
158+ return vim .api .nvim_buf_get_lines (0 , num , num + 1 , false )[1 ]
159+ end
160+
123161return M
0 commit comments