-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shortcut functions (for use in mappings) (#23)
- Loading branch information
1 parent
cf79942
commit 0f75ea8
Showing
4 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-- SPDX-FileCopyrightText: 2023 Michael Weimann <[email protected]> | ||
-- | ||
-- SPDX-License-Identifier: MIT | ||
|
||
local M = {} | ||
|
||
local quote_default_opts = { | ||
quote_char = '"', | ||
} | ||
|
||
M.quote = function (value, opts) | ||
opts = opts or {} | ||
opts = vim.tbl_extend("force", quote_default_opts, opts) | ||
|
||
local quoted = value:gsub(opts.quote_char, "\\" .. opts.quote_char) | ||
return opts.quote_char .. quoted .. opts.quote_char | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
-- SPDX-FileCopyrightText: 2023 Michael Weimann <[email protected]> | ||
-- | ||
-- SPDX-License-Identifier: MIT | ||
|
||
local live_grep_args = require("telescope").extensions.live_grep_args | ||
local helpers = require("telescope-live-grep-args.helpers") | ||
|
||
local function get_visual() | ||
local _, ls, cs = unpack(vim.fn.getpos('v')) | ||
local _, le, ce = unpack(vim.fn.getpos('.')) | ||
return vim.api.nvim_buf_get_text(0, ls - 1, cs - 1, le - 1, ce, {}) | ||
end | ||
|
||
local grep_under_default_opts = { | ||
postfix = " -F ", | ||
quote = true, | ||
trim = true, | ||
} | ||
|
||
local function process_grep_under_text(value, opts) | ||
opts = opts or {} | ||
opts = vim.tbl_extend("force", grep_under_default_opts, opts) | ||
|
||
if opts.trim then | ||
value = vim.trim(value) | ||
end | ||
|
||
if opts.quote then | ||
value = helpers.quote(value, opts) | ||
end | ||
|
||
if opts.postfix then | ||
value = value .. opts.postfix | ||
end | ||
|
||
return value | ||
end | ||
|
||
local M = {} | ||
|
||
|
||
M.grep_word_under_cursor = function(opts) | ||
local word_under_cursor = vim.fn.expand("<cword>") | ||
word_under_cursor = process_grep_under_text(word_under_cursor, opts) | ||
live_grep_args.live_grep_args({ default_text = word_under_cursor }) | ||
end | ||
|
||
M.grep_visual_selection = function(opts) | ||
local visual = get_visual() | ||
local text = visual[1] or "" | ||
text = process_grep_under_text(text, opts) | ||
live_grep_args.live_grep_args({ default_text = text }) | ||
end | ||
|
||
return M |