nvim-treesitter-textobjects icon indicating copy to clipboard operation
nvim-treesitter-textobjects copied to clipboard

include_surrounding_whitespace should be an option on the mapping itself

Open myarcana opened this issue 1 year ago • 2 comments

Instead of requiring users to implement and write their own monolithic function themselves as in

keymaps = {
    ["af"] = "@function.outer",
    ["if"] = "@function.inner",
    ["aa"] = "@attribute.outer",
    ["ia"] = "@attribute.inner",
    ....
],
include_surrounding_whitespace = function (a)
    if a.query_string == "@function.outer" then
        return false
    elseif a.query_string == "@function.inner" then
        return false
    elseif a.query_string == "@attribute.outer" then
        return true
    elseif a.query_string == "@attribute.inner" then
        return false
    ....
end

I think making it an option on the keymap is a nicer and more expected API:

keymaps = {
    ["af"] = "@function.outer",
    ["if"] = "@function.inner",
    ["aa"] = { query = "@attribute.outer", includes_surrounding_whitespace = true },
    ["ia"] = "@attribute.inner",
    ....
],

myarcana avatar Apr 04 '23 00:04 myarcana

I am also having a problem trying to make an include_surrounding_whitespace function now,

include_surrounding_whitespace = function (a)
    print(vim.inspect(a))
    print(vim.inspect(a.query_name))
    ...

prints

{                                                                                                                                                                                                                                 
  query_string = "@attribute.outer",
  selection_mode = "v"
}
nil

The query_name field on the parameter is somehow nil, even though the table looks pretty correct according to vim.inspect

myarcana avatar Apr 04 '23 00:04 myarcana

@myarcana, I think you're looking for query_string, like in your original snippet, not for query_name? I have a variation of your snippet in my config, and it works:

     include_surrounding_whitespace = function (query)
        if string.find(query.query_string, "outer") then
          return true
        end

        return false
      end

+1 on handling it via an option

victorivanov avatar Apr 06 '23 00:04 victorivanov