sideways.vim
sideways.vim copied to clipboard
Ability to move conditions around && and ||
For example in Java:
if (a && b && c != null) {
...
I would like to change a && b to b && a or b && c != null to c != null && b with Sideways.
I've created a branch that supports this particular case, only for java: if-clause-support. A general solution might be possible, but it would also be tricky. For ruby in particular, I discuss the idea here: https://github.com/AndrewRadev/sideways.vim/issues/13.
A few issues:
-
Nesting conditions in brackets will allow you to swap conditions at the "top" level, but not within the brackets. So, in
if (foo && (bar || baz)), you'd be able to swap the "foo" and the "bar || baz" groups, but within the brackets, you wouldn't be able to swap "bar" and "baz". This is a matter of detecting what kind of list this is -- I literally check "does it start with if (", and if it doesn't (and this one just starts with(, somewhere within an if-clause), then I don't know, I guess it's probably comma-delimited? -
It's implemented as java-specific, because different languages have different if-clause syntax. In ruby and rust, you don't have
(brackets around the condition, in other languages you do. Some langs give youandandor, java doesn't. Stuff like that. I don't know a good solution here, other than writing your own patterns (or copying them) and putting them in an ftplugin definition. Which I should really document at some point.
If you give me more examples of "I'd like this to work like this", I could either tell you "this won't work for these and these reasons", or try to implement it within the framework of the plugin. That way, we could refine a useful functionality and eventually merge it to master, or give it up as unfeasible and/or come up with different solutions. For now, the branch is there, maybe try it out and see what you're missing.
Ah, sorry, I didn't see that there already was an issue for that. I guess this one could be closed as duplicate.
I'm not sure I can give more examples. The ones that you came up with are enough.
I don't know how the plugin works exactly, but can't you expose a "logic_operators" setting per language (&&, || in Java, and, or in CoffeeScript, etc) and then when the user tries to swap something in an if-clause (this could also be a setting per language (if (...) in Java, if ... \n in CoffeeScript)), it would search left and right from the cursor to either a "logic_operator" or a paren and swap these... I have no idea if that makes sense at all in VimScript. I'll leave you to decide that.
Anyway, thanks for the branch. I guess it is useful enough (you could extend it also for C/C++/JavaScript as they have the same syntax) to be merged in master.
Now I just noticed that I cannot live with argument text objects and tried to do the same for the conditions in an if-clause. So a "condition text object" could be a nice addition to any language.