theme-tools icon indicating copy to clipboard operation
theme-tools copied to clipboard

Extract snippet from file `refactor.extract` CodeAction

Open charlespwd opened this issue 4 years ago • 4 comments

The LSP defines some CodeActionKinds that have special meanings in some Language Clients. One of those is the RefactorExtract action kind.

We could probably offer a RefactorExtract action when you highlight a large range of text inside a section|template|snippet.

When executed, we could do something like this:

  • For range Y in input file X:
    1. Find all non-global variables in Y
    2. Create a file snippets/Z.liquid with the contents of Y
    3. Replace Y in X by the following:
      {%
        render '#{Z}',
        #{all_the_non_global_variables_as_arguments}
      %}
    

To be answered:

  • Can we prompt for a filename during that process?

Resources

  1. https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#textDocument_codeAction

charlespwd avatar Sep 28 '21 13:09 charlespwd

Partially depends on #412 .

Having to manually rename newFile is kind of gross.

charlespwd avatar Oct 08 '24 14:10 charlespwd

Sketching out the flow

sequenceDiagram
    actor A as User
    participant C as Client
    participant S as Server
    A-->>C: Select code and right click
    C->>+S: "textDocument/codeAction" request,<br>context.triggerKind: "invoked"
    S->>-C: CodeAction[]<br>kind: "refactor.extract"<br>command: "shopifyLiquid/extractSnippet"<br>args: uri, range
    C-->>A: Show options
    A-->>C: Select "Extract to new file"
    C->>S: "workspace/executeCommand" request<br>command: "shopify/extractSnippet"<br>args: uri, range
    activate S
    S->>S: Compute WorkspaceEdit for refactor
    S->>C: "workspace/applyEdit" request
    activate C
    C->>S: applied!
    deactivate C
    S->>C: done!
    deactivate S

Therefore here are the things I need to build:

  • A CodeAction provider of kind "refactor.extract"
  • A ExecuteCommand provider for "shopify/extractSnippet" commands

charlespwd avatar Oct 30 '24 13:10 charlespwd

would “Select to refactor into a new file” be possible to take a “Section block” and move it to a /blocks/_blockName.liquid file, and then update the schema for that section to now accept that block?

-Ben

aswamy avatar Dec 09 '24 07:12 aswamy

@aswamy @benjaminsehl, yes we def could add a different refactor.extract code action for this.

charlespwd avatar Jan 06 '25 14:01 charlespwd