internal static void MoveOnPropertyChangedInside(this DocumentEditor editor, IfStatementSyntax ifTrySet, ExpressionStatementSyntax onPropertyChanged) { editor.RemoveNode(onPropertyChanged); editor.AddOnPropertyChanged(ifTrySet, OnPropertyChanged()); ExpressionStatementSyntax OnPropertyChanged() { if (onPropertyChanged.HasLeadingTrivia && onPropertyChanged.GetLeadingTrivia() is { } leadingTrivia&& leadingTrivia.TryFirst(out var first) && first.IsKind(SyntaxKind.EndOfLineTrivia)) { onPropertyChanged = onPropertyChanged.WithLeadingTrivia(leadingTrivia.Remove(first)); } return(onPropertyChanged.WithAdditionalAnnotations(Formatter.Annotation)); } }
internal static void AddOnPropertyChanged(this DocumentEditor editor, IfStatementSyntax ifTrySet, ExpressionStatementSyntax onPropertyChanged) { switch (ifTrySet.Statement) { case BlockSyntax block: editor.AddOnPropertyChanged(block, onPropertyChanged, 0); break; case ExpressionStatementSyntax expressionStatement: _ = editor.ReplaceNode( ifTrySet, x => x.WithStatement(SyntaxFactory.Block(expressionStatement, onPropertyChanged))); break; case EmptyStatementSyntax _: case null: _ = editor.ReplaceNode( ifTrySet, x => x.WithStatement(SyntaxFactory.Block(onPropertyChanged))); break; } }