vim: move reusable functions to util
This commit is contained in:
parent
e34671ddaa
commit
c07b06d4a7
2 changed files with 27 additions and 22 deletions
|
@ -18,27 +18,7 @@ local builtin = function(picker, opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---Preserve register contents over function call.
|
local util = require('fschauen.util')
|
||||||
---@param reg string: register to save, must be a valid register name.
|
|
||||||
---@param func function: function that may freely clobber the register.
|
|
||||||
---@return any: return value of calling `func`.
|
|
||||||
local with_saved_register = function(reg, func)
|
|
||||||
local saved = vim.fn.getreg(reg)
|
|
||||||
local result = func()
|
|
||||||
vim.fn.setreg(reg, saved)
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
|
|
||||||
---Get selected text.
|
|
||||||
---@return string: selected text, or work under cursor if not in visual mode.
|
|
||||||
local get_selected_text = function()
|
|
||||||
if vim.fn.mode() ~= 'v' then return vim.fn.expand '<cword>' end
|
|
||||||
|
|
||||||
return with_saved_register('v', function()
|
|
||||||
vim.cmd [[noautocmd sil norm "vy]]
|
|
||||||
return vim.fn.getreg 'v'
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
local pickers = setmetatable({
|
local pickers = setmetatable({
|
||||||
all_files = builtin('find_files', {
|
all_files = builtin('find_files', {
|
||||||
|
@ -58,7 +38,7 @@ local pickers = setmetatable({
|
||||||
}),
|
}),
|
||||||
selection = function(title)
|
selection = function(title)
|
||||||
return function()
|
return function()
|
||||||
local text = get_selected_text()
|
local text = util.get_selected_text()
|
||||||
return require('telescope.builtin').grep_string {
|
return require('telescope.builtin').grep_string {
|
||||||
prompt_title = string.format(title .. ': %s ', text),
|
prompt_title = string.format(title .. ': %s ', text),
|
||||||
search = text,
|
search = text,
|
||||||
|
|
|
@ -1,9 +1,34 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
---Check whether file/directory exists.
|
||||||
|
---@param path string: file or directory path.
|
||||||
|
---@return string|boolean: type if path exists, false otherwise.
|
||||||
M.exists = function(path)
|
M.exists = function(path)
|
||||||
local stat = vim.loop.fs_stat(path)
|
local stat = vim.loop.fs_stat(path)
|
||||||
return (stat and stat.type) or false
|
return (stat and stat.type) or false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Preserve register contents over function call.
|
||||||
|
---@param reg string: register to save, must be a valid register name.
|
||||||
|
---@param func function: function that may freely clobber the register.
|
||||||
|
---@return any: return value of calling `func`.
|
||||||
|
M.preserve_register = function(reg, func)
|
||||||
|
local saved = vim.fn.getreg(reg)
|
||||||
|
local result = func()
|
||||||
|
vim.fn.setreg(reg, saved)
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
---Get selected text.
|
||||||
|
---@return string: selected text, or work under cursor if not in visual mode.
|
||||||
|
M.get_selected_text = function()
|
||||||
|
if vim.fn.mode() ~= 'v' then return vim.fn.expand '<cword>' end
|
||||||
|
|
||||||
|
return M.preserve_register('v', function()
|
||||||
|
vim.cmd [[noautocmd sil norm "vy]]
|
||||||
|
return vim.fn.getreg 'v'
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue