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

Distinguish between methods and functions

Open briandipalma opened this issue 4 years ago • 2 comments

Is your feature request related to a problem? Please describe. I want a way to distinguish between methods and functions in JavaScript and other languages.

Describe the solution you'd like I'd like:

			goto_next_start = {
				["]k"] = "@class.outer",
				["]m"] = "(method_definition) @function",
				["]f"] = "@function.outer",
				["]b"] = "@block.outer",
			}

to work (I realize I'm not specifying javascript for ]m but I was just testing it out before cleaning it up.

Describe alternatives you've considered None

Additional context I'd like to do something like this eventually:

			goto_next_start = {
				["]k"] = "@class.outer",
				["]m"] = {
                                   javascript: "(method_definition) @function",
                                   java = "(method_declaration) @function",
                                   "@function.outer"
                                }
				["]f"] = "@function.outer",
				["]b"] = "@block.outer",
			}

So that I can correctly move from method to method in the languages I most often work with and fall back to the more generic solution when an override isn't defined.

briandipalma avatar Dec 03 '21 17:12 briandipalma

I managed to make something like this work. I first defined the following function:

function default(d, t)
  local mt = {__index = function() return d end}
  setmetatable(t, mt)
end

And you can use it like this

keymaps = {
  ["af"] = default("@function.outer", {go = nil}),
  ["if"] = default("@function.inner", {go = nil}),
  ["ac"] = default("@class.outer", {go = nil}),
  ["ic"] = default("@class.inner", {go = nil}),
}

I use it to prevent these bindings from messing with the ones from vim-go, but to do what you describe you could do something like:

goto_next_start = {
  ["]m"] = default(
    "@function.outer", {
      javascript = "(method_definition) @function",
      java = "(method_declaration) @function"})

0xflick avatar Jan 19 '22 03:01 0xflick

Actually #155 breaks this

0xflick avatar Jan 19 '22 06:01 0xflick