Skip to content

Commit 0795506

Browse files
Add code fix for adding missing seq before {…}
1 parent c8f85bc commit 0795506

19 files changed

+278
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+
namespace Microsoft.VisualStudio.FSharp.Editor
4+
5+
open System.Collections.Immutable
6+
open System.Composition
7+
open FSharp.Compiler.Syntax
8+
open FSharp.Compiler.Text
9+
open Microsoft.CodeAnalysis.CodeFixes
10+
open Microsoft.CodeAnalysis.Text
11+
open CancellableTasks
12+
13+
[<Sealed>]
14+
[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = CodeFix.ChangeToUpcast); Shared>]
15+
type internal AddMissingSeqCodeFixProvider() =
16+
inherit CodeFixProvider()
17+
18+
static let title = SR.AddMissingSeq()
19+
static let fixableDiagnosticIds = ImmutableArray.Create("FS3873", "FS0740")
20+
21+
override _.FixableDiagnosticIds = fixableDiagnosticIds
22+
23+
override this.RegisterCodeFixesAsync context = context.RegisterFsharpFix this
24+
25+
override this.GetFixAllProvider() = this.RegisterFsharpFixAll()
26+
27+
interface IFSharpCodeFixProvider with
28+
member _.GetCodeFixIfAppliesAsync context =
29+
cancellableTask {
30+
let! sourceText = context.GetSourceTextAsync()
31+
let! parseFileResults = context.Document.GetFSharpParseResultsAsync(nameof AddMissingSeqCodeFixProvider)
32+
33+
let getSourceLineStr line =
34+
sourceText.Lines[Line.toZ line].ToString()
35+
36+
let range =
37+
RoslynHelpers.TextSpanToFSharpRange(context.Document.FilePath, context.Span, sourceText)
38+
39+
let needsParens =
40+
(range.Start, parseFileResults.ParseTree)
41+
||> ParsedInput.exists (fun path node ->
42+
match path, node with
43+
| SyntaxNode.SynExpr outer :: _, SyntaxNode.SynExpr(expr & SynExpr.ComputationExpr _) when
44+
expr.Range |> Range.equals range
45+
->
46+
let seqRange =
47+
range
48+
|> Range.withEnd (Position.mkPos range.Start.Line (range.Start.Column + 3))
49+
50+
let inner =
51+
SynExpr.App(
52+
ExprAtomicFlag.NonAtomic,
53+
false,
54+
SynExpr.Ident(Ident(nameof seq, seqRange)),
55+
expr,
56+
Range.unionRanges seqRange expr.Range
57+
)
58+
59+
let outer =
60+
match outer with
61+
| SynExpr.App(flag, isInfix, funcExpr, _, outerAppRange) ->
62+
SynExpr.App(flag, isInfix, funcExpr, inner, outerAppRange)
63+
| outer -> outer
64+
65+
inner
66+
|> SynExpr.shouldBeParenthesizedInContext getSourceLineStr (SyntaxNode.SynExpr outer :: path)
67+
| _ -> false)
68+
69+
let text = sourceText.ToString(TextSpan(context.Span.Start, context.Span.Length))
70+
let newText = if needsParens then $"(seq {text})" else $"seq {text}"
71+
72+
return
73+
ValueSome
74+
{
75+
Name = CodeFix.AddMissingSeq
76+
Message = title
77+
Changes = [ TextChange(context.Span, newText) ]
78+
}
79+
}

vsintegration/src/FSharp.Editor/Common/Constants.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,6 @@ module internal CodeFix =
208208

209209
[<Literal>]
210210
let RemoveUnnecessaryParentheses = "RemoveUnnecessaryParentheses"
211+
212+
[<Literal>]
213+
let AddMissingSeq = "AddMissingSeq"

vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<Compile Include="CodeFixes\RenameParamToMatchSignature.fs" />
142142
<Compile Include="CodeFixes\UseTripleQuotedInterpolation.fs" />
143143
<Compile Include="CodeFixes\RemoveUnnecessaryParentheses.fs" />
144+
<Compile Include="CodeFixes\AddMissingSeq.fs" />
144145
<Compile Include="Build\SetGlobalPropertiesForSdkProjects.fs" />
145146
<Compile Include="AutomaticCompletion\BraceCompletionSessionProvider.fsi" />
146147
<Compile Include="AutomaticCompletion\BraceCompletionSessionProvider.fs" />

vsintegration/src/FSharp.Editor/FSharp.Editor.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ Use live (unsaved) buffers for analysis</value>
359359
<data name="RemoveUnnecessaryParentheses" xml:space="preserve">
360360
<value>Remove unnecessary parentheses</value>
361361
</data>
362+
<data name="AddMissingSeq" xml:space="preserve">
363+
<value>Add missing 'seq'</value>
364+
</data>
362365
<data name="RemarksHeader" xml:space="preserve">
363366
<value>Remarks:</value>
364367
</data>

vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)