Skip to content

Commit e091728

Browse files
authored
Merge pull request #70669 from Cosifne/dev/shech/FilterDocumentsInSolution
Only change documents in solution
2 parents 34bb83a + 9295bc9 commit e091728

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎src/Features/Core/Portable/CodeRefactorings/SyncNamespace/AbstractChangeNamespaceService.cs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
using Microsoft.CodeAnalysis.AddImport;
1515
using Microsoft.CodeAnalysis.CodeCleanup;
1616
using Microsoft.CodeAnalysis.CodeGeneration;
17+
using Microsoft.CodeAnalysis.CodeRefactorings.SyncNamespace;
1718
using Microsoft.CodeAnalysis.Editing;
19+
using Microsoft.CodeAnalysis.ErrorReporting;
1820
using Microsoft.CodeAnalysis.FindSymbols;
1921
using Microsoft.CodeAnalysis.Formatting;
2022
using Microsoft.CodeAnalysis.Host;
@@ -470,7 +472,20 @@ private static SyntaxNode CreateImport(SyntaxGenerator syntaxGenerator, string n
470472
.ConfigureAwait(false);
471473
var solutionWithChangedNamespace = documentWithNewNamespace.Project.Solution;
472474

473-
var refLocationGroups = refLocationsInOtherDocuments.GroupBy(loc => loc.Document.Id);
475+
var refLocationsInSolution = refLocationsInOtherDocuments
476+
.Where(loc => solutionWithChangedNamespace.ContainsDocument(loc.Document.Id))
477+
.ToImmutableArray();
478+
479+
if (refLocationsInSolution.Length != refLocationsInOtherDocuments.Count)
480+
{
481+
// We have received feedback indicate some documents are not in the solution.
482+
// Report this as non-fatal error if this happens.
483+
FatalError.ReportNonFatalError(
484+
new SyncNamespaceDocumentsNotInSolutionException(refLocationsInOtherDocuments
485+
.Where(loc => !solutionWithChangedNamespace.ContainsDocument(loc.Document.Id)).Distinct().SelectAsArray(loc => loc.Document.Id)));
486+
}
487+
488+
var refLocationGroups = refLocationsInSolution.GroupBy(loc => loc.Document.Id);
474489

475490
var fixedDocuments = await Task.WhenAll(
476491
refLocationGroups.Select(refInOneDocument =>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Immutable;
7+
using Microsoft.CodeAnalysis.PooledObjects;
8+
9+
namespace Microsoft.CodeAnalysis.CodeRefactorings.SyncNamespace
10+
{
11+
internal class SyncNamespaceDocumentsNotInSolutionException(ImmutableArray<DocumentId> documentIds) : Exception
12+
{
13+
private readonly ImmutableArray<DocumentId> _documentIds = documentIds;
14+
15+
public override string ToString()
16+
{
17+
using var _ = PooledStringBuilder.GetInstance(out var builder);
18+
foreach (var documentId in _documentIds)
19+
{
20+
builder.AppendLine($"{documentId.GetDebuggerDisplay()}, IsSourceGeneratedDocument: {documentId.IsSourceGenerated}");
21+
}
22+
23+
return builder.ToString();
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)