// 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 Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; namespace CommunityToolkit.Mvvm.SourceGenerators.Extensions; /// /// Extension methods for the type. /// internal static class MemberDeclarationSyntaxExtensions { /// /// Replaces a specific modifier. /// /// The input instance. /// The target modifier kind to replace. /// The new modifier kind to add or replace. /// A instance with the target modifier. public static MemberDeclarationSyntax ReplaceModifier(this MemberDeclarationSyntax memberDeclaration, SyntaxKind oldKind, SyntaxKind newKind) { int index = memberDeclaration.Modifiers.IndexOf(oldKind); if (index != -1) { return memberDeclaration.WithModifiers(memberDeclaration.Modifiers.Replace(memberDeclaration.Modifiers[index], Token(newKind))); } return memberDeclaration; } /// /// Removes a specific modifier. /// /// The input instance. /// The modifier kind to remove. /// A instance without the specified modifier. public static MemberDeclarationSyntax RemoveModifier(this MemberDeclarationSyntax memberDeclaration, SyntaxKind kind) { int index = memberDeclaration.Modifiers.IndexOf(kind); if (index != -1) { return memberDeclaration.WithModifiers(memberDeclaration.Modifiers.RemoveAt(index)); } return memberDeclaration; } }