Skip to content

Advanced Configuration

linrongbin16 edited this page Oct 8, 2023 · 18 revisions

Live Grep

Search only on current buffer

Add below commands:

  • FzfxLiveGrepB
  • FzfxLiveGrepBV (by visual select)
  • FzfxLiveGrepBW (by cursor word)
  • FzfxLiveGrepBP (by yank text)

Search command: rg, preview command: bat.

Credit: @damanis, see: https://github.com/linrongbin16/fzfx.nvim/issues/230#issuecomment-1751723785.

local function file_previewer_rg(line)
    local line_helpers = require("fzfx.line_helpers")
    local parsed = line_helpers.parse_grep(line)
    local style = "numbers,changes"
    if
        type(vim.env["BAT_STYLE"]) == "string"
        and string.len(vim.env["BAT_STYLE"]) > 0
    then
        style = vim.env["BAT_STYLE"]
    end
    local theme = "base16"
    if
        type(vim.env["BAT_THEME"]) == "string"
        and string.len(vim.env["BAT_THEME"]) > 0
    then
        theme = vim.env["BAT_THEME"]
    end
    -- "%s --style=%s --theme=%s --color=always --pager=never --highlight-line=%s -- %s"
    return {
        vim.fn.executable("batcat") > 0 and "batcat" or "bat",
        "--style=" .. style,
        "--theme=" .. theme,
        "--color=always",
        "--pager=never",
        "--highlight-line=" .. parsed.lineno,
        "--",
        parsed.filename,
    }
end

local function merge_query_options(merged, option)
    local option_splits =
        vim.split(option, " ", { plain = true, trimempty = true })
    for _, o in ipairs(option_splits) do
        if type(o) == "string" and string.len(o) > 0 then
            table.insert(merged, o)
        end
    end
    return merged
end

require("fzfx").setup({
    users = {
        live_grep_buffer = {
            commands = {
                -- normal
                {
                    name = "FzfxLiveGrepB",
                    feed = "args",
                    opts = {
                        bang = true,
                        nargs = "*",
                        desc = "Live grep only on current buffer",
                    },
                },
                -- visual
                {
                    name = "FzfxLiveGrepBV",
                    feed = "visual",
                    opts = {
                        bang = true,
                        nargs = "*",
                        desc = "Live grep only on current buffer by visual select",
                    },
                },
                -- cword
                {
                    name = "FzfxLiveGrepBW",
                    feed = "cword",
                    opts = {
                        bang = true,
                        nargs = "*",
                        desc = "Live grep only on current buffer by cursor word",
                    },
                },
                -- put
                {
                    name = "FzfxLiveGrepBP",
                    feed = "put",
                    opts = {
                        bang = true,
                        nargs = "*",
                        desc = "Live grep only on current buffer by yank text",
                    },
                },
            },
            providers = {
                buffer_mode = {
                    key = "ctrl-o",
                    provider = function(query, context)
                        local utils = require("fzfx.utils")
                        local log = require("fzfx.log")
                        local path = require("fzfx.path")

                        local parsed_query = utils.parse_flag_query(query or "")
                        local content = parsed_query[1]
                        local option = parsed_query[2]
                        if not utils.is_buf_valid(context.bufnr) then
                            log.echo(
                                log.LogLevels.INFO,
                                "not valid buffer(%s).",
                                vim.inspect(context.bufnr)
                            )
                            return nil
                        end
                        local current_bufpath = path.reduce(
                            vim.api.nvim_buf_get_name(context.bufnr)
                        )
                        if
                            type(option) == "string"
                            and string.len(option) > 0
                        then
                            -- "rg --column -n --no-heading --color=always -S %s -- %s"
                            local args = {
                                "rg",
                                "--column",
                                "-n",
                                "--no-heading",
                                "--color=always",
                                "-S",
                                "-g",
                                current_bufpath,
                            }
                            args = merge_query_options(args, option)
                            table.insert(args, "--")
                            table.insert(args, content)
                            return args
                        else
                            -- "rg --column -n --no-heading --color=always -S -- %s"
                            return {
                                "rg",
                                "--column",
                                "-n",
                                "--no-heading",
                                "--color=always",
                                "-S",
                                "-g",
                                current_bufpath,
                                "--",
                                content,
                            }
                        end
                    end,
                    provider_type = "command_list",
                    line_opts = {
                        prepend_icon_by_ft = true,
                        prepend_icon_path_delimiter = ":",
                        prepend_icon_path_position = 1,
                    },
                },
            },
            previewers = {
                buffer_mode = {
                    previewer = file_previewer_rg,
                    previewer_type = "command_list",
                },
            },
            actions = {
                ["esc"] = require("fzfx.actions").nop,
                ["enter"] = require("fzfx.actions").edit_rg,
                ["double-click"] = require("fzfx.actions").edit_rg,
            },
            fzf_opts = {
                "--multi",
                "--disabled",
                { "--prompt", "Live Grep Buffer > " },
                { "--delimiter", ":" },
                { "--preview-window", "+{2}-/2" },
            },
            other_opts = {
                reload_on_change = true,
            },
        },
    },
})

Search only on opened buffers

Clone this wiki locally