Refactorings to extract a selected expression into a local function, a module function or a private member - #20538
Open
xperiandri wants to merge 9 commits into
Open
xperiandri wants to merge 9 commits into
xperiandri wants to merge 9 commits into
Conversation
3 tasks
Contributor
❗ Release notes requiredYou can open this PR in browser to add release notes: open in github.dev
|
…g or a literal The selected expression is bound to a new name in front of the statement that uses it: above a statement that starts its line, or, when the body of a binding, match clause, branch or lambda shares the line with its keyword, on new lines under that keyword. A constant inside a module-level declaration can instead become a [<Literal>] in front of that declaration. The shared selection and placement logic lives in Refactor/RefactoringHelpers.fs, and the refactoring test framework gains helpers that run a provider on a selected span. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…tion, a module function or a private member The values the selection reads from the enclosing member or module-level declaration become parameters in the order they are first used; module values, members and 'this' stay where they are. A member that uses 'this' or 'base' can extract into a private member called on its self identifier. Parameter type annotations follow a new Code Fixes option: always, only for receivers of member and indexer accesses and operator operands, or never. A selection that assigns to a captured mutable local, or captures a byref-like value, is not offered. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
C# offers Introduce Constant without a selection. With a caret alone only the literal is offered; a let binding still needs a selection. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
A caret between 'fun' and the end of '->' acts as a selection of the parenthesized lambda; in the body a caret offers nothing new. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…ader Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
xperiandri
force-pushed
the
feature/extract-function-refactoring
branch
from
September 14, 2026 14:36
9689259 to
dc118df
Compare
…e of its section Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
xperiandri
marked this pull request as ready for review
September 14, 2026 19:40
Contributor
|
🔍 Tooling Safety Check — Affects-Design-Time
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Stacked on #20537: this branch contains its commits; the extract-function commits on top of them belong to this PR.
Adds three refactorings (
Ctrl+.on a selection) that extract the selected expression into a local function, a module-level function, or a private member — building on the selection/placement logic from #20537. As with Extract to let binding, a caret alone inside thefun <params> ->header of a parenthesized lambda extracts the whole lambda.Values the selection reads from its enclosing function, member or lambda (but doesn't itself define) become parameters, in the order they're first used; module-level values, other members and
this/basestay where they are and aren't parameterized. A member with a named self identifier (member this.M, notmember _.M) additionally gets "Extract to private member", called on that identifier; a selection usingbaseoffers only that variant (a module or local function can't reachbase). Parameter types default to fully annotated (FullType.FormatWithConstraints), since the extracted binding is its own generalization point and would otherwise default surprising types (e.g.intfromx * ybefore the call site is seen) or fail outright on a plain member access; a new Parameter types in Extract to function option under Tools ▸ Options ▸ F# ▸ Code Fixes lets that be relaxed to "only where inference needs it" or "never". The refactoring isn't offered at all when the selection assigns to a capturedmutable(the new parameter would only get a copy) or reads abyref/byref-like value from outside the selection.Covered cases
Offered
printfn "%d" (w * h + 1)→let extractedFunction (w: int) (h: int) = w * h + 1+printfn "%d" (extractedFunction w h)let private extractedFunction (w: int) (h: int) = w * h + 1abovelet area …(w * h + 1)with its parentheses gives the same result, and the call stays parenthesized1 + 2→let extractedFunction () = 1 + 2+extractedFunction ()thisbecomes a private member after the enclosing member:l * this.RateinList.sumBy (fun l -> l * this.Rate)→this.ExtractedMethod(l)+member private this.ExtractedMethod(l: int) = l * this.RateAlways→let extractedFunction (s: string) (n: int) =,WhenNeeded→(s: string) n(only the receiver ofs.ToUpper()is annotated),Never→s nw * h + 1) offers "Extract to local function" and "Extract to module function"; a selection usingbase(base.ToString() + "!") offers only "Extract to private member"x ->in(fun x -> x + n)) offers the same actions, with the same results, as selecting the whole(fun x -> x + n)Not offered
x + nin(fun x -> x + n)for i in 1 .. 3 do n <- n + ix + 1withx: byref<int>Relates to #14449 (extract method).
Checklist
🤖 Generated with Claude Code