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 and nosami committed Feb 22, 2021
1 parent 961a8c7 commit 52ea267
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 0 deletions.
@@ -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 vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj
Expand Up @@ -184,6 +184,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 vsintegration/src/FSharp.Editor/FSharp.Editor.resx
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 vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf
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 vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf
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 vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf
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 vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf
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 vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf
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 vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf
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 vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf
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 vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf
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 vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf
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 vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf
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 vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf
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 vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf
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 vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf
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 52ea267

Please sign in to comment.