telescope.nvim icon indicating copy to clipboard operation
telescope.nvim copied to clipboard

Provide description for additional mappings

Open helins opened this issue 11 months ago • 2 comments

Is your feature request related to a problem? Please describe. When adding mappings in the configuration of a picker, there does not seem to be a way for providing a description that would be rendered by which_key.

Describe the solution you'd like Ideally, a description could be provided under desc in the opts of the mapping, to mimick the standard way.

Additional context Without a description, which_key shows as a description some gibberish like _13_.

helins avatar Mar 12 '24 15:03 helins

we kinda already support this but i think not consistently, or there is some bug. e.g. it doesnt work for key_func.type == "command" but it just works if you pass in a function: ["<c-c>"] = { function() vim.cmd [[ stopinsert ]] end, opts = { desc = "stopinsert" } },. at least on latest master, it does not work on 0.1.5 yet because it would be a new feature, so semver says we are not allowed to backport new features :laughing:

can you provide the additional mappings that you've added? because i think there should never be a _13_.

Conni2461 avatar Mar 12 '24 15:03 Conni2461

Came here to comment the same issue. Here's a not-so-minimal example

  pickers.new(
    opts,
    {
      prompt_title = "Session Viewer",
      finder = finders.new_table(
        {
          results = vim.tbl_flatten(sessions),
          entry_maker = function(entry)
            -- ... More stuff here ...
            return result
          end,
        }
      ),
      sorter = telescope_config.file_sorter(opts),
      attach_mappings = function(prompt_buffer, map)
        map(
          "n",
          "n",
          function() _USE_NICE_NAMES = not _USE_NICE_NAMES end,
          -- NOTE: `desc` is currently unsupported by telescope.nvim
          -- Reference: https://github.com/nvim-telescope/telescope.nvim/issues/2981
          --
          {desc = "Toggle [n]ice names or full path displays."}
        )

        map(
          "i",
          "<CR>",
          function()
            actions.close(prompt_buffer)

            local selection = state.get_selected_entry()

            -- Schedule buffers cleanup to avoid callback issues and source the session
            vim.schedule(
              function()
                local path = selection.value

                configuration.DATA.run_pre_switch_hook(path)

                vim.cmd.wall()
                vim.cmd[[%bwipeout]]
                vim.cmd.source(path)

                configuration.DATA.run_post_switch_hook(path)
              end
            )
          end,
          -- NOTE: `desc` is currently unsupported by telescope.nvim
          -- Reference: https://github.com/nvim-telescope/telescope.nvim/issues/2981
          --
          {desc = "Select a session to load."}
        )

        return true
      end,
    }):find()

When you press ? in the Telescope floating window, it renders ()_USE_NICE_NAMESnot_USE_NICE… rather than the intended desc

ColinKennedy avatar Mar 27 '24 07:03 ColinKennedy

I'm having the same problem, and it looks like the problem is related to here:

https://github.com/nvim-telescope/telescope.nvim/blob/f12b15e1b3a33524eb06a1ae7bc852fb1fd92197/lua/telescope/builtin/init.lua#L555-L564

Once I changed it to the following code it worked fine:

if pconf.mappings then 
   defaults.attach_mappings = function(_, map) 
     for mode, tbl in pairs(pconf.mappings) do 
       for key, action in pairs(tbl) do 
-        map(mode, key, action) 
+        map(mode, key, action, action.opts) 
       end 
     end 
     return true 
   end 
 end 

Is this the expected behavior? @Conni2461

P.S.

Some quirks (very subjective) make me prefer placing configurations during initialization rather than at runtime. So until this issue is truly “resolved”, I use the following code as a workaround....

  Autocmd.new_augroup('telescope').on('WinEnter', function(args)
    local bufnr = args.buf

    libP.async.run(function()
      libP.async.util.scheduler()

      if not Plgs.is_loaded('telescope.nvim') then
        return
      end

      if not this.api.is_prompt_buf(bufnr) then
        return
      end

      local picker = this.api.get_current_picker(bufnr)
      if not picker then
        return
      end

      Keymap.register(e_mode.NORMAL, '<C-CR>', Tools.wrap_f(this.api.delete_entries, state.bufnr), { buffer = state.bufnr, desc = 'TELESCOPE: delete entries.' })
    end)
  end)

ngpong avatar Jun 10 '24 06:06 ngpong

This appears to be fixed on the master branch. Probably here https://github.com/nvim-telescope/telescope.nvim/pull/2892 (which should probably backported to the 0.1.x branch). I tried to replicate this using a couple of ways to map actions and for both, I'm able to set a description.

My minimal config:

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",
    -- dir = "~/projects/telescope.nvim",
    -- tag = "0.1.7",
    dependencies = {
      "nvim-lua/plenary.nvim",
    },
    config = function()
      local FOO = true
      require("telescope").setup({
        defaults = {
          mappings = {
            n = {
              ["<C-i>"] = {
                function()
                  FOO = not FOO
                end,
                type = "action",
                opts = { desc = "<C-i> noop" },
              },
            },
          },
        },
        pickers = {
          find_files = {
            attach_mappings = function(_, map)
              map("n", "<C-j>", function()
                FOO = not FOO
                end, { desc = "<C-j> noop" })
              return true
            end,
          },
        },
      })
    end,
  },
}

require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

image

jamestrew avatar Jun 12 '24 02:06 jamestrew

Unless I'm corrected, I'll consider this closed.

jamestrew avatar Jun 26 '24 01:06 jamestrew