p42ai / js-assistant

120+ refactorings and code-assists for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=p42ai.refactor
MIT License
119 stars 7 forks source link

Idea: Support Renaming Unresolved Variables #25

Closed hediet closed 2 years ago

hediet commented 2 years ago

After copy&pasting, I ended up with this code:

public extendInputRange(extendedInputRange: LineRange): LineRangeMapping {

    const startDelta = extendedInputRange.startLineNumber - l.inputRange.startLineNumber;
    const endDelta = extendedInputRange.endLineNumberExclusive - l.inputRange.endLineNumberExclusive;
    return new LineRangeMapping(
        inputRange,
        new LineRange(
            l.outputRange.startLineNumber + startDelta,
            l.outputRange.lineCount - startDelta + endDelta
        )
    );
}

Now I would like to replace l with this. This is not very easy to do.

It would be awesome if I could rename l to this.

lgrammel commented 2 years ago

Thanks for the suggestion! I'll think about how to best resolve this with a single refactoring.

It's already possible to achieve the renaming with a refactoring combo:

  1. extract l
  2. change it's value
  3. inline l

example-20220612

lgrammel commented 2 years ago

I thought more about this and evaluated various ways of achieving this. It turns out you can easily achieve this by using multiple cursors:

  1. select "l"
  2. start multiple cursors with ⇧⌘L
  3. change to this
  4. end multiple cursors with Esc

Since there is a pretty elegant built-in solution, I'm wondering how P42 could improve on this.

hediet commented 2 years ago

This only works if l is not part of some other word.

lgrammel commented 2 years ago

Ah, right. One idea I have is a replace expression code assist where you could select an expression, invoke the action, and then all it's occurrences would be replaced with a text that you'd enter in a text box. Would that be closer to what you imagine?

hediet commented 2 years ago

That would be cool!

lgrammel commented 2 years ago

I've added a "Select expression occurrences" code action in v1.117.0. When you select an expression (selection boundaries must match expression), you can invoke the P42 action menu and start a multi-cursor selection on all occurrences:

action-select-expression-occurrences

This is different from the VS Code standard multi-cursor, because it checks that the expression occurrences use the same variables, this context, etc.