Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal to add roslyn 'tags' to diagnostic response #1410

Merged
merged 11 commits into from
Mar 19, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class DiagnosticLocation : QuickFix
{
public string LogLevel { get; set; }
public string Id { get; set; }
public string[] Tags { get; set; }

public override bool Equals(object obj)
{
Expand Down
8 changes: 8 additions & 0 deletions src/OmniSharp.Roslyn.CSharp/Helpers/DiagnosticExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using Microsoft.CodeAnalysis;
using OmniSharp.Models.Diagnostics;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;

namespace OmniSharp.Helpers
{
internal static class DiagnosticExtensions
{
private static readonly ImmutableHashSet<string> _tagFilter =
ImmutableHashSet.Create<string>("Unnecessary");

internal static DiagnosticLocation ToDiagnosticLocation(this Diagnostic diagnostic)
{
var span = diagnostic.Location.GetMappedLineSpan();
Expand All @@ -20,6 +24,10 @@ internal static DiagnosticLocation ToDiagnosticLocation(this Diagnostic diagnost
EndColumn = span.EndLinePosition.Character,
Text = $"{diagnostic.GetMessage()} ({diagnostic.Id})",
LogLevel = diagnostic.Severity.ToString(),
Tags = diagnostic
.Descriptor.CustomTags
.Where(x => _tagFilter.Contains(x))
.ToArray(),
Id = diagnostic.Id
};
}
Expand Down
41 changes: 38 additions & 3 deletions tests/OmniSharp.Roslyn.CSharp.Tests/DiagnosticsFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using OmniSharp.Models.CodeCheck;
using OmniSharp.Models.Diagnostics;
using OmniSharp.Roslyn.CSharp.Services.Diagnostics;
using TestUtility;
using Xunit;
Expand Down Expand Up @@ -45,7 +46,7 @@ private OmniSharpTestHost GetHost(bool roslynAnalyzersEnabled)
[InlineData(false)]
public async Task CheckAllFiles(bool roslynAnalyzersEnabled)
{
using(var host = GetHost(roslynAnalyzersEnabled))
using (var host = GetHost(roslynAnalyzersEnabled))
{
host.AddFilesToWorkspace(
new TestFile("a.cs", "class C1 { int n = true; }"),
Expand All @@ -63,7 +64,7 @@ public async Task CheckAllFiles(bool roslynAnalyzersEnabled)
[InlineData(false)]
public async Task WhenFileIsDeletedFromWorkSpaceThenResultsAreNotReturnedAnyMore(bool roslynAnalyzersEnabled)
{
using(var host = GetHost(roslynAnalyzersEnabled))
using (var host = GetHost(roslynAnalyzersEnabled))
{
host.AddFilesToWorkspace(new TestFile("a.cs", "class C1 { int n = true; }"));
await host.RequestCodeCheckAsync();
Expand All @@ -83,7 +84,7 @@ public async Task WhenFileIsDeletedFromWorkSpaceThenResultsAreNotReturnedAnyMore
[Fact]
public async Task AnalysisSupportBuiltInIDEAnalysers()
{
using(var host = GetHost(roslynAnalyzersEnabled: true))
using (var host = GetHost(roslynAnalyzersEnabled: true))
{
host.AddFilesToWorkspace(
new TestFile("a.cs", "class C1 { int n = true; }"));
Expand All @@ -92,5 +93,39 @@ public async Task AnalysisSupportBuiltInIDEAnalysers()
Assert.Contains(quickFixes.QuickFixes, x => x.Text.Contains("IDE0044"));
}
}

[Fact]
public async Task WhenUnusedImportExistsWithoutAnalyzersEnabled_ThenReturnEmptyTags()
{
using (var host = GetHost(roslynAnalyzersEnabled: false))
{
host.AddFilesToWorkspace(
new TestFile("returnemptytags.cs", @"using System.IO;"));

var quickFixResponse = await host.RequestCodeCheckAsync("returnemptytags.cs");

var allDiagnostics = quickFixResponse.QuickFixes.OfType<DiagnosticLocation>();

Assert.Empty(allDiagnostics.SelectMany(x => x.Tags));
}
}

[Fact]
public async Task WhenUnusedImportIsFoundAndAnalyzersEnabled_ThenReturnUnnecessaryTag()
{
using (var host = GetHost(roslynAnalyzersEnabled: true))
{
host.AddFilesToWorkspace(
new TestFile("returnidetags.cs", @"using System.IO;"));

var quickFixResponse = await host.RequestCodeCheckAsync("returnidetags.cs");

Assert.Contains("Unnecessary", quickFixResponse
.QuickFixes
.OfType<DiagnosticLocation>()
.Single(x => x.Id == "IDE0005")
.Tags);
}
}
}
}