Skip to content

Commit

Permalink
Add ChangeToUpcast code fix (dotnet#10463)
Browse files Browse the repository at this point in the history
  • Loading branch information
cartermp authored and nosami committed Feb 22, 2021
1 parent 108e304 commit 49e1b3a
Show file tree
Hide file tree
Showing 16 changed files with 192 additions and 0 deletions.
55 changes: 55 additions & 0 deletions CodeFix/ChangeToUpcast.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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.Tasks

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

[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = "ChangeToUpcast"); Shared>]
type internal FSharpChangeToUpcastCodeFixProvider() =
inherit CodeFixProvider()

let fixableDiagnosticIds = set ["FS3198"]

override __.FixableDiagnosticIds = Seq.toImmutableArray fixableDiagnosticIds

override this.RegisterCodeFixesAsync context : Task =
asyncMaybe {
let! sourceText = context.Document.GetTextAsync(context.CancellationToken)
let text = sourceText.GetSubText(context.Span).ToString()

// Only works if it's one or the other
let isDowncastOperator = text.Contains(":?>")
let isDowncastKeyword = text.Contains("downcast")
do! Option.guard ((isDowncastOperator || isDowncastKeyword) && not (isDowncastOperator && isDowncastKeyword))

let replacement =
if isDowncastOperator then
text.Replace(":?>", ":>")
else
text.Replace("downcast", "upcast")

let title =
if isDowncastOperator then
SR.UseUpcastOperator()
else
SR.UseUpcastKeyword()

let diagnostics =
context.Diagnostics
|> Seq.filter (fun x -> fixableDiagnosticIds |> Set.contains x.Id)
|> Seq.toImmutableArray

let codeFix =
CodeFixHelpers.createTextChangeCodeFix(
title,
context,
(fun () -> asyncMaybe.Return [| TextChange(context.Span, replacement) |]))

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 @@ -184,6 +184,7 @@
<Compile Include="Commands\FsiCommandService.fs" />
<Compile Include="Commands\XmlDocCommandService.fs" />
<Compile Include="CodeFix\CodeFixHelpers.fs" />
<Compile Include="CodeFix\ChangeToUpcast.fs" />
<Compile Include="CodeFix\AddMissingEqualsToTypeDefinition.fs" />
<Compile Include="CodeFix\ConvertToSingleEqualsEqualityExpression.fs" />
<Compile Include="CodeFix\ChangeRefCellDerefToNotExpression.fs" />
Expand Down
6 changes: 6 additions & 0 deletions FSharp.Editor.resx
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@
<data name="FSharpDisposableTopLevelValuesClassificationType" xml:space="preserve">
<value>F# Dispostable Values (top-level)</value>
</data>
<data name="UseUpcastKeyword" xml:space="preserve">
<value>Use 'upcast'</value>
</data>
<data name="UseUpcastOperator" xml:space="preserve">
<value>Use ':&gt;' operator</value>
</data>
<data name="AddMissingEqualsToTypeDefinition" xml:space="preserve">
<value>Add missing '=' to type definition</value>
</data>
Expand Down
10 changes: 10 additions & 0 deletions xlf/FSharp.Editor.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@
<target state="translated">Formátování</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastOperator">
<source>Use ':&gt;' operator</source>
<target state="new">Use ':&gt;' operator</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
Expand Down
10 changes: 10 additions & 0 deletions xlf/FSharp.Editor.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@
<target state="translated">Formatierung</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastOperator">
<source>Use ':&gt;' operator</source>
<target state="new">Use ':&gt;' operator</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
Expand Down
10 changes: 10 additions & 0 deletions xlf/FSharp.Editor.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@
<target state="translated">Formato</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastOperator">
<source>Use ':&gt;' operator</source>
<target state="new">Use ':&gt;' operator</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
Expand Down
10 changes: 10 additions & 0 deletions xlf/FSharp.Editor.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@
<target state="translated">Mise en forme</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastOperator">
<source>Use ':&gt;' operator</source>
<target state="new">Use ':&gt;' operator</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
Expand Down
10 changes: 10 additions & 0 deletions xlf/FSharp.Editor.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@
<target state="translated">Formattazione</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastOperator">
<source>Use ':&gt;' operator</source>
<target state="new">Use ':&gt;' operator</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
Expand Down
10 changes: 10 additions & 0 deletions xlf/FSharp.Editor.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@
<target state="translated">書式設定</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastOperator">
<source>Use ':&gt;' operator</source>
<target state="new">Use ':&gt;' operator</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
Expand Down
10 changes: 10 additions & 0 deletions xlf/FSharp.Editor.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@
<target state="translated">서식</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastOperator">
<source>Use ':&gt;' operator</source>
<target state="new">Use ':&gt;' operator</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
Expand Down
10 changes: 10 additions & 0 deletions xlf/FSharp.Editor.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@
<target state="translated">Formatowanie</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastOperator">
<source>Use ':&gt;' operator</source>
<target state="new">Use ':&gt;' operator</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
Expand Down
10 changes: 10 additions & 0 deletions xlf/FSharp.Editor.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@
<target state="translated">Formatação</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastOperator">
<source>Use ':&gt;' operator</source>
<target state="new">Use ':&gt;' operator</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
Expand Down
10 changes: 10 additions & 0 deletions xlf/FSharp.Editor.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@
<target state="translated">Форматирование</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastOperator">
<source>Use ':&gt;' operator</source>
<target state="new">Use ':&gt;' operator</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
Expand Down
10 changes: 10 additions & 0 deletions xlf/FSharp.Editor.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@
<target state="translated">Biçimlendirme</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastOperator">
<source>Use ':&gt;' operator</source>
<target state="new">Use ':&gt;' operator</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
Expand Down
10 changes: 10 additions & 0 deletions xlf/FSharp.Editor.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@
<target state="translated">正在格式化</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastOperator">
<source>Use ':&gt;' operator</source>
<target state="new">Use ':&gt;' operator</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
Expand Down
10 changes: 10 additions & 0 deletions xlf/FSharp.Editor.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@
<target state="translated">格式化</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="new">Use 'upcast'</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastOperator">
<source>Use ':&gt;' operator</source>
<target state="new">Use ':&gt;' operator</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
Expand Down

0 comments on commit 49e1b3a

Please sign in to comment.