chrisgrieser / nvim-rip-substitute

Perform search and replace operations in the current buffer using a modern user interface and contemporary regex syntax.
MIT License
127 stars 7 forks source link

feat: add range option for sub function #5

Closed JarKz closed 1 month ago

JarKz commented 1 month ago

Main concept

Adds the range option for function sub(), which can be used for ranging lines instead of using Visual line mode for most cases.

Motivation

It's very helpful for substituting the variables in range from current line and below. Some programming languages allows reuse the variable with the same name for another type, so the LSP can't substitute correctly in this case. And it can work in opposite case - when I only want to change the variable name in range from specific line above to current.

Implementation

I added the optional table argument for function sub() named opts. Also added the opts.lua module. It can be used easily for extending the function by new features.

Checklist

chrisgrieser commented 1 month ago

I see the value in such functionality, but I don't think that passing a range as a function parameter is the best approach to do so. Manually parsing the vim range introduces a lot of unnecessary extra logic, also I think there are various range syntaxes that vim accepts that your function does not accept, making it unintuitive for the user.

I think defining an ex command that accepts a range makes much more sense, since then the parsing of the range is done by vim. Here is how I've done it in one of my other plugins: https://github.com/chrisgrieser/nvim-scissors/blob/cc285adfc9a22ff38c449d6ab85bccb4499b1f8b/plugin/user-commands.lua#L1-L5 https://github.com/chrisgrieser/nvim-scissors/blob/cc285adfc9a22ff38c449d6ab85bccb4499b1f8b/lua/scissors/init.lua#L85-L88

JarKz commented 1 month ago

Hmm, I understand the problem about complexity. So you suggest me use absolute line numbers instead of manual parsing using nvim commands?

If yes, it makes a lot of sense.

chrisgrieser commented 1 month ago

If you want a way to accept vim ranges, I'd suggest using vim.api.nvim_create_user_command as in the linked example, yes.

JarKz commented 1 month ago

Moved from parsing range command to using only line numbers, which can be passed via the opts table. Also added RipSub nvim user command for using range.

@chrisgrieser , is it ok now?

chrisgrieser commented 1 month ago

sorry, your solution is still much too complicated and error-prone. Since it's quicker, I just implemented the solution myself, sorry.

Take a look here, vim already does all the parsing for you, so these few lines of code already do everything you needed: https://github.com/chrisgrieser/nvim-rip-substitute/commit/602b999d02039c4562584e38dcbe461335ccd70a?diff=unified&w=1

JarKz commented 1 month ago

Nope, I'm glad that this functionality is available.