|
2 | 2 |
|
3 | 3 | namespace Microsoft.VisualStudio.FSharp.Editor
|
4 | 4 |
|
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 |
0 commit comments