codemirror / dev

Development repository for the CodeMirror editor project
https://codemirror.net/
Other
5.94k stars 376 forks source link

selectParentSyntax does not select whole file #1436

Closed jrvidal closed 2 months ago

jrvidal commented 2 months ago

Describe the issue

It seems like selectParentSyntax() is unable to extend the selection all the way up to the syntax tree root (e.g. the whole Script in a JS file). I'm not sure this is intended, but I do find it surprising.

For instance, in the demo editor in https://codemirror.net, if a click on console and hit Ctrl+i repeatedly, it gets stuck in the function block.

Seems like the culprit is here, but I don't know what the node.parent?.parent check is supposed to prevent.

Browser and platform

No response

Reproduction link

No response

marijnh commented 2 months ago

This is intentional behavior. You have ctrl-a to select the entire file. Treating it as a syntactic unit doesn't really add anything meaningful.

jrvidal commented 2 months ago

That's fair, I suppose I can use the returned boolean as a hint to extend the selection manually myself.

However, I'll note that it doesn't just prevent the selection to cover the whole file. It also blocks top-level items from being selected e.g. in the chain ... -> Block -> FunctionDeclaration -> Script, I'm stuck at Block. And that's not something I can work around on my own as easily.

EDIT: oh, wait, selectParentSyntax() always returns true. Is that something that would make sense to change? Not sure if false is supposed to mean "unable to run" or, instead, "no changes to be made".

marijnh commented 2 months ago

Oh I see what you mean. That isn't intentional. Attached patches should improve it.

jrvidal commented 2 months ago

Thanks for the prompt response, those fixes will come in handy.