Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide description for additional mappings #2981

Closed
helins opened this issue Mar 12, 2024 · 5 comments
Closed

Provide description for additional mappings #2981

helins opened this issue Mar 12, 2024 · 5 comments
Labels
enhancement Enhancement to performance, inner workings or existent features

Comments

@helins
Copy link

helins commented Mar 12, 2024

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 helins added the enhancement Enhancement to performance, inner workings or existent features label Mar 12, 2024
@Conni2461
Copy link
Member

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 😆

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

@ColinKennedy
Copy link

ColinKennedy commented Mar 27, 2024

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

@NGPONG
Copy link

NGPONG commented Jun 10, 2024

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

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)
end
end
return true
end
end

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)

@jamestrew
Copy link
Contributor

This appears to be fixed on the master branch. Probably here #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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement to performance, inner workings or existent features
Projects
None yet
Development

No branches or pull requests

5 participants