TreeUtils { @CheckForNull public static RegularArgument argumentByKeyword(String keyword, List<Argument> arguments) { for (int i = 0; i < arguments.size(); i++) { Argument argument = arguments.get(i); if (hasKeyword(argument, keyword)) { return ((RegularArgument) argument); } } return null; } private  TreeUtils(); @CheckForNull static Tree firstAncestor(Tree tree, Predicate<Tree> predicate); @CheckForNull static Tree firstAncestorOfKind(Tree tree, Kind... kinds); static List<Token> tokens(Tree tree); static List<Token> nonWhitespaceTokens(Tree tree); static boolean hasDescendant(Tree tree, Predicate<Tree> predicate); static Stream<Expression> flattenTuples(Expression expression); static Optional<Symbol> getSymbolFromTree(@Nullable Tree tree); @CheckForNull static ClassSymbol getClassSymbolFromDef(@Nullable ClassDef classDef); @CheckForNull static FunctionSymbol getFunctionSymbolFromDef(@Nullable FunctionDef functionDef); static List<Parameter> nonTupleParameters(FunctionDef functionDef); static List<Parameter> positionalParameters(FunctionDef functionDef); static List<FunctionDef> topLevelFunctionDefs(ClassDef classDef); @CheckForNull static RegularArgument argumentByKeyword(String keyword, List<Argument> arguments); @CheckForNull static RegularArgument nthArgumentOrKeyword(int argPosition, String keyword, List<Argument> arguments); static boolean isBooleanLiteral(Tree tree); static String nameFromExpression(Expression expression); @CheckForNull static LocationInFile locationInFile(Tree tree, @Nullable String fileId);  }
@Test public void test_argumentByKeyword() { FileInput fileInput = PythonTestUtils.parse( "def foo(p0, p1, p2): ...", "foo(p1 = 1, p2 = 2)" ); CallExpression callExpr = PythonTestUtils.getLastDescendant(fileInput, tree -> tree.is(Kind.CALL_EXPR)); RegularArgument p1 = TreeUtils.argumentByKeyword("p1", callExpr.arguments()); assertThat(p1.expression().is(Kind.NUMERIC_LITERAL)).isTrue(); assertThat(((NumericLiteral) p1.expression()).valueAsLong()).isEqualTo(1); RegularArgument p2 = TreeUtils.argumentByKeyword("p2", callExpr.arguments()); assertThat(p2.expression().is(Kind.NUMERIC_LITERAL)).isTrue(); assertThat(((NumericLiteral) p2.expression()).valueAsLong()).isEqualTo(2); RegularArgument p3 = TreeUtils.argumentByKeyword("p3", callExpr.arguments()); assertThat(p3).isNull(); }