DavidFeldhoff / al-codeactions

MIT License
17 stars 8 forks source link

[Request] CodeAction - Replace with Label #149

Closed JeremyVyska closed 1 year ago

JeremyVyska commented 1 year ago

I'm curious if this would be possible.

I love the "Extract to Label" (and locked) variant. Often, I have older code that uses the same hardcoded text over and over. Extract to Label works on the first, but then I have lingering instances.

Would it be possible to have a CodeAction on a String Literal that you can "Replace with Label" that looks to see if there's a label with the same value (local or global), and if so, swaps the Literal? Would be great if it could do a whole file.

Example nonsense (bad) pre-code:

codeunit 50000 "Cheese Press and Shred"
{
    procedure SquishTheCheeses(var CheeseBoard: Record "PTE Cheese Board") : Text
    begin
        if CheeseBoard.Get('CHEDDAR') then
            exit('Squeezed it, 18 months, nice!');
        if CheeseBoard.Get('MOZ') then
            exit('Squeezed, but should you have?');
    end;

    procedure ShredTheCheeses() : Text
    begin
        if CheeseBoard.Get('CHEDDAR') then
            exit('Time for tacos');
        if CheeseBoard.Get('MOZ') then
            exit('Time for pizza');
    end;
}

After using extract-to-label:

codeunit 50000 "Cheese Press and Shred"
{
    var
        CheddarTok: Label 'CHEDDAR';

    procedure SquishTheCheeses(var CheeseBoard: Record "PTE Cheese Board") : Text
    begin
        if CheeseBoard.Get(CheddarTok) then
            exit('Squeezed it, 18 months, nice!');
        if CheeseBoard.Get('MOZ') then
            exit('Squeezed, but should you have?');
    end;

    procedure ShredTheCheeses() : Text
    begin
        if CheeseBoard.Get('CHEDDAR') then
            exit('Time for tacos');
        if CheeseBoard.Get('MOZ') then
            exit('Time for pizza');
    end;
}

In example two, it'd be great to select the CHEDDAR in the ShredTheCheese function, do the CodeAction to find that CheddarTok matches, so swaps the literal.

Super fun would be a Command to run a pass on the whole file doing a complete find/swap.

DavidFeldhoff commented 1 year ago

Hey Jeremy :) I understood. But I'm not sure about the best possible approach to solve it. Furthermore I have a few more things on my list that I would prioritize higher as for this one I think we have good workarounds, like using Ctrl+D or Ctrl+Shift+L or Ctrl+D and sometimes Ctrl+K Ctrl+D if you want to skip instances. Or if there are many changes you could simply do a regex search replace with (?!Label )'CHEDDAR' and CheddarTok.

Nevertheless I'll look into it, but please be patient if I need some time :)

DavidFeldhoff commented 1 year ago

Two additions:

  1. Both solutions provided by you are definitely possible.
  2. I take the tacos :)
DavidFeldhoff commented 1 year ago

Hi @JeremyVyska , took some time, but now I've implemented it. It's now like this: If there are further occurences of the same string literal then you're asked if they should be replaced as well directly. Feel free to try it out in v1.0.25.