Skip to content

Function to map query name into user friendly name #816

@chancez

Description

@chancez

Is your feature request related to a problem? Please describe.
I'm switching to the main branch and all is good, but when defining mappings as documented https://github.com/nvim-treesitter/nvim-treesitter-textobjects/tree/main?tab=readme-ov-file#text-objects-select it would be great if we could get "user friendly" descriptions for each text object.

Describe the solution you'd like
A function we can import that converts @function.outer, etc into something a bit more "friendly", eg: "outer function" at it's simplest. But ideally each text object would have a "description" defined that could be retrieved and used for keymaps/etc.

Describe alternatives you've considered
Right now I'm just converting @foo.bar into bar foo using gsub:

local keymaps = {
  -- You can use the capture groups defined in textobjects.scm
  ['af'] = '@function.outer',
  ['if'] = '@function.inner',
  ['ac'] = '@class.outer',
  ['ic'] = '@class.inner',
}

for keys, query in pairs(keymaps) do
  -- Convert "@foo.bar" into "bar foo"
  local desc = query:sub(2):gsub('(%a+).(%a+)', '%2 %1')
  vim.keymap.set({ "x", "o" }, keys, function()
    select.select_textobject(query, "textobjects")
  end, { desc = desc })
end

Additional context
Add any other context or screenshots about the feature request here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions