Skip to content

Commit

Permalink
Move diagnostic creation logic into its own file
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
brianrourkeboll committed Oct 19, 2023
1 parent e30168b commit a2c9fe2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ open Microsoft.CodeAnalysis.Text
open Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics

open FSharp.Compiler.Diagnostics
open FSharp.Compiler.EditorServices
open CancellableTasks
open Microsoft.VisualStudio.FSharp.Editor.Telemetry

Expand Down Expand Up @@ -112,35 +111,7 @@ type internal FSharpDocumentDiagnosticAnalyzer [<ImportingConstructor>] () =
let! unnecessaryParentheses =
match diagnosticType with
| DiagnosticsType.Semantic -> CancellableTask.singleton ImmutableArray.Empty
| DiagnosticsType.Syntax ->
cancellableTask {
let fsharpSourceText = sourceText.ToFSharpSourceText()

let! unnecessaryParentheses =
UnnecessaryParentheses.getUnnecessaryParentheses
(FSharp.Compiler.Text.Line.toZ >> fsharpSourceText.GetLineString)
parseResults.ParseTree

let descriptor =
let title = "Parentheses can be removed."

DiagnosticDescriptor(
"IDE0047",
title,
title,
"Style",
DiagnosticSeverity.Hidden,
isEnabledByDefault = true,
description = null,
helpLinkUri = null
)

return
unnecessaryParentheses
|> Seq.map (fun range ->
Diagnostic.Create(descriptor, RoslynHelpers.RangeToLocation(range, sourceText, document.FilePath)))
|> Seq.toImmutableArray
}
| DiagnosticsType.Syntax -> UnnecessaryParenthesesDiagnosticAnalyzer.GetDiagnostics document

if errors.Count = 0 && unnecessaryParentheses.IsEmpty then
return ImmutableArray.Empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,56 @@

namespace Microsoft.VisualStudio.FSharp.Editor

//open System.Composition
//open System.Collections.Immutable
//open System.Threading
//open System.Threading.Tasks

//open FSharp.Compiler.EditorServices

//open Microsoft.CodeAnalysis
//open Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics

////type IFSharpUnnecessaryParenthesesDiagnosticAnalyzer = inherit IFSharpDocumentDiagnosticAnalyzer

//[<Export(typeof<IFSharpUnnecessaryParenthesesDiagnosticAnalyzer>)>]
//type internal UnnecessaryParenthesesDiagnosticAnalyzer [<ImportingConstructor>] () =
// static let descriptor =
// let title = "Parentheses can be removed."
// DiagnosticDescriptor(
// "IDE0047",
// title,
// title,
// "Style",
// DiagnosticSeverity.Hidden,
// isEnabledByDefault=true,
// description=null,
// helpLinkUri=null)

// interface IFSharpUnnecessaryParenthesesDiagnosticAnalyzer with
// member _.AnalyzeSemanticsAsync(document: Document, cancellationToken: CancellationToken) =
// ignore (document, cancellationToken)
// Task.FromResult ImmutableArray.Empty

// member _.AnalyzeSyntaxAsync(document: Document, cancellationToken: CancellationToken) =
// asyncMaybe {
// let! parseResults = document.GetFSharpParseResultsAsync(nameof UnnecessaryParenthesesDiagnosticAnalyzer) |> liftAsync
// let! unnecessaryParentheses = UnnecessaryParentheses.getUnnecessaryParentheses parseResults.ParseTree |> liftAsync
// let! ct = Async.CancellationToken |> liftAsync
// let! sourceText = document.GetTextAsync ct
// return
// unnecessaryParentheses
// |> Seq.map (fun range -> Diagnostic.Create(descriptor, RoslynHelpers.RangeToLocation(range, sourceText, document.FilePath)))
// |> Seq.toImmutableArray
// }
// |> Async.map (Option.defaultValue ImmutableArray.Empty)
// |> RoslynHelpers.StartAsyncAsTask cancellationToken
open System.Composition
open System.Collections.Immutable
open System.Threading
open System.Threading.Tasks
open FSharp.Compiler.EditorServices
open FSharp.Compiler.Text
open Microsoft.CodeAnalysis
open Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics
open CancellableTasks

// This interface is not defined in Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics
// and so we are not currently exporting the type below as an implementation of it
// using [<Export(typeof<IFSharpUnnecessaryParenthesesDiagnosticAnalyzer>)>], since it would not be recognized.
type IFSharpUnnecessaryParenthesesDiagnosticAnalyzer = inherit IFSharpDocumentDiagnosticAnalyzer

[<Sealed>]
type internal UnnecessaryParenthesesDiagnosticAnalyzer [<ImportingConstructor>] () =
static let completedTask = Task.FromResult ImmutableArray.Empty

static let descriptor =
let title = "Parentheses can be removed."

DiagnosticDescriptor(
"IDE0047",
title,
title,
"Style",
DiagnosticSeverity.Hidden,
isEnabledByDefault = true,
description = null,
helpLinkUri = null)

static member GetDiagnostics(document: Document) =
cancellableTask {
let! parseResults = document.GetFSharpParseResultsAsync(nameof UnnecessaryParenthesesDiagnosticAnalyzer)
let! cancellationToken = CancellableTask.getCancellationToken ()
let! sourceText = document.GetTextAsync cancellationToken
let getLineString line = sourceText.Lines[Line.toZ line].ToString()
let! unnecessaryParentheses = UnnecessaryParentheses.getUnnecessaryParentheses getLineString parseResults.ParseTree
return
unnecessaryParentheses
|> Seq.map (fun range -> Diagnostic.Create(descriptor, RoslynHelpers.RangeToLocation(range, sourceText, document.FilePath)))
|> Seq.toImmutableArray
}

interface IFSharpUnnecessaryParenthesesDiagnosticAnalyzer with
member _.AnalyzeSemanticsAsync(document: Document, cancellationToken: CancellationToken) =
ignore (document, cancellationToken)
completedTask

member _.AnalyzeSyntaxAsync(document: Document, cancellationToken: CancellationToken) =
UnnecessaryParenthesesDiagnosticAnalyzer.GetDiagnostics document
|> CancellableTask.start cancellationToken
2 changes: 1 addition & 1 deletion vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@
<Compile Include="Formatting\EditorFormattingService.fs" />
<Compile Include="Debugging\BreakpointResolutionService.fs" />
<Compile Include="Debugging\LanguageDebugInfoService.fs" />
<Compile Include="Diagnostics\UnnecessaryParenthesesDiagnosticAnalyzer.fs" />
<Compile Include="Diagnostics\DocumentDiagnosticAnalyzer.fs" />
<Compile Include="Diagnostics\SimplifyNameDiagnosticAnalyzer.fs" />
<Compile Include="Diagnostics\UnusedDeclarationsAnalyzer.fs" />
<Compile Include="Diagnostics\UnusedOpensDiagnosticAnalyzer.fs" />
<Compile Include="Diagnostics\UnnecessaryParenthesesDiagnosticAnalyzer.fs" />
<Compile Include="DocComments\XMLDocumentation.fs" />
<Compile Include="TaskList\TaskListService.fs" />
<Compile Include="Completion\CompletionUtils.fs" />
Expand Down

0 comments on commit a2c9fe2

Please sign in to comment.