Skip to content

Commit

Permalink
Add refactoring to replace ! with .Value (#11227)
Browse files Browse the repository at this point in the history
  • Loading branch information
cartermp authored Mar 19, 2021
1 parent 04c27e3 commit db9805c
Show file tree
Hide file tree
Showing 19 changed files with 148 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/fsharp/service/FSharpParseFileResults.fs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,17 @@ type FSharpParseFileResults(diagnostics: FSharpDiagnostic[], input: ParsedInput,
None
| _ -> defaultTraverse expr })

member _.TryRangeOfExpressionBeingDereferencedContainingPos expressionPos =
SyntaxTraversal.Traverse(expressionPos, input, { new SyntaxVisitorBase<_>() with
member _.VisitExpr(_, _, defaultTraverse, expr) =
match expr with
| SynExpr.App(_, false, SynExpr.Ident funcIdent, expr, _) ->
if funcIdent.idText = "op_Dereference" && rangeContainsPos expr.Range expressionPos then
Some expr.Range
else
None
| _ -> defaultTraverse expr })

member _.FindParameterLocations pos =
ParameterLocations.Find(pos, input)

Expand Down
3 changes: 3 additions & 0 deletions src/fsharp/service/FSharpParseFileResults.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type public FSharpParseFileResults =
/// </summary>
member TryRangeOfRefCellDereferenceContainingPos: expressionPos: pos -> range option

/// Gets the range of an expression being dereferenced. For `!expr`, gives the range of `expr`
member TryRangeOfExpressionBeingDereferencedContainingPos: expressionPos: pos -> range option

/// Notable parse info for ParameterInfo at a given location
member FindParameterLocations: pos:pos -> ParameterLocations option

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,7 @@ FSharp.Compiler.CodeAnalysis.FSharpParseFileResults: Microsoft.FSharp.Core.FShar
FSharp.Compiler.CodeAnalysis.FSharpParseFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.Range] TryRangeOfNameOfNearestOuterBindingContainingPos(FSharp.Compiler.Text.Position)
FSharp.Compiler.CodeAnalysis.FSharpParseFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.Range] TryRangeOfRecordExpressionContainingPos(FSharp.Compiler.Text.Position)
FSharp.Compiler.CodeAnalysis.FSharpParseFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.Range] TryRangeOfRefCellDereferenceContainingPos(FSharp.Compiler.Text.Position)
FSharp.Compiler.CodeAnalysis.FSharpParseFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.Range] TryRangeOfExpressionBeingDereferencedContainingPos(FSharp.Compiler.Text.Position)
FSharp.Compiler.CodeAnalysis.FSharpParseFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.Range] ValidateBreakpointLocation(FSharp.Compiler.Text.Position)
FSharp.Compiler.CodeAnalysis.FSharpParseFileResults: Microsoft.FSharp.Core.FSharpOption`1[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Text.Range]] GetAllArgumentsForFunctionApplicationAtPostion(FSharp.Compiler.Text.Position)
FSharp.Compiler.CodeAnalysis.FSharpParseFileResults: Microsoft.FSharp.Core.FSharpOption`1[System.Tuple`2[FSharp.Compiler.Syntax.Ident,System.Int32]] TryIdentOfPipelineContainingPosAndNumArgsApplied(FSharp.Compiler.Text.Position)
Expand Down
1 change: 1 addition & 0 deletions vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<Compile Include="Commands\FsiCommandService.fs" />
<Compile Include="Commands\XmlDocCommandService.fs" />
<Compile Include="Refactor\AddExplicitTypeToParameter.fs" />
<Compile Include="Refactor\ChangeDerefToValueRefactoring.fs" />
<Compile Include="CodeFix\CodeFixHelpers.fs" />
<Compile Include="CodeFix\AddTypeAnnotationToObjectOfIndeterminateType.fs" />
<Compile Include="CodeFix\AddMissingRecToMutuallyRecFunctions.fs" />
Expand Down
3 changes: 3 additions & 0 deletions vsintegration/src/FSharp.Editor/FSharp.Editor.resx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@
<data name="ConvertToNotEqualsEqualityExpression" xml:space="preserve">
<value>Use '&lt;&gt;' for inequality check</value>
</data>
<data name="UseValueInsteadOfDeref" xml:space="preserve">
<value>Use '.Value' to dereference expression</value>
</data>
<data name="AddTypeAnnotation" xml:space="preserve">
<value>Add type annotation</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

namespace Microsoft.VisualStudio.FSharp.Editor

open System
open System.Composition
open System.Threading

open FSharp.Compiler
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.Symbols
open FSharp.Compiler.Text
open FSharp.Compiler.Syntax

open Microsoft.CodeAnalysis.Text
open Microsoft.CodeAnalysis.CodeRefactorings
open Microsoft.CodeAnalysis.CodeActions

[<ExportCodeRefactoringProvider(FSharpConstants.FSharpLanguageName, Name = "ChangeDerefToValue"); Shared>]
type internal FSharpChangeDerefToValueRefactoring
[<ImportingConstructor>]
(
checkerProvider: FSharpCheckerProvider,
projectInfoManager: FSharpProjectOptionsManager
) =
inherit CodeRefactoringProvider()

static let userOpName = "ChangeDerefToValue"

override _.ComputeRefactoringsAsync context =
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=userOpName) |> liftAsync

let selectionRange = RoslynHelpers.TextSpanToFSharpRange(document.FilePath, context.Span, sourceText)
let! derefRange = parseResults.TryRangeOfRefCellDereferenceContainingPos selectionRange.Start
let! exprRange = parseResults.TryRangeOfExpressionBeingDereferencedContainingPos selectionRange.Start

let combinedRange = Range.unionRanges derefRange exprRange
let! combinedSpan = RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, combinedRange)
let replacementString =
// Trim off the `!`
sourceText.GetSubText(combinedSpan).ToString().[1..] + ".Value"

let title = SR.UseValueInsteadOfDeref()

let getChangedText (sourceText: SourceText) =
sourceText.WithChanges(TextChange(combinedSpan, replacementString))

let codeAction =
CodeAction.Create(
title,
(fun (cancellationToken: CancellationToken) ->
async {
let! sourceText = context.Document.GetTextAsync(cancellationToken) |> Async.AwaitTask
return context.Document.WithText(getChangedText sourceText)
} |> RoslynHelpers.StartAsyncAsTask(cancellationToken)),
title)
context.RegisterRefactoring(codeAction)
}
|> Async.Ignore
|> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@
<target state="translated">Negovat výraz pomocí not</target>
<note />
</trans-unit>
<trans-unit id="UseValueInsteadOfDeref">
<source>Use '.Value' to dereference expression</source>
<target state="new">Use '.Value' to dereference expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="translated">Uzavřít výraz do závorek</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@
<target state="translated">"not" zum Negieren eines Ausdruck verwenden</target>
<note />
</trans-unit>
<trans-unit id="UseValueInsteadOfDeref">
<source>Use '.Value' to dereference expression</source>
<target state="new">Use '.Value' to dereference expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="translated">Ausdruck in Klammern einschließen</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@
<target state="translated">Usar "not" para negar la expresión</target>
<note />
</trans-unit>
<trans-unit id="UseValueInsteadOfDeref">
<source>Use '.Value' to dereference expression</source>
<target state="new">Use '.Value' to dereference expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="translated">Encapsular la expresión entre paréntesis</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@
<target state="translated">Utiliser 'not' pour annuler l'expression</target>
<note />
</trans-unit>
<trans-unit id="UseValueInsteadOfDeref">
<source>Use '.Value' to dereference expression</source>
<target state="new">Use '.Value' to dereference expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="translated">Mettre l'expression entre parenthèses</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@
<target state="translated">Usare 'not' per negare l'espressione</target>
<note />
</trans-unit>
<trans-unit id="UseValueInsteadOfDeref">
<source>Use '.Value' to dereference expression</source>
<target state="new">Use '.Value' to dereference expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="translated">Racchiudere l'espressione tra parentesi</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@
<target state="translated">式を否定するには 'not' を使用する</target>
<note />
</trans-unit>
<trans-unit id="UseValueInsteadOfDeref">
<source>Use '.Value' to dereference expression</source>
<target state="new">Use '.Value' to dereference expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="translated">式をかっこで囲む</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@
<target state="translated">식을 부정하려면 'not' 사용</target>
<note />
</trans-unit>
<trans-unit id="UseValueInsteadOfDeref">
<source>Use '.Value' to dereference expression</source>
<target state="new">Use '.Value' to dereference expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="translated">식을 괄호로 래핑</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@
<target state="translated">Użyj operatora „not”, aby zanegować wyrażenie</target>
<note />
</trans-unit>
<trans-unit id="UseValueInsteadOfDeref">
<source>Use '.Value' to dereference expression</source>
<target state="new">Use '.Value' to dereference expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="translated">Ujmij wyrażenie w nawiasy</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@
<target state="translated">Use 'not' para negar a expressão</target>
<note />
</trans-unit>
<trans-unit id="UseValueInsteadOfDeref">
<source>Use '.Value' to dereference expression</source>
<target state="new">Use '.Value' to dereference expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="translated">Coloque a expressão entre parênteses</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@
<target state="translated">Используйте "not" для отрицания выражения.</target>
<note />
</trans-unit>
<trans-unit id="UseValueInsteadOfDeref">
<source>Use '.Value' to dereference expression</source>
<target state="new">Use '.Value' to dereference expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="translated">Заключите выражение в круглые скобки.</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@
<target state="translated">İfadeyi negatif yapmak için 'not' kullanın</target>
<note />
</trans-unit>
<trans-unit id="UseValueInsteadOfDeref">
<source>Use '.Value' to dereference expression</source>
<target state="new">Use '.Value' to dereference expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="translated">İfadeyi parantez içinde sarmalayın</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@
<target state="translated">使用 "not" 对表达式求反</target>
<note />
</trans-unit>
<trans-unit id="UseValueInsteadOfDeref">
<source>Use '.Value' to dereference expression</source>
<target state="new">Use '.Value' to dereference expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="translated">将表达式用括号括起来</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@
<target state="translated">使用 'not' 來否定運算式</target>
<note />
</trans-unit>
<trans-unit id="UseValueInsteadOfDeref">
<source>Use '.Value' to dereference expression</source>
<target state="new">Use '.Value' to dereference expression</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="translated">使用括弧包裝運算式</target>
Expand Down

0 comments on commit db9805c

Please sign in to comment.