-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Update filenames in file banners when moving a type to a new file. #76270
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// 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 System.IO; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.LanguageService; | ||
using Microsoft.CodeAnalysis.Shared.Extensions; | ||
using Microsoft.CodeAnalysis.Shared.Utilities; | ||
|
||
namespace Microsoft.CodeAnalysis.AddFileBanner; | ||
|
||
internal static class AddFileBannerHelpers | ||
{ | ||
public static async Task<Document> CopyBannerAsync( | ||
Document destinationDocument, | ||
string? destinationFilePath, | ||
Document sourceDocument, | ||
CancellationToken cancellationToken) | ||
{ | ||
var bannerService = destinationDocument.GetRequiredLanguageService<IFileBannerFactsService>(); | ||
|
||
var fromRoot = await sourceDocument.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
var sourceBanner = bannerService.GetFileBanner(fromRoot); | ||
|
||
sourceBanner = UpdateEmbeddedFileNames( | ||
sourceDocument, destinationFilePath, sourceBanner, bannerService.CreateTrivia); | ||
|
||
var destinationRoot = await destinationDocument.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
var destinationBanner = bannerService.GetFileBanner(destinationRoot); | ||
|
||
var firstToken = destinationRoot.GetFirstToken(); | ||
var newRoot = destinationRoot.ReplaceToken( | ||
firstToken, | ||
firstToken.WithLeadingTrivia( | ||
sourceBanner.Concat(firstToken.LeadingTrivia.Skip(destinationBanner.Length)))); | ||
return destinationDocument.WithSyntaxRoot(newRoot); | ||
} | ||
|
||
/// <summary> | ||
/// Looks at <paramref name="banner"/> to see if it contains the name of <paramref name="sourceDocument"/> | ||
/// in it. If so, those names will be replaced with <paramref name="destinationFilePath"/>. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
/// </summary> | ||
private static ImmutableArray<SyntaxTrivia> UpdateEmbeddedFileNames( | ||
Document sourceDocument, | ||
string? destinationFilePath, | ||
ImmutableArray<SyntaxTrivia> banner, | ||
Func<SyntaxTrivia, string, SyntaxTrivia> createTrivia) | ||
{ | ||
var sourceName = IOUtilities.PerformIO(() => Path.GetFileName(sourceDocument.FilePath)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll do a pass later to address these |
||
var destinationName = IOUtilities.PerformIO(() => Path.GetFileName(destinationFilePath)); | ||
if (string.IsNullOrEmpty(sourceName) || string.IsNullOrEmpty(destinationName)) | ||
return banner; | ||
|
||
var result = new FixedSizeArrayBuilder<SyntaxTrivia>(banner.Length); | ||
foreach (var trivia in banner) | ||
{ | ||
var updated = createTrivia(trivia, trivia.ToFullString().Replace(sourceName, destinationName)); | ||
result.Add(updated); | ||
} | ||
|
||
return result.MoveToImmutable(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.AddFileBanner; | ||
using Microsoft.CodeAnalysis.Editing; | ||
using Microsoft.CodeAnalysis.Formatting; | ||
using Microsoft.CodeAnalysis.LanguageService; | ||
|
@@ -27,7 +28,6 @@ private sealed class MoveTypeEditor( | |
string fileName, | ||
CancellationToken cancellationToken) : Editor(service, state, fileName, cancellationToken) | ||
{ | ||
|
||
/// <summary> | ||
/// Given a document and a type contained in it, moves the type | ||
/// out to its own document. The new document's name typically | ||
|
@@ -111,8 +111,7 @@ private async Task<Solution> RemoveUnnecessaryImportsAsync( | |
private async Task<Document> AddNewDocumentWithSingleTypeDeclarationAsync(DocumentId newDocumentId) | ||
{ | ||
var document = SemanticDocument.Document; | ||
Debug.Assert(document.Name != FileName, | ||
$"New document name is same as old document name:{FileName}"); | ||
Debug.Assert(document.Name != FileName, $"New document name is same as old document name:{FileName}"); | ||
|
||
var root = SemanticDocument.Root; | ||
var projectToBeUpdated = document.Project; | ||
|
@@ -146,7 +145,10 @@ private async Task<Document> AddNewDocumentWithSingleTypeDeclarationAsync(Docume | |
// get the updated document, give it the minimal set of imports that the type | ||
// inside it needs. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this comment confuses me, it doesn't look like imports are handled in this method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. Seems incorrect. Was already like that, so I'll move to another pr in this space to cleanup. |
||
var newDocument = solutionWithNewDocument.GetRequiredDocument(newDocumentId); | ||
return newDocument; | ||
var newDocumentWithUpdatedBanner = await AddFileBannerHelpers.CopyBannerAsync( | ||
newDocument, FileName, document, this.CancellationToken).ConfigureAwait(false); | ||
|
||
return newDocumentWithUpdatedBanner; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -194,6 +196,7 @@ private async Task<Solution> RemoveTypeFromSourceDocumentAsync(Document sourceDo | |
documentEditor.RemoveNode(State.TypeNode, SyntaxRemoveOptions.KeepUnbalancedDirectives); | ||
|
||
var updatedDocument = documentEditor.GetChangedDocument(); | ||
updatedDocument = await AddFileBannerHelpers.CopyBannerAsync(updatedDocument, sourceDocument.FilePath, sourceDocument, this.CancellationToken).ConfigureAwait(false); | ||
|
||
return updatedDocument.Project.Solution; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved to a common location so it can be used in MoveType.