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

revset, templater: add deprecation warnings #4515

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions cli/examples/custom-commit-templater/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use jj_lib::object_id::ObjectId;
use jj_lib::repo::Repo;
use jj_lib::revset::FunctionCallNode;
use jj_lib::revset::PartialSymbolResolver;
use jj_lib::revset::RevsetDiagnostics;
use jj_lib::revset::RevsetExpression;
use jj_lib::revset::RevsetFilterExtension;
use jj_lib::revset::RevsetFilterPredicate;
Expand Down Expand Up @@ -121,7 +122,7 @@ impl CommitTemplateLanguageExtension for HexCounter {
let mut table = CommitTemplateBuildFnTable::empty();
table.commit_methods.insert(
"has_most_digits",
|language, _build_context, property, call| {
|language, _diagnostics, _build_context, property, call| {
call.expect_no_arguments()?;
let most_digits = language
.cache_extension::<MostDigitsInId>()
Expand All @@ -134,7 +135,7 @@ impl CommitTemplateLanguageExtension for HexCounter {
);
table.commit_methods.insert(
"num_digits_in_id",
|_language, _build_context, property, call| {
|_language, _diagnostics, _build_context, property, call| {
call.expect_no_arguments()?;
Ok(L::wrap_integer(
property.map(|commit| num_digits_in_id(commit.id())),
Expand All @@ -143,7 +144,7 @@ impl CommitTemplateLanguageExtension for HexCounter {
);
table.commit_methods.insert(
"num_char_in_id",
|_language, _build_context, property, call| {
|_language, _diagnostics, _build_context, property, call| {
let [string_arg] = call.expect_exact_arguments()?;
let char_arg =
template_parser::expect_string_literal_with(string_arg, |string, span| {
Expand Down Expand Up @@ -185,6 +186,7 @@ impl RevsetFilterExtension for EvenDigitsFilter {
}

fn even_digits(
_diagnostics: &mut RevsetDiagnostics,
function: &FunctionCallNode,
_context: &RevsetParseContext,
) -> Result<Rc<RevsetExpression>, RevsetParseError> {
Expand Down
4 changes: 2 additions & 2 deletions cli/examples/custom-operation-templater/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl OperationTemplateLanguageExtension for HexCounter {
let mut table = OperationTemplateBuildFnTable::empty();
table.operation_methods.insert(
"num_digits_in_id",
|_language, _build_context, property, call| {
|_language, _diagnostics, _build_context, property, call| {
call.expect_no_arguments()?;
Ok(L::wrap_integer(
property.map(|operation| num_digits_in_id(operation.id())),
Expand All @@ -62,7 +62,7 @@ impl OperationTemplateLanguageExtension for HexCounter {
);
table.operation_methods.insert(
"num_char_in_id",
|_language, _build_context, property, call| {
|_language, _diagnostics, _build_context, property, call| {
let [string_arg] = call.expect_exact_arguments()?;
let char_arg =
template_parser::expect_string_literal_with(string_arg, |string, span| {
Expand Down
98 changes: 69 additions & 29 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ use jj_lib::commit::Commit;
use jj_lib::dag_walk;
use jj_lib::file_util;
use jj_lib::fileset;
use jj_lib::fileset::FilesetDiagnostics;
use jj_lib::fileset::FilesetExpression;
use jj_lib::git;
use jj_lib::git_backend::GitBackend;
Expand Down Expand Up @@ -92,6 +93,7 @@ use jj_lib::repo_path::RepoPathUiConverter;
use jj_lib::repo_path::UiPathParseError;
use jj_lib::revset;
use jj_lib::revset::RevsetAliasesMap;
use jj_lib::revset::RevsetDiagnostics;
use jj_lib::revset::RevsetExpression;
use jj_lib::revset::RevsetExtensions;
use jj_lib::revset::RevsetFilterPredicate;
Expand Down Expand Up @@ -131,6 +133,7 @@ use crate::command_error::config_error_with_message;
use crate::command_error::handle_command_result;
use crate::command_error::internal_error;
use crate::command_error::internal_error_with_message;
use crate::command_error::print_parse_diagnostics;
use crate::command_error::user_error;
use crate::command_error::user_error_with_hint;
use crate::command_error::user_error_with_message;
Expand Down Expand Up @@ -163,7 +166,7 @@ use crate::revset_util::RevsetExpressionEvaluator;
use crate::template_builder;
use crate::template_builder::TemplateLanguage;
use crate::template_parser::TemplateAliasesMap;
use crate::template_parser::TemplateParseResult;
use crate::template_parser::TemplateDiagnostics;
use crate::templater::PropertyPlaceholder;
use crate::templater::TemplateRenderer;
use crate::text_util;
Expand Down Expand Up @@ -340,13 +343,17 @@ impl CommandHelper {
template_text: &str,
wrap_self: impl Fn(PropertyPlaceholder<C>) -> L::Property,
) -> Result<TemplateRenderer<'a, C>, CommandError> {
let mut diagnostics = TemplateDiagnostics::new();
let aliases = self.load_template_aliases(ui)?;
Ok(template_builder::parse(
let template = template_builder::parse(
language,
&mut diagnostics,
template_text,
&aliases,
wrap_self,
)?)
)?;
print_parse_diagnostics(ui, "In template expression", &diagnostics)?;
Ok(template)
}

pub fn workspace_loader(&self) -> Result<&dyn WorkspaceLoader, CommandError> {
Expand Down Expand Up @@ -680,15 +687,21 @@ impl WorkspaceCommandEnvironment {

fn load_immutable_heads_expression(
&self,
_ui: &Ui,
ui: &Ui,
) -> Result<Rc<RevsetExpression>, CommandError> {
revset_util::parse_immutable_heads_expression(&self.revset_parse_context())
.map_err(|e| config_error_with_message("Invalid `revset-aliases.immutable_heads()`", e))
let mut diagnostics = RevsetDiagnostics::new();
let expression = revset_util::parse_immutable_heads_expression(
&mut diagnostics,
&self.revset_parse_context(),
)
.map_err(|e| config_error_with_message("Invalid `revset-aliases.immutable_heads()`", e))?;
print_parse_diagnostics(ui, "In `revset-aliases.immutable_heads()`", &diagnostics)?;
Ok(expression)
}

fn load_short_prefixes_expression(
&self,
_ui: &Ui,
ui: &Ui,
) -> Result<Option<Rc<RevsetExpression>>, CommandError> {
let revset_string = self
.settings()
Expand All @@ -698,10 +711,14 @@ impl WorkspaceCommandEnvironment {
if revset_string.is_empty() {
Ok(None)
} else {
let (expression, modifier) =
revset::parse_with_modifier(&revset_string, &self.revset_parse_context()).map_err(
|err| config_error_with_message("Invalid `revsets.short-prefixes`", err),
)?;
let mut diagnostics = RevsetDiagnostics::new();
let (expression, modifier) = revset::parse_with_modifier(
&mut diagnostics,
&revset_string,
&self.revset_parse_context(),
)
.map_err(|err| config_error_with_message("Invalid `revsets.short-prefixes`", err))?;
print_parse_diagnostics(ui, "In `revsets.short-prefixes`", &diagnostics)?;
let (None | Some(RevsetModifier::All)) = modifier;
Ok(Some(revset::optimize(expression)))
}
Expand Down Expand Up @@ -767,13 +784,21 @@ impl WorkspaceCommandEnvironment {
/// be one of the `L::wrap_*()` functions.
pub fn parse_template<'a, C: Clone + 'a, L: TemplateLanguage<'a> + ?Sized>(
&self,
_ui: &Ui,
ui: &Ui,
language: &L,
template_text: &str,
wrap_self: impl Fn(PropertyPlaceholder<C>) -> L::Property,
) -> TemplateParseResult<TemplateRenderer<'a, C>> {
let aliases = &self.template_aliases_map;
template_builder::parse(language, template_text, aliases, wrap_self)
) -> Result<TemplateRenderer<'a, C>, CommandError> {
let mut diagnostics = TemplateDiagnostics::new();
let template = template_builder::parse(
language,
&mut diagnostics,
template_text,
&self.template_aliases_map,
wrap_self,
)?;
print_parse_diagnostics(ui, "In template expression", &diagnostics)?;
Ok(template)
}

/// Creates commit template language environment for this workspace and the
Expand Down Expand Up @@ -1082,25 +1107,30 @@ impl WorkspaceCommandHelper {
/// Parses the given fileset expressions and concatenates them all.
pub fn parse_union_filesets(
&self,
_ui: &Ui,
ui: &Ui,
file_args: &[String], // TODO: introduce FileArg newtype?
) -> Result<FilesetExpression, CommandError> {
let mut diagnostics = FilesetDiagnostics::new();
let expressions: Vec<_> = file_args
.iter()
.map(|arg| fileset::parse_maybe_bare(arg, self.path_converter()))
.map(|arg| fileset::parse_maybe_bare(&mut diagnostics, arg, self.path_converter()))
.try_collect()?;
print_parse_diagnostics(ui, "In fileset expression", &diagnostics)?;
Ok(FilesetExpression::union_all(expressions))
}

pub fn auto_tracking_matcher(&self, _ui: &Ui) -> Result<Box<dyn Matcher>, CommandError> {
pub fn auto_tracking_matcher(&self, ui: &Ui) -> Result<Box<dyn Matcher>, CommandError> {
let mut diagnostics = FilesetDiagnostics::new();
let pattern = self.settings().config().get_string("snapshot.auto-track")?;
let expression = fileset::parse(
&mut diagnostics,
&pattern,
&RepoPathUiConverter::Fs {
cwd: "".into(),
base: "".into(),
},
)?;
print_parse_diagnostics(ui, "In `snapshot.auto-track`", &diagnostics)?;
Ok(expression.to_matcher())
}

Expand Down Expand Up @@ -1309,26 +1339,31 @@ impl WorkspaceCommandHelper {

fn parse_revset_with_modifier(
&self,
_ui: &Ui,
ui: &Ui,
revision_arg: &RevisionArg,
) -> Result<(RevsetExpressionEvaluator<'_>, Option<RevsetModifier>), CommandError> {
let mut diagnostics = RevsetDiagnostics::new();
let context = self.revset_parse_context();
let (expression, modifier) = revset::parse_with_modifier(revision_arg.as_ref(), &context)?;
let (expression, modifier) =
revset::parse_with_modifier(&mut diagnostics, revision_arg.as_ref(), &context)?;
print_parse_diagnostics(ui, "In revset expression", &diagnostics)?;
Ok((self.attach_revset_evaluator(expression), modifier))
}

/// Parses the given revset expressions and concatenates them all.
pub fn parse_union_revsets(
&self,
_ui: &Ui,
ui: &Ui,
revision_args: &[RevisionArg],
) -> Result<RevsetExpressionEvaluator<'_>, CommandError> {
let mut diagnostics = RevsetDiagnostics::new();
let context = self.revset_parse_context();
let expressions: Vec<_> = revision_args
.iter()
.map(|arg| revset::parse_with_modifier(arg.as_ref(), &context))
.map(|arg| revset::parse_with_modifier(&mut diagnostics, arg.as_ref(), &context))
.map_ok(|(expression, None | Some(RevsetModifier::All))| expression)
.try_collect()?;
print_parse_diagnostics(ui, "In revset expression", &diagnostics)?;
let expression = RevsetExpression::union_all(&expressions);
Ok(self.attach_revset_evaluator(expression))
}
Expand Down Expand Up @@ -1369,7 +1404,7 @@ impl WorkspaceCommandHelper {
language: &L,
template_text: &str,
wrap_self: impl Fn(PropertyPlaceholder<C>) -> L::Property,
) -> TemplateParseResult<TemplateRenderer<'a, C>> {
) -> Result<TemplateRenderer<'a, C>, CommandError> {
self.env
.parse_template(ui, language, template_text, wrap_self)
}
Expand All @@ -1381,17 +1416,22 @@ impl WorkspaceCommandHelper {
template_text: &str,
wrap_self: impl Fn(PropertyPlaceholder<C>) -> L::Property,
) -> TemplateRenderer<'a, C> {
let aliases = &self.env.template_aliases_map;
template_builder::parse(language, template_text, aliases, wrap_self)
.expect("parse error should be confined by WorkspaceCommandHelper::new()")
template_builder::parse(
language,
&mut TemplateDiagnostics::new(),
template_text,
&self.env.template_aliases_map,
wrap_self,
)
.expect("parse error should be confined by WorkspaceCommandHelper::new()")
}

/// Parses commit template into evaluation tree.
pub fn parse_commit_template(
&self,
ui: &Ui,
template_text: &str,
) -> TemplateParseResult<TemplateRenderer<'_, Commit>> {
) -> Result<TemplateRenderer<'_, Commit>, CommandError> {
let language = self.commit_template_language();
self.parse_template(
ui,
Expand All @@ -1406,7 +1446,7 @@ impl WorkspaceCommandHelper {
&self,
ui: &Ui,
template_text: &str,
) -> TemplateParseResult<TemplateRenderer<'_, Operation>> {
) -> Result<TemplateRenderer<'_, Operation>, CommandError> {
let language = self.operation_template_language();
self.parse_template(
ui,
Expand Down Expand Up @@ -2029,7 +2069,7 @@ impl WorkspaceCommandTransaction<'_> {
&self,
ui: &Ui,
template_text: &str,
) -> TemplateParseResult<TemplateRenderer<'_, Commit>> {
) -> Result<TemplateRenderer<'_, Commit>, CommandError> {
let language = self.commit_template_language();
self.helper.env.parse_template(
ui,
Expand Down
18 changes: 18 additions & 0 deletions cli/src/command_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::sync::Arc;

use itertools::Itertools as _;
use jj_lib::backend::BackendError;
use jj_lib::dsl_util::Diagnostics;
use jj_lib::fileset::FilePatternParseError;
use jj_lib::fileset::FilesetParseError;
use jj_lib::fileset::FilesetParseErrorKind;
Expand Down Expand Up @@ -835,3 +836,20 @@ fn handle_clap_error(ui: &mut Ui, err: &clap::Error, hints: &[ErrorHint]) -> io:
print_error_hints(ui, hints)?;
Ok(ExitCode::from(2))
}

/// Prints diagnostic messages emitted during parsing.
pub fn print_parse_diagnostics<T: error::Error>(
ui: &Ui,
context_message: &str,
diagnostics: &Diagnostics<T>,
) -> io::Result<()> {
for diag in diagnostics {
writeln!(ui.warning_default(), "{context_message}")?;
for err in iter::successors(Some(diag as &dyn error::Error), |err| err.source()) {
writeln!(ui.stderr(), "{err}")?;
}
// If we add support for multiple error diagnostics, we might have to do
// find_source_parse_error_hint() and print it here.
}
Ok(())
}
6 changes: 5 additions & 1 deletion cli/src/commands/debug/fileset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ use std::fmt::Debug;
use std::io::Write as _;

use jj_lib::fileset;
use jj_lib::fileset::FilesetDiagnostics;

use crate::cli_util::CommandHelper;
use crate::command_error::print_parse_diagnostics;
use crate::command_error::CommandError;
use crate::ui::Ui;

Expand All @@ -36,7 +38,9 @@ pub fn cmd_debug_fileset(
let workspace_command = command.workspace_helper(ui)?;
let path_converter = workspace_command.path_converter();

let expression = fileset::parse_maybe_bare(&args.path, path_converter)?;
let mut diagnostics = FilesetDiagnostics::new();
let expression = fileset::parse_maybe_bare(&mut diagnostics, &args.path, path_converter)?;
print_parse_diagnostics(ui, "In fileset expression", &diagnostics)?;
writeln!(ui.stdout(), "-- Parsed:")?;
writeln!(ui.stdout(), "{expression:#?}")?;
writeln!(ui.stdout())?;
Expand Down
6 changes: 5 additions & 1 deletion cli/src/commands/debug/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ use std::io::Write as _;

use jj_lib::object_id::ObjectId;
use jj_lib::revset;
use jj_lib::revset::RevsetDiagnostics;

use crate::cli_util::CommandHelper;
use crate::command_error::print_parse_diagnostics;
use crate::command_error::CommandError;
use crate::revset_util;
use crate::ui::Ui;
Expand All @@ -38,7 +40,9 @@ pub fn cmd_debug_revset(
let workspace_ctx = workspace_command.revset_parse_context();
let repo = workspace_command.repo().as_ref();

let expression = revset::parse(&args.revision, &workspace_ctx)?;
let mut diagnostics = RevsetDiagnostics::new();
let expression = revset::parse(&mut diagnostics, &args.revision, &workspace_ctx)?;
print_parse_diagnostics(ui, "In revset expression", &diagnostics)?;
writeln!(ui.stdout(), "-- Parsed:")?;
writeln!(ui.stdout(), "{expression:#?}")?;
writeln!(ui.stdout())?;
Expand Down
Loading