CKolkey / ts-node-action

Neovim Plugin for running functions on nodes.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

I'm looking for a way to hang more actions off the same node type with a direct keymap.

unphased opened this issue · comments

We can attach more than one action to a given node type, but this forces us to make a choice each time. For example I am going to author a new node action on a property_identifier for a few of the languages I use often which will grab the var name and make a simple debug print out of it in the next line of code.

But I don't want to have to delete the existing actions.cycle_case() mapped to property_identifier, and neither do i want to insert another action to make to trigger this.

I'd prefer to be able to establish different sets of node actions that are mapped to different key binds! So I could have, say, a mapping that will apply "traditional" actions like cycle case and toggle multiline/operator/quote etc, a separate mapping that applies "expanding" refactoring automations as an example, another mapping that applies "contracting" refactoring automations... etc. You get the idea.

I discovered require("ts-node-action").available_actions() from the doc, so I think i will be able to make binds that call this and do something appropriate based on what this emits.

I had to hack this function like so:

local function do_action(action, node)
  local act = type(action) == "table" and action[1][1] or action
  local replacement, opts = act(node)
  if replacement then
    replace_node(node, replacement, opts or {})
  end
end

to get

vim.keymap.set('n', '<leader>E', function ()
    local actions = require('ts-node-action').available_actions()
    -- run the last one
    log('actions and last', actions, actions[#actions].action())
  end, {
  desc = "Node Action -- last"
})

to work.