[Help] How do i search in the current file with live_grep? #1477
Answered
by
ibhagwan
Ajaymamtora
asked this question in
Q&A
-
I have the current -- Module for integrating nvim fzf-lua search functionality
local M = {}
local icons = require("core.ui.icons")
local fzf_lua = require("fzf-lua")
local utils_lsp = require("utils.lsp")
local utils_files = require("utils.files")
-- Default search options
local default_options = {
case_sensitivity = false, -- false for "ignore", true for "sensitive"
fixed_strings = true, -- true for "fixed", false for "regex"
}
-- Function to get the current options string
local function get_options_string()
local case = default_options.case_sensitivity and "S" or "I"
local mode = default_options.fixed_strings and "F" or "R"
return string.format("[%s,%s]", case, mode)
end
-- Function to update the prompt with current options
local function update_prompt(prompt)
return string.format("%s %s", prompt, get_options_string())
end
-- Function to toggle case sensitivity
local function toggle_case_sensitivity()
default_options.case_sensitivity = not default_options.case_sensitivity
print("Case sensitivity: " .. (default_options.case_sensitivity and "ON" or "OFF"))
end
-- Function to toggle literal mode
local function toggle_literal_mode()
default_options.fixed_strings = not default_options.fixed_strings
print("Literal mode: " .. (default_options.fixed_strings and "ON" or "OFF"))
end
-- Central search function
function M.search(query, opts)
opts = opts or {}
opts.search = query
opts.prompt = update_prompt(opts.prompt or ("Search" .. icons.common.search .. " "))
opts.fzf_opts = vim.tbl_extend("force", {
["--layout"] = "reverse",
["--info"] = "inline",
}, opts.fzf_opts or {})
opts.winopts = opts.winopts
or {
height = 0.9,
width = 0.9,
preview = {
hidden = "nohidden",
vertical = "down:45%",
},
}
local search_paths = utils_lsp.get_workspace_folders(true)
opts.cwd = utils_files.find_nearest_common_parent(search_paths) or vim.uv.cwd()
-- Set up the rg command
opts.rg_opts = table.concat({
"--column",
"--line-number",
"--no-heading",
"--color=always",
"--max-columns=512",
default_options.case_sensitivity and "--case-sensitive" or "--ignore-case",
default_options.fixed_strings and "--fixed-strings" or "",
}, " ")
-- Modify the previewer to handle non-existent files
opts.previewer = "builtin"
opts.preview = function(info, opts)
if info.filename then
if vim.fn.filereadable(info.filename) == 0 then
return { "File does not exist: " .. info.filename }
else
return require("fzf-lua.previewer.builtin").file_preview(info, opts)
end
else
-- If no filename, it's likely a line from the current buffer
return require("fzf-lua.previewer.builtin").buffer_preview(info, opts)
end
end
fzf_lua.live_grep(opts)
end
-- Search within the current file
function M.search_in_current_file()
local current_file = vim.fn.expand("%:p")
local opts = {
prompt = "Search in current file " .. icons.common.search .. " ",
cmd = string.format(
"rg --line-number --no-heading --color=always --max-columns=512 '' %s",
vim.fn.shellescape(current_file)
),
cwd = vim.fn.expand("%:p:h"),
fzf_opts = {
["--delimiter"] = ":",
["--nth"] = "2..",
},
previewer = "builtin",
preview = function(info, opts)
return require("fzf-lua.previewer.builtin").buffer_preview(info, opts)
end,
}
fzf_lua.fzf_exec(opts.cmd, opts)
end
function M.search_in_root()
local search_paths = utils_lsp.get_workspace_folders(true)
local exclusions = utils_files.get_rg_exclusions()
local opts = {
prompt = "Search in project " .. icons.common.search .. " ",
search_dirs = search_paths,
}
-- Add exclusion patterns
for _, pattern in ipairs(exclusions) do
opts.rg_opts = (opts.rg_opts or "") .. " --glob '!" .. pattern .. "'"
end
M.search(nil, opts)
end
function M.search_in_paths(paths)
local num_paths = #paths
local opts = {
prompt = string.format(
"Search in %d path%s" .. icons.common.search .. " ",
num_paths,
num_paths > 1 and "s" or ""
),
search_dirs = paths,
}
M.search(nil, opts)
end
-- Create user commands
vim.api.nvim_create_user_command("FzfLuaSearchInCurrentFile", M.search_in_current_file, {})
vim.api.nvim_create_user_command("FzfLuaSearchInRoot", M.search_in_root, {})
vim.api.nvim_create_user_command("FzfLuaSearchInPaths", function(opts)
local paths = vim.split(opts.args, "%s+")
M.search_in_paths(paths)
end, { nargs = "+" })
-- Create user commands for toggling options
vim.api.nvim_create_user_command("FzfLuaToggleCaseSensitivity", toggle_case_sensitivity, {})
vim.api.nvim_create_user_command("FzfLuaToggleLiteralMode", toggle_literal_mode, {})
return M and I'm running 'FzfLuaSearchInCurrentFile' which gives me the picker but the search doesnt do anything and the previewer is empty: |
Beta Was this translation helpful? Give feedback.
Answered by
ibhagwan
Oct 5, 2024
Replies: 1 comment 3 replies
-
Why not use |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
Ajaymamtora
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why not use
:FzfLua lgrep_curbuf
orgrep_curbuf
?