cli: add helper method that resolves user symbols in revset expression

revset_util::evaluate() is inlined as there's only one caller.
This commit is contained in:
Yuya Nishihara 2024-11-05 20:26:36 +09:00
parent 62e4943c04
commit b0eb7764d4
2 changed files with 21 additions and 21 deletions

View file

@ -847,15 +847,18 @@ fn evaluate_revset_expression<'repo>(
span: pest::Span<'_>, span: pest::Span<'_>,
expression: &UserRevsetExpression, expression: &UserRevsetExpression,
) -> Result<Box<dyn Revset + 'repo>, TemplateParseError> { ) -> Result<Box<dyn Revset + 'repo>, TemplateParseError> {
let make_error = || TemplateParseError::expression("Failed to evaluate revset", span);
let repo = language.repo;
let symbol_resolver = revset_util::default_symbol_resolver( let symbol_resolver = revset_util::default_symbol_resolver(
language.repo, repo,
language.revset_parse_context.symbol_resolvers(), language.revset_parse_context.symbol_resolvers(),
language.id_prefix_context, language.id_prefix_context,
); );
let revset = let revset = expression
revset_util::evaluate(language.repo, &symbol_resolver, expression).map_err(|err| { .resolve_user_expression(repo, &symbol_resolver)
TemplateParseError::expression("Failed to evaluate revset", span).with_source(err) .map_err(|err| make_error().with_source(err))?
})?; .evaluate(repo)
.map_err(|err| make_error().with_source(err))?;
Ok(revset) Ok(revset)
} }

View file

@ -25,6 +25,7 @@ use jj_lib::id_prefix::IdPrefixContext;
use jj_lib::repo::Repo; use jj_lib::repo::Repo;
use jj_lib::revset; use jj_lib::revset;
use jj_lib::revset::DefaultSymbolResolver; use jj_lib::revset::DefaultSymbolResolver;
use jj_lib::revset::ResolvedRevsetExpression;
use jj_lib::revset::Revset; use jj_lib::revset::Revset;
use jj_lib::revset::RevsetAliasesMap; use jj_lib::revset::RevsetAliasesMap;
use jj_lib::revset::RevsetDiagnostics; use jj_lib::revset::RevsetDiagnostics;
@ -91,14 +92,23 @@ impl<'repo> RevsetExpressionEvaluator<'repo> {
self.expression = self.expression.intersection(other); self.expression = self.expression.intersection(other);
} }
/// Evaluates the expression. /// Resolves user symbols in the expression, returns new expression.
pub fn evaluate(&self) -> Result<Box<dyn Revset + 'repo>, UserRevsetEvaluationError> { pub fn resolve(&self) -> Result<Rc<ResolvedRevsetExpression>, RevsetResolutionError> {
let symbol_resolver = default_symbol_resolver( let symbol_resolver = default_symbol_resolver(
self.repo, self.repo,
self.extensions.symbol_resolvers(), self.extensions.symbol_resolvers(),
self.id_prefix_context, self.id_prefix_context,
); );
evaluate(self.repo, &symbol_resolver, &self.expression) self.expression
.resolve_user_expression(self.repo, &symbol_resolver)
}
/// Evaluates the expression.
pub fn evaluate(&self) -> Result<Box<dyn Revset + 'repo>, UserRevsetEvaluationError> {
self.resolve()
.map_err(UserRevsetEvaluationError::Resolution)?
.evaluate(self.repo)
.map_err(UserRevsetEvaluationError::Evaluation)
} }
/// Evaluates the expression to an iterator over commit ids. Entries are /// Evaluates the expression to an iterator over commit ids. Entries are
@ -181,19 +191,6 @@ pub fn load_revset_aliases(
Ok(aliases_map) Ok(aliases_map)
} }
pub fn evaluate<'a>(
repo: &'a dyn Repo,
symbol_resolver: &DefaultSymbolResolver,
expression: &UserRevsetExpression,
) -> Result<Box<dyn Revset + 'a>, UserRevsetEvaluationError> {
let resolved = expression
.resolve_user_expression(repo, symbol_resolver)
.map_err(UserRevsetEvaluationError::Resolution)?;
resolved
.evaluate(repo)
.map_err(UserRevsetEvaluationError::Evaluation)
}
/// Wraps the given `IdPrefixContext` in `SymbolResolver` to be passed in to /// Wraps the given `IdPrefixContext` in `SymbolResolver` to be passed in to
/// `evaluate()`. /// `evaluate()`.
pub fn default_symbol_resolver<'a>( pub fn default_symbol_resolver<'a>(