Extract snippet from file `refactor.extract` CodeAction
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:
- Find all non-global variables in Y
- Create a file
snippets/Z.liquidwith the contents of Y - 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
- https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#textDocument_codeAction
Partially depends on #412 .
Having to manually rename newFile is kind of gross.
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
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 @benjaminsehl, yes we def could add a different refactor.extract code action for this.