Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract snippet from file refactor.extract CodeAction #433

Open
charlespwd opened this issue Sep 28, 2021 · 4 comments
Open

Extract snippet from file refactor.extract CodeAction #433

charlespwd opened this issue Sep 28, 2021 · 4 comments
Assignees
Labels
area:theme-check enhancement New feature or request

Comments

@charlespwd
Copy link
Contributor

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
@lukeh-shopify lukeh-shopify transferred this issue from Shopify/theme-check Jul 19, 2024
@charlespwd
Copy link
Contributor Author

Partially depends on #412 .

Having to manually rename newFile is kind of gross.

@charlespwd charlespwd self-assigned this Oct 28, 2024
@charlespwd
Copy link
Contributor Author

charlespwd commented Oct 30, 2024

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
Loading

Therefore here are the things I need to build:

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

@aswamy
Copy link
Contributor

aswamy commented Dec 9, 2024

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

@charlespwd
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:theme-check enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants