Skip to content

feat(live_grep): attach mapping is configurable through options#3392

Open
richardmarbach wants to merge 5 commits intonvim-telescope:masterfrom
richardmarbach:master
Open

feat(live_grep): attach mapping is configurable through options#3392
richardmarbach wants to merge 5 commits intonvim-telescope:masterfrom
richardmarbach:master

Conversation

@richardmarbach
Copy link
Copy Markdown

@richardmarbach richardmarbach commented Dec 29, 2024

Description

I started writing a new picker to improve the refinement of the live_grep picker search results and noticed that all I really wanted to do was add custom attachment_mappings to the existing live_grep picker. It would be nice if the attachment_mappings were configurable instead of needing to create a copy of an existing picker.

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

I changed my mapping for live grep to use a custom attach_mapping:

local refine_word_action = function(prompt_bufnr)
  require("telescope.actions.generate").refine(prompt_bufnr, {
    prompt_title = "Refine word results",
  })
end

local attach_mappings = function(_, map)
  map("i", "<c-space>", refine_word_action)
  return true
end

vim.keymap.set("<leader>sg", function() require("telescope.builtin").live_grep({ attach_mappings = attach_mappings }) end)

To check:

  1. Execute the mapping, e.g. <leader>sg
  2. search some text to populate the search buffer (e.g. telescope)
  3. press the refinement shortcut (e.g. <c-space>)

The prompt title should now read "Refine word results"

Configuration:

  • Neovim version (nvim --version): NVIM v0.10.3
  • Operating system and version: macOS Sequonia Version 15.1.1

Checklist:

  • My code follows the style guidelines of this project (stylua)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (lua annotations)

Copy link
Copy Markdown

@alerque alerque left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this option availability be documented somewhere?

@Kaiser-Yang
Copy link
Copy Markdown

I think this is the correct behaviour. Now, we can not delete the default mappings by configuration.

To do so, we must use filetype autocmd to delete one mapping users may not want.

BTW, maybe you should update the pickers.new instead of calls of the function.

-- For attach mappings, we want people to be able to pass in another function
-- and apply their mappings after we've applied our defaults.
if k == "attach_mappings" then
local opt_value = result[k]
result[k] = function(...)
v(...)
return opt_value(...)
end
end

@Kaiser-Yang
Copy link
Copy Markdown

I find a way do this without this PR easily, just delete the default key mappings in attach_mappings:

      lsp_dynamic_workspace_symbols = {
        attach_mappings = function(buffer)
          vim.keymap.del('i', '<c-space>', { buffer = buffer })
          return true
        end,
      },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants