Skip to content

Commit f3613a3

Browse files
Youssef1313Sergio0694
authored andcommitted
Trivia handling
1 parent 53f3195 commit f3613a3

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.CodeFixers/UseGeneratedDependencyPropertyOnManualPropertyCodeFixer.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,39 @@ private static void ConvertToPartialProperty(
298298

299299
syntaxEditor.ReplaceNode(propertyDeclaration, updatedPropertyDeclaration);
300300

301+
// Special handling for the leading trivia of members following the field declaration we are about to remove.
302+
// There is an edge case that can happen when a type declaration is as follows:
303+
//
304+
// class ContainingType
305+
// {
306+
// public static readonly DependencyProperty NameProperty = ...;
307+
//
308+
// public void SomeOtherMember() { }
309+
//
310+
// public string? Name { ... }
311+
// }
312+
//
313+
// In this case, just removing the target field for the dependency property being rewritten (that is, 'NameProperty')
314+
// will cause an extra blank line to be left after the edits, right above the member immediately following the field.
315+
// To work around this, we look for such a member and check its trivia, and then manually remove a leading blank line.
316+
if (fieldDeclaration.Parent is TypeDeclarationSyntax fieldParentTypeDeclaration)
317+
{
318+
int fieldDeclarationIndex = fieldParentTypeDeclaration.Members.IndexOf(fieldDeclaration);
319+
320+
// Check whether there is a member immediatley following the field
321+
if (fieldDeclarationIndex >= 0 && fieldDeclarationIndex < fieldParentTypeDeclaration.Members.Count - 1)
322+
{
323+
MemberDeclarationSyntax nextMember = fieldParentTypeDeclaration.Members[fieldDeclarationIndex + 1];
324+
SyntaxTriviaList leadingTrivia = nextMember.GetLeadingTrivia();
325+
326+
// Check whether this member has a first leading trivia that's just a blank line: we want to remove this one
327+
if (leadingTrivia.Count > 0 && leadingTrivia[0].IsKind(SyntaxKind.EndOfLineTrivia))
328+
{
329+
syntaxEditor.ReplaceNode(nextMember, (nextMember, _) => nextMember.WithLeadingTrivia(leadingTrivia.RemoveAt(0)));
330+
}
331+
}
332+
}
333+
301334
// Also remove the field declaration (it'll be generated now)
302335
syntaxEditor.RemoveNode(fieldDeclaration);
303336

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests/Test_UseGeneratedDependencyPropertyOnManualPropertyCodeFixer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,6 @@ public string? [|Name2|]
483483
}
484484
""";
485485

486-
// There is an extra leading blank line here for now, likely a 'SyntaxEditor' bug
487486
const string @fixed = """
488487
using CommunityToolkit.WinUI;
489488
using Windows.UI.Xaml;
@@ -495,7 +494,6 @@ namespace MyApp;
495494
496495
public partial class MyControl : Control
497496
{
498-
499497
/// <summary>This is another member</summary>
500498
public int Blah => 42;
501499

0 commit comments

Comments
 (0)