You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Robust refactoring is on the roadmap as a long term/difficult task. Is it time to introduce extract variable and extract function as refactoring options? I think the new XML parsing approach could help here, for example in identifying the appropriate location to insert the variable definition when extracting a variable.
VSCode refactoring overview
VSCode-specific, but gives some indication of how the refactoring Code Actions are expected to be used.
@randy3k That looks very relevant. The codetools package also has some functions that could be useful (findGlobals, findLocals etc.), if it's okay to add that as a dependency.
I'm happy to work on this, although also very happy to leave it to someone else if anyone is keen.
I have been thinking about how to handle scopes when extracting variables. I had a look at how RStudio does it but I would like to use a slightly different approach to ensure that
The scopes of the original statements do not change, and
The extracted variable is placed in the same scope as the line it was extracted from.
I will proceed with the proposed versions below unless anyone has objections.
Identifying scopes of control structures in R is a little bit more complicated than I had assumed it would be. It seems that the R syntax tree does not consider the body of an if (for example) to be a child of the if. In this example, the body (id 8) and the if (id 1) both have parent 11:
So, checking whether the selected text for 'extract variable' is in an if etc. will probably involve using xml2::xml_siblings rather than just xml2::xml_parents.
A few test cases to include:
# Case 1 (select '1', in body of if on same line)
if(TRUE) 1
# Case 2 (select '1', in body of if on different line)
if(TRUE)
1
# Case 3 (select '1', in body of if on same line, within another expression)
x <- if(TRUE) 1
# Case 4 (select '1', within expression in body of if on same line)
x <- if(TRUE) 1 + 2
I'd still like to get this working at some point but it's taking a long time to get the top of my priority list. If there's anyone out there who wants this feature more urgently in the meantime they should feel free to take over.
Thanks! A very simplistic "Extract variable" would solve ~95% of my use cases, even if context is not taken into account. (It's mostly about replacing "Ctrl + X" + type + move + type + "Ctrl + V" by a shortcut.) If the extracted code is selected after the operation, it's trivial to move it elsewhere.
Activity
randy3k commentedon Oct 20, 2019
It is handled by the codeAction provider. We could also use this code from RStudio.
andycraig commentedon Oct 20, 2019
@randy3k That looks very relevant. The codetools package also has some functions that could be useful (
findGlobals,findLocalsetc.), if it's okay to add that as a dependency.I'm happy to work on this, although also very happy to leave it to someone else if anyone is keen.
randy3k commentedon Oct 20, 2019
I don't think I would have the time and energy to look into it. I'd pass it for this time.
andycraig commentedon Oct 20, 2019
@randy3k No worries :) I’ll have a go at it.
andycraig commentedon Oct 27, 2019
I have been thinking about how to handle scopes when extracting variables. I had a look at how RStudio does it but I would like to use a slightly different approach to ensure that
I will proceed with the proposed versions below unless anyone has objections.
Example 1: Control structures without {}
Original code:
Select 'x + 1', extract as 'y'.
RStudio result:
Proposed result:
Example 2: One-line {}
Original code:
Select '1', extract as 'y'.
RStudio result:
Proposed result:
randy3k commentedon Oct 27, 2019
I agree ☝️
andycraig commentedon Feb 24, 2020
Identifying scopes of control structures in R is a little bit more complicated than I had assumed it would be. It seems that the R syntax tree does not consider the body of an
if(for example) to be a child of theif. In this example, the body (id 8) and theif(id 1) both have parent 11:So, checking whether the selected text for 'extract variable' is in an
ifetc. will probably involve usingxml2::xml_siblingsrather than justxml2::xml_parents.A few test cases to include:
andycraig commentedon Sep 13, 2020
I'd still like to get this working at some point but it's taking a long time to get the top of my priority list. If there's anyone out there who wants this feature more urgently in the meantime they should feel free to take over.
[-]Extract variable/function[/-][+]Refactoring: Extract variable/function, Inline variable[/+]krlmlr commentedon Mar 8, 2021
We already have "Rename variable", is this provided explicitly by this extension, or is this some implicit magic?
renkun-ken commentedon Mar 8, 2021
@krlmlr #339 implements the rename provider.
krlmlr commentedon Mar 8, 2021
Thanks! A very simplistic "Extract variable" would solve ~95% of my use cases, even if context is not taken into account. (It's mostly about replacing "Ctrl + X" + type + move + type + "Ctrl + V" by a shortcut.) If the extracted code is selected after the operation, it's trivial to move it elsewhere.
This is a bit above me right now, though.
albert-ying commentedon Aug 15, 2021
Is there any update on this? It would be much easier to do functional programming if there is an easy way to extract code trunk as a function.
andycraig commentedon Aug 15, 2021
Hi @albert-ying, no update. I haven't been using R a lot recently so I am unlikely to get to this any time soon.