templater: inline build_method_call()

I don't think this function will grow to unmaintainable size, and I'm going
to add global function dispatcher there.
This commit is contained in:
Yuya Nishihara 2024-03-14 20:33:10 +09:00
parent f9f740b263
commit 5ec56fb3e9

View file

@ -18,8 +18,8 @@ use itertools::Itertools as _;
use jj_lib::backend::{Signature, Timestamp}; use jj_lib::backend::{Signature, Timestamp};
use crate::template_parser::{ use crate::template_parser::{
self, BinaryOp, ExpressionKind, ExpressionNode, FunctionCallNode, MethodCallNode, self, BinaryOp, ExpressionKind, ExpressionNode, FunctionCallNode, TemplateAliasesMap,
TemplateAliasesMap, TemplateParseError, TemplateParseErrorKind, TemplateParseResult, UnaryOp, TemplateParseError, TemplateParseErrorKind, TemplateParseResult, UnaryOp,
}; };
use crate::templater::{ use crate::templater::{
ConcatTemplate, ConditionalTemplate, IntoTemplate, LabelTemplate, ListPropertyTemplate, ConcatTemplate, ConditionalTemplate, IntoTemplate, LabelTemplate, ListPropertyTemplate,
@ -515,18 +515,6 @@ fn build_binary_operation<'a, L: TemplateLanguage<'a> + ?Sized>(
Ok(Expression::unlabeled(property)) Ok(Expression::unlabeled(property))
} }
fn build_method_call<'a, L: TemplateLanguage<'a> + ?Sized>(
language: &L,
build_ctx: &BuildContext<L::Property>,
method: &MethodCallNode,
) -> TemplateParseResult<Expression<L::Property>> {
let mut expression = build_expression(language, build_ctx, &method.object)?;
expression.property =
language.build_method(build_ctx, expression.property, &method.function)?;
expression.labels.push(method.function.name.to_owned());
Ok(expression)
}
fn builtin_string_methods<'a, L: TemplateLanguage<'a> + ?Sized>( fn builtin_string_methods<'a, L: TemplateLanguage<'a> + ?Sized>(
) -> TemplateBuildMethodFnMap<'a, L, String> { ) -> TemplateBuildMethodFnMap<'a, L, String> {
// Not using maplit::hashmap!{} or custom declarative macro here because // Not using maplit::hashmap!{} or custom declarative macro here because
@ -1047,7 +1035,13 @@ pub fn build_expression<'a, L: TemplateLanguage<'a> + ?Sized>(
ExpressionKind::FunctionCall(function) => { ExpressionKind::FunctionCall(function) => {
build_global_function(language, build_ctx, function) build_global_function(language, build_ctx, function)
} }
ExpressionKind::MethodCall(method) => build_method_call(language, build_ctx, method), ExpressionKind::MethodCall(method) => {
let mut expression = build_expression(language, build_ctx, &method.object)?;
expression.property =
language.build_method(build_ctx, expression.property, &method.function)?;
expression.labels.push(method.function.name.to_owned());
Ok(expression)
}
ExpressionKind::Lambda(_) => Err(TemplateParseError::unexpected_expression( ExpressionKind::Lambda(_) => Err(TemplateParseError::unexpected_expression(
"Lambda cannot be defined here", "Lambda cannot be defined here",
node.span, node.span,