nvim-telescope / telescope.nvim

Find, Filter, Preview, Pick. All lua, all the time.
MIT License
15.42k stars 824 forks source link

Configuration Recipes: Customize buffers display to look like LeaderF bug -- this doesn't work when loading buffers from "mksession" #3259

Open rodhash opened 1 month ago

rodhash commented 1 month ago

Description

This snippet below works just fine if we open many buffers manually: https://github.com/nvim-telescope/telescope.nvim/wiki/Configuration-Recipes#customize-buffers-display-to-look-like-leaderf

For instance:

image

Now, if we save a session with many opened buffers (mksession) and later open all of them with "so", this snippet doesn't work very well, the buffer name gets truncated:

image

Neovim version

NVIM v0.11.0-dev-301+g6c3f7e7e2
Build type: RelWithDebInfo     
LuaJIT 2.1.1716656478

Operating system and version

macOS 14.6.1

Telescope version / branch / rev

0.1.8

checkhealth telescope

telescope: health#telescope#check

Checking for required plugins ~
- OK plenary installed.
- OK nvim-treesitter installed.

Checking external dependencies ~
- OK rg: found ripgrep 14.1.0
- OK fd: found fd 10.1.0

===== Installed extensions ===== ~

Telescope Extension: `advanced_git_search` ~
- No healthcheck provided

Telescope Extension: `ascii` ~
- No healthcheck provided

Telescope Extension: `color_names` ~
- No healthcheck provided

Telescope Extension: `conflicts` ~
- No healthcheck provided

Telescope Extension: `emoji` ~
- No healthcheck provided

Telescope Extension: `env` ~
- No healthcheck provided

Telescope Extension: `file_browser` ~
- No healthcheck provided

Telescope Extension: `fzf` ~
- OK lib working as expected
- OK file_sorter correctly configured
- OK generic_sorter correctly configured

Telescope Extension: `git_diffs` ~
- No healthcheck provided

Telescope Extension: `git_submodules` ~
- No healthcheck provided

Telescope Extension: `gitmoji` ~
- No healthcheck provided

Telescope Extension: `glyph` ~
- No healthcheck provided

Telescope Extension: `http` ~
- No healthcheck provided

Telescope Extension: `node_modules` ~
- No healthcheck provided

Telescope Extension: `noice` ~
- No healthcheck provided

Telescope Extension: `notify` ~
- No healthcheck provided

Telescope Extension: `recent_files` ~
- No healthcheck provided

Telescope Extension: `session-lens` ~
- No healthcheck provided

Telescope Extension: `software-licenses` ~
- No healthcheck provided

Telescope Extension: `terraform_doc` ~
- No healthcheck provided

Telescope Extension: `tmux` ~
- No healthcheck provided

Telescope Extension: `whaler` ~
- No healthcheck provided

Telescope Extension: `xray23` ~
- No healthcheck provided

Telescope Extension: `z` ~
- No healthcheck provided

Steps to reproduce

  1. Using the referred snippet for the Telescope Buffers, open many buffers manually (2 or 3 should be enough)
  2. Run: Telescope buffers

This works just fine and all buffers are shown. Now, continuing ...

  1. Save session: mksession /tmp/mysession
  2. Quit nvim
  3. Open nvim without providing any filename
  4. Load saved session: so /tmp/mysession
  5. Run: Telescope buffers

The buffer names are not shown when using the snippet "to look like leaderF" .. but using the standard Telescope Buffers then it works fine.

Expected behavior

Snippet "Customize buffers display to look like LeaderF" to display the buffers names properly, as the standard "Buffers" do.

Actual behavior

Snippet "Customize buffers display to look like LeaderF" does not display buffers name, after loading a saved session.

ps:

The minimal version works fine with standard config but after adding the buffers display configuration to look like LeaderF, then "Telescope buffers" is not showing anything

Any idea what's wrong with this minimal?

Minimal config

-- I'm having some issues adding the referred snippet to this minimal
-- This minimal was working fine, but after adding the referred snippet now something got broken

local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs { "config", "data", "state", "cache" } do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.uv.fs_stat(lazypath) then
  vim.fn.system {
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  }
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  {
    "nvim-telescope/telescope.nvim",
    dependencies = {
      "nvim-lua/plenary.nvim",
      "nvim-tree/nvim-web-devicons",
    },
    config = function()
      -- ADD INIT.LUA SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
      require("telescope").setup {
        pickers = {
          buffers = {
            -- entry_maker = require"custom_buffers_picker".gen_from_buffer_like_leaderf()
            entry_maker = function ()
              -- In: lua/rc/telescope/my_make_entry.lua
              local my_make_entry = {}

              local devicons = require"nvim-web-devicons"
              local entry_display = require("telescope.pickers.entry_display")

              local filter = vim.tbl_filter
              local map = vim.tbl_map

              function my_make_entry.gen_from_buffer_like_leaderf(opts)
                opts = opts or {}
                local default_icons, _ = devicons.get_icon("file", "", {default = true})

                local bufnrs = filter(function(b)
                  return 1 == vim.fn.buflisted(b)
                end, vim.api.nvim_list_bufs())

                local max_bufnr = math.max(unpack(bufnrs))
                local bufnr_width = #tostring(max_bufnr)

                local max_bufname = math.max(
                  unpack(
                    map(function(bufnr)
                      return vim.fn.strdisplaywidth(vim.fn.fnamemodify(vim.api.nvim_buf_get_name(bufnr), ":p:t"))
                    end, bufnrs)
                  )
                )

                local displayer = entry_display.create {
                  separator = " ",
                  items = {
                    { width = bufnr_width },
                    { width = 4 },
                    { width = vim.fn.strwidth(default_icons) },
                    { width = max_bufname },
                    { remaining = true },
                  },
                }

                local make_display = function(entry)
                  return displayer {
                    {entry.bufnr, "TelescopeResultsNumber"},
                    {entry.indicator, "TelescopeResultsComment"},
                    {entry.devicons, entry.devicons_highlight},
                    entry.file_name,
                    -- {entry.dir_name, "Comment"}
                    {entry.dir_name, "FoldColumn"}
                  }
                end

                return function(entry)
                  local bufname = entry.info.name ~= "" and entry.info.name or "[No Name]"
                  local hidden = entry.info.hidden == 1 and "h" or "a"
                  local readonly = vim.api.nvim_buf_get_option(entry.bufnr, "readonly") and "=" or " "
                  local changed = entry.info.changed == 1 and "󰔤" or ""
                  local indicator = entry.flag .. hidden .. readonly .. changed
                  -- local indicator = "" .. "" .. readonly .. changed
                  -- local indicator = changed

                  local dir_name = vim.fn.fnamemodify(bufname, ":p:h")
                  local file_name = vim.fn.fnamemodify(bufname, ":p:t")

                  local icons, highlight = devicons.get_icon(bufname, string.match(bufname, "%a+$"), { default = true })

                  return {
                    valid = true,

                    value = bufname,
                    ordinal = entry.bufnr .. " : " .. file_name,
                    display = make_display,

                    bufnr = entry.bufnr,

                    lnum = entry.info.lnum ~= 0 and entry.info.lnum or 1,
                    indicator =  indicator,
                    devicons = icons,
                    devicons_highlight = highlight,

                    file_name = file_name,
                    dir_name = dir_name,
                  }
                end
              end

              return my_make_entry
                          end
          }
        },

      }
    end,
  },
}

require("lazy").setup(plugins, {
  root = root .. "/plugins",
})
rodhash commented 4 weeks ago

The same issue happens when using https://github.com/scottmckendry/telescope-resession.nvim, which seems to be a better option than mksession.

The buffer names get truncated after restoring a session:

image

But if I go back to the standard config, buffer names appear as expected (not truncated):

image

My main issue with the standard config (which works fine) is, sometimes it shows long path before the filename.. so it gets messy .. that's why I'm looking for the leaderF style

jamestrew commented 4 days ago

Hmm... I'm not able to replace this Looks like the custom gen_from_buffers_like_leaderf is handling the getting of the buffer filename with the longest length.

You may need to do some debugging on your end. Alternatively, on the master branch, there is a path_display = { 'filename_first' } option that yields similar results. eg: :Telescope buffers path_display={'filename_first'}