feat: Add selection and start actions

This commit is contained in:
TJ DeVries
2020-08-29 22:15:40 -04:00
parent 7115368593
commit ae9b769299
4 changed files with 73 additions and 28 deletions

25
lua/telescope/actions.lua Normal file
View File

@@ -0,0 +1,25 @@
-- Actions functions that are useful for people creating their own mappings.
local state = require('telescope.state')
local actions = {}
--- Get the current picker object for the prompt
function actions.get_current_picker(prompt_bufnr)
return state.get_status(prompt_bufnr).picker
end
--- Move the current selection of a picker {change} rows.
--- Handles not overflowing / underflowing the list.
function actions.shift_current_selection(prompt_bufnr, change)
actions.get_current_picker(prompt_bufnr):move_selection(change)
end
--- Get the current entry
function actions.get_selected_entry(prompt_bufnr)
return actions.get_current_picker(prompt_bufnr):get_selection()
end
return actions

View File

@@ -1,6 +1,7 @@
-- TODO: Customize keymap -- TODO: Customize keymap
local a = vim.api local a = vim.api
local actions = require('telescope.actions')
local state = require('telescope.state') local state = require('telescope.state')
local mappings = {} local mappings = {}
@@ -29,10 +30,6 @@ mappings.set_keymap = function(prompt_bufnr, results_bufnr)
default_mapper('<CR>', 'enter') default_mapper('<CR>', 'enter')
end end
local function update_current_selection(prompt_bufnr, change)
state.get_status(prompt_bufnr).picker:move_selection(change)
end
function __TelescopeMapping(prompt_bufnr, results_bufnr, characters) function __TelescopeMapping(prompt_bufnr, results_bufnr, characters)
if keymap[characters] then if keymap[characters] then
@@ -44,11 +41,11 @@ end
-- TODO: Move from top to bottom, etc. -- TODO: Move from top to bottom, etc.
-- TODO: It seems like doing this brings us back to the beginning of the prompt, which is not great. -- TODO: It seems like doing this brings us back to the beginning of the prompt, which is not great.
keymap["control-n"] = function(prompt_bufnr, _) keymap["control-n"] = function(prompt_bufnr, _)
update_current_selection(prompt_bufnr, 1) actions.shift_current_selection(prompt_bufnr, 1)
end end
keymap["control-p"] = function(prompt_bufnr, _) keymap["control-p"] = function(prompt_bufnr, _)
update_current_selection(prompt_bufnr, -1) actions.shift_current_selection(prompt_bufnr, -1)
end end
keymap["enter"] = function(prompt_bufnr, results_bufnr) keymap["enter"] = function(prompt_bufnr, results_bufnr)

View File

@@ -193,6 +193,7 @@ function Picker:find(opts)
return return
end end
-- TODO: This really isn't the place to do this.
local display = entry.display local display = entry.display
if has_devicons then if has_devicons then
@@ -200,6 +201,8 @@ function Picker:find(opts)
display = (icon or ' ') .. ' ' .. display display = (icon or ' ') .. ' ' .. display
end end
display = ' ' .. display
-- log.info("Setting row", row, "with value", entry) -- log.info("Setting row", row, "with value", entry)
vim.api.nvim_buf_set_lines(results_bufnr, row, row + 1, false, {display}) vim.api.nvim_buf_set_lines(results_bufnr, row, row + 1, false, {display})
end end
@@ -388,17 +391,33 @@ function Picker:set_selection(row)
end end
local status = state.get_status(self.prompt_bufnr) local status = state.get_status(self.prompt_bufnr)
local results_bufnr = status.results_bufnr
a.nvim_buf_clear_namespace(status.results_bufnr, ns_telescope_selection, 0, -1) -- Handle adding '> ' to beginning of selections
if self._selection_row then
a.nvim_buf_set_lines(results_bufnr, self._selection_row, self._selection_row + 1, false, {' ' .. a.nvim_buf_get_lines(results_bufnr, self._selection_row, self._selection_row + 1, false)[1]:sub(3)})
end
a.nvim_buf_set_lines(results_bufnr, row, row + 1, false, {'> ' .. a.nvim_buf_get_lines(results_bufnr, row, row + 1, false)[1]:sub(3)})
a.nvim_buf_clear_namespace(results_bufnr, ns_telescope_selection, 0, -1)
a.nvim_buf_add_highlight( a.nvim_buf_add_highlight(
status.results_bufnr, results_bufnr,
ns_telescope_selection, ns_telescope_selection,
'Error', 'TelescopeSelection',
row, row,
0, 0,
-1 -1
) )
-- if self._match_id then
-- -- vim.fn.matchdelete(self._match_id)
-- vim.fn.clearmatches(results_win)
-- end
-- self._match_id = vim.fn.matchaddpos("Conceal", { {row + 1, 1, 2} }, 0, -1, { window = results_win, conceal = ">" })
-- TODO: Don't let you go over / under the buffer limits -- TODO: Don't let you go over / under the buffer limits
-- TODO: Make sure you start exactly at the bottom selected -- TODO: Make sure you start exactly at the bottom selected

View File

@@ -1,28 +1,32 @@
highlight default link TelescopeSelection Visual
" let s:term_command = "rg preview_quit_map -l | fzf --preview 'bat --color=always --style=grid {-1}' --print0" " let s:term_command = "rg preview_quit_map -l | fzf --preview 'bat --color=always --style=grid {-1}' --print0"
" let s:term_command = "rg preview_quit_map -l | fzf --preview 'bat --color=always --style=grid {-1}' > file.txt" " let s:term_command = "rg preview_quit_map -l | fzf --preview 'bat --color=always --style=grid {-1}' > file.txt"
let s:term_command = "(rg preview_quit_map -l | fzf --preview 'bat --color=always --style=grid {-1}')" " let s:term_command = "(rg preview_quit_map -l | fzf --preview 'bat --color=always --style=grid {-1}')"
function! s:on_exit() abort " function! s:on_exit() abort
let g:result = readfile('file.txt') " let g:result = readfile('file.txt')
endfunction " endfunction
function! TestFunc() abort " function! TestFunc() abort
let g:term_output_stdout = [] " let g:term_output_stdout = []
let g:term_output_stderr = [] " let g:term_output_stderr = []
let g:term_output_onexit = [] " let g:term_output_onexit = []
vnew " vnew
let term_id = termopen(s:term_command, { " let term_id = termopen(s:term_command, {
\ 'on_stdout': { j, d, e -> add(g:term_output_stdout, d) }, " \ 'on_stdout': { j, d, e -> add(g:term_output_stdout, d) },
\ 'on_stderr': { j, d, e -> add(g:term_output_stderr, d) }, " \ 'on_stderr': { j, d, e -> add(g:term_output_stderr, d) },
\ 'on_exit': { j, d, e -> s:on_exit() }, " \ 'on_exit': { j, d, e -> s:on_exit() },
\ 'stdout_buffered': v:false, " \ 'stdout_buffered': v:false,
\ }) " \ })
endfunction " endfunction
function! PrintStuff() abort " function! PrintStuff() abort
echo len(g:term_output_stdout) len(g:term_output_stderr) len(g:term_output_onexit) " echo len(g:term_output_stdout) len(g:term_output_stderr) len(g:term_output_onexit)
endfunction " endfunction
" call TestFunc() " call TestFunc()