Skip to content

Commit

Permalink
Add UseMutationWhenValueIsMutable code fix (dotnet#10488)
Browse files Browse the repository at this point in the history
  • Loading branch information
cartermp authored Nov 18, 2020
1 parent a531932 commit 2573998
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 0 deletions.
73 changes: 73 additions & 0 deletions CodeFix/UseMutationWhenValueIsMutable.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 System.Threading
open System.Threading.Tasks

open Microsoft.CodeAnalysis.Text
open Microsoft.CodeAnalysis.CodeFixes

open FSharp.Compiler.SourceCodeServices
open FSharp.Compiler.Range

[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = "UseMutationWhenValueIsMutable"); Shared>]
type internal FSharpUseMutationWhenValueIsMutableFixProvider
[<ImportingConstructor>]
(
checkerProvider: FSharpCheckerProvider,
projectInfoManager: FSharpProjectOptionsManager
) =
inherit CodeFixProvider()

static let userOpName = "UseMutationWhenValueIsMutable"

let fixableDiagnosticIds = set ["FS0020"]

override _.FixableDiagnosticIds = Seq.toImmutableArray fixableDiagnosticIds

override _.RegisterCodeFixesAsync context : Task =
asyncMaybe {
let diagnostics =
context.Diagnostics
|> Seq.filter (fun x -> fixableDiagnosticIds |> Set.contains x.Id)
|> Seq.toImmutableArray

let document = context.Document
do! Option.guard (not(isSignatureFile document.FilePath))
let position = context.Span.Start
let checker = checkerProvider.Checker
let! parsingOptions, projectOptions = projectInfoManager.TryGetOptionsForEditingDocumentOrProject(document, CancellationToken.None, userOpName)
let! sourceText = document.GetTextAsync () |> liftTaskAsync
let defines = CompilerEnvironment.GetCompilationDefinesForEditing parsingOptions
let textLine = sourceText.Lines.GetLineFromPosition position
let textLinePos = sourceText.Lines.GetLinePosition position
let fcsTextLineNumber = Line.fromZ textLinePos.Line
let! _, _, checkFileResults = checker.ParseAndCheckDocument (document, projectOptions, sourceText=sourceText, userOpName=userOpName)
let! lexerSymbol = Tokenizer.getSymbolAtPosition (document.Id, sourceText, position, document.FilePath, defines, SymbolLookupKind.Greedy, false, false)
let! symbolUse = checkFileResults.GetSymbolUseAtLocation(fcsTextLineNumber, lexerSymbol.Ident.idRange.EndColumn, textLine.ToString(), lexerSymbol.FullIsland)

match symbolUse.Symbol with
| :? FSharpMemberOrFunctionOrValue as mfv when mfv.IsValue && mfv.IsMutable ->
let title = SR.UseMutationWhenValueIsMutable()
let! symbolSpan = RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, symbolUse.RangeAlternate)
let mutable pos = symbolSpan.End
let mutable ch = sourceText.GetSubText(pos).ToString()

// We're looking for the possibly erroneous '='
while pos <= context.Span.Length && ch <> "=" do
pos <- pos + 1
ch <- sourceText.GetSubText(pos).ToString()

let codeFix =
CodeFixHelpers.createTextChangeCodeFix(
title,
context,
(fun () -> asyncMaybe.Return [| TextChange(TextSpan(pos + 1, 1), "<-") |]))

context.RegisterCodeFix(codeFix, diagnostics)
| _ -> ()
}
|> Async.Ignore
|> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)
1 change: 1 addition & 0 deletions FSharp.Editor.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Compile Include="Commands\FsiCommandService.fs" />
<Compile Include="Commands\XmlDocCommandService.fs" />
<Compile Include="CodeFix\CodeFixHelpers.fs" />
<Compile Include="CodeFix\UseMutationWhenValueIsMutable.fs" />
<Compile Include="CodeFix\MakeDeclarationMutable.fs" />
<Compile Include="CodeFix\ChangeToUpcast.fs" />
<Compile Include="CodeFix\AddMissingEqualsToTypeDefinition.fs" />
Expand Down
3 changes: 3 additions & 0 deletions FSharp.Editor.resx
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,7 @@
<data name="ChangePrefixNegationToInfixSubtraction" xml:space="preserve">
<value>Use subtraction instead of negation</value>
</data>
<data name="UseMutationWhenValueIsMutable" xml:space="preserve">
<value>Use '&lt;-' to mutate value</value>
</data>
</root>
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Formátování</target>
<note />
</trans-unit>
<trans-unit id="UseMutationWhenValueIsMutable">
<source>Use '&lt;-' to mutate value</source>
<target state="new">Use '&lt;-' to mutate value</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Formatierung</target>
<note />
</trans-unit>
<trans-unit id="UseMutationWhenValueIsMutable">
<source>Use '&lt;-' to mutate value</source>
<target state="new">Use '&lt;-' to mutate value</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Formato</target>
<note />
</trans-unit>
<trans-unit id="UseMutationWhenValueIsMutable">
<source>Use '&lt;-' to mutate value</source>
<target state="new">Use '&lt;-' to mutate value</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Mise en forme</target>
<note />
</trans-unit>
<trans-unit id="UseMutationWhenValueIsMutable">
<source>Use '&lt;-' to mutate value</source>
<target state="new">Use '&lt;-' to mutate value</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Formattazione</target>
<note />
</trans-unit>
<trans-unit id="UseMutationWhenValueIsMutable">
<source>Use '&lt;-' to mutate value</source>
<target state="new">Use '&lt;-' to mutate value</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">書式設定</target>
<note />
</trans-unit>
<trans-unit id="UseMutationWhenValueIsMutable">
<source>Use '&lt;-' to mutate value</source>
<target state="new">Use '&lt;-' to mutate value</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">서식</target>
<note />
</trans-unit>
<trans-unit id="UseMutationWhenValueIsMutable">
<source>Use '&lt;-' to mutate value</source>
<target state="new">Use '&lt;-' to mutate value</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Formatowanie</target>
<note />
</trans-unit>
<trans-unit id="UseMutationWhenValueIsMutable">
<source>Use '&lt;-' to mutate value</source>
<target state="new">Use '&lt;-' to mutate value</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Formatação</target>
<note />
</trans-unit>
<trans-unit id="UseMutationWhenValueIsMutable">
<source>Use '&lt;-' to mutate value</source>
<target state="new">Use '&lt;-' to mutate value</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Форматирование</target>
<note />
</trans-unit>
<trans-unit id="UseMutationWhenValueIsMutable">
<source>Use '&lt;-' to mutate value</source>
<target state="new">Use '&lt;-' to mutate value</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Biçimlendirme</target>
<note />
</trans-unit>
<trans-unit id="UseMutationWhenValueIsMutable">
<source>Use '&lt;-' to mutate value</source>
<target state="new">Use '&lt;-' to mutate value</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">正在格式化</target>
<note />
</trans-unit>
<trans-unit id="UseMutationWhenValueIsMutable">
<source>Use '&lt;-' to mutate value</source>
<target state="new">Use '&lt;-' to mutate value</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">格式化</target>
<note />
</trans-unit>
<trans-unit id="UseMutationWhenValueIsMutable">
<source>Use '&lt;-' to mutate value</source>
<target state="new">Use '&lt;-' to mutate value</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
Expand Down

0 comments on commit 2573998

Please sign in to comment.