make inline statements possible

This commit is contained in:
davidodenwald 2022-11-18 22:59:29 +01:00
parent 84543ebba2
commit 6086008c32
5 changed files with 25 additions and 5 deletions

View file

@ -7,6 +7,7 @@ export interface Node {
id: string; id: string;
type: "root" | "expression" | "statement" | "block" | "ignore"; type: "root" | "expression" | "statement" | "block" | "ignore";
content: string; content: string;
ownLine: boolean;
originalText: string; originalText: string;
index: number; index: number;
length: number; length: number;
@ -15,7 +16,6 @@ export interface Node {
export interface Expression extends Node { export interface Expression extends Node {
type: "expression"; type: "expression";
ownLine: boolean;
} }
export type Delimiter = "" | "-" | "+"; export type Delimiter = "" | "-" | "+";
@ -35,7 +35,6 @@ export interface Block extends Node {
export interface IgnoreBlock extends Node { export interface IgnoreBlock extends Node {
type: "ignore"; type: "ignore";
ownLine: boolean;
} }
export type Keyword = export type Keyword =

View file

@ -21,6 +21,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
id: "0", id: "0",
type: "root" as const, type: "root" as const,
content: text, content: text,
ownLine: false,
originalText: text, originalText: text,
index: 0, index: 0,
length: 0, length: 0,
@ -101,6 +102,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
id: placeholder, id: placeholder,
type: "statement", type: "statement",
content: statement, content: statement,
ownLine: newline,
originalText: matchText, originalText: matchText,
index: match.index + i + pre.length, index: match.index + i + pre.length,
length: matchText.length, length: matchText.length,
@ -116,6 +118,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
id: generatePlaceholder(), id: generatePlaceholder(),
type: "statement" as const, type: "statement" as const,
content: statement, content: statement,
ownLine: newline,
originalText: matchText, originalText: matchText,
index: match.index + i + pre.length, index: match.index + i + pre.length,
length: matchText.length, length: matchText.length,
@ -154,6 +157,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
id: generatePlaceholder(), id: generatePlaceholder(),
type: "statement" as const, type: "statement" as const,
content: statement, content: statement,
ownLine: newline,
originalText: matchText, originalText: matchText,
index: match.index + i + pre.length, index: match.index + i + pre.length,
length: matchText.length, length: matchText.length,
@ -175,6 +179,7 @@ export const parse: Parser<Node>["parse"] = (text) => {
start: start, start: start,
end: end, end: end,
content, content,
ownLine: newline,
originalText: matchText, originalText: matchText,
index: start.index, index: start.index,
length: end.index + end.length - start.index, length: end.index + end.length - start.index,

View file

@ -47,7 +47,7 @@ const printStatement = (node: Statement): builders.Doc => {
node.endDelimiter, node.endDelimiter,
"%}", "%}",
], ],
{ shouldBreak: true } { shouldBreak: node.ownLine }
); );
return node.keyword === "else" return node.keyword === "else"
@ -130,13 +130,23 @@ export const embed: Printer<Node>["embed"] = (
}); });
if (node.type === "block") { if (node.type === "block") {
if (node.content.includes("\n")) {
return builders.group([ return builders.group([
path.call(print, "nodes", (node as Block).start.id), path.call(print, "nodes", (node as Block).start.id),
builders.indent([builders.softline, utils.stripTrailingHardline(mapped)]), builders.indent([
builders.softline,
utils.stripTrailingHardline(mapped),
]),
builders.hardline, builders.hardline,
path.call(print, "nodes", (node as Block).end.id), path.call(print, "nodes", (node as Block).end.id),
]); ]);
} }
return builders.group([
path.call(print, "nodes", (node as Block).start.id),
utils.stripTrailingHardline(mapped),
path.call(print, "nodes", (node as Block).end.id),
]);
}
return [...mapped, builders.hardline]; return [...mapped, builders.hardline];
}; };

View file

@ -0,0 +1,3 @@
<title>{% block title %}{% endblock %}</title>
<div>class="{% if class %}{{ class }}{% endif %}"</div>

View file

@ -0,0 +1,3 @@
<title>{%block title%} {%endblock%}</title>
<div>class="{%if class%}{{class}}{%endif%}"</div>