Skip to content

Commit

Permalink
Implement triple quoted interpolation code fix (#12945)
Browse files Browse the repository at this point in the history
  • Loading branch information
kerams authored Apr 6, 2022
1 parent 08992b3 commit db6ce1b
Show file tree
Hide file tree
Showing 19 changed files with 133 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/fsharp/service/FSharpParseFileResults.fs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ type FSharpParseFileResults(diagnostics: FSharpDiagnostic[], input: ParsedInput,
Some(app.Range, lambdaArgs.Range, lambdaBody.Range)
| _ -> defaultTraverse binding })

member _.TryRangeOfStringInterpolationContainingPos pos =
SyntaxTraversal.Traverse(pos, input, { new SyntaxVisitorBase<_>() with
member _.VisitExpr(_, _, defaultTraverse, expr) =
match expr with
| SynExpr.InterpolatedString(range = range) when rangeContainsPos range pos ->
Some range
| _ -> defaultTraverse expr })

member _.TryRangeOfExprInYieldOrReturn pos =
SyntaxTraversal.Traverse(pos, input, { new SyntaxVisitorBase<_>() with
member _.VisitExpr(_path, _, defaultTraverse, expr) =
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 @@ -20,6 +20,9 @@ type public FSharpParseFileResults =
/// Attempts to find the range of an attempted lambda expression or pattern, the argument range, and the expr range when writing a C#-style "lambda" (which is actually an operator application)
member TryRangeOfParenEnclosingOpEqualsGreaterUsage: opGreaterEqualPos: pos -> (range * range * range) option

/// Attempts to find the range of the string interpolation that contains a given position.
member TryRangeOfStringInterpolationContainingPos: pos: pos -> range option

/// Attempts to find the range of an expression `expr` contained in a `yield expr` or `return expr` expression (and bang-variants).
member TryRangeOfExprInYieldOrReturn: pos: pos -> range option

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2069,6 +2069,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] TryRangeOfStringInterpolationContainingPos(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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 Microsoft.CodeAnalysis.Text
open Microsoft.CodeAnalysis.CodeFixes

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

let fixableDiagnosticIds = ["FS3373"]

override _.FixableDiagnosticIds = Seq.toImmutableArray fixableDiagnosticIds

override _.RegisterCodeFixesAsync context =
asyncMaybe {
let! parseResults = context.Document.GetFSharpParseResultsAsync(nameof(FSharpUseTripleQuotedInterpolationCodeFixProvider)) |> liftAsync

let! sourceText = context.Document.GetTextAsync(context.CancellationToken)
let errorRange = RoslynHelpers.TextSpanToFSharpRange(context.Document.FilePath, context.Span, sourceText)

let! interpolationRange = parseResults.TryRangeOfStringInterpolationContainingPos errorRange.Start
let! interpolationSpan = RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, interpolationRange)

let replacement =
let interpolation = sourceText.GetSubText(interpolationSpan).ToString()
TextChange(interpolationSpan, "$\"\"" + interpolation.[ 1 .. ] + "\"\"")

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

let title = SR.UseTripleQuotedInterpolation()

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

context.RegisterCodeFix(codeFix, diagnostics)
}
|> Async.Ignore
|> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)
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 @@ -121,6 +121,7 @@
<Compile Include="CodeFix\MissingReferenceCodeFixProvider.fs" />
<Compile Include="CodeFix\FixIndexerAccess.fs" />
<Compile Include="CodeFix\RenameParamToMatchSignature.fs" />
<Compile Include="CodeFix\UseTripleQuotedInterpolation.fs" />
<Compile Include="Build\SetGlobalPropertiesForSdkProjects.fs" />
<Compile Include="AutomaticCompletion\BraceCompletionSessionProvider.fsi" />
<Compile Include="AutomaticCompletion\BraceCompletionSessionProvider.fs" />
Expand Down
5 changes: 4 additions & 1 deletion vsintegration/src/FSharp.Editor/FSharp.Editor.resx
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,7 @@
<data name="UseNameof" xml:space="preserve">
<value>Use 'nameof'</value>
</data>
</root>
<data name="UseTripleQuotedInterpolation" xml:space="preserve">
<value>Use triple quoted string interpolation.</value>
</data>
</root>
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">Použít nameof</target>
<note />
</trans-unit>
<trans-unit id="UseTripleQuotedInterpolation">
<source>Use triple quoted string interpolation.</source>
<target state="new">Use triple quoted string interpolation.</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="translated">Použijte upcast</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">"nameof" verwenden</target>
<note />
</trans-unit>
<trans-unit id="UseTripleQuotedInterpolation">
<source>Use triple quoted string interpolation.</source>
<target state="new">Use triple quoted string interpolation.</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="translated">"upcast" verwenden</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 'nameof'</target>
<note />
</trans-unit>
<trans-unit id="UseTripleQuotedInterpolation">
<source>Use triple quoted string interpolation.</source>
<target state="new">Use triple quoted string interpolation.</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="translated">Usar "upcast"</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 « nameof »</target>
<note />
</trans-unit>
<trans-unit id="UseTripleQuotedInterpolation">
<source>Use triple quoted string interpolation.</source>
<target state="new">Use triple quoted string interpolation.</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="translated">Utiliser 'upcast'</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">Usa 'nameof'</target>
<note />
</trans-unit>
<trans-unit id="UseTripleQuotedInterpolation">
<source>Use triple quoted string interpolation.</source>
<target state="new">Use triple quoted string interpolation.</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="translated">Usare 'upcast'</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">'nameof' を使用する</target>
<note />
</trans-unit>
<trans-unit id="UseTripleQuotedInterpolation">
<source>Use triple quoted string interpolation.</source>
<target state="new">Use triple quoted string interpolation.</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="translated">'upcast' を使用する</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">'nameof' 사용</target>
<note />
</trans-unit>
<trans-unit id="UseTripleQuotedInterpolation">
<source>Use triple quoted string interpolation.</source>
<target state="new">Use triple quoted string interpolation.</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="translated">'upcast' 사용</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 wyrażenia "nameof"</target>
<note />
</trans-unit>
<trans-unit id="UseTripleQuotedInterpolation">
<source>Use triple quoted string interpolation.</source>
<target state="new">Use triple quoted string interpolation.</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="translated">Użyj operatora „upcast”</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">Usar 'nameof'</target>
<note />
</trans-unit>
<trans-unit id="UseTripleQuotedInterpolation">
<source>Use triple quoted string interpolation.</source>
<target state="new">Use triple quoted string interpolation.</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="translated">Usar 'upcast'</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">Использовать "nameof"</target>
<note />
</trans-unit>
<trans-unit id="UseTripleQuotedInterpolation">
<source>Use triple quoted string interpolation.</source>
<target state="new">Use triple quoted string interpolation.</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="translated">Используйте "upcast"</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">“Nameof” kullanın</target>
<note />
</trans-unit>
<trans-unit id="UseTripleQuotedInterpolation">
<source>Use triple quoted string interpolation.</source>
<target state="new">Use triple quoted string interpolation.</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="translated">'upcast' kullan</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">使用 "nameof"</target>
<note />
</trans-unit>
<trans-unit id="UseTripleQuotedInterpolation">
<source>Use triple quoted string interpolation.</source>
<target state="new">Use triple quoted string interpolation.</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</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">使用 'nameof'</target>
<note />
</trans-unit>
<trans-unit id="UseTripleQuotedInterpolation">
<source>Use triple quoted string interpolation.</source>
<target state="new">Use triple quoted string interpolation.</target>
<note />
</trans-unit>
<trans-unit id="UseUpcastKeyword">
<source>Use 'upcast'</source>
<target state="translated">使用「向上轉型」</target>
Expand Down

0 comments on commit db6ce1b

Please sign in to comment.