Consolidate file-level directive manipulation#53136
Consolidate file-level directive manipulation#53136jjonescz merged 7 commits intodotnet:release/10.0.3xxfrom
Conversation
| } | ||
| } | ||
|
|
||
| public static SourceFile RemoveDirectivesFromFile(ImmutableArray<CSharpDirective> directives, SourceFile sourceFile) |
There was a problem hiding this comment.
Moved these to VirtualProjectBuildingCommand because
- they now need to use the FileBasedAppSourceEditor which isn't available in this project,
- there is no need for these APIs to live in this shared project since they are not used by anyone but dotnet CLI (@tmat I assume you don't need these in dotnet-watch?).
There was a problem hiding this comment.
Pull request overview
This PR consolidates file-level directive (#:) manipulation logic by making both dotnet project convert and dotnet package add/remove use the same FileBasedAppSourceEditor machinery. The consolidation eliminates duplicate code and improves whitespace handling consistency across commands.
Changes:
- Moved directive removal logic from
VirtualProjectBuildertoVirtualProjectBuildingCommand, leveragingFileBasedAppSourceEditorfor consistent behavior - Refactored
WhiteSpaceInfostruct to distinguish between blank lines (BlankLineLength) and same-line whitespace (RestLength), enabling smarter whitespace removal - Fixed whitespace handling bugs where leading/trailing blank lines weren't removed correctly in certain scenarios
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/Cli/dotnet/Commands/Run/FileBasedAppSourceEditor.cs | Updated Remove method to use new BlankLineLength/RestLength properties for more precise blank line removal |
| src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs | Added RemoveDirectivesFromFile methods that use FileBasedAppSourceEditor for directive removal |
| src/Cli/dotnet/Commands/Project/Convert/ProjectConvertCommand.cs | Updated to call VirtualProjectBuildingCommand.RemoveDirectivesFromFile instead of VirtualProjectBuilder.RemoveDirectivesFromFile |
| src/Cli/Microsoft.DotNet.FileBasedPrograms/FileLevelDirectiveHelpers.cs | Refactored WhiteSpaceInfo to track blank lines separately from partial-line whitespace; updated whitespace calculation to exclude the directive's own span |
| src/Microsoft.DotNet.ProjectTools/VirtualProjectBuilder.cs | Removed RemoveDirectivesFromFile methods (consolidated into VirtualProjectBuildingCommand) |
| test/dotnet.Tests/CommandTests/Run/FileBasedAppSourceEditorTests.cs | Added tests for leading whitespace handling and whitespace outside lines |
| test/dotnet.Tests/CommandTests/Project/Convert/DotnetProjectConvertTests.cs | Updated test expectation and method call to reflect new whitespace handling behavior |
| src/Cli/Microsoft.DotNet.FileBasedPrograms/InternalAPI.Unshipped.txt | Updated API surface to reflect WhiteSpaceInfo property renames |
| public required WhiteSpaceInfo LeadingWhiteSpace { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Additional trailing whitespace not included in <see cref="Span"/>. |
There was a problem hiding this comment.
checking my understanding: #: directives cannot have trailing // comments or similar? Is that right?
There was a problem hiding this comment.
that's right, they cannot, everything on the same line as the directive is just its "content"
dotnet project convertused custom logic to remove#:directives which didn't handle whitespace as nicely as theFileBasedAppSourceEditor(used bydotnet package add/remove). This makes both commands use the same machinery.Furthermore, fixes some bugs in whitespace handling (discovered by the directive tests that now use the new machinery).