Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

templater: migrate core methods to table lookup, add typo hints #3150

Merged
merged 6 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ rpassword = { workspace = true }
scm-record = { workspace = true }
serde = { workspace = true }
slab = { workspace = true }
strsim = { workspace = true }
tempfile = { workspace = true }
textwrap = { workspace = true }
thiserror = { workspace = true }
Expand Down
23 changes: 17 additions & 6 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ use crate::git_util::{
is_colocated_git_workspace, print_failed_git_export, print_git_import_stats,
};
use crate::merge_tools::{ConflictResolveError, DiffEditError, DiffGenerateError};
use crate::template_parser::{TemplateAliasesMap, TemplateParseError};
use crate::template_parser::{TemplateAliasesMap, TemplateParseError, TemplateParseErrorKind};
use crate::templater::Template;
use crate::ui::{ColorChoice, Ui};
use crate::{commit_templater, text_util};
Expand Down Expand Up @@ -405,9 +405,10 @@ impl From<RevsetEvaluationError> for CommandError {

impl From<RevsetParseError> for CommandError {
fn from(err: RevsetParseError) -> Self {
let message = iter::successors(Some(&err), |e| e.origin()).join("\n");
// Only for the top-level error as we can't attach hint to inner errors
let hint = match err.kind() {
let err_chain = iter::successors(Some(&err), |e| e.origin());
let message = err_chain.clone().join("\n");
// Only for the bottom error, which is usually the root cause
let hint = match err_chain.last().unwrap().kind() {
RevsetParseErrorKind::NotPrefixOperator {
op: _,
similar_op,
Expand Down Expand Up @@ -452,8 +453,18 @@ impl From<RevsetResolutionError> for CommandError {

impl From<TemplateParseError> for CommandError {
fn from(err: TemplateParseError) -> Self {
let message = iter::successors(Some(&err), |e| e.origin()).join("\n");
user_error(format!("Failed to parse template: {message}"))
let err_chain = iter::successors(Some(&err), |e| e.origin());
let message = err_chain.clone().join("\n");
// Only for the bottom error, which is usually the root cause
let hint = match err_chain.last().unwrap().kind() {
TemplateParseErrorKind::NoSuchKeyword { candidates, .. }
| TemplateParseErrorKind::NoSuchFunction { candidates, .. }
| TemplateParseErrorKind::NoSuchMethod { candidates, .. } => {
format_similarity_hint(candidates)
}
_ => None,
};
user_error_with_hint_opt(format!("Failed to parse template: {message}"), hint)
}
}

Expand Down
11 changes: 7 additions & 4 deletions cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use once_cell::unsync::OnceCell;

use crate::formatter::Formatter;
use crate::template_builder::{
self, BuildContext, CoreTemplatePropertyKind, IntoTemplateProperty, TemplateBuildMethodFnMap,
TemplateLanguage,
self, BuildContext, CoreTemplateBuildFnTable, CoreTemplatePropertyKind, IntoTemplateProperty,
TemplateBuildMethodFnMap, TemplateLanguage,
};
use crate::template_parser::{self, FunctionCallNode, TemplateAliasesMap, TemplateParseResult};
use crate::templater::{
Expand Down Expand Up @@ -67,7 +67,8 @@ impl<'repo> TemplateLanguage<'repo> for CommitTemplateLanguage<'repo> {
) -> TemplateParseResult<Self::Property> {
match property {
CommitTemplatePropertyKind::Core(property) => {
template_builder::build_core_method(self, build_ctx, property, function)
let table = &self.build_fn_table.core;
table.build_method(self, build_ctx, property, function)
}
CommitTemplatePropertyKind::Commit(property) => {
let table = &self.build_fn_table.commit_methods;
Expand Down Expand Up @@ -228,7 +229,7 @@ type CommitTemplateBuildMethodFnMap<'repo, T> =

/// Symbol table of methods available in the commit template.
struct CommitTemplateBuildFnTable<'repo> {
// TODO: add core methods/functions table
core: CoreTemplateBuildFnTable<'repo, CommitTemplateLanguage<'repo>>,
commit_methods: CommitTemplateBuildMethodFnMap<'repo, Commit>,
ref_name_methods: CommitTemplateBuildMethodFnMap<'repo, RefName>,
commit_or_change_id_methods: CommitTemplateBuildMethodFnMap<'repo, CommitOrChangeId>,
Expand All @@ -239,6 +240,7 @@ impl CommitTemplateBuildFnTable<'_> {
/// Creates new symbol table containing the builtin methods.
fn builtin() -> Self {
CommitTemplateBuildFnTable {
core: CoreTemplateBuildFnTable::builtin(),
commit_methods: builtin_commit_methods(),
ref_name_methods: builtin_ref_name_methods(),
commit_or_change_id_methods: builtin_commit_or_change_id_methods(),
Expand Down Expand Up @@ -802,4 +804,5 @@ pub fn parse<'repo>(
};
let node = template_parser::parse(template_text, aliases_map)?;
template_builder::build(&language, &node)
.map_err(|err| err.extend_alias_candidates(aliases_map))
}
11 changes: 7 additions & 4 deletions cli/src/operation_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use jj_lib::operation::Operation;

use crate::formatter::Formatter;
use crate::template_builder::{
self, BuildContext, CoreTemplatePropertyKind, IntoTemplateProperty, TemplateBuildMethodFnMap,
TemplateLanguage,
self, BuildContext, CoreTemplateBuildFnTable, CoreTemplatePropertyKind, IntoTemplateProperty,
TemplateBuildMethodFnMap, TemplateLanguage,
};
use crate::template_parser::{self, FunctionCallNode, TemplateAliasesMap, TemplateParseResult};
use crate::templater::{
Expand Down Expand Up @@ -55,7 +55,8 @@ impl TemplateLanguage<'static> for OperationTemplateLanguage {
) -> TemplateParseResult<Self::Property> {
match property {
OperationTemplatePropertyKind::Core(property) => {
template_builder::build_core_method(self, build_ctx, property, function)
let table = &self.build_fn_table.core;
table.build_method(self, build_ctx, property, function)
}
OperationTemplatePropertyKind::Operation(property) => {
let table = &self.build_fn_table.operation_methods;
Expand Down Expand Up @@ -134,7 +135,7 @@ type OperationTemplateBuildMethodFnMap<T> =

/// Symbol table of methods available in the operation template.
struct OperationTemplateBuildFnTable {
// TODO: add core methods/functions table
core: CoreTemplateBuildFnTable<'static, OperationTemplateLanguage>,
operation_methods: OperationTemplateBuildMethodFnMap<Operation>,
operation_id_methods: OperationTemplateBuildMethodFnMap<OperationId>,
}
Expand All @@ -143,6 +144,7 @@ impl OperationTemplateBuildFnTable {
/// Creates new symbol table containing the builtin methods.
fn builtin() -> Self {
OperationTemplateBuildFnTable {
core: CoreTemplateBuildFnTable::builtin(),
operation_methods: builtin_operation_methods(),
operation_id_methods: builtin_operation_id_methods(),
}
Expand Down Expand Up @@ -253,4 +255,5 @@ pub fn parse(
};
let node = template_parser::parse(template_text, aliases_map)?;
template_builder::build(&language, &node)
.map_err(|err| err.extend_alias_candidates(aliases_map))
}
Loading