提交 991018a0 编写于 作者: H HeeJae Chang 提交者: Sam Harwell

Add IDocumentOperationService.SupportDiagnostics

上级 6262cc0d
......@@ -5,8 +5,8 @@
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using System.Threading;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Diagnostics.Telemetry;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Workspaces.Diagnostics;
using Roslyn.Utilities;
......@@ -77,7 +77,7 @@ internal static class DiagnosticResultSerializer
var others = diagnosticDataSerializer.ReadFrom(reader, project, cancellationToken);
var analysisResult = DiagnosticAnalysisResult.CreateFromSerialization(
project.Id,
project,
version,
syntaxLocalMap,
semanticLocalMap,
......@@ -143,7 +143,16 @@ internal static class DiagnosticResultSerializer
for (var i = 0; i < count; i++)
{
var documentId = DocumentId.ReadFrom(reader);
var diagnostics = serializer.ReadFrom(reader, project.GetDocument(documentId), cancellationToken);
var document = project.GetDocument(documentId);
var diagnostics = serializer.ReadFrom(reader, document, cancellationToken);
if (document?.SupportsDiagnostics() == false)
{
// drop diagnostics for document that doesn't support
// diagnostics
continue;
}
map.Add(documentId, GetOrDefault(diagnostics));
}
......
......@@ -7,6 +7,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Experimentation;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Shared.Options;
using Microsoft.CodeAnalysis.Workspaces.Diagnostics;
using Roslyn.Utilities;
......@@ -82,7 +83,7 @@ public async Task<DiagnosticAnalysisResult> GetAnalysisDataAsync(Project project
// loading data can be cancelled any time.
var serializer = new DiagnosticDataSerializer(_owner.AnalyzerVersion, lastResult.Version);
var builder = new Builder(project.Id, lastResult.Version, lastResult.DocumentIds);
var builder = new Builder(project, lastResult.Version, lastResult.DocumentIds);
foreach (var documentId in lastResult.DocumentIds)
{
......@@ -141,7 +142,7 @@ public async Task<DiagnosticAnalysisResult> GetAnalysisDataAsync(Document docume
// loading data can be cancelled any time.
var serializer = new DiagnosticDataSerializer(_owner.AnalyzerVersion, lastResult.Version);
var builder = new Builder(document.Project.Id, lastResult.Version);
var builder = new Builder(document.Project, lastResult.Version);
if (!await TryDeserializeDocumentAsync(serializer, document, builder, cancellationToken).ConfigureAwait(false))
{
......@@ -182,7 +183,7 @@ public async Task<DiagnosticAnalysisResult> GetProjectAnalysisDataAsync(Project
// loading data can be cancelled any time.
var serializer = new DiagnosticDataSerializer(_owner.AnalyzerVersion, lastResult.Version);
var builder = new Builder(project.Id, lastResult.Version);
var builder = new Builder(project, lastResult.Version);
if (!await TryDeserializeAsync(serializer, project, project.Id, _owner.NonLocalStateName, s_addOthers, builder, cancellationToken).ConfigureAwait(false))
{
......@@ -300,7 +301,7 @@ private async Task<DiagnosticAnalysisResult> LoadInitialAnalysisDataAsync(Projec
// loading data can be cancelled any time.
var version = await GetDiagnosticVersionAsync(project, cancellationToken).ConfigureAwait(false);
var serializer = new DiagnosticDataSerializer(_owner.AnalyzerVersion, version);
var builder = new Builder(project.Id, version);
var builder = new Builder(project, version);
foreach (var document in project.Documents)
{
......@@ -327,7 +328,7 @@ private async Task<DiagnosticAnalysisResult> LoadInitialAnalysisDataAsync(Docume
var version = await GetDiagnosticVersionAsync(project, cancellationToken).ConfigureAwait(false);
var serializer = new DiagnosticDataSerializer(_owner.AnalyzerVersion, version);
var builder = new Builder(project.Id, version);
var builder = new Builder(project, version);
if (!await TryDeserializeDocumentAsync(serializer, document, builder, cancellationToken).ConfigureAwait(false))
{
......@@ -342,7 +343,7 @@ private async Task<DiagnosticAnalysisResult> LoadInitialProjectAnalysisDataAsync
// loading data can be cancelled any time.
var version = await GetDiagnosticVersionAsync(project, cancellationToken).ConfigureAwait(false);
var serializer = new DiagnosticDataSerializer(_owner.AnalyzerVersion, version);
var builder = new Builder(project.Id, version);
var builder = new Builder(project, version);
if (!await TryDeserializeAsync(serializer, project, project.Id, _owner.NonLocalStateName, s_addOthers, builder, cancellationToken).ConfigureAwait(false))
{
......@@ -458,7 +459,7 @@ private bool IsEmpty(DiagnosticAnalysisResult result, DocumentId documentId)
// we have this builder to avoid allocating collections unnecessarily.
private class Builder
{
private readonly ProjectId _projectId;
private readonly Project _project;
private readonly VersionStamp _version;
private readonly ImmutableHashSet<DocumentId> _documentIds;
......@@ -467,9 +468,9 @@ private class Builder
private ImmutableDictionary<DocumentId, ImmutableArray<DiagnosticData>>.Builder _nonLocals;
private ImmutableArray<DiagnosticData> _others;
public Builder(ProjectId projectId, VersionStamp version, ImmutableHashSet<DocumentId> documentIds = null)
public Builder(Project project, VersionStamp version, ImmutableHashSet<DocumentId> documentIds = null)
{
_projectId = projectId;
_project = project;
_version = version;
_documentIds = documentIds;
}
......@@ -496,13 +497,18 @@ public void AddOthers(ProjectId unused, ImmutableArray<DiagnosticData> diagnosti
private void Add(ref ImmutableDictionary<DocumentId, ImmutableArray<DiagnosticData>>.Builder locals, DocumentId documentId, ImmutableArray<DiagnosticData> diagnostics)
{
if (_project.GetDocument(documentId)?.SupportsDiagnostics() == false)
{
return;
}
locals = locals ?? ImmutableDictionary.CreateBuilder<DocumentId, ImmutableArray<DiagnosticData>>();
locals.Add(documentId, diagnostics);
}
public DiagnosticAnalysisResult ToResult()
{
return DiagnosticAnalysisResult.CreateFromSerialization(_projectId, _version,
return DiagnosticAnalysisResult.CreateFromSerialization(_project, _version,
_syntaxLocals?.ToImmutable() ?? ImmutableDictionary<DocumentId, ImmutableArray<DiagnosticData>>.Empty,
_semanticLocals?.ToImmutable() ?? ImmutableDictionary<DocumentId, ImmutableArray<DiagnosticData>>.Empty,
_nonLocals?.ToImmutable() ?? ImmutableDictionary<DocumentId, ImmutableArray<DiagnosticData>>.Empty,
......
......@@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics.Telemetry;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.Remote;
using Microsoft.CodeAnalysis.Shared.Extensions;
......@@ -227,7 +228,7 @@ private static bool AnalysisEnabled(Document document)
{
// change it to check active file (or visible files), not open files if active file tracking is enabled.
// otherwise, use open file.
return document.IsOpen();
return document.IsOpen() && document.SupportsDiagnostics();
}
private IEnumerable<StateSet> GetStateSetsForFullSolutionAnalysis(IEnumerable<StateSet> stateSets, Project project)
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Host;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Workspaces.Diagnostics
......@@ -100,8 +102,9 @@ public static DiagnosticAnalysisResult CreateFromBuild(Project project, Immutabl
// so we put everything in as semantic local with default version. this lets us to replace those to live diagnostics when needed easily.
var version = VersionStamp.Default;
// filter out any document that doesn't support diagnostics
var group = diagnostics.GroupBy(d => d.DocumentId);
// filter out any document that doesn't support diagnostics.
// g.Key == null means diagnostics on the project which assigned to "others" error category
var group = diagnostics.GroupBy(d => d.DocumentId).Where(g => g.Key == null || project.GetDocument(g.Key).SupportsDiagnostics()).ToList();
var result = new DiagnosticAnalysisResult(
project.Id,
......@@ -117,7 +120,7 @@ public static DiagnosticAnalysisResult CreateFromBuild(Project project, Immutabl
}
public static DiagnosticAnalysisResult CreateFromSerialization(
ProjectId projectId,
Project project,
VersionStamp version,
ImmutableDictionary<DocumentId, ImmutableArray<DiagnosticData>> syntaxLocalMap,
ImmutableDictionary<DocumentId, ImmutableArray<DiagnosticData>> semanticLocalMap,
......@@ -125,8 +128,12 @@ public static DiagnosticAnalysisResult CreateFromBuild(Project project, Immutabl
ImmutableArray<DiagnosticData> others,
ImmutableHashSet<DocumentId> documentIds = null)
{
VerifyDocumentMap(project, syntaxLocalMap);
VerifyDocumentMap(project, semanticLocalMap);
VerifyDocumentMap(project, nonLocalMap);
return new DiagnosticAnalysisResult(
projectId,
project.Id,
version,
syntaxLocalMap,
semanticLocalMap,
......@@ -139,7 +146,7 @@ public static DiagnosticAnalysisResult CreateFromBuild(Project project, Immutabl
public static DiagnosticAnalysisResult CreateFromBuilder(DiagnosticAnalysisResultBuilder builder)
{
return CreateFromSerialization(
builder.Project.Id,
builder.Project,
builder.Version,
builder.SyntaxLocals,
builder.SemanticLocals,
......@@ -233,5 +240,14 @@ private ImmutableHashSet<DocumentId> CreateDocumentIds()
return ImmutableHashSet.CreateRange(documents);
}
[Conditional("DEBUG")]
private static void VerifyDocumentMap(Project project, ImmutableDictionary<DocumentId, ImmutableArray<DiagnosticData>> map)
{
foreach (var documentId in map.Keys)
{
Debug.Assert(project.GetDocument(documentId)?.SupportsDiagnostics() == true);
}
}
}
}
......@@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Host;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Workspaces.Diagnostics
......@@ -112,7 +113,7 @@ public void AddExternalSemanticDiagnostics(DocumentId documentId, IEnumerable<Di
private void AppendDiagnostics(ref Dictionary<DocumentId, List<DiagnosticData>> map, Document documentOpt, Diagnostic diagnostic)
{
if (documentOpt is null)
if (documentOpt?.SupportsDiagnostics() == false)
{
return;
}
......
......@@ -13,5 +13,15 @@ public static bool CanApplyChange(this TextDocumentState document)
{
return document?.Services.GetService<IDocumentOperationService>().CanApplyChange ?? false;
}
public static bool SupportsDiagnostics(this TextDocument document)
{
return document?.State.SupportsDiagnostics() ?? false;
}
public static bool SupportsDiagnostics(this TextDocumentState document)
{
return document?.Services.GetService<IDocumentOperationService>().SupportDiagnostics ?? false;
}
}
}
......@@ -13,5 +13,10 @@ internal interface IDocumentOperationService : IDocumentService
/// document version of <see cref="Workspace.CanApplyChange(ApplyChangesKind)"/>
/// </summary>
bool CanApplyChange { get; }
/// <summary>
/// indicates whether this document supports diagnostics or not
/// </summary>
bool SupportDiagnostics { get; }
}
}
......@@ -40,6 +40,7 @@ private class DocumentOperationService : IDocumentOperationService
// even support rename through IDynamicFileInfoProvider pattern once we address that in next
// iteration for razor. for now, we keep existing behavior
public bool CanApplyChange => true;
public bool SupportDiagnostics => true;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册