Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend usage of telescope-rg #14

Open
ofirgall opened this issue May 9, 2022 · 6 comments
Open

Extend usage of telescope-rg #14

ofirgall opened this issue May 9, 2022 · 6 comments

Comments

@ofirgall
Copy link

ofirgall commented May 9, 2022

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
Copy link
Collaborator

weeman1337 commented Jul 1, 2022

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

@weeman1337
Copy link
Collaborator

weeman1337 commented Jul 3, 2022

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

This comment was marked as resolved.

@ofirgall
Copy link
Author

ofirgall commented Jul 3, 2022

@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
Copy link
Collaborator

#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
Copy link

gegoune commented Apr 3, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants