-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
Extract single expression part of line #443
Comments
Currently, the return values of a extracted function come from here: refactoring.nvim/lua/refactoring/refactor/106.lua Lines 31 to 84 in 93d69cc
The code search for variables declared in the selected region and for references to variables after the selected region. Then, it does an intersection between both sets. Your example does not work because your selected region A PR adding this feature would be welcomed. |
Thanks for explaining @TheLeoP! I now understand why it does what it does. I wonder if it would make sense/could be possible to have a special case for when the visual selection is actually an expression and therefore would make sense to have as a return value? Not sure how easy it is though to know what's an expression in contrast to eg a statement from treesitter. |
In treesitter everything works with captures and you can check them using In the particular case of pyhton, it looks like the following capture would detect the right side of any expression statement (like the one in this example): (expression_statement
(assignment
right: (_) @expression)) And probably something similar can be done with every language with a treesitter parsers. Of course, implementing this feature would be more complicated than simply finding this captures. You would need to check if the node that describes the selected region is an expression and treat it as an special case inside |
I think it would be nice if one could do this for all expressions, so even if it's not only the right-hand side of an assignment, maybe for example in foo = x + |y + z| where The alternative could be to just say that whenever the visual selection is charwise, we just make the visual selection the return value of the new function. This might give something invalid depending on what the user selects but maybe that's okay? Btw, I'm happy to write the implementation once we've agreed on what is should do :) |
Maybe just doing this for charwise selection would make sense? It would then at least be consistent with extract variable. Or maybe alternatively when there are no variable expressions in the selection? |
I think that it would make sense for charwise visual selection to behave like that only if there are no returned values. Right now, if you visually select charwise your example like: |foo = x + y + z| and foo is used after that line, the function would correctly return a value and simply changing the behaviour of charwise selection may (would?) broke that. Also, since you mentioned arbitrary visual selection of expressions, you may come across some problems similar to the one explained in this comment |
Yeah I think that would make sense 👍 Should that maybe even be independent of the selection mode? Ie if the selected node (however it was selected) has no assignment/returned values, we simply place it as the return value of the new function?
Yep I noticed this indeed, but makes sense due to the tree structure of binary operations. I also noticed another bug with indentation when extracting a variable in Python, will open another issue for that. Edit: |
I think that makes sense. If some problem with this approach becomes evident during implementation, we could always change this. |
Consider the following python snippet:
If I select the whole first line of the body of the function and call
:Refactor extract
, I get:However if I just select
a + b
, ie this where|
indicates the visual selection:I instead get:
which is not useful. I would expect this to give:
The text was updated successfully, but these errors were encountered: