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

templates: add raw_text to directly output content #4593

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions cli/src/template_builder.rs
avamsi marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: for future reference, can you include the "why" in raw_text() patch description?
#4593 (comment)

Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,11 @@ fn builtin_functions<'a, L: TemplateLanguage<'a> + ?Sized>() -> TemplateBuildFun
content, labels,
))))
});
map.insert("raw_text", |language, diagnostics, build_ctx, function| {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reminder: you'll need to update the templates doc.

let [content_node] = function.expect_exact_arguments()?;
let content = expect_plain_text_expression(language, diagnostics, build_ctx, content_node)?;
Ok(L::wrap_string(content))
});
map.insert("if", |language, diagnostics, build_ctx, function| {
let ([condition_node, true_node], [false_node]) = function.expect_arguments()?;
let condition =
Expand Down Expand Up @@ -2334,6 +2339,29 @@ mod tests {
@"text");
}

#[test]
fn test_raw_text_function() {
// Note: same as test_label_function above, wrapped with raw_text.
let mut env = TestTemplateEnv::new();
env.add_keyword("empty", || L::wrap_boolean(Literal(true)));
env.add_color("error", crossterm::style::Color::DarkRed);
env.add_color("warning", crossterm::style::Color::DarkYellow);

// Literal
insta::assert_snapshot!(
env.render_ok(r#"raw_text(label("error", "text"))"#), @"text");

// Evaluated property
insta::assert_snapshot!(
env.render_ok(r#"raw_text(label("error".first_line(), "text"))"#),
@"text");

// Template
insta::assert_snapshot!(
env.render_ok(r#"raw_text(label(if(empty, "error", "warning"), "text"))"#),
@"text");
}

#[test]
fn test_coalesce_function() {
let mut env = TestTemplateEnv::new();
Expand Down