提交 386162ea 编写于 作者: K Kevin Pilch-Bisson

Merge pull request #10049 from jramsay/ContainedDocumentSupportsSyntaxTreeFuture

Filter GetRelatedDocumentIds and GetLinkedDocumentIds by language to resolve NullReferenceExceptions in Salsa ContainedDocument scenarios
// 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;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.SolutionCrawler;
......@@ -16,6 +17,17 @@ public AbstractRoslynTableDataSource(Workspace workspace) : base(workspace)
ConnectToSolutionCrawlerService(workspace);
}
protected ImmutableArray<DocumentId> GetDocumentsWithSameFilePath(Solution solution, DocumentId documentId)
{
var document = solution.GetDocument(documentId);
if (document == null)
{
return ImmutableArray<DocumentId>.Empty;
}
return solution.GetDocumentIdsWithFilePath(document.FilePath);
}
private void ConnectToSolutionCrawlerService(Workspace workspace)
{
var crawlerService = workspace.Services.GetService<ISolutionCrawlerService>();
......
......@@ -111,7 +111,7 @@ private bool CheckAggregateKey(AggregatedKey key, DiagnosticsUpdatedArgs args)
return true;
}
var documents = args.Solution.GetRelatedDocumentIds(args.DocumentId);
var documents = GetDocumentsWithSameFilePath(args.Solution, args.DocumentId);
return key.DocumentIds == documents;
}
......@@ -129,7 +129,7 @@ private object CreateAggregationKey(object data)
return GetItemKey(data);
}
var documents = args.Solution.GetRelatedDocumentIds(args.DocumentId);
var documents = GetDocumentsWithSameFilePath(args.Solution, args.DocumentId);
return new AggregatedKey(documents, liveArgsId.Analyzer, liveArgsId.Kind);
}
......
......@@ -122,7 +122,7 @@ private bool CheckAggregateKey(ImmutableArray<DocumentId> key, TodoItemsUpdatedA
return true;
}
var documents = args.Solution.GetRelatedDocumentIds(args.DocumentId);
var documents = GetDocumentsWithSameFilePath(args.Solution, args.DocumentId);
return key == documents;
}
......@@ -134,7 +134,7 @@ private object CreateAggregationKey(object data)
return GetItemKey(data);
}
return args.Solution.GetRelatedDocumentIds(args.DocumentId);
return GetDocumentsWithSameFilePath(args.Solution, args.DocumentId);
}
public override ImmutableArray<TableItem<TodoItem>> Deduplicate(IEnumerable<IList<TableItem<TodoItem>>> groupedItems)
......
// 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;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Shared.Extensions
{
......@@ -61,5 +60,17 @@ public static Solution WithTextDocumentText(this Solution solution, DocumentId d
return solution.WithAdditionalDocumentText(documentId, text, mode);
}
}
public static IEnumerable<DocumentId> FilterDocumentIdsByLanguage(this Solution solution, ImmutableArray<DocumentId> documentIds, string language)
{
foreach (var documentId in documentIds)
{
var document = solution.GetDocument(documentId);
if (document != null && document.Project.Language == language)
{
yield return documentId;
}
}
}
}
}
......@@ -9,6 +9,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
......@@ -386,7 +387,8 @@ public async Task<IEnumerable<TextChange>> GetTextChangesAsync(Document oldDocum
public ImmutableArray<DocumentId> GetLinkedDocumentIds()
{
var documentIdsWithPath = this.Project.Solution.GetDocumentIdsWithFilePath(this.FilePath);
return documentIdsWithPath.Remove(this.Id);
var filteredDocumentIds = this.Project.Solution.FilterDocumentIdsByLanguage(documentIdsWithPath, this.Project.Language).ToImmutableArray();
return filteredDocumentIds.Remove(this.Id);
}
/// <summary>
......
......@@ -9,6 +9,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Collections.Immutable;
using Roslyn.Utilities;
......@@ -1836,7 +1837,8 @@ internal ImmutableArray<DocumentId> GetRelatedDocumentIds(DocumentId documentId)
return ImmutableArray.Create<DocumentId>(documentId);
}
return this.GetDocumentIdsWithFilePath(filePath);
var documentIds = this.GetDocumentIdsWithFilePath(filePath);
return this.FilterDocumentIdsByLanguage(documentIds, projectState.ProjectInfo.Language).ToImmutableArray();
}
/// <summary>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册