treewalker.nvim icon indicating copy to clipboard operation
treewalker.nvim copied to clipboard

Treewalker SwapRight/SwapLeft seem to not always work and sometimes even break entire block

Open I-Own-You opened this issue 3 months ago • 4 comments

https://github.com/user-attachments/assets/71f396e4-bbd0-43ed-b702-e1e48714797b

main problems:

  1. it doesnt swap nodes if the node above is first inside a block
  2. it literally swaps elements within a construction (for loop variables as example) with the nodes of the block itself (next statement after for loop as example)
  3. it doesnt always want to swap left/right
  4. this issue appears in every parser as i checked

code to reproduce

package main

import (
	"fmt"
)

func plus(a int, b int) int {
	return a + b
}

func plusPlus(a, b, c int) int {
	return a + b + c
}

func vals() (int, int) {
	return 3, 7
}

func anotherFunc() (a int, b int) {
	return a, b

}

func sum(nums ...int) {
	fmt.Print(nums, " ")
	total := 0
	for _, num := range nums {
		total += num
	}
	fmt.Println(total)
}

func intSeq() func() int {
	i := 0
	return func() int {
		i++
		return i
	}
}

func fact(n int) int {
	if n == 0 {
		return 1
	}
	return n * fact(n-1)
}

func myFunc() func() {
	a := func() {}
	
	return a
	
	return func() {}
}

func main() {

	res := plus(1, 2)
	fmt.Println("1+2 =", res)
	res = plusPlus(1, 2, 3)
	fmt.Println("1+2+3 =", res)

	a, b := vals()
	fmt.Println(a)
	fmt.Println(b)

	_, c := vals()
	fmt.Println(c)

	sum(1, 2)
	sum(1, 2, 3)

	nums := []int{1, 2, 3, 4}
	sum(nums...)

	nextInt := intSeq()
	fmt.Println(nextInt())
	fmt.Println(nextInt())
	fmt.Println(nextInt()) 

	anotherNextInt := intSeq()
	fmt.Println(anotherNextInt()) 
	fmt.Println(anotherNextInt())
	fmt.Println(anotherNextInt())

	fmt.Println(fact(7))

	var fib func(n int) int
	fib = func(n int) int {
		if n < 2 {
			return n
		}
		return fib(n-1) + fib(n-2)
	}
	fmt.Println(fib(7))

	var AnonFunc = func(a int, b int) int {
		return a + b
	}(1, 2) 
	fmt.Printf("AnonFunc: %v\n", AnonFunc)

	outerFunc := func(x int) func(y int) int {
		innerFunc := func(y int) int {
			fmt.Println(x, y)
			return x * y
		}

		return innerFunc
	}
	fmt.Printf("outerFunc(5): %v\n", outerFunc(5)(2))
}

I-Own-You avatar Aug 22 '26 23:08 I-Own-You

Addressed - please try it out and reopen if it's still broken for you, another thanks for another issue!

aaronik avatar Aug 24 '26 18:08 aaronik

it somehow fixed, but not everyting

https://github.com/user-attachments/assets/2deb59cb-8e52-4bca-8bc5-0d178b23138e

for the last example, swap left/right should not act as swap up/down, right ? it can lead to inconsistency, but if it should, then its okay, i guess ?

I-Own-You avatar Aug 24 '26 20:08 I-Own-You

That all looks OK to me:

I see three things in there:

  1. return a + b + c where variables can't be laterally swapped. The parser represents this not as three different nodes of the same type, and to fix an earlier bug, we had to change it so that only like nodes were swapped. This didn't break any tests, so it didn't break any explicit expectations - it might have broken accidental behavior, but we can't have everything.

  2. Swapping variables like you have in that multiple assignment example _, c = ... - everything in there looks to be working as far as I can tell - you can't swap left the leftmost item, it's already as far left as it can go, and it doesn't purport to wrap.

  3. Swapping left/right does look similar to swapping up/down if the node you're on is at a line level - see https://github.com/aaronik/treewalker.nvim#swapping:

Swap{Left,Right} operate on a literal nodewise basis, and are meant for swapping function arguments, enum members, list elements, etc -- things that are many per line. If used on a top level node on a line, it'll swap the same nodes as Up/Down, but won't take the comments, decorators or annotations with them.

(emphasis mine on the above)

aaronik avatar Aug 24 '26 21:08 aaronik

then we consider it fixed

I-Own-You avatar Aug 24 '26 21:08 I-Own-You