Skip to content

better getModuleAt#905

Merged
davidanthoff merged 3 commits intomasterfrom
sp/get-module-at-inside-only
Mar 3, 2021
Merged

better getModuleAt#905
davidanthoff merged 3 commits intomasterfrom
sp/get-module-at-inside-only

Conversation

@pfitzseb
Copy link
Copy Markdown
Member

@pfitzseb pfitzseb commented Mar 3, 2021

by ensuring that the offset is inside (not on the edge of) the EXPR's span.

Fixes julia-vscode/julia-vscode#1600 and similar issues (at least when the cursor actually is outside of the module definition).

For every PR, please check the following:

by ensuring that the offset is inside (not on the edge of) the EXPR's span
@pfitzseb pfitzseb added the bug label Mar 3, 2021
@pfitzseb pfitzseb added this to the Next Patch milestone Mar 3, 2021
offset = get_offset2(doc, params.position.line, params.position.character, true)
x = get_expr(getcst(doc), offset)
offset = get_offset2(doc, params.position.line, params.position.character)
x = get_expr_or_parent(getcst(doc), offset - 1)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure there's an off-by-one error somewhere between the CST and what get_offset returns, which might very well cause issues in other cases...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a repo case? I think it would be super valuable for us to fix these kind of problems for good...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a systematic error/difference. When you have the cursor at position (0,0) then get_offset returns 1, but we usually start indexing the position in the CST from 0. Neither is incorrect, but afaict we're inconsistent about it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So get_offset2 is all around 1-based, I think. And then the idea is that it designates the position to the left of the indexed byte. So generally, the lowest offset that we ever need/get is 1, because that designates the point to the left of the first character in any string. But we can get offset positions that are larger than the length of a string, because when we designate a range say for the entire content of a string, then the stop bye position will be lastbyte+1.

I think that all follows the conventions on the VS Code side of things and the LSP, with the exception that our offsets internally are 1 rather than 0 based.

I have to admit I don't really understand why CST would ever return an offset of 0 :) If that is a 1-based offset, then that would amount to a -1 offset in 0-based land, which I don't understand why we would ever want that?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think we just usually iterate over the text with a zero-based offset (because that's much more natural for an offset as opposed to an index). See e.g. the definition of get_expr, which includes a pos=0 default argument. I've added a comment and made the code a bit clearer though.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think that means that get_offset2 is probably misnamed, and it should really be named get_string_index or something like that, because it most definitely returns an index and not an offset in the way you defined it (which makes sense to me).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Let's tackle that in a follow up though.

davidanthoff
davidanthoff previously approved these changes Mar 3, 2021
offset = get_offset2(doc, params.position.line, params.position.character, true)
x = get_expr(getcst(doc), offset)
offset = get_offset2(doc, params.position.line, params.position.character)
x = get_expr_or_parent(getcst(doc), offset - 1)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a repo case? I think it would be super valuable for us to fix these kind of problems for good...

@davidanthoff davidanthoff merged commit 8f7a6a9 into master Mar 3, 2021
@davidanthoff davidanthoff deleted the sp/get-module-at-inside-only branch March 3, 2021 21:11
@oppo-source oppo-source removed the request for review from a team April 16, 2021 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Re-evaluating module leads to nested module

2 participants