Introduce second argument to ifBreak (#302)

master
Christopher Chedeau 2017-01-18 12:13:50 -08:00 committed by James Long
parent 87b0ed3042
commit 4c1a7b1082
1 changed files with 32 additions and 4 deletions

View File

@ -51,8 +51,15 @@ function conditionalGroup(states, opts) {
); );
} }
function ifBreak(contents) { function ifBreak(breakContents, flatContents) {
return { type: "if-break", contents }; if (breakContents) {
assertDoc(breakContents);
}
if (flatContents) {
assertDoc(flatContents);
}
return { type: "if-break", breakContents, flatContents };
} }
function iterDoc(topDoc, func) { function iterDoc(topDoc, func) {
@ -78,6 +85,13 @@ function iterDoc(topDoc, func) {
for (var i = doc.parts.length - 1; i >= 0; i--) { for (var i = doc.parts.length - 1; i >= 0; i--) {
docs.push(doc.parts[i]); docs.push(doc.parts[i]);
} }
} else if (doc.type === "if-break") {
if (doc.breakContents) {
docs.push(doc.breakContents);
}
if (doc.flatContents) {
docs.push(doc.flatContents);
}
} else if (doc.type !== "line") { } else if (doc.type !== "line") {
docs.push(doc.contents); docs.push(doc.contents);
} }
@ -175,7 +189,14 @@ function fits(next, restCommands, width) {
break; break;
case "if-break": case "if-break":
if (mode === MODE_BREAK) { if (mode === MODE_BREAK) {
cmds.push([ ind, mode, doc.contents ]); if (doc.breakContents) {
cmds.push([ ind, mode, doc.breakContents ]);
}
}
if (mode === MODE_FLAT) {
if (doc.flatContents) {
cmds.push([ ind, mode, doc.flatContents ]);
}
} }
break; break;
@ -297,7 +318,14 @@ function print(w, doc) {
break; break;
case "if-break": case "if-break":
if (mode === MODE_BREAK) { if (mode === MODE_BREAK) {
cmds.push([ ind, MODE_BREAK, doc.contents ]); if (doc.breakContents) {
cmds.push([ ind, mode, doc.breakContents ]);
}
}
if (mode === MODE_FLAT) {
if (doc.flatContents) {
cmds.push([ ind, mode, doc.flatContents ]);
}
} }
break; break;