nvim-telescope / telescope-live-grep-args.nvim

Live grep with args
727 stars 40 forks source link

Extend usage of `telescope-rg` #14

Open ofirgall opened 2 years ago

ofirgall commented 2 years ago

Description

Hey, first I want to thank thanks for developing this plugin I'm a daily-heavy user of it.

I have create some "features" with/for telescope-rg that the community might like to use. I was wondering if you would like to add those features as part of the plugin or as advanced configuration or something like that.

Feature overview

  1. Open live_grep with current word under cursor
  2. Open live_grep with text captured with move operator
  3. Open live_grep from visual mode with selected text
  4. Open live_grep with filter to search files in the directory of the buffer
  5. Open live_grep from nvim-tree, search in the directory that was selected

How I Implemented the Features

Helper functions

escape_rg_text = function(text)
    text = text:gsub('%(', '\\%(')
    text = text:gsub('%)', '\\%)')
    text = text:gsub('%[', '\\%[')
    text = text:gsub('%]', '\\%]')
    text = text:gsub('%{', '\\%{')
    text = text:gsub('%}', '\\%}')
    text = text:gsub('"', '\\"')
    text = text:gsub('-', '\\-')
    text = text:gsub('+', '\\-')

    return text
end

live_grep_raw = function(opts, mode)
    opts = opts or {}
    opts.prompt_title = 'Live Grep Raw (-t[ty] include, -T exclude -g"[!] [glob])"'
    if not opts.default_text then
        if mode then
            opts.default_text = '"' .. escape_rg_text(get_text(mode)) .. '"'
        else
            opts.default_text = '"'
        end
    end

    require('telescope').extensions.live_grep_raw.live_grep_raw(opts)
end

get_text = function(mode)
    current_line = vim.api.nvim_get_current_line()
    if mode == 'v' then
        start_pos = vim.api.nvim_buf_get_mark(0, "<")
        end_pos = vim.api.nvim_buf_get_mark(0, ">")
    elseif mode == 'n' then
        start_pos = vim.api.nvim_buf_get_mark(0, "[")
        end_pos = vim.api.nvim_buf_get_mark(0, "]")
    end

    return string.sub(current_line, start_pos[2]+1, end_pos[2]+1)
end

Open live_grep with current word under cursor

map('n', 'KD', function() live_grep_raw({default_text = vim.fn.expand("<cword>")}) end)

Open live_grep with text captured with move operator

map('n', 'KF', ':set opfunc=LiveGrepRawOperator<CR>g@')
vim.cmd("function! LiveGrepRawOperator(...) \n lua live_grep_raw({}, 'n') \n endfunction") -- used by `KF`

Open live_grep from visual mode with selected text

map('v', 'KJ', '<Esc><cmd>lua live_grep_raw({}, "v")<cr>')

Open live_grep with filter to search files in the directory of the buffer

map('n', 'Kj', function() live_grep_raw({default_text = '-g"' .. vim.fn.fnamemodify(vim.fn.expand("%"), ":.:h") .. '/*" '}) end)

Open live_grep from nvim-tree, search in the directory that was selected

local node_relative_path = function(node)
    return vim.fn.fnamemodify(node.absolute_path, ":~:.")
end

local find_in_path = function(node)
    opts = {}
    opts.default_text = '-g"'.. node_relative_path(node) .. '/**" "'
    require('telescope').extensions.live_grep_raw.live_grep_raw(opts)
end

require'nvim-tree'.setup {
    view = {
        mappings = {
            list = {
                { key = "f", action = "find in path", action_cb = find_in_path },
            }
        }
    }
}
weeman1337 commented 2 years ago

I really like this :+1: Maybe I will add some functions that can be used for personal mappings.

weeman1337 commented 2 years ago

Lets start simple: #23 (will add the others soon)

Is that something you would expect? I don't want to set up mappings by default. That should be done by the user.

gegoune commented 2 years ago

Could it also search for visual selection?

ofirgall commented 2 years ago

@weeman1337 It looks good, its up to you what to do this I just shared my config.

@gegoune You can look at the 3rd feature.

weeman1337 commented 1 year ago

https://github.com/nvim-telescope/telescope-live-grep-args.nvim/pull/23 is ready to be merged. It provides shortcuts for grep word under cursor and grep with visual selection. Would be great is someone can test the branch.

gegoune commented 1 year ago

@weeman1337 I have tested both shortcuts (and with postfix) - all works great so far. Thank you so much, it simplifies my config. :)