Skip to content

Commit a2c9fe2

Browse files
Move diagnostic creation logic into its own file
* We're still calling the logic directly inside of `DocumentDiagnosticAnalyzer` rather than exporting the type as an implementation of the `IFSharpUnnecessaryParenthesesDiagnosticAnalyzer` interface, since that interface does not exist upstream in Roslyn, and it seems like we would currently prefer not to go through the logistics of upstreaming it.
1 parent e30168b commit a2c9fe2

File tree

3 files changed

+55
-75
lines changed

3 files changed

+55
-75
lines changed

vsintegration/src/FSharp.Editor/Diagnostics/DocumentDiagnosticAnalyzer.fs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ open Microsoft.CodeAnalysis.Text
1313
open Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics
1414

1515
open FSharp.Compiler.Diagnostics
16-
open FSharp.Compiler.EditorServices
1716
open CancellableTasks
1817
open Microsoft.VisualStudio.FSharp.Editor.Telemetry
1918

@@ -112,35 +111,7 @@ type internal FSharpDocumentDiagnosticAnalyzer [<ImportingConstructor>] () =
112111
let! unnecessaryParentheses =
113112
match diagnosticType with
114113
| DiagnosticsType.Semantic -> CancellableTask.singleton ImmutableArray.Empty
115-
| DiagnosticsType.Syntax ->
116-
cancellableTask {
117-
let fsharpSourceText = sourceText.ToFSharpSourceText()
118-
119-
let! unnecessaryParentheses =
120-
UnnecessaryParentheses.getUnnecessaryParentheses
121-
(FSharp.Compiler.Text.Line.toZ >> fsharpSourceText.GetLineString)
122-
parseResults.ParseTree
123-
124-
let descriptor =
125-
let title = "Parentheses can be removed."
126-
127-
DiagnosticDescriptor(
128-
"IDE0047",
129-
title,
130-
title,
131-
"Style",
132-
DiagnosticSeverity.Hidden,
133-
isEnabledByDefault = true,
134-
description = null,
135-
helpLinkUri = null
136-
)
137-
138-
return
139-
unnecessaryParentheses
140-
|> Seq.map (fun range ->
141-
Diagnostic.Create(descriptor, RoslynHelpers.RangeToLocation(range, sourceText, document.FilePath)))
142-
|> Seq.toImmutableArray
143-
}
114+
| DiagnosticsType.Syntax -> UnnecessaryParenthesesDiagnosticAnalyzer.GetDiagnostics document
144115

145116
if errors.Count = 0 && unnecessaryParentheses.IsEmpty then
146117
return ImmutableArray.Empty

vsintegration/src/FSharp.Editor/Diagnostics/UnnecessaryParenthesesDiagnosticAnalyzer.fs

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,56 @@
22

33
namespace Microsoft.VisualStudio.FSharp.Editor
44

5-
//open System.Composition
6-
//open System.Collections.Immutable
7-
//open System.Threading
8-
//open System.Threading.Tasks
9-
10-
//open FSharp.Compiler.EditorServices
11-
12-
//open Microsoft.CodeAnalysis
13-
//open Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics
14-
15-
////type IFSharpUnnecessaryParenthesesDiagnosticAnalyzer = inherit IFSharpDocumentDiagnosticAnalyzer
16-
17-
//[<Export(typeof<IFSharpUnnecessaryParenthesesDiagnosticAnalyzer>)>]
18-
//type internal UnnecessaryParenthesesDiagnosticAnalyzer [<ImportingConstructor>] () =
19-
// static let descriptor =
20-
// let title = "Parentheses can be removed."
21-
// DiagnosticDescriptor(
22-
// "IDE0047",
23-
// title,
24-
// title,
25-
// "Style",
26-
// DiagnosticSeverity.Hidden,
27-
// isEnabledByDefault=true,
28-
// description=null,
29-
// helpLinkUri=null)
30-
31-
// interface IFSharpUnnecessaryParenthesesDiagnosticAnalyzer with
32-
// member _.AnalyzeSemanticsAsync(document: Document, cancellationToken: CancellationToken) =
33-
// ignore (document, cancellationToken)
34-
// Task.FromResult ImmutableArray.Empty
35-
36-
// member _.AnalyzeSyntaxAsync(document: Document, cancellationToken: CancellationToken) =
37-
// asyncMaybe {
38-
// let! parseResults = document.GetFSharpParseResultsAsync(nameof UnnecessaryParenthesesDiagnosticAnalyzer) |> liftAsync
39-
// let! unnecessaryParentheses = UnnecessaryParentheses.getUnnecessaryParentheses parseResults.ParseTree |> liftAsync
40-
// let! ct = Async.CancellationToken |> liftAsync
41-
// let! sourceText = document.GetTextAsync ct
42-
// return
43-
// unnecessaryParentheses
44-
// |> Seq.map (fun range -> Diagnostic.Create(descriptor, RoslynHelpers.RangeToLocation(range, sourceText, document.FilePath)))
45-
// |> Seq.toImmutableArray
46-
// }
47-
// |> Async.map (Option.defaultValue ImmutableArray.Empty)
48-
// |> RoslynHelpers.StartAsyncAsTask cancellationToken
5+
open System.Composition
6+
open System.Collections.Immutable
7+
open System.Threading
8+
open System.Threading.Tasks
9+
open FSharp.Compiler.EditorServices
10+
open FSharp.Compiler.Text
11+
open Microsoft.CodeAnalysis
12+
open Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics
13+
open CancellableTasks
14+
15+
// This interface is not defined in Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics
16+
// and so we are not currently exporting the type below as an implementation of it
17+
// using [<Export(typeof<IFSharpUnnecessaryParenthesesDiagnosticAnalyzer>)>], since it would not be recognized.
18+
type IFSharpUnnecessaryParenthesesDiagnosticAnalyzer = inherit IFSharpDocumentDiagnosticAnalyzer
19+
20+
[<Sealed>]
21+
type internal UnnecessaryParenthesesDiagnosticAnalyzer [<ImportingConstructor>] () =
22+
static let completedTask = Task.FromResult ImmutableArray.Empty
23+
24+
static let descriptor =
25+
let title = "Parentheses can be removed."
26+
27+
DiagnosticDescriptor(
28+
"IDE0047",
29+
title,
30+
title,
31+
"Style",
32+
DiagnosticSeverity.Hidden,
33+
isEnabledByDefault = true,
34+
description = null,
35+
helpLinkUri = null)
36+
37+
static member GetDiagnostics(document: Document) =
38+
cancellableTask {
39+
let! parseResults = document.GetFSharpParseResultsAsync(nameof UnnecessaryParenthesesDiagnosticAnalyzer)
40+
let! cancellationToken = CancellableTask.getCancellationToken ()
41+
let! sourceText = document.GetTextAsync cancellationToken
42+
let getLineString line = sourceText.Lines[Line.toZ line].ToString()
43+
let! unnecessaryParentheses = UnnecessaryParentheses.getUnnecessaryParentheses getLineString parseResults.ParseTree
44+
return
45+
unnecessaryParentheses
46+
|> Seq.map (fun range -> Diagnostic.Create(descriptor, RoslynHelpers.RangeToLocation(range, sourceText, document.FilePath)))
47+
|> Seq.toImmutableArray
48+
}
49+
50+
interface IFSharpUnnecessaryParenthesesDiagnosticAnalyzer with
51+
member _.AnalyzeSemanticsAsync(document: Document, cancellationToken: CancellationToken) =
52+
ignore (document, cancellationToken)
53+
completedTask
54+
55+
member _.AnalyzeSyntaxAsync(document: Document, cancellationToken: CancellationToken) =
56+
UnnecessaryParenthesesDiagnosticAnalyzer.GetDiagnostics document
57+
|> CancellableTask.start cancellationToken

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@
7070
<Compile Include="Formatting\EditorFormattingService.fs" />
7171
<Compile Include="Debugging\BreakpointResolutionService.fs" />
7272
<Compile Include="Debugging\LanguageDebugInfoService.fs" />
73+
<Compile Include="Diagnostics\UnnecessaryParenthesesDiagnosticAnalyzer.fs" />
7374
<Compile Include="Diagnostics\DocumentDiagnosticAnalyzer.fs" />
7475
<Compile Include="Diagnostics\SimplifyNameDiagnosticAnalyzer.fs" />
7576
<Compile Include="Diagnostics\UnusedDeclarationsAnalyzer.fs" />
7677
<Compile Include="Diagnostics\UnusedOpensDiagnosticAnalyzer.fs" />
77-
<Compile Include="Diagnostics\UnnecessaryParenthesesDiagnosticAnalyzer.fs" />
7878
<Compile Include="DocComments\XMLDocumentation.fs" />
7979
<Compile Include="TaskList\TaskListService.fs" />
8080
<Compile Include="Completion\CompletionUtils.fs" />

0 commit comments

Comments
 (0)