cshuaimin / ssr.nvim

Treesitter based structural search and replace plugin for Neovim.
MIT License
897 stars 12 forks source link

Feature Request: script modifier / rewrite properties #25

Open bo5o opened 1 year ago

bo5o commented 1 year ago

As described here, in structural replace it is possible to apply small script modifiers to values. For example, Field.name.toLowerCase() turns the matched value into lower-case and then replaces it.

example

It would be nice if this was possible in ssr.nvim with lua scripts or to have something like Comby's rewrite properties.

cshuaimin commented 1 year ago

This would definitely be a great enhancement! However adding a bunch of rewrite properties is very limited, you can only use a predefined set of simple functions. And use full power of Lua scripting is an overkill.

An observation is that these rewrite properties only apply to leaf nodes, like an identifier or quoted string. Texts of leaf nodes are not structured, so I cannot use structured edits. However this is what vim’s really good at. I want to make ssr.nvim can call vim’s built commands, so I can use gu for toLowerCase and backtick to toggle case. I also want to call commands from plugins, so I can use dial.nvim to change more complicated text cases. I can use the same technique I’m already familiar with in vim. No need to learn ssr.nvim specific functions.

Lastly, I’m thinking about a “structural macro recording” feature, where ssr.nvim translates ”big” changes to structural replace and keeps “in-leaf-node” edits. Making ssr.nvim uses vim commands instead of predefined properties or Lua scripts also fits well in this situation.

(Just some thoughts, I don’t think I have energy to implement anything recently 😢)

bo5o commented 11 months ago

If I understand correctly you'd want something like an enhanced macro that records keypresses/commands on tree-sitter nodes and replays those on every structural edit?

Maybe an example (in Python, but doesn't really matter):

one = (1, "one")
two = (2, "two")

I want to switch the position of two values in a tuple and at the same time make the variable name uppercase.

With Comby-like rewrite properties I can imagine something like this

SEARCH:
$var = ($num, $str)
REPLACE:
$var.UPPERCASE = ($str, $num)

How would the macro approach look like for the same? Where/when would I record the macro? I am not sure it would be easy to capture what is a structural edit and what is a regular edit?

horriblename commented 1 month ago

wrote a PR for using lua expressions in the REPLACE field #39

I wrote it before reading the discussion here, but I just want to leave some thoughts here.