Skip to content

Commit 9315545

Browse files
author
Omar Tawfik
committed
Draft version of document diagnostics
1 parent 7280d69 commit 9315545

File tree

7 files changed

+98
-9
lines changed

7 files changed

+98
-9
lines changed

vsintegration/Vsix/VisualFSharpDesktop/Source.extension.vsixmanifest

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@
5757
<Asset Type="Microsoft.VisualStudio.ItemTemplate" d:Source="Project" Path="ItemTemplates" d:TargetPath="|XMLFile;TemplateProjectOutputGroup|" d:ProjectName="XMLFile" d:VsixSubPath="ItemTemplates" />
5858

5959
<Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="FSharp.Editor" Path="|FSharp.Editor|" AssemblyName="|FSharp.Editor;AssemblyName|" />
60+
<Asset Type="Microsoft.VisualStudio.Analyzer" d:Source="Project" d:ProjectName="FSharp.Editor" Path="|FSharp.Editor|" />
6061
</Assets>
6162
</PackageManifest>

vsintegration/Vsix/VisualFSharpFull/Source.extension.vsixmanifest

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@
6161
<Asset Type="Microsoft.VisualStudio.ItemTemplate" d:Source="Project" Path="ItemTemplates" d:TargetPath="|XMLFile;TemplateProjectOutputGroup|" d:ProjectName="XMLFile" d:VsixSubPath="ItemTemplates" />
6262

6363
<Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="FSharp.Editor" Path="|FSharp.Editor|" AssemblyName="|FSharp.Editor;AssemblyName|" />
64+
<Asset Type="Microsoft.VisualStudio.Analyzer" d:Source="Project" d:ProjectName="FSharp.Editor" Path="|FSharp.Editor|" />
6465
</Assets>
6566
</PackageManifest>

vsintegration/Vsix/VisualFSharpWeb/Source.extension.vsixmanifest

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@
5555
<Asset Type="Microsoft.VisualStudio.ItemTemplate" d:Source="Project" Path="ItemTemplates" d:TargetPath="|XMLFile;TemplateProjectOutputGroup|" d:ProjectName="XMLFile" d:VsixSubPath="ItemTemplates" />
5656

5757
<Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="FSharp.Editor" Path="|FSharp.Editor|" AssemblyName="|FSharp.Editor;AssemblyName|" />
58+
<Asset Type="Microsoft.VisualStudio.Analyzer" d:Source="Project" d:ProjectName="FSharp.Editor" Path="|FSharp.Editor|" />
5859
</Assets>
5960
</PackageManifest>

vsintegration/src/FSharp.Editor/DocumentDiagnosticAnalyzer.fs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,48 @@ open Microsoft.CodeAnalysis.SolutionCrawler
1616

1717
open Microsoft.FSharp.Compiler
1818
open Microsoft.FSharp.Compiler.SourceCodeServices
19+
open Microsoft.FSharp.Compiler.Range
1920

2021
open Microsoft.VisualStudio.FSharp.LanguageService
2122

2223
[<DiagnosticAnalyzer(FSharpCommonConstants.FSharpLanguageName)>]
2324
type internal FSharpDocumentDiagnosticAnalyzer() =
2425
inherit DocumentDiagnosticAnalyzer()
2526

27+
// We are constructing our own descriptors at run-time. Compiler service is already doing error formatting and localization.
2628
override this.SupportedDiagnostics with get() = ImmutableArray<DiagnosticDescriptor>.Empty
2729

28-
override this.AnalyzeSyntaxAsync(document: Document, cancellationToken: CancellationToken): Task<ImmutableArray<Diagnostic>> =
30+
override this.AnalyzeSyntaxAsync(_, _): Task<ImmutableArray<Diagnostic>> =
31+
Task.FromResult(ImmutableArray<Diagnostic>.Empty)
32+
33+
override this.AnalyzeSemanticsAsync(document: Document, cancellationToken: CancellationToken): Task<ImmutableArray<Diagnostic>> =
2934
let computation = async {
3035
let! sourceText = document.GetTextAsync(cancellationToken) |> Async.AwaitTask
3136
let options = CommonRoslynHelpers.GetFSharpProjectOptionsForRoslynProject(document.Project)
3237
let! parseResults = FSharpChecker.Instance.ParseFileInProject(document.Name, sourceText.ToString(), options)
33-
34-
parseResults.Errors |> Seq.iter(fun (error) -> printf "%A" error) |> ignore
35-
36-
return ImmutableArray<Diagnostic>.Empty
38+
let! checkResultsAnswer = FSharpChecker.Instance.CheckFileInProject(parseResults, document.Name, 0, sourceText.ToString(), options)
39+
40+
let errors = match checkResultsAnswer with
41+
| FSharpCheckFileAnswer.Aborted -> failwith "Compilation isn't complete yet"
42+
| FSharpCheckFileAnswer.Succeeded(results) -> results.Errors
43+
44+
let diagnostics = errors |> Seq.map(fun (error) ->
45+
let id = "FS" + error.ErrorNumber.ToString()
46+
let emptyString = LocalizableString.op_Implicit("")
47+
let description = LocalizableString.op_Implicit(error.Message)
48+
let severity = if error.Severity = FSharpErrorSeverity.Error then DiagnosticSeverity.Error else DiagnosticSeverity.Warning
49+
let descriptor = new DiagnosticDescriptor(id, emptyString, description, error.Subcategory, severity, true, emptyString, String.Empty, null)
50+
51+
let location = match (error.StartLineAlternate - 1, error.EndLineAlternate - 1) with
52+
| (-1, _) -> Location.None
53+
| (_, -1) -> Location.None
54+
| (startl, endl) ->
55+
let linePositionSpan = LinePositionSpan(LinePosition(startl, error.StartColumn), LinePosition(endl, error.EndColumn))
56+
Location.Create(error.FileName, sourceText.Lines.GetTextSpan(linePositionSpan) , linePositionSpan)
57+
58+
Diagnostic.Create(descriptor, location))
59+
return Seq.toArray(diagnostics).ToImmutableArray()
3760
}
3861

3962
Async.StartAsTask(computation, TaskCreationOptions.None, cancellationToken)
40-
41-
override this.AnalyzeSemanticsAsync(_: Document, _: CancellationToken): Task<ImmutableArray<Diagnostic>> =
42-
Task.FromResult(ImmutableArray<Diagnostic>.Empty)
63+
.ContinueWith(CommonRoslynHelpers.GetCompletedTaskResult, cancellationToken)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
2+
3+
namespace Microsoft.VisualStudio.FSharp.Editor
4+
5+
open System
6+
open System.Composition
7+
open System.Collections.Immutable
8+
open System.Threading
9+
open System.Threading.Tasks
10+
11+
open Microsoft.CodeAnalysis
12+
open Microsoft.CodeAnalysis.Diagnostics
13+
open Microsoft.CodeAnalysis.Host.Mef
14+
open Microsoft.CodeAnalysis.Text
15+
open Microsoft.CodeAnalysis.SolutionCrawler
16+
17+
open Microsoft.FSharp.Compiler
18+
open Microsoft.FSharp.Compiler.SourceCodeServices
19+
open Microsoft.FSharp.Compiler.Range
20+
21+
open Microsoft.VisualStudio.FSharp.LanguageService
22+
23+
[<ExportLanguageService(typeof<IDocumentDifferenceService>, FSharpCommonConstants.FSharpLanguageName)>]
24+
type FSharpDocumentDifferenceService() =
25+
interface IDocumentDifferenceService with
26+
member this.GetDifferenceAsync(_,_,_) =
27+
// No incremental anaylsis for now.
28+
Task.FromResult(new DocumentDifferenceResult(InvocationReasons.DocumentChanged))

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@
4545
<Compile Include="LanguageDebugInfoService.fs">
4646
<Link>Debugging\LanguageDebugInfoService.fs</Link>
4747
</Compile>
48+
<Compile Include="DocumentDifferenceService.fs">
49+
<Link>Diagnostics\DocumentDifferenceService.fs</Link>
50+
</Compile>
4851
<Compile Include="DocumentDiagnosticAnalyzer.fs">
49-
<Link>Diagnostics\DocumentDiagnosticAnalyzer.fs</Link>
52+
<Link>Diagnostics\DocumentDiagnosticAnalyzer.fs</Link>
53+
</Compile>
54+
<Compile Include="ProjectDiagnosticAnalyzer.fs">
55+
<Link>Diagnostics\ProjectDiagnosticAnalyzer.fs</Link>
5056
</Compile>
5157
<Compile Include="ProjectSiteService.fs" />
5258
<Compile Include="ContentType.fs" />
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
2+
3+
namespace Microsoft.VisualStudio.FSharp.Editor
4+
5+
open System
6+
open System.Composition
7+
open System.Collections.Immutable
8+
open System.Threading
9+
open System.Threading.Tasks
10+
11+
open Microsoft.CodeAnalysis
12+
open Microsoft.CodeAnalysis.Diagnostics
13+
open Microsoft.CodeAnalysis.Host.Mef
14+
open Microsoft.CodeAnalysis.Text
15+
open Microsoft.CodeAnalysis.SolutionCrawler
16+
17+
open Microsoft.FSharp.Compiler
18+
open Microsoft.FSharp.Compiler.SourceCodeServices
19+
open Microsoft.FSharp.Compiler.Range
20+
21+
open Microsoft.VisualStudio.FSharp.LanguageService
22+
23+
[<DiagnosticAnalyzer(FSharpCommonConstants.FSharpLanguageName)>]
24+
type internal FSharpProjectDiagnosticAnalyzer() =
25+
inherit ProjectDiagnosticAnalyzer()
26+
27+
// We are constructing our own descriptors at run-time. Compiler service is already doing error formatting and localization.
28+
override this.SupportedDiagnostics with get() = ImmutableArray<DiagnosticDescriptor>.Empty
29+
30+
override this.AnalyzeProjectAsync(_, _): Task<ImmutableArray<Diagnostic>> =
31+
Task.FromResult(ImmutableArray<Diagnostic>.Empty)

0 commit comments

Comments
 (0)