Commit graph

116 commits

Author SHA1 Message Date
Yuya Nishihara
d0715a0935 cli: load [template-aliases] section and pass around aliases table 2023-02-14 10:06:28 +09:00
Yuya Nishihara
9d356b8094 templater: add table of alias (decl, defn) rules
This is basically a copy of revset::RevsetAliasesMap. We could extract a
common table struct, but that wouldn't be worth the effort since the core
alias substitution logic can't be easily abstracted.
2023-02-14 10:06:28 +09:00
Yuya Nishihara
81c8543621 cli: switch "debug template" command to new AST
This should be more readable than raw pest tree.
2023-02-13 02:11:19 +09:00
Yuya Nishihara
677ac54855 templater: build evaluatable expression from AST nodes 2023-02-13 02:11:19 +09:00
Yuya Nishihara
9dc68af4f9 templater: introduce AST structs
This prepares for template aliases support #1190. Unlike revset, template
expressions can be of various types, whereas alias substitution will process
untyped nodes. That's one reason that ExpressionNode is closer to parsed tree
than evaluatable Property structs. Another reason is that it's uneasy to split
name/type checking into "parsing" and "building property function" stages.

We could do alias expansion at once while building Property functions, but
that would make testing harder because Property isn't Debug + PartialEq.
2023-02-13 02:11:19 +09:00
Yuya Nishihara
7c6ddf9773 templater: duplicate parsing functions for reference
I'm going to split 'parse() -> Expression' functions into 'parse() -> AST'
and 'build(AST) -> Expression'. The duplicated functions will be baseline of
new 'parse() -> AST' functions.
2023-02-13 02:11:19 +09:00
Vamsi Avula
aacdcf629b author/committer templates: add a username method
I haven't used a proper email address parser as I'm not really sure if it's
worth the extra dependency and effort -- thoughts?
2023-02-11 15:38:27 +05:30
Yuya Nishihara
686c1fb522 templater: allow trailing comma
While rewriting the default log template, I find it's annoying that I have
to remove "," from the last line.
2023-02-07 23:19:36 +09:00
Yuya Nishihara
cd164be762 templater: inline extract_entire_prefix_and_trimmed_tail() and simplify 2023-02-07 11:42:39 +09:00
Yuya Nishihara
ebf9887d65 templater: migrate shortest_prefix_and_brackets() over shortest() 2023-02-07 11:42:39 +09:00
Yuya Nishihara
3ccac9cda5 templater: rename shortest_styled_prefix() to shortest()
Now it is the true "shortest prefix" function with the default parameter,
so let's simply call it "shortest()".
2023-02-07 11:42:39 +09:00
Ilya Grigoriev
de163216c0 Templater: make shortest id functions default to 0 length
This makes them print the shortest unique prefix only.
2023-02-05 22:15:27 -08:00
Ilya Grigoriev
b87facff7a Templater: Give short id function a total_len argument
This will be tested in the next commit.
2023-02-05 21:18:42 -08:00
Ilya Grigoriev
58828803d4 Templater: Give shortest id functions a total_len argument. 2023-02-05 21:18:42 -08:00
Yuya Nishihara
5bfecf112a templater: add parsing rule for integer literal, add i64 property
This will be used as a parameter of id.shortest*() methods. For now, only
decimal literal is supported, and there's no unary negate operator.
"0"-prefix is disallowed because it looks like an octal number.

I don't think we would want multiple integer types in the template language,
so I chose i64 as the integer type of reasonable width.
2023-02-05 17:21:54 +09:00
Yuya Nishihara
fe8c41bbd7 templater: extract primary rule, inline maybe_method instead 2023-02-05 17:21:54 +09:00
Yuya Nishihara
e4fbb1fe83 templater: extract "function" parsing to function
It's getting bigger.
2023-02-05 17:21:54 +09:00
Yuya Nishihara
9a5a88238b templater: fix handling of method chaining of parenthesized expression 2023-02-05 17:21:54 +09:00
Yuya Nishihara
16a744fe18 templater: make internal parse_template_str() return expression
An "Expression" object is more useful while writing tests.
2023-02-05 17:21:54 +09:00
Yuya Nishihara
5342d2cfb7 templater: implement string.contains() method as example 2023-02-05 11:53:11 +09:00
Yuya Nishihara
a4be118981 templater: check number of method arguments 2023-02-05 11:53:11 +09:00
Yuya Nishihara
28c1989257 templater: add helper to extract argument pairs 2023-02-05 11:53:11 +09:00
Yuya Nishihara
945f9c1552 templater: pass parse_keyword() down to method parsing functions
It's needed to parse argument expressions.
2023-02-05 11:53:11 +09:00
Yuya Nishihara
db172aa7c9 templater: process property composition by parse_<type>_method()
This prepares for adding method arguments support. Since a method argument
should be evaluated in the surrounding scope, its type will be
'TemplateProperty<I>', not 'TemplateProperty<J>' for the receiver type 'J'.
Then, the receiver of type 'TemplateProperty<I, Output = J>', and arguments
of type 'TemplateProperty<I, Output = Pn>' will be composed to an input of
type 'TemplateProperty<I, Output = (J, Pn...)>'.

wrap_fn() is removed since it's only useful for nullary methods, and we
no longer need Box<dyn> to abstract the return value.
2023-02-05 11:53:11 +09:00
Yuya Nishihara
bdf809c6c3 templater: do not turn method arguments into iterator by caller
parse_<type>_method() will need args_pair.as_span() to report invalid
argument error.
2023-02-05 11:53:11 +09:00
Yuya Nishihara
5072469eca templater: extract generic parsing functions
We'll probably need a better abstraction, but a parameterized keyword
function is a good first step. This unblocks the use of parse_term() for
context-less properties (or literals). I'm going to add a proper support
for context-aware method arguments, but literals can be parsed without it.

parse_keyword() closure is passed by reference to avoid infinite expansion.
2023-02-05 11:53:11 +09:00
Yuya Nishihara
1b78fb5b5d templater: add shorthand for Result<T, TemplateParseError>
This will appear in "impl Fn(..) -> .." and we can't alias it.
2023-02-05 11:53:11 +09:00
Yuya Nishihara
13b5661094 templater: add separate(sep, contents...) function
This is a copy of Mercurial's separate() function.
2023-02-04 15:19:02 +09:00
Yuya Nishihara
5850575d53 templater: add separate variants for exact/range argument count errors
I believe this isn't a good abstraction, but I need to add one more variant
for "at least n arguments" error. A stringified count like "2 to 3" could be
embedded in an error variant, but I don't think it's good idea to build error
message in that way.
2023-02-04 15:19:02 +09:00
Yuya Nishihara
e2eda9f72f templater: inline "current_working_copy" keyword
Maybe I didn't make this change before because the closure needs to capture
WorkspaceId by value. Since the inlined version looks pretty simple, let's
go forward to do that.
2023-02-04 01:58:13 +09:00
Yuya Nishihara
45a018ec6b templater: report invalid argument count as error
We'll probably need a helper to extract N..M arguments, but let's revisit
it later.
2023-02-03 12:06:04 +09:00
Yuya Nishihara
baf911459f templater: report invalid argument type as error 2023-02-03 12:06:04 +09:00
Yuya Nishihara
4eff802d87 templater: report keyword/function/method name error 2023-02-03 12:06:04 +09:00
Yuya Nishihara
8653cc885a templater: wrap inner parsing functions with Result<T> 2023-02-03 12:06:04 +09:00
Yuya Nishihara
7090fa0374 templater: report syntax error, don't panic()
Even though the template syntax is experimental, panicking parser makes
it difficult to write tests. So let's add minimal error handling. The error
types are basically copied from the revset module.

I made write_commit_summary() fall back to the default template if user
template had syntax error. It should be better than reporting parse error
after e.g. "jj abandon" finished successfully.
2023-02-03 12:06:04 +09:00
Yuya Nishihara
d6592573d0 templater: abstract Dynamic/LabelTemplate over labels type, unify them 2023-02-02 01:12:23 +09:00
Yuya Nishihara
1911734acc templater: add wrapper that turns template back to string property
I have no idea whether or not any template expressions are intentionally
allowed as a label, but it makes sense to write something like
'label("phase-" phase, ...)' (if we had a phase keyword.) So I decided to
add .into_plain_text() instead of stricter .try_into_string().
2023-02-02 01:12:23 +09:00
Yuya Nishihara
6da43734cb templater: migrate trivial commit keywords to closure wrapper
Perhaps, non-trivial keywords can be extracted to free functions, and both
parsing and property functions can be eventually moved to a commit templater
module?
2023-02-01 17:54:35 +09:00
Yuya Nishihara
e4cb1afd61 templater: fix crash on "".first_line() 2023-02-01 16:13:43 +09:00
Yuya Nishihara
259314b442 templater: reuse parse_commit_term() to coerce if() condition to bool
This should fix some inconsistent parsing of condition term.
2023-02-01 10:25:28 +09:00
Yuya Nishihara
553bee6ac4 templater: extract method that coerces property to boolean 2023-02-01 10:25:28 +09:00
Yuya Nishihara
5eee7186ba templater: add enum that holds property or template
This allows us to merge parse_boolean_commit_property() into
parse_commit_term(). We'll probably need similar type coercion methods
for the other basic types.

I considered adding something like Property::Template(), which could be
reused for map operation (e.g. 'revset().map(commit_id).join(" ")'.)
However, a mapped commit template would be different from the top-level
commit template regarding the lifetime of the context.
"Expression::<Commit>::Template()" takes "for<'b> &'b Commit" as an argument,
whereas a mapped template property would capture Commit object somewhere.
2023-02-01 10:25:28 +09:00
Yuya Nishihara
8d7ba8ddd3 templater: extract PropertyAndLabels::into_template() 2023-02-01 10:25:28 +09:00
Yuya Nishihara
41afdd48c7 templater: reorder declaration of PropertyAndLabels struct 2023-02-01 10:25:28 +09:00
Yuya Nishihara
f1e6146d6d templater: add newtype to implement property over closure
Many template keywords and methods are one liners, and I think that's actually
good because writing tests for templater would be more involved than for pure
functions.

This patch introduces a wrapper for such one-line functions, and migrates
method parser to that. Some of the commit keyword structs can also be ported
to this wrapper.
2023-01-31 16:27:52 +09:00
Ilya Grigoriev
26cc290157 Templater: Create shortest_styled_prefix function for ids 2023-01-30 22:48:38 -08:00
Ilya Grigoriev
3ee87aa5a7 Rename short => shortest 2023-01-30 22:48:38 -08:00
Martin von Zweigbergk
6cf7d98465 templater: replace is_git_head keyword by git_head keyword
I'm about to make it possible for `HEAD@git` to be conflicted. For
that, we need to be able to include the trailing `?`. This patch
prepares for that.
2023-01-30 09:05:03 -08:00
Yuya Nishihara
3d965f22ab templater: disallow empty template in function arguments and parens
Before, "f()" was parsed as a function call with one empty argument. In
practice, this change means "if(divergent,,false_template)" is no longer
valid.
2023-01-30 12:50:40 +09:00
Yuya Nishihara
a5bb48d47e templater: introduce top-level rule that wraps template terms
An epsilon rule will be moved here to disambiguate f() with f("").
2023-01-30 12:50:40 +09:00