Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with rename variable involving specs #1183

Merged
merged 6 commits into from
Jan 30, 2022

Conversation

plux
Copy link
Contributor

@plux plux commented Jan 26, 2022

Description

Tweak the heuristic for variable renaming inside functions so it doesn't bleed over to following -spec.
Add possibility to rename variables in specs.

Fixes #1180.

Comment on lines 225 to 227
POIs = els_poi:sort(els_dt_document:pois(Document, [ function_clause
, spec
])),
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably consider all possible top-level forms. I haven't checked, but I'd imagine similar issue will happen with things like:

foo(A) -> A.
-define(?MACRO(A), A).

and other top-level items (like -callback and -type).

TBH, I'm not sure why the logic tries to look for the beginning point of the next form. Wouldn't it be enough to look for the end point of the current clause?

Copy link
Member

@robertoaloi robertoaloi Jan 26, 2022

Choose a reason for hiding this comment

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

If I remember correctly, it's to ensure that:

  • renaming is applied to all clauses of the functions (one may argue this is not always desirable)
  • it works even when the current function is not fully parsable (still being written).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good points, I will add support for other top-level forms as well.
The reason for the currently implemented logic is that function_clause ranges don't contain the beginning and the end of the body of the clause, but rather just the function clause head.

FunRanges -> maps:get(from, lists:last(FunRanges))
end,
%% Find beginning of first function clause after VarRange
To =
Copy link
Contributor

Choose a reason for hiding this comment

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

just an idea: the function POI has a wrapping_range data field (for folding ranges). The end of the wrapping range could be used as an upper bound if the next function clause is outside of wrapping range. (There could be variables in other forms than spec, like type or macro definitions)

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe a better idea is for els_parser to store the full range of a function clause in the function_clause POI data

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I will see if I can implement an upper bound using the function wrapping_range.
If els_parser could return the full range of the function_clause that would make the implementation of this functionality cleaner, not sure how tricky it is to implement in els_parser..


-record(foo, {a :: Var,
b :: [Var]}).
%% BUG: `Var' in MACRO(`Var') is not considered a variable POI
Copy link
Contributor

Choose a reason for hiding this comment

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

thanks for finding this bug, indeed els_parser currently does not expect sub-POIs in the define name, so it does not recurse into it (els_parser:attribute_subtrees)

Copy link
Member

Choose a reason for hiding this comment

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

Should we file a ticket about this?

Copy link
Member

@robertoaloi robertoaloi left a comment

Choose a reason for hiding this comment

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

To me this looks like a very good step forward. I think we can merge this in and eventually improve it later. Thanks @plux !


-record(foo, {a :: Var,
b :: [Var]}).
%% BUG: `Var' in MACRO(`Var') is not considered a variable POI
Copy link
Member

Choose a reason for hiding this comment

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

Should we file a ticket about this?

@robertoaloi robertoaloi merged commit db82b7c into erlang-ls:main Jan 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rename on a variable renames variables in a following spec
4 participants