vim/telescope: fix grep title and improve getting selected text
This commit is contained in:
parent
8096290986
commit
ed839c8266
1 changed files with 19 additions and 27 deletions
|
@ -28,8 +28,7 @@ local config = function()
|
||||||
man_pages = ' Man pages ',
|
man_pages = ' Man pages ',
|
||||||
all_files = ' ALL Files ',
|
all_files = ' ALL Files ',
|
||||||
dotfiles = ' Find dotfiles ',
|
dotfiles = ' Find dotfiles ',
|
||||||
grep_string = ' grep: %s ',
|
grep = ' grep: %s ',
|
||||||
grep_visual = ' grep: %s ',
|
|
||||||
}
|
}
|
||||||
|
|
||||||
telescope.setup {
|
telescope.setup {
|
||||||
|
@ -75,7 +74,6 @@ local config = function()
|
||||||
buffers = { prompt_title = titles.buffers },
|
buffers = { prompt_title = titles.buffers },
|
||||||
find_files = { prompt_title = titles.find_files },
|
find_files = { prompt_title = titles.find_files },
|
||||||
git_commits = { prompt_title = titles.git_commits },
|
git_commits = { prompt_title = titles.git_commits },
|
||||||
grep_string = { prompt_title = titles.grep_string },
|
|
||||||
help_tags = { prompt_title = titles.help_tags },
|
help_tags = { prompt_title = titles.help_tags },
|
||||||
keymaps = { prompt_title = titles.keymaps },
|
keymaps = { prompt_title = titles.keymaps },
|
||||||
live_grep = { prompt_title = titles.live_grep },
|
live_grep = { prompt_title = titles.live_grep },
|
||||||
|
@ -101,27 +99,21 @@ local config = function()
|
||||||
telescope.load_extension 'file_browser'
|
telescope.load_extension 'file_browser'
|
||||||
telescope.load_extension 'fzf'
|
telescope.load_extension 'fzf'
|
||||||
|
|
||||||
local selected_range = function()
|
local with_saved_register = function(register, func)
|
||||||
local _, s_row, s_col, _ = unpack(vim.fn.getpos('v'))
|
local saved = vim.fn.getreg(register)
|
||||||
local _, e_row, e_col, _ = unpack(vim.fn.getpos('.'))
|
local result = func()
|
||||||
|
vim.fn.setreg(register, saved)
|
||||||
local mode = vim.api.nvim_get_mode().mode
|
return result
|
||||||
local visual_line = (mode == 'V' or mode == 'CTRL-V')
|
|
||||||
|
|
||||||
if s_row < e_row or (s_row == e_row and s_col <= e_col) then
|
|
||||||
if visual_line then s_col, e_col = 1, #vim.fn.getline(e_row) end
|
|
||||||
return s_row - 1, s_col - 1, e_row - 1, e_col
|
|
||||||
else
|
|
||||||
if visual_line then e_col, s_col = 1, #vim.fn.getline(s_row) end
|
|
||||||
return e_row - 1, e_col - 1, s_row - 1, s_col
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local selected_text = function()
|
local get_selected_text = function()
|
||||||
local r0, c0, r1, c1 = selected_range()
|
if vim.fn.mode() ~= 'v' then return vim.fn.expand '<cword>' end
|
||||||
return vim.fn.join(vim.api.nvim_buf_get_text(0, r0, c0, r1, c1, {}), '\n')
|
|
||||||
end
|
|
||||||
|
|
||||||
|
return with_saved_register('v', function()
|
||||||
|
vim.cmd [[noautocmd sil norm "vy]]
|
||||||
|
return vim.fn.getreg 'v'
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
local custom = {
|
local custom = {
|
||||||
all_files = function()
|
all_files = function()
|
||||||
|
@ -141,11 +133,11 @@ local config = function()
|
||||||
}
|
}
|
||||||
end,
|
end,
|
||||||
|
|
||||||
grep_visual = function()
|
grep = function()
|
||||||
local selection = selected_text()
|
local selected = get_selected_text()
|
||||||
builtin.grep_string {
|
builtin.grep_string {
|
||||||
prompt_title = string.format(titles.grep_visual, selection),
|
prompt_title = string.format(titles.grep, selected),
|
||||||
search = selection,
|
search = selected,
|
||||||
}
|
}
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
@ -180,8 +172,8 @@ local config = function()
|
||||||
map('n', '<leader>fk', builtin.keymaps, { desc = ' [F]ind [K]eymaps' })
|
map('n', '<leader>fk', builtin.keymaps, { desc = ' [F]ind [K]eymaps' })
|
||||||
map('n', '<leader>fm', custom.man_pages, { desc = ' [F]ind [M]an pages' })
|
map('n', '<leader>fm', custom.man_pages, { desc = ' [F]ind [M]an pages' })
|
||||||
map('n', '<leader>fo', builtin.vim_options, { desc = ' [F]ind vim [O]ptions' })
|
map('n', '<leader>fo', builtin.vim_options, { desc = ' [F]ind vim [O]ptions' })
|
||||||
map('n', '<leader>fs', builtin.grep_string, { desc = ' [F]ind [S]tring' })
|
map('n', '<leader>fs', custom.grep, { desc = ' [F]ind [S]tring' })
|
||||||
map('v', '<leader>fs', custom.grep_visual, { desc = ' [F]ind visual [S]election' })
|
map('v', '<leader>fs', custom.grep, { desc = ' [F]ind visual [S]election' })
|
||||||
map('n', '<leader>br', '<cmd>Telescope file_browser<cr>', { desc = ' file [BR]owser' })
|
map('n', '<leader>br', '<cmd>Telescope file_browser<cr>', { desc = ' file [BR]owser' })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue