public static EnumMemberDeclarationSyntax Apply(EnumMemberDeclarationSyntax node, Func <EnumMemberDeclarationSyntax, SyntaxTrivia, EnumMemberDeclarationSyntax> builder) { var leadingTrivia = node.GetLeadingTrivia(); var trailingTrivia = node.GetTrailingTrivia(); node = node.WithoutLeadingTrivia(); node = node.WithoutTrailingTrivia(); var wp = leadingTrivia.FirstOrDefault(w => w.Kind() == SyntaxKind.WhitespaceTrivia); node = builder?.Invoke(node, wp); node = node.WithLeadingTrivia(leadingTrivia); node = node.WithTrailingTrivia(trailingTrivia); return(node); }
private Document Delete(Document document, EnumMemberDeclarationSyntax node) { var enumDeclaration = (EnumDeclarationSyntax)node.Parent; var members = enumDeclaration.Members; var newMembers = members.Remove(node); var newEnumDeclaration = enumDeclaration.WithMembers(newMembers); // If we're removing the last enum member, we may need to move any trailing trivia // to the enum member that comes before it. var memberIndex = members.IndexOf(node); if (memberIndex == members.Count - 1 && newMembers.Count > 0) { var trailingTrivia = node.GetTrailingTrivia(); var lastMember = newEnumDeclaration.Members.Last(); newEnumDeclaration = newEnumDeclaration.ReplaceNode(lastMember, lastMember.WithTrailingTrivia(trailingTrivia)); } return document.ReplaceNodeAsync(enumDeclaration, newEnumDeclaration, CancellationToken.None) .WaitAndGetResult_CodeModel(CancellationToken.None); }
public override SyntaxNode VisitEnumMemberDeclaration(EnumMemberDeclarationSyntax node) { String name = node.Identifier.ValueText; String replaceName = ""; if (SharedContainer.Instance.nameMap.ContainsKey(name)) { replaceName = SharedContainer.Instance.nameMap[name]; } else { replaceName = SharedContainer.Instance.RandomString(SharedContainer.Instance.nameMap.Count() + 1); SharedContainer.Instance.nameMap[name] = replaceName; } EnumMemberDeclarationSyntax newSyntax = node.WithIdentifier(Identifier(replaceName)).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); return(node.ReplaceNode(node, newSyntax)); }