Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/.VisualStudio/18.vNext.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Suggest active-pattern cases from unopened modules and offer valid Add Open and qualification edits. ([PR #20719](https://github.com/dotnet/fsharp/pull/20719))
* Add maintainer-requested F# Apex Visual Studio integration tests on DartLab-1ES CloudTest, using PR pipeline definitions pinned to an explicitly requested commit SHA. ([PR #20625](https://github.com/dotnet/fsharp/pull/20625))
* **Extract to let binding** and **Extract to literal** refactorings for a selected expression. The value is bound in front of the statement that uses it, so it is computed before the code that preceded the selection in that statement; a constant in a module-level declaration becomes a `[<Literal>]` in front of that declaration, also with just the caret inside the constant. ([PR #20537](https://github.com/dotnet/fsharp/pull/20537))
* Code-fixes for FS3888 (compiler-semantic attribute on the `.fs` but not the `.fsi`): copy the attribute into the `.fsi`, or remove it from the `.fs`. ([Issue #19560](https://github.com/dotnet/fsharp/issues/19560), [PR #19880](https://github.com/dotnet/fsharp/pull/19880))
* Expand `<inheritdoc/>` in IDE tooltips, completion, and signature help, inheriting XML documentation from base classes, interfaces, overridden members, and constructors. ([Issue #19175](https://github.com/dotnet/fsharp/issues/19175), [PR #19188](https://github.com/dotnet/fsharp/pull/19188))
* Move F# editor and Interactive options to 43 native Unified Settings with independent roaming and a one-time import of existing settings. Preserve the legacy groupings and Interactive setting order, with restart requirements displayed by Unified Settings. Quick Info's preferred description width is a checkbox with a dependent integer control. Remove the eight legacy options pages; the user-wide compiler setting remains external. Typing and deletion completion triggers are independent. ([PR #20877](https://github.com/dotnet/fsharp/pull/20877))
Expand Down
2 changes: 2 additions & 0 deletions vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@
<Compile Include="Commands\HelpContextService.fs" />
<Compile Include="Commands\FsiCommandService.fs" />
<Compile Include="Commands\XmlDocCommandService.fs" />
<Compile Include="Refactor\RefactoringHelpers.fs" />
<Compile Include="Refactor\AddReturnType.fs" />
<Compile Include="Refactor\ChangeTypeofWithNameToNameofExpression.fs" />
<Compile Include="Refactor\AddExplicitTypeToParameter.fs" />
<Compile Include="Refactor\ChangeDerefToValueRefactoring.fs" />
<Compile Include="Refactor\ExtractLetBinding.fs" />
<Compile Include="CodeFixes\IFSharpCodeFix.fs" />
<Compile Include="CodeFixes\CodeFixHelpers.fs" />
<Compile Include="CodeFixes\ChangeEqualsInFieldTypeToColon.fs" />
Expand Down
6 changes: 6 additions & 0 deletions vsintegration/src/FSharp.Editor/FSharp.Editor.resx
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@
{2}</value>
<comment>Tooltip of an F# declaration in the Copilot Chat "#" picker, as Copilot shows a C# one. {0} is the kind of declaration (Class, Method, Field...), {1} the file name, {2} the declaration's name.</comment>
</data>
<data name="ExtractToLetBinding" xml:space="preserve">
<value>Extract to let binding</value>
</data>
<data name="ExtractToLiteral" xml:space="preserve">
<value>Extract to literal</value>
</data>
<data name="UnifiedSettingsIntelliSenseTitle" xml:space="preserve">
<value>IntelliSense</value>
</data>
Expand Down
73 changes: 73 additions & 0 deletions vsintegration/src/FSharp.Editor/Refactor/ExtractLetBinding.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

namespace Microsoft.VisualStudio.FSharp.Editor

open System.Composition

open Microsoft.CodeAnalysis.CodeRefactorings

open FSharp.Compiler.Syntax

open RefactoringHelpers
open CancellableTasks

[<ExportCodeRefactoringProvider(FSharpConstants.FSharpLanguageName, Name = "ExtractLetBinding"); Shared>]
type internal FSharpExtractLetBindingRefactoring [<ImportingConstructor>] () =
inherit CodeRefactoringProvider()

static let letBindingChanges (extraction: Extraction) =
match anchorsOf extraction.Target.Expr extraction.Target.Path with
| [] -> ValueNone
| anchor :: _ ->
let name = uniqueName "extracted" extraction.Names

let header =
match tryFunctionTypeText extraction.CheckResults extraction.Target with
| ValueSome functionType -> $"let {name}: {functionType}"
| ValueNone -> $"let {name}"

tryDeclareInFront extraction anchor header name

static let literalChanges (extraction: Extraction) =
let constant =
match extraction.Target.Expr with
| SynExpr.Paren(expr = inner) -> inner
| expr -> expr

if isLiteralConstant constant then
let name = uniqueName "ExtractedConstant" extraction.Names

let attribute =
literalAttributeAt extraction.CheckResults extraction.Target.Expr.Range.Start

tryDeclareInFrontOfModuleLet extraction [ $"[<{attribute}>]" ] $"let {name}" name
else
ValueNone

static let offer (isSelected: bool) (extraction: Extraction) =
if isSelected then
registerRefactoring extraction (SR.ExtractToLetBinding()) "let" (letBindingChanges extraction)

registerRefactoring extraction (SR.ExtractToLiteral()) "literal" (literalChanges extraction)

override _.ComputeRefactoringsAsync context =
cancellableTask {
let document = context.Document

if not document.IsFSharpSignatureFile then
let! sourceText = document.GetTextAsync context.CancellationToken
let! parseResults = document.GetFSharpParseResultsAsync(nameof FSharpExtractLetBindingRefactoring)
let parseTree = parseResults.ParseTree
let selected = trySelectedTarget sourceText parseTree context.Span

let target =
selected
|> ValueOption.orElseWith (fun () -> tryConstantAtCaret sourceText parseTree context.Span)

match target with
| ValueNone -> ()
| ValueSome target ->
let! extraction = prepareExtraction context (nameof FSharpExtractLetBindingRefactoring) sourceText parseTree target
offer selected.IsSome extraction
}
|> CancellableTask.startAsTask context.CancellationToken
Loading
Loading