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

Unable to switch context from/to a comment inside a node

Open I-Own-You opened this issue 1 month ago • 11 comments

golang:

func kek() {
	if true { // if you try to swap right from here it will not work, so it will not enter inside if
		// if you try to swap left from here, it will work
	}
	a := 2
}

lua:

ripgrep = {
    module = "blink-ripgrep",
    name = "Ripgrep",
    opts = { -- if you try to swap right from here, it goes inside transform_items function instead of the comment inside "opts"
        -- if you try to swap left from here, it goes do the transform_items function instead of "opts" parent
    },
    transform_items = function(_, items)
        for _, item in ipairs(items) do
            item.labelDetails = {
                description = " 󰥨 ",
            }
        end
        return items
    end,
},

but as soon as you introduce something else beside a comment it acts as expected.

i checked c/rust and it acts the same, other languages will act the same i guess.

lua is the weirdest one since it jumps to the next node below somehow

I-Own-You avatar Nov 05 '26 12:11 I-Own-You

Oh this is actually expected behavior - comments are not jump targets, they're ignored by the plugin. This is deliberate - the AST very much treats comments as nodes, but for me when moving around my code, i don't want to end up on comments hardly ever. This example you're talking about is the most sensible one I've seen to land on comments, but it's still deliberately out of the purview of the plugin.

As for the lua swapping, that's also deliberate, although yeah in this case it seems a little weird. If you put your cursor on opts, then go right, you'll end up on the next node that's not a comment and is below and to the right of opts, which is that for. That's all deliberate :)

aaronik avatar Nov 05 '26 16:11 aaronik

this really ends up bad if you are deeply nested, at least for lua, not that much of an issue but sometimes its overwhelming(in lua especially) where you are deeply nested and being on a comment, swapping left could literally take you hundreads of lines below inside the exact same level of nesting or less if there is no same nested thing, i mean C-o fixes this but still

thanks for the explanation, i wanted to know why this works like this since i literally tried to swap left from a comment deeply nested and i flew like 80 lines down and 4 nested context inside.

I-Own-You avatar Nov 05 '26 17:11 I-Own-You

Yep, C-o and u are your friends here.

I'd like the plugin to work perfectly for every use case, but I'm not sure just how to do that.

aaronik avatar Nov 05 '26 17:11 aaronik

Yep, C-o and u are your friends here.

I'd like the plugin to work perfectly for every use case, but I'm not sure just how to do that.

its not really that i demand it, i was just wondering why i flew around, its not that important, i will just know to not do such things in lua so maybe in the future you will do something with it

I-Own-You avatar Nov 05 '26 17:11 I-Own-You

How would you suggest it work to better fit all use cases? Out of curiosity?

aaronik avatar Nov 05 '26 17:11 aaronik

How would you suggest it work to better fit all use cases? Out of curiosity?

i would make it so that you could actually interact with the commnts as other nodes do it so its expected (if it doesnt change a lot of the logic of course).

if the desire to not interact with the comments is personal, i would just make it work with the comments since i would be more thinking about the flow of the plugin and not my taste, but its me, i prevail technical corectness of stuff rather than my wishes because this would mean the plugin works the same everywhere and not because i wanted something specific which leads to inconsistency.

I-Own-You avatar Nov 05 '26 17:11 I-Own-You

take my words as a pure critical thinking response, the intention is not to look down upon your work. I am writing this because nowadays a lot of people are too emotional and soft where they get everything as an attack

I-Own-You avatar Nov 05 '26 17:11 I-Own-You

So, my taste, which equals the original direction of the plugin which has persisted throughout the entirety of the plugin, is to keep comments out of being jump targets. Your taste, which would be a massive behavioral shift for the plugin as its been since day 1, is to land on comments. I don't see at all why you're saying your taste is some kind of technical correctness - it's just more taste.

I have an idea - why don't you go ahead and try the tree-climber plugin at https://github.com/drybalka/tree-climber.nvim. That one takes a much more literal approach to AST nodes. Treewalker makes dozens of alterations in order to have a very specific way of walking around code. Try tree-climber and let me know the differences you find. That'll shed some light on what is "taste" vs what is "correct".

aaronik avatar Nov 05 '26 17:11 aaronik

i have tried the other plugins and landed on this as the most mature and supported since the other ones are either not maintained or even archived or do different things.

so as i said, you took it too personal, i will probably stop here and just use the plugin as it is and try to fix it myself if i encounter future problems.

thank you for your work.

I-Own-You avatar Nov 05 '26 18:11 I-Own-You

Ah, this just occurred to me - what about the scope_confined setting (see https://github.com/aaronik/treewalker.nvim#options) - this would lock you into the current scope, preventing the more wild jumps I hear you talking about here

aaronik avatar Nov 05 '26 20:11 aaronik

this option is enabled since the beggining of using this plugin, it doesnt help with the comment in lua, dont bother with this, its not that big of a deal, current functionality basically covers everything mostly

I-Own-You avatar Nov 07 '26 02:11 I-Own-You