fdschmidt93 / telescope-egrepify.nvim

Variable user customization for telescope.live_grep to set rg flags on-the-fly
MIT License
123 stars 13 forks source link

Files without matches #16

Closed otavioschwanck closed 1 year ago

otavioschwanck commented 1 year ago

Description

For some reason, super randomly, it shows a lot of filenames without any items inside. Is super rare to happen.

i managed to record a vídeo showing the problem (first i show the problem, reseting the picker fixes it): bug

maybe checking the count of matches before adding the title should fix it?

Neovim version

nvim --version
NVIM v0.9.2
Build type: Release
LuaJIT 2.1.0-beta3

    arquivo vimrc de sistema: "$VIM/sysinit.vim"
            padrão para $VIM: "/opt/homebrew/Cellar/neovim/0.9.2/share/nvim"

Run :checkhealth for more info

Operating system and version

MacOS

Steps to reproduce

  1. Clone mood-nvim https://github.com/otavioschwanck/mood-nvim and go to its folder (happened on this project)
  2. Open neovim with this minimal config
  3. Keep searching for charge, if not happen the bug, close and open again.

Expected behavior

Don't show the titles (filenames) when it don't have any match.

Actual behavior

a lot of titles without matches

Minimal config

-------------------------------------------------------------------------------
-- Options
-------------------------------------------------------------------------------
vim.opt.number = true
vim.opt.cursorline = true
vim.opt.smartindent = true
vim.opt.expandtab = true
vim.opt.tabstop = 4
vim.opt.shiftwidth = 4
vim.opt.laststatus = 3
vim.opt.cmdheight = 0
vim.opt.numberwidth = 4
vim.opt.signcolumn = "yes"

-------------------------------------------------------------------------------
-- Keymap
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Bootstrap Package Manager
-------------------------------------------------------------------------------
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    print("Installing 'folke/lazy.nvim'...")
    vim.fn.system({ "git", "clone", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.rtp:prepend(lazypath)

-------------------------------------------------------------------------------
-- Plugins
-------------------------------------------------------------------------------
require("lazy").setup({
    {
        "nvim-telescope/telescope-fzf-native.nvim",
        build = "cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release && cmake --build build --config Release && cmake --install build --prefix build",
    },
    {
        "fdschmidt93/telescope-egrepify.nvim",
        keys = {
            { "<C-p>", "<CMD>Telescope egrepify<CR>", mode = { "n", "i", "v" } },
        },
    },
  { "catppuccin/nvim", name = "catppuccin", priority = 1000 },
    {
        "nvim-telescope/telescope.nvim",
        dependencies = "nvim-lua/plenary.nvim",
        config = function()
            require("telescope").setup({
        defaults = {
          sorting_strategy = "ascending",
          layout_config = {
            horizontal = {
              prompt_position = "top",
            },
            vertical = {
              prompt_position = "top",
            },
          },
        },
                extensions = {
          egrepify = {
            filename_hl = "@attribute",
            lnum_hl = "CursorLineNr",
          },
                    fzf = {
                        fuzzy = true,
                        override_generic_sorter = true,
                        override_file_sorter = true,
                    },
                },
            })

            require("telescope").load_extension("egrepify")
            require("telescope").load_extension("fzf")
        end,
    },
})

vim.cmd("colorscheme catppuccin")
otavioschwanck commented 1 year ago

Managed to find the bug:

The problem is with the new create_layout of telescope.

The bug only occur after calling git_files (everytime).

My create_layout:

local Layout = require("nui.layout")
local TSLayout = require("telescope.pickers.layout")
local Popup = require("nui.popup")

local function layout(picker)
    local border = {
        results = {
            top_left = "├",
            top = "─",
            top_right = "┤",
            right = "│",
            bottom_right = "┘",
            bottom = "─",
            bottom_left = "└",
            left = "│",
        },
        results_patch = {
            minimal = {
                bottom_right = "┘",
            },
            horizontal = {
                bottom_right = "┴",
            },
            vertical = {
                bottom_right = "┘",
            },
        },
        prompt = {
            top_left = "┌",
            top = "─",
            top_right = "┬",
            right = "│",
            bottom_right = "",
            bottom = "",
            bottom_left = "",
            left = "│",
        },
        prompt_patch = {
            minimal = {
                top_left = "┌",
                top_right = "┐",
            },
            horizontal = {
                top_left = "┌",
                top_right = "┬",
            },
            vertical = {
                top_left = "├",
                top_right = "┤",
            },
        },
        preview = {
            top_left = "┌",
            top = "─",
            top_right = "┐",
            right = "│",
            bottom_right = "┘",
            bottom = "─",
            bottom_left = "└",
            left = "│",
        },
        preview_patch = {
            minimal = {},
            horizontal = {
                bottom = "─",
                bottom_left = "",
                bottom_right = "┘",
                left = "",
                top_left = "",
            },
            vertical = {
                bottom = "",
                bottom_left = "",
                bottom_right = "",
                left = "│",
                top_left = "┌",
            },
        },
    }

    local results = Popup({
        focusable = false,
        border = {
            style = border.results,
            text = {
                top_align = "center",
            },
        },
        win_options = {
            winhighlight = "Normal:Normal",
        },
    })

    local prompt = Popup({
        enter = true,
        border = {
            style = border.prompt,
            text = {
                top_align = "center",
                top = picker.prompt_title,
            },
        },
        win_options = {
            winhighlight = "Normal:Normal",
        },
    })

    local preview = Popup({
        focusable = false,
        border = {
            style = border.preview,
            text = {
                top_align = "center",
            },
        },
    })

    local box_by_kind = {
        vertical = Layout.Box({
            Layout.Box(preview, { grow = 1 }),
            Layout.Box(prompt, { size = 2 }),
            Layout.Box(results, { grow = 1 }),
        }, { dir = "col" }),
        horizontal = Layout.Box({
            Layout.Box({
                Layout.Box(prompt, { size = 2 }),
                Layout.Box(results, { grow = 1 }),
            }, { dir = "col", size = "50%" }),
            Layout.Box(preview, { size = "50%" }),
        }, { dir = "row" }),
        minimal = Layout.Box({
            Layout.Box(prompt, { size = 2 }),
            Layout.Box(results, { grow = 1 }),
        }, { dir = "col" }),
    }

    local height, width = vim.o.lines, vim.o.columns
    local function get_box()
        local box_kind = "horizontal"

        if picker.layout_strategy == "center" then
            box_kind = "minimal"
        elseif width < 150 or (picker.prompt_title == "Live Grep" and width < 300) then
            box_kind = "vertical"
            if height < 30 then
                box_kind = "minimal"
            end
        elseif width < 120 then
            box_kind = "minimal"
        end
        return box_by_kind[box_kind], box_kind
    end

    local function prepare_layout_parts(layout, box_type)
        layout.results = TSLayout.Window(results)
        results.border:set_style(border.results_patch[box_type])

        layout.prompt = TSLayout.Window(prompt)
        prompt.border:set_style(border.prompt_patch[box_type])

        if box_type == "minimal" then
            layout.preview = nil
        else
            layout.preview = TSLayout.Window(preview)
            preview.border:set_style(border.preview_patch[box_type])
        end
    end

    local total_width
    local total_height

    if picker.layout_strategy == "center" then
        total_width = "30%"
    elseif width < 200 then
        total_width = "90%"
    elseif width < 400 and picker.prompt_title == "Live Grep" then
        total_width = "90%"
    else
        total_width = "80%"
    end

    local box, box_kind = get_box()

    if picker.layout_strategy == "center" then
        total_height = "30%"
    elseif box_kind == "vertical" then
        total_height = "90%"
    else
        total_height = "80%"
    end

    local layout = Layout({
        relative = "editor",
        position = "50%",
        size = {
            height = total_height,
            width = total_width,
        },
    }, box)

    layout.picker = picker
    prepare_layout_parts(layout, box_kind)

    local layout_update = layout.update
    function layout:update()
        local box, box_kind = get_box()
        prepare_layout_parts(layout, box_kind)
        layout_update(self, box)
    end

    return TSLayout(layout)
end

return layout

The problem is with that, not this plugin, closing here!