Skip to content

Commit

Permalink
Merge pull request #70669 from Cosifne/dev/shech/FilterDocumentsInSol…
Browse files Browse the repository at this point in the history
…ution

Only change documents in solution
  • Loading branch information
Cosifne authored Nov 8, 2023
2 parents 34bb83a + 9295bc9 commit e091728
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
using Microsoft.CodeAnalysis.AddImport;
using Microsoft.CodeAnalysis.CodeCleanup;
using Microsoft.CodeAnalysis.CodeGeneration;
using Microsoft.CodeAnalysis.CodeRefactorings.SyncNamespace;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host;
Expand Down Expand Up @@ -470,7 +472,20 @@ private static SyntaxNode CreateImport(SyntaxGenerator syntaxGenerator, string n
.ConfigureAwait(false);
var solutionWithChangedNamespace = documentWithNewNamespace.Project.Solution;

var refLocationGroups = refLocationsInOtherDocuments.GroupBy(loc => loc.Document.Id);
var refLocationsInSolution = refLocationsInOtherDocuments
.Where(loc => solutionWithChangedNamespace.ContainsDocument(loc.Document.Id))
.ToImmutableArray();

if (refLocationsInSolution.Length != refLocationsInOtherDocuments.Count)
{
// We have received feedback indicate some documents are not in the solution.
// Report this as non-fatal error if this happens.
FatalError.ReportNonFatalError(
new SyncNamespaceDocumentsNotInSolutionException(refLocationsInOtherDocuments
.Where(loc => !solutionWithChangedNamespace.ContainsDocument(loc.Document.Id)).Distinct().SelectAsArray(loc => loc.Document.Id)));
}

var refLocationGroups = refLocationsInSolution.GroupBy(loc => loc.Document.Id);

var fixedDocuments = await Task.WhenAll(
refLocationGroups.Select(refInOneDocument =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.PooledObjects;

namespace Microsoft.CodeAnalysis.CodeRefactorings.SyncNamespace
{
internal class SyncNamespaceDocumentsNotInSolutionException(ImmutableArray<DocumentId> documentIds) : Exception
{
private readonly ImmutableArray<DocumentId> _documentIds = documentIds;

public override string ToString()
{
using var _ = PooledStringBuilder.GetInstance(out var builder);
foreach (var documentId in _documentIds)
{
builder.AppendLine($"{documentId.GetDebuggerDisplay()}, IsSourceGeneratedDocument: {documentId.IsSourceGenerated}");
}

return builder.ToString();
}
}
}

0 comments on commit e091728

Please sign in to comment.