Require colon in show rule

This commit is contained in:
Laurenz 2023-02-12 20:23:52 +01:00
parent 77c29b36d4
commit 7b2cdb9d95
13 changed files with 38 additions and 29 deletions

View File

@ -110,7 +110,7 @@ element. And apart from a function, the left-hand side of a show rule can also
take a number of other _selectors_ that define what to apply the transformation take a number of other _selectors_ that define what to apply the transformation
to: to:
- **Everything:** `{show rest => ..}` \ - **Everything:** `{show: rest => ..}` \
Transform everything after the show rule. This is useful to apply a more Transform everything after the show rule. This is useful to apply a more
complex layout to your whole document without wrapping everything in a giant complex layout to your whole document without wrapping everything in a giant
function call. function call.

View File

@ -89,8 +89,9 @@ a table listing all syntax that is available in code mode:
| Named function | `{let f(x) = 2 * x}` | [Function]($type/function) | | Named function | `{let f(x) = 2 * x}` | [Function]($type/function) |
| Set rule | `{set text(14pt)}` | [Styling]($styling/#set-rules) | | Set rule | `{set text(14pt)}` | [Styling]($styling/#set-rules) |
| Set-if rule | `{set text(..) if .. }` | [Styling]($styling/#set-rules) | | Set-if rule | `{set text(..) if .. }` | [Styling]($styling/#set-rules) |
| Show rule | `{show raw: it => {..}}` | [Styling]($styling/#set-rules) |
| Show-set rule | `{show par: set block(..)}` | [Styling]($styling/#show-rules) | | Show-set rule | `{show par: set block(..)}` | [Styling]($styling/#show-rules) |
| Show rule with function | `{show raw: it => {..}}` | [Styling]($styling/#show-rules) |
| Show-everything rule | `{show: columns.with(2)}` | [Styling]($styling/#show-rules) |
| Conditional | `{if x == 1 {..} else {..}}` | [Scripting]($scripting/#conditionals) | | Conditional | `{if x == 1 {..} else {..}}` | [Scripting]($scripting/#conditionals) |
| For loop | `{for x in (1, 2, 3) {..}}` | [Scripting]($scripting/#loops) | | For loop | `{for x in (1, 2, 3) {..}}` | [Scripting]($scripting/#loops) |
| While loop | `{while x < 10 {..}}` | [Scripting]($scripting/#loops) | | While loop | `{while x < 10 {..}}` | [Scripting]($scripting/#loops) |

View File

@ -268,11 +268,11 @@ abstract to be in two columns, we need to apply the column function to our whole
document. document.
Instead of wrapping the whole document in a giant function call, we can use an Instead of wrapping the whole document in a giant function call, we can use an
"everything" show rule. This show rule does not feature a colon. Instead, we "everything" show rule. To write such a show rule, put a colon directly behind
directly provide a function that is given the rest of the document as a the show keyword and then provide a function. This function is given the rest of
parameter. We have called the parameter `rest` here, but you are free to choose the document as a parameter. We have called the parameter `rest` here, but you
any name. The rule can then do anything with this content. In our case, it are free to choose any name. The function can then do anything with this
passes it on to the `columns` function. content. In our case, it passes it on to the `columns` function.
```example ```example
>>> #let title = [ >>> #let title = [
@ -323,7 +323,7 @@ passes it on to the `columns` function.
>>> #v(4mm) >>> #v(4mm)
<<< ... <<< ...
#show rest => columns(2, rest) #show: rest => columns(2, rest)
= Introduction = Introduction
#lorem(300) #lorem(300)
@ -391,7 +391,7 @@ a way to set any of that, we need to write our own heading show rule.
>>> ] >>> ]
>>> >>>
>>> #v(4mm) >>> #v(4mm)
>>> #show rest => columns(2, rest) >>> #show: rest => columns(2, rest)
>>> >>>
>>> = Introduction >>> = Introduction
>>> #lorem(35) >>> #lorem(35)
@ -480,7 +480,7 @@ differentiate between section and subsection headings:
>>> ] >>> ]
>>> >>>
>>> #v(4mm) >>> #v(4mm)
>>> #show rest => columns(2, rest) >>> #show: rest => columns(2, rest)
>>> >>>
>>> = Introduction >>> = Introduction
>>> #lorem(35) >>> #lorem(35)

View File

@ -48,7 +48,7 @@ function to our whole document. Let's do that with our `amazed` function.
>>> #let amazed(term, color: blue) = { >>> #let amazed(term, color: blue) = {
>>> text(color, box[✨ #term ✨]) >>> text(color, box[✨ #term ✨])
>>> } >>> }
#show amazed #show: amazed
I choose to focus on the good I choose to focus on the good
in my life and let go of any in my life and let go of any
negative thoughts or beliefs. negative thoughts or beliefs.
@ -72,7 +72,7 @@ that content block.
#doc #doc
] ]
#show template #show: template
I am learning something cool today. I am learning something cool today.
It's going great so far! It's going great so far!
``` ```
@ -125,7 +125,7 @@ previous chapter.
columns(2, doc) columns(2, doc)
} }
#show doc => conf([Paper title], doc) #show: doc => conf([Paper title], doc)
= Introduction = Introduction
#lorem(90) #lorem(90)
@ -161,7 +161,7 @@ things readable, we'll add those as named arguments. In the end, we want it to
work like this: work like this:
```typ ```typ
#show doc => conf( #show: doc => conf(
title: [Towards Improved Modelling], title: [Towards Improved Modelling],
authors: ( authors: (
( (
@ -324,7 +324,7 @@ path of the file after the `{from}` keyword.
>>>} >>>}
<<< #import "conf.typ": conf <<< #import "conf.typ": conf
#show doc => conf( #show: doc => conf(
title: [ title: [
Towards Improved Modelling Towards Improved Modelling
], ],

View File

@ -723,6 +723,12 @@ fn code_completions(ctx: &mut CompletionContext, hashtag: bool) {
"Redefines the look of an element.", "Redefines the look of an element.",
); );
ctx.snippet_completion(
"show rule (everything)",
"show: ${}",
"Transforms everything that follows.",
);
ctx.snippet_completion( ctx.snippet_completion(
"let binding", "let binding",
"let ${name} = ${value}", "let ${name} = ${value}",

View File

@ -279,9 +279,10 @@ fn highlight_ident(node: &LinkedNode) -> Option<Tag> {
ancestor = ancestor.parent()?; ancestor = ancestor.parent()?;
} }
// Are we directly before a show rule colon? // Are we directly before or behind a show rule colon?
if next_leaf.map(|leaf| leaf.kind()) == Some(SyntaxKind::Colon) if ancestor.parent_kind() == Some(SyntaxKind::ShowRule)
&& ancestor.parent_kind() == Some(SyntaxKind::ShowRule) && (next_leaf.map(|leaf| leaf.kind()) == Some(SyntaxKind::Colon)
|| node.prev_leaf().map(|leaf| leaf.kind()) == Some(SyntaxKind::Colon))
{ {
return Some(Tag::Function); return Some(Tag::Function);
} }

View File

@ -852,10 +852,11 @@ fn set_rule(p: &mut Parser) {
fn show_rule(p: &mut Parser) { fn show_rule(p: &mut Parser) {
let m = p.marker(); let m = p.marker();
p.assert(SyntaxKind::Show); p.assert(SyntaxKind::Show);
code_expr(p); if !p.at(SyntaxKind::Colon) {
if p.eat_if(SyntaxKind::Colon) {
code_expr(p); code_expr(p);
} }
p.expect(SyntaxKind::Colon);
code_expr(p);
p.wrap(m, SyntaxKind::ShowRule); p.wrap(m, SyntaxKind::ShowRule);
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 85 KiB

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 40 KiB

View File

@ -116,7 +116,7 @@
// Everything should be in smallcaps. // Everything should be in smallcaps.
#for color in (red, blue, green, yellow) [ #for color in (red, blue, green, yellow) [
#set text("Roboto") #set text("Roboto")
#show it => text(fill: color, it) #show: it => text(fill: color, it)
#smallcaps(if color != green [ #smallcaps(if color != green [
Some Some
] else [ ] else [

View File

@ -9,25 +9,25 @@
T. Ypst T. Ypst
] ]
#show columns.with(2) #show: columns.with(2)
Great typography is at the essence of great storytelling. It is the medium that Great typography is at the essence of great storytelling. It is the medium that
transports meaning from parchment to reader, the wave that sparks a flame transports meaning from parchment to reader, the wave that sparks a flame
in booklovers and the great fulfiller of human need. in booklovers and the great fulfiller of human need.
--- ---
// Test bare show in content block. // Test bare show in content block.
A #[_B #show c => [*#c*]; C_] D A #[_B #show: c => [*#c*]; C_] D
--- ---
// Test style precedence. // Test style precedence.
#set text(fill: eastern, size: 1.5em) #set text(fill: eastern, size: 1.5em)
#show text.with(fill: forest) #show: text.with(fill: forest)
Forest Forest
--- ---
#show [Shown] #show: [Shown]
Ignored Ignored
--- ---
// Error: 5-19 show is only allowed directly in code and content blocks // Error: 5-20 show is only allowed directly in code and content blocks
#{ (show body => 2) * body } #{ (show: body => 2) * body }

View File

@ -64,7 +64,7 @@ Another text.
world world
{ {
set text(blue) set text(blue)
show it => { show: it => {
show "o": "Ø" show "o": "Ø"
it it
} }

View File

@ -40,7 +40,7 @@ Expanded by height.
--- ---
// Test relative sizing. // Test relative sizing.
#set text(fill: white) #set text(fill: white)
#show rect.with(width: 100pt, height: 50pt, inset: 0pt, fill: rgb("aaa")) #show: rect.with(width: 100pt, height: 50pt, inset: 0pt, fill: rgb("aaa"))
#set align(center + horizon) #set align(center + horizon)
#stack( #stack(
dir: ltr, dir: ltr,