Skip to content

Commit

Permalink
Telescope updates, added live_grep_raw from nvim-telescope/telescope.…
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatan-branting committed Apr 12, 2021
1 parent 02dcd7d commit 3f1860f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
80 changes: 80 additions & 0 deletions config/nvim/lua/plugins/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local layout = function(picker, columns, lines)
end

local resolve = require("telescope.config.resolve")
local finders = require('telescope.finders')

local function get_initial_window_options(picker)
local popup_border = resolve.win_option(picker.window.border)
Expand Down Expand Up @@ -111,6 +112,85 @@ nnoremap { '<leader>c', function()
require('telescope.builtin').find_files()
end }

local make_entry = require('telescope.make_entry')
local pickers = require('telescope.pickers')
local previewers = require('telescope.previewers')
local sorters = require('telescope.sorters')
local utils = require('telescope.utils')
local conf = require('telescope.config').values

sorters.grep_highlighter_only = function(opts)
opts = opts or {}

return sorters.Sorter:new {
scoring_function = function() return 0 end,

highlighter = function(_, prompt, display)
return {}
end,
}
end

require('telescope.builtin').live_grep_raw = function(opts)
opts.vimgrep_arguments = opts.vimgrep_arguments or conf.vimgrep_arguments
opts.entry_maker = opts.entry_maker or make_entry.gen_from_vimgrep(opts)
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd)

local tbl_clone = function(original)
local copy = {}
for key, value in pairs(original) do
copy[key] = value
end
return copy
end

local cmd_generator = function(prompt)
if not prompt or prompt == "" then
return nil
end

local query = prompt
local args = tbl_clone(opts.vimgrep_arguments)
local single_quoted = prompt:match("'(.*)'")
local double_quoted = prompt:match('"(.*)"')
local paths = {}

if single_quoted then
query = single_quoted
elseif double_quoted then
query = double_quoted
end

if single_quoted or double_quoted then
local before_args = prompt:match("(.-)['\"]")
local after_args = prompt:match(".*['\"](.*)")
local all_args = before_args .. ' ' .. after_args
for arg in all_args:gmatch("%S+") do
if arg:match('^-') then
table.insert(args, arg)
else
-- Show graceful grep error
-- Path cannot come before query
end
end
for path in after_args:gmatch("%S+") do
if not path:match('^-') then
table.insert(paths, path)
end
end
end

return vim.tbl_flatten { args, '--', query, paths }
end

pickers.new(opts, {
prompt_title = 'Live Grep Raw',
finder = finders.new_job(cmd_generator, opts.entry_maker, opts.max_results, opts.cwd),
previewer = conf.grep_previewer(opts),
sorter = sorters.grep_highlighter_only(opts),
}):find()
end


local actions = require('telescope.actions')

Expand Down
9 changes: 5 additions & 4 deletions config/nvim/mappings.vim
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)
nnoremap <silent> K :call CocAction('doHover')<CR>
nnoremap gw :exe ':Rg '.expand('<cword>')<CR>
nnoremap gw :exe ':Telescope grep_string search='.expand('<cword>')<CR>
vnoremap gw :<C-u>call <SID>GrepFromSelected(visualmode())<CR>
nnoremap <C-e> $
Expand All @@ -64,8 +64,9 @@ let g:which_key_map.o = [':Fern . -drawer -toggle -width=32 -reveal=%', 'explore

" File jumping
noremap <silent> <Leader>p :Telescope find_files<CR>
nnoremap <silent><Leader>f :Telescope live_grep<CR>
nnoremap <silent> <Leader>e :Telescope buffers<CR>
nnoremap <silent><Leader>f :Telescope live_grep_raw<CR>
nnoremap <silent> <Leader>e :Telescope buffers sort_lastused=true<CR>
nnoremap <silent> <Leader>i :Telescope find_files cwd=<C-R>=expand('%:h')<CR><CR>
let g:which_key_map.i = 'files-from-cwd'
let g:which_key_map.e = 'buffers'
let g:which_key_map.p = 'files-from-workspace'
Expand Down Expand Up @@ -247,7 +248,7 @@ function! s:GrepFromSelected(type)
let word = substitute(@@, '\n$', '', 'g')
let word = escape(word, '| ')
let @@ = saved_unnamed_register
execute 'Rg '.word
execute 'Telescope grep_string search='.word
endfunction

" Search and repace selection
Expand Down

0 comments on commit 3f1860f

Please sign in to comment.