rhaiscript / lsp

Language server for Rhai.
Apache License 2.0
43 stars 4 forks source link

Range error with `rhai-scope` #85

Closed schungx closed 1 year ago

schungx commented 2 years ago

Failed to load. Error log attached.

panicked at 'Bad range: node range 0..487, range 505..524', C:\Users\Stephen\.cargo\registry\src\github.com-1ecc6299db9ec823\rowan-0.15.8\src\cursor.rs:786:13

lsp_log.txt

The error is raised in collect_symbols when collecting scope symbols. document_symbols.rs:79

schungx commented 2 years ago

We're looking at improper range values here.

pub fn covering_element(&self, range: TextRange) -> SyntaxElement {
    let mut res: SyntaxElement = self.clone().into();
    loop {
        assert!(
            res.text_range().contains_range(range),
            "Bad range: node range {:?}, range {:?}",
            res.text_range(),
            range,
        );
        res = match &res {
            NodeOrToken::Token(_) => return res,
            NodeOrToken::Node(node) => match node.child_or_token_at_range(range) {
                Some(it) => it,
                None => return res,
            },
        };
    }
}
schungx commented 2 years ago

The range (505..524) seems to be the length of the exact variable definition statement inside a rhai-scope code block, so I believe some offsets are screwed up...

schungx commented 2 years ago

OK, I found a pattern.

Seems like, for code inside a rhai-scope block, each character advances the position by 4 instead of 1. Therefore, if a rhai-scope block is long enough, it goes over the entire script's range.

schungx commented 1 year ago

Tried the latest version, but still getting range errors with rhai-scope.

tamasfe commented 1 year ago

Yeah I haven't touched on this yet.

tamasfe commented 1 year ago

Do you happen to have an MCVE for this?

schungx commented 1 year ago

Of course, it happens every single time when I open the foo.rhai file from examples.

Vscode will try 5 times, then fail the extension.

Of course, I'm on Windows, so I suspect it is a Windows thing, otherwise you'd have caught it ages ago.

The pattern is counting character offsets which are multiplied four times. I delete one character, the incorrect range shrinks by 4 places, all the way until the rhai-scope block is short enough (or the script is long enough) that the range no longer becomes an error. Then it works fine.

tamasfe commented 1 year ago

Seems to be a windows issue indeed, I do not account for CRLF somewhere, changing the file to LF makes the panics go away.

schungx commented 1 year ago

OK, changing that one file foo.rhai to LF makes ALL the problems go away! Everything Just Works(TM).

schungx commented 1 year ago

This seems to have fixed, even with CRLF. Closing.