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

Code action: Extract module to file #983

Merged
merged 4 commits into from
May 25, 2024
Merged

Code action: Extract module to file #983

merged 4 commits into from
May 25, 2024

Conversation

zth
Copy link
Collaborator

@zth zth commented May 25, 2024

Adds a code action to extract a module to a separate file.

  1. Put your cursor on/inside of a defined module.
  2. Choose "Extract module as file"
  3. The locally defined module is now removed from the current file, and extracted into a separate ".res" next to the current file.

@zth zth requested a review from cristianoc May 25, 2024 09:21
Printf.printf "%s\nnewText:\n%s<--here\n%s%s\n"
(Protocol.stringifyRange range)
indent indent newText)))
|> List.iter (fun dc ->
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section just improves the test output a bit now that we have several document change actions.

Comment on lines +78 to +85
type createFileOptions = {overwrite: bool option; ignoreIfExists: bool option}
type createFile = {uri: string; options: createFileOptions option}

type documentChange =
| TextDocumentEdit of textDocumentEdit
| CreateFile of createFile

type codeActionEdit = {documentChanges: documentChange list}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

@zth zth May 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes in this file are how you wire up a new code action. Essentially, you create an AST iterator that's responsible for "reacting" to anything you want to add code actions for. You then build a code action, and extract the information you need.

There are a few helpers like printStandaloneStructure and printExpr etc that are designed to help you print modified code in a way that makes it fit in as good as possible formatting wise in the current file.

Also note that this code action doesn't need to look up any types or any project specific things. It only operates on the raw AST. That makes it cheap. But, there are code actions that need type information to work (exhaustive switch is one example), and that works perfectly well too.

@zth zth merged commit 34b4c23 into master May 25, 2024
6 checks passed
@zth zth deleted the extract-module-to-file branch May 25, 2024 10:09
jfrolich pushed a commit to jfrolich/rescript-vscode that referenced this pull request Sep 3, 2024
* code action for extracting a module to a new file

* make sure comments are retained

* fix broken serialization

* changelog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants