Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: script modifier / rewrite properties #25

Open
bo5o opened this issue Jul 16, 2023 · 3 comments
Open

Feature Request: script modifier / rewrite properties #25

bo5o opened this issue Jul 16, 2023 · 3 comments

Comments

@bo5o
Copy link

bo5o commented Jul 16, 2023

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
Copy link
Owner

cshuaimin commented Jul 17, 2023

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
Copy link
Author

bo5o commented Jul 26, 2023

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
Copy link

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.

  • integration with multicursor plugins might be more in line with what cshuaimin suggested.

  • while lua is probably overkill, it's familiar to the user and makes it easier for users to extend this functionality with their own scripts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants