commit bcdecc67522dc572e9bde2305a4f47d791c12591
parent 5656968d451bdd448429e6351afcbf7cc222fb85
Author: Sebastian <sebastian@sebsite.pw>
Date: Tue, 1 Mar 2022 17:18:30 -0500
unparse, haredoc: linewrap long tagged/tuple types
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Diffstat:
3 files changed, 114 insertions(+), 24 deletions(-)
diff --git a/cmd/haredoc/html.ha b/cmd/haredoc/html.ha
@@ -559,22 +559,52 @@ fn type_html(
z += fmt::fprintf(out, "<span class='type'>{}</span>",
unparse::builtin_type(t))?;
case let t: ast::tagged_type =>
- z += fmt::fprint(out, "(")?;
+ // rough estimate of current line length
+ let linelen: size = z + (indent + 1) * 8;
+ z = 0;
+ linelen += fmt::fprint(out, "(")?;
for (let i = 0z; i < len(t); i += 1) {
- z += type_html(out, indent, *t[i], brief)?;
- if (i < len(t) - 1) {
- z += fmt::fprint(out, " | ")?;
+ linelen += type_html(out, indent, *t[i], brief)?;
+ if (i + 1 == len(t)) break;
+ linelen += fmt::fprint(out, " |")?;
+ // use 72 instead of 80 to give a bit of leeway for long
+ // type names
+ if (linelen > 72) {
+ z += linelen;
+ linelen = (indent + 1) * 8;
+ z += fmt::fprintln(out)?;
+ for (let i = 0z; i < indent; i += 1) {
+ z += fmt::fprint(out, "\t")?;
+ };
+ } else {
+ linelen += fmt::fprint(out, " ")?;
};
};
+ z += linelen;
z += fmt::fprint(out, ")")?;
case let t: ast::tuple_type =>
- z += fmt::fprint(out, "(")?;
+ // rough estimate of current line length
+ let linelen: size = z + (indent + 1) * 8;
+ z = 0;
+ linelen += fmt::fprint(out, "(")?;
for (let i = 0z; i < len(t); i += 1) {
- z += type_html(out, indent, *t[i], brief)?;
- if (i < len(t) - 1) {
- z += fmt::fprint(out, ", ")?;
+ linelen += type_html(out, indent, *t[i], brief)?;
+ if (i + 1 == len(t)) break;
+ linelen += fmt::fprint(out, ",")?;
+ // use 72 instead of 80 to give a bit of leeway for long
+ // type names
+ if (linelen > 72) {
+ z += linelen;
+ linelen = (indent + 1) * 8;
+ z += fmt::fprintln(out)?;
+ for (let i = 0z; i < indent; i += 1) {
+ z += fmt::fprint(out, "\t")?;
+ };
+ } else {
+ linelen += fmt::fprint(out, " ")?;
};
};
+ z += linelen;
z += fmt::fprint(out, ")")?;
case let t: ast::pointer_type =>
if (t.flags & ast::pointer_flags::NULLABLE != 0) {
diff --git a/cmd/haredoc/tty.ha b/cmd/haredoc/tty.ha
@@ -328,22 +328,52 @@ fn type_tty(
case ast::union_type =>
n += struct_union_type_tty(out, indent, t)?;
case let t: ast::tagged_type =>
- n += fmt::fprint(out, "(")?;
+ // rough estimate of current line length
+ let linelen: size = n + (indent + 1) * 8;
+ n = 0;
+ linelen += fmt::fprint(out, "(")?;
for (let i = 0z; i < len(t); i += 1) {
- n += type_tty(out, indent, *t[i])?;
- if (i + 1 < len(t)) {
- n += fmt::fprint(out, " | ")?;
+ linelen += type_tty(out, indent, *t[i])?;
+ if (i + 1 == len(t)) break;
+ linelen += fmt::fprint(out, " |")?;
+ // use 72 instead of 80 to give a bit of leeway for long
+ // type names
+ if (linelen > 72) {
+ n += linelen;
+ linelen = (indent + 1) * 8;
+ n += fmt::fprintln(out)?;
+ for (let i = 0z; i <= indent; i += 1) {
+ n += fmt::fprint(out, "\t")?;
+ };
+ } else {
+ linelen += fmt::fprint(out, " ")?;
};
};
+ n += linelen;
n += fmt::fprint(out, ")")?;
case let t: ast::tuple_type =>
- n += fmt::fprint(out, "(")?;
+ // rough estimate of current line length
+ let linelen: size = n + (indent + 1) * 8;
+ n = 0;
+ linelen += fmt::fprint(out, "(")?;
for (let i = 0z; i < len(t); i += 1) {
- n += type_tty(out, indent, *t[i])?;
- if (i + 1 < len(t)) {
- n += fmt::fprint(out, ", ")?;
+ linelen += type_tty(out, indent, *t[i])?;
+ if (i + 1 == len(t)) break;
+ linelen += fmt::fprint(out, ",")?;
+ // use 72 instead of 80 to give a bit of leeway for long
+ // type names
+ if (linelen > 72) {
+ n += linelen;
+ linelen = (indent + 1) * 8;
+ n += fmt::fprintln(out)?;
+ for (let i = 0z; i <= indent; i += 1) {
+ n += fmt::fprint(out, "\t")?;
+ };
+ } else {
+ linelen += fmt::fprint(out, " ")?;
};
};
+ n += linelen;
n += fmt::fprint(out, ")")?;
};
return n;
diff --git a/hare/unparse/type.ha b/hare/unparse/type.ha
@@ -205,22 +205,52 @@ export fn _type(
case ast::union_type =>
n += struct_union_type(out, indent, t)?;
case let t: ast::tagged_type =>
- n += fmt::fprint(out, "(")?;
+ // rough estimate of current line length
+ let linelen: size = n + (indent + 1) * 8;
+ n = 0;
+ linelen += fmt::fprint(out, "(")?;
for (let i = 0z; i < len(t); i += 1) {
- n += _type(out, indent, *t[i])?;
- if (i + 1 < len(t)) {
- n += fmt::fprint(out, " | ")?;
+ linelen += _type(out, indent, *t[i])?;
+ if (i + 1 == len(t)) break;
+ linelen += fmt::fprint(out, " |")?;
+ // use 72 instead of 80 to give a bit of leeway for long
+ // type names
+ if (linelen > 72) {
+ n += linelen;
+ linelen = (indent + 1) * 8;
+ n += fmt::fprintln(out)?;
+ for (let i = 0z; i <= indent; i += 1) {
+ n += fmt::fprint(out, "\t")?;
+ };
+ } else {
+ linelen += fmt::fprint(out, " ")?;
};
};
+ n += linelen;
n += fmt::fprint(out, ")")?;
case let t: ast::tuple_type =>
- n += fmt::fprint(out, "(")?;
+ // rough estimate of current line length
+ let linelen: size = n + (indent + 1) * 8;
+ n = 0;
+ linelen += fmt::fprint(out, "(")?;
for (let i = 0z; i < len(t); i += 1) {
- n += _type(out, indent, *t[i])?;
- if (i + 1 < len(t)) {
- n += fmt::fprint(out, ", ")?;
+ linelen += _type(out, indent, *t[i])?;
+ if (i + 1 == len(t)) break;
+ linelen += fmt::fprint(out, ",")?;
+ // use 72 instead of 80 to give a bit of leeway for long
+ // type names
+ if (linelen > 72) {
+ n += linelen;
+ linelen = (indent + 1) * 8;
+ n += fmt::fprintln(out)?;
+ for (let i = 0z; i <= indent; i += 1) {
+ n += fmt::fprint(out, "\t")?;
+ };
+ } else {
+ linelen += fmt::fprint(out, " ")?;
};
};
+ n += linelen;
n += fmt::fprint(out, ")")?;
};
return n;