Skip to content

Add a new command to extract expression into a fresh let binding #1948

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Tim-ats-d
Copy link
Contributor

@Tim-ats-d Tim-ats-d commented Jul 2, 2025

This PR adds a new command refactoring-extract-region to merlin protocol which allows extracting an expression into a let binding. See the interface comments for supported cases

Implementation

The implementation of the substitution is a bit hacky and relies on string manipulation, especially when the generated binding is a and. I couldn't deal with typedtree locations for the generated code, so I had to make a few manual location calculations that I tried to make as explicit as possible. Please tell me if there's a better way to achieve it.

It still lacks a bit of testing, I'll be adding more in the next few days.

Misc

I'have also added a fold function in Std.Option for the sake of convenience

 let fold ~none ~some = function
    | Some v -> some v
    | None -> none

cc @xvw @voodoos

@Tim-ats-d Tim-ats-d changed the title Add a new command to extract expression in a fresh let binding Add a new command to extract expression into a fresh let binding Jul 2, 2025
@Tim-ats-d
Copy link
Contributor Author

Tim-ats-d commented Jul 2, 2025

Just realized that I should probably add the corresponding documentation in the /doc/dev/PROTOCOL.md file

@Tim-ats-d Tim-ats-d force-pushed the refactor-extraction branch from e1e25d9 to a7a3729 Compare July 16, 2025 10:27
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.

3 participants