Cant swap node in golang from left to right
Hi again, i encountered another problem in go where you cannot swap 2 nodes from right to left (but left ro right works)
package main
import "errors"
func main() {
}
func swapALot(a, b, c, d, e, f int) (int, error) {
return 2, errors.New("error") // if you try to swap the right node errors.New to the left, it will work
return errors.New("error"), 2 // if you try to swap the left node errors.New to the right, it will not work
// swapping "2" in both ways works
}
Another great catch! Keep them coming :)
2. Another great catch! Keep them coming :)
i have a question, in the code i provided, i cant swap complex nodes, what i means is that if i try to swap not being on errors. then it will not work, its by design ? since i always need to go to the start of the construction, its not convenient when you go backwards and end up on the right part of the construction. In other languages its the same
Another good call - how about now, does it work more intuitively? It works on the closest swappable node now
4. Another good call - how about now, does it work more intuitively? It works on the closest swappable node now
yes it works now, but you probably broke someting since now you can swap node out of its context:
func swapALot(a, b, c, d, e, f int) (error, error) {
a := 2
return 2, errors.New("error"), errors.New("dsfsdf") // swap any node from here to left multiple times, it will go and replace the entire line with the above line, it will do the same with swap to righ and swap the below line
c := 2
}
so, because the smallest node can now be active, i guess it tries to act as the whole line and swaps up/down, but isnt it better to just limit swap right/left to not even swap up/down ? so it kind of only goes right/left since its more the logical way, even on return keyword i should not be able to go down/above with swap right/left, only swap up/down should have that logic ?
Ah, yes - this is explicitly allowed as per the README. It's an idea though to disallow this behavior, but it is a behavior change. I'll think on it
6. Ah, yes - this is explicitly allowed as per the README. It's an idea though to disallow this behavior, but it is a behavior change. I'll think on it
i really hope you will look into it because there are examples of different actions which leads to inconsistency, i guess ?
for example:
- swapping funcitons with left/right do not swap them up/down, only up/down do that
- swapping something else (variables for example) WILL swap up/down by using swap left/right which is not intuitive since now i need to think about this quirk ?
the costraint that swap left/right swaps ONLY left/right would help a lot in cognitive overload.
thanks for the plugin, still
For the time being, I notice there's a bug where lateral swaps displayed broken behavior when there were trailing comments - I've fixed that now. So it still doesn't prevent multiline swaps the way you're asking, but it does stop some of the wonky, inconsistent behavior you may have been seeing.
That points to the reason why I'm hesitant to prevent multiline swaps - the plugin works swapping multiline arguments. ex. consider this lua:
it("two", function()
vim.fn.cursor(143, 1) -- In a bigger function
treewalker.right()
assert_cursor_at(144, 3)
treewalker.right()
assert_cursor_at(147, 5)
treewalker.right()
assert_cursor_at(149, 7)
end)
if you put your cursor on "two" and swap right, it'll swap with the function. Then similarly swap left again and it'll bring you back. That's important behavior in the plugin.
Also your point 1. I think is false - swapping functions with left/right will move them up/down.
Here's the thing - this plugin works on AST nodes, which are always vertical in concept. Just because on your screen the nodes are next to each other doesn't mean they are in the AST. In the AST they're still on top / below. So actually, all swaps, whether up/down or left/right, are operating up/down in the AST. When you swap left/right, it's performing the exact same operation as up/down.
In this plugin, it's actually up/down which is the weird case - that case I decided to make always line-wise, breaking out of the literal definition of swapping AST nodes, and introducing all kinds of extra checks in the plugin for line start indentation and node type checking to bring decorators and comments along. up/down is the strange case that gets all the special logic. Left/right is the more natural AST swapping case.
And if you look at other plugins or implement this for yourself with a more literal AST swapping behavior, you'll find there's only one dimension in which to swap nodes, and the nodes get really granular and it's not super useful.
Anyways those are some of the reasons why we have this predicament you're seeing with lateral swapping not behaving the way you want.
EDIT: One way to think about lateral vs vertical swaps in Treewalker is, lateral swaps take more granular targets, and vertical swaps take larget, less granular targets
8. Also your point 1. I think is false - swapping functions with left/right will move them up/down.
actually, if i am beiing on func keyword, yes, it will, but if i am on the function name it will not.
overall i agree that its an AST manipulation and not a manual implementation, its just that sometimes you fly around swapping left/right since return 2 being on 2 and swapping left should really just stay as with the last argument swapping to right should stay, but thats what it is
thanks for the explanation
Oh yeah I see what you're saying about the func keyword and lateral swapping - for left/right swaps, since it's more granular and identifies the node based on where your cursor is, if your cursor is on the keyword func, and go structures the AST as func being a node and containing the whole definition inside that node, a swap swaps the whole node. But the name is a smaller node with no siblings, so no swaps can happen.
Here the vertical swaps are again a special case - Treewalker is structured such that vertical swapping only looks at the highest node on the line - it automatically walks all the way up the tree until it gets to that highest node, then treats that as the node to swap. So that's the "less granular behavior" that vertical swapping does