private static string SafeParameterName(IParameterSymbol parameter, IdentifierNameSyntax context) { if (context.TryFirstAncestor <MemberDeclarationSyntax>(out var ancestor)) { using (var walker = IdentifierTokenWalker.Borrow(ancestor)) { var name = parameter.Name; while (walker.TryFind(name, out _)) { name += "_"; } return(name); } } return(parameter.Name); }
public static void Test() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { public C() { var i = 1; } } }"); var node = syntaxTree.FindTypeDeclaration("C"); using var walker = IdentifierTokenWalker.Borrow(node); CollectionAssert.AreEqual(new[] { "C", "C", "var", "i" }, walker.IdentifierTokens.Select(x => x.ValueText)); Assert.AreEqual(true, walker.TryFind("i", out var match)); Assert.AreEqual("i", match.ValueText); Assert.AreEqual(false, walker.TryFind("missing", out _)); }