Skip to content

Commit

Permalink
Add ChangeRefCellDerefToNotExpression code fixer (dotnet#10469)
Browse files Browse the repository at this point in the history
* Add ChangeRefCellDerefToNotExpression code fixer

* Area
  • Loading branch information
cartermp committed Nov 17, 2020
1 parent 063eb98 commit 7f7964c
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 1 deletion.
52 changes: 52 additions & 0 deletions CodeFix/ChangeRefCellDerefToNotExpression.fs
@@ -0,0 +1,52 @@
// 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 = "ChangeRefCellDerefToNotExpression"); Shared>]
type internal FSharpChangeRefCellDerefToNotExpressionCodeFixProvider
[<ImportingConstructor>]
(
checkerProvider: FSharpCheckerProvider,
projectInfoManager: FSharpProjectOptionsManager
) =
inherit CodeFixProvider()

static let userOpName = "FSharpChangeRefCellDerefToNotExpressionCodeFix"
let fixableDiagnosticIds = set ["FS0001"]

override __.FixableDiagnosticIds = Seq.toImmutableArray fixableDiagnosticIds

override this.RegisterCodeFixesAsync context : Task =
asyncMaybe {
let document = context.Document
let! parsingOptions, _ = projectInfoManager.TryGetOptionsForEditingDocumentOrProject(document, context.CancellationToken, userOpName)
let! sourceText = context.Document.GetTextAsync(context.CancellationToken)
let! parseResults = checkerProvider.Checker.ParseFile(document.FilePath, sourceText.ToFSharpSourceText(), parsingOptions, userOpName) |> liftAsync

let errorRange = RoslynHelpers.TextSpanToFSharpRange(document.FilePath, context.Span, sourceText)
let! derefRange = parseResults.TryRangeOfRefCellDereferenceContainingPos errorRange.Start
let! derefSpan = RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, derefRange)

let title = SR.UseNotForNegation()

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(derefSpan, "not ") |]))

context.RegisterCodeFix(codeFix, diagnostics)
}
|> Async.Ignore
|> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)
1 change: 1 addition & 0 deletions FSharp.Editor.fsproj
Expand Up @@ -89,6 +89,7 @@
<Compile Include="Commands\FsiCommandService.fs" />
<Compile Include="Commands\XmlDocCommandService.fs" />
<Compile Include="CodeFix\CodeFixHelpers.fs" />
<Compile Include="CodeFix\ChangeRefCellDerefToNotExpression.fs" />
<Compile Include="CodeFix\WrapExpressionInParentheses.fs" />
<Compile Include="CodeFix\ChangePrefixNegationToInfixSubtraction.fs" />
<Compile Include="CodeFix\AddNewKeywordToDisposableConstructorInvocation.fs" />
Expand Down
5 changes: 4 additions & 1 deletion FSharp.Editor.resx
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Expand Down Expand Up @@ -219,6 +219,9 @@
<data name="FSharpDisposableTopLevelValuesClassificationType" xml:space="preserve">
<value>F# Dispostable Values (top-level)</value>
</data>
<data name="UseNotForNegation" xml:space="preserve">
<value>Use 'not' to negate expression</value>
</data>
<data name="WrapExpressionInParentheses" xml:space="preserve">
<value>Wrap expression in parentheses</value>
</data>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.cs.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Formátování</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.de.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Formatierung</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.es.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Formato</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.fr.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Mise en forme</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.it.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Formattazione</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.ja.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">書式設定</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.ko.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">서식</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.pl.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Formatowanie</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.pt-BR.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Formatação</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.ru.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Форматирование</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.tr.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Biçimlendirme</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.zh-Hans.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">正在格式化</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.zh-Hant.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">格式化</target>
<note />
</trans-unit>
<trans-unit id="UseNotForNegation">
<source>Use 'not' to negate expression</source>
<target state="new">Use 'not' to negate expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
Expand Down

0 comments on commit 7f7964c

Please sign in to comment.