Skip to content

Commit 0f75ea8

Browse files
authored
Shortcut functions (for use in mappings) (#23)
1 parent cf79942 commit 0f75ea8

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,25 @@ This table provides some mapping ideas:
134134
| `actions.quote_prompt({ postfix = ' -t' })` | Quote prompt and add `-t` | `foo``"foo" -t` |
135135

136136

137+
### Shortcut functions
138+
139+
Live grep args ships some additional shortcuts you can map.
140+
141+
This is an example to live grep for the word under the cursor:
142+
143+
```
144+
local live_grep_args_shortcuts = require("telescope-live-grep-args.shortcuts")
145+
keymap.set("n", "<leader>gc", live_grep_args_shortcuts.grep_word_under_cursor)
146+
```
147+
148+
Available shortcuts:
149+
150+
| Name | Action | Options |
151+
| --- | --- | --- |
152+
| `grep_word_under_cursor` | Start live grep with word under cursor | <ul><li>`postfix`: postfix value to add; defaults to ` -F ` (Treat the pattern as a literal string)</li><li>`quote`: Whether to quote the value; defaults to true</li><li>`trim`: Whether to trim the value; defaults to true</li></ul> |
153+
| `grep_visual_selection` | Start live grep with visual selection | see `grep_word_under_cursor` |
154+
155+
137156
## Development
138157

139158
### Running the tests

lua/telescope-live-grep-args/actions/quote_prompt.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
-- SPDX-License-Identifier: MIT
44

55
local action_state = require "telescope.actions.state"
6+
local helpers = require "telescope-live-grep-args.helpers"
67

78
local default_opts = {
89
quote_char = '"',
@@ -20,8 +21,7 @@ return function (opts)
2021
if opts.trim then
2122
prompt = vim.trim(prompt)
2223
end
23-
prompt = prompt:gsub(opts.quote_char, "\\" .. opts.quote_char)
24-
prompt = opts.quote_char .. prompt .. opts.quote_char .. opts.postfix
24+
prompt = helpers.quote(prompt, { quote_char = opts.quote_char }) .. opts.postfix
2525
picker:set_prompt(prompt)
2626
end
2727
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- SPDX-FileCopyrightText: 2023 Michael Weimann <[email protected]>
2+
--
3+
-- SPDX-License-Identifier: MIT
4+
5+
local M = {}
6+
7+
local quote_default_opts = {
8+
quote_char = '"',
9+
}
10+
11+
M.quote = function (value, opts)
12+
opts = opts or {}
13+
opts = vim.tbl_extend("force", quote_default_opts, opts)
14+
15+
local quoted = value:gsub(opts.quote_char, "\\" .. opts.quote_char)
16+
return opts.quote_char .. quoted .. opts.quote_char
17+
end
18+
19+
return M
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
-- SPDX-FileCopyrightText: 2023 Michael Weimann <[email protected]>
2+
--
3+
-- SPDX-License-Identifier: MIT
4+
5+
local live_grep_args = require("telescope").extensions.live_grep_args
6+
local helpers = require("telescope-live-grep-args.helpers")
7+
8+
local function get_visual()
9+
local _, ls, cs = unpack(vim.fn.getpos('v'))
10+
local _, le, ce = unpack(vim.fn.getpos('.'))
11+
return vim.api.nvim_buf_get_text(0, ls - 1, cs - 1, le - 1, ce, {})
12+
end
13+
14+
local grep_under_default_opts = {
15+
postfix = " -F ",
16+
quote = true,
17+
trim = true,
18+
}
19+
20+
local function process_grep_under_text(value, opts)
21+
opts = opts or {}
22+
opts = vim.tbl_extend("force", grep_under_default_opts, opts)
23+
24+
if opts.trim then
25+
value = vim.trim(value)
26+
end
27+
28+
if opts.quote then
29+
value = helpers.quote(value, opts)
30+
end
31+
32+
if opts.postfix then
33+
value = value .. opts.postfix
34+
end
35+
36+
return value
37+
end
38+
39+
local M = {}
40+
41+
42+
M.grep_word_under_cursor = function(opts)
43+
local word_under_cursor = vim.fn.expand("<cword>")
44+
word_under_cursor = process_grep_under_text(word_under_cursor, opts)
45+
live_grep_args.live_grep_args({ default_text = word_under_cursor })
46+
end
47+
48+
M.grep_visual_selection = function(opts)
49+
local visual = get_visual()
50+
local text = visual[1] or ""
51+
text = process_grep_under_text(text, opts)
52+
live_grep_args.live_grep_args({ default_text = text })
53+
end
54+
55+
return M

0 commit comments

Comments
 (0)