.githooks/
compiler/
kernel/
lib/
examples/
std/
arch/
bits/
char/
collections/
fs/
hash/
lang/
alloc/
ast/
gen/
il/
layout/
lower/
manifest/
module/
parser/
resolver/
scanner/
sexpr/
alloc.rad
1.2 KiB
ast.rad
36.0 KiB
gen.rad
513 B
il.rad
48.5 KiB
layout.rad
6.9 KiB
lower.rad
487.6 KiB
manifest.rad
22.5 KiB
module.rad
17.8 KiB
package.rad
1.5 KiB
parser.rad
114.1 KiB
pretty.rad
103.6 KiB
resolver.rad
508.1 KiB
scanner.rad
19.3 KiB
sexpr.rad
7.1 KiB
strings.rad
3.3 KiB
types.rad
620 B
mem/
sys/
arch.rad
68 B
bits.rad
1.6 KiB
char.rad
1.5 KiB
collections.rad
39 B
fmt.rad
11.6 KiB
fs.rad
11.0 KiB
hash.rad
41 B
intrinsics.rad
467 B
io.rad
1.7 KiB
lang.rad
335 B
mem.rad
12.0 KiB
mem.ras
919 B
process.rad
6.2 KiB
std.manifest
4.9 KiB
std.rad
349 B
sys.rad
1.0 KiB
testing.rad
2.4 KiB
tests.rad
26.6 KiB
vec.rad
3.2 KiB
scripts/
seed/
sublime/
test/
vim/
.gitignore
333 B
.gitsigners
224 B
AGENTS.md
8.3 KiB
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
2.5 KiB
README
3.4 KiB
STYLE
3.2 KiB
std.lib
2.1 KiB
std.lib.test
1.1 KiB
lib/std/lang/layout.rad
raw
| 1 | //! Renderer-independent source layout. |
| 2 | //! |
| 3 | //! A layout is a tree of boxes. Each child of a box states where it goes |
| 4 | //! relative to the previous sibling: on the same line, on a new line, or on a |
| 5 | //! new line one level deeper. Soft placements let a renderer choose between a |
| 6 | //! flat and a broken form of each box, by its own measure of width, and |
| 7 | //! choices let it pick between whole alternative layouts. Tab stops line up |
| 8 | //! the content of consecutive rows. Layouts hold no output state, so text |
| 9 | //! printers, editors, and other views can render the same layout. |
| 10 | |
| 11 | export mod text; |
| 12 | @test mod tests; |
| 13 | |
| 14 | use std::lang::alloc; |
| 15 | use std::mem::heap; |
| 16 | use std::lang::ast; |
| 17 | |
| 18 | /// Number of columns that tab stops can line up. |
| 19 | export constant CELL_COLUMNS: u32 = 2; |
| 20 | |
| 21 | /// Purpose of a text fragment, for renderers that style or select text. |
| 22 | export union Role: Copy { |
| 23 | /// Reserved word, eg. `fn` or `match`. |
| 24 | Keyword, |
| 25 | /// Identifier, eg. a variable, type, or field name. |
| 26 | Name, |
| 27 | /// Number, string, character, boolean, or `nil` literal. |
| 28 | Literal, |
| 29 | /// Operator, eg. `+`, `==`, `and`, or `=`. |
| 30 | Operator, |
| 31 | /// Punctuation, eg. `(`, `,`, or `;`. |
| 32 | Punct, |
| 33 | /// Line comment, including the leading `//`. |
| 34 | Comment, |
| 35 | } |
| 36 | |
| 37 | /// Horizontal gap between a child and the content before it on the same line. |
| 38 | export union Gap: Copy { |
| 39 | /// No gap. |
| 40 | None, |
| 41 | /// One space. |
| 42 | Space, |
| 43 | } |
| 44 | |
| 45 | /// Where a child goes relative to the previous sibling in its box. |
| 46 | /// |
| 47 | /// The first child of a box goes relative to the content before the box. |
| 48 | export union Placement: Copy { |
| 49 | /// On the same line. |
| 50 | Inline(Gap), |
| 51 | /// On a new line at the box indentation. |
| 52 | Line, |
| 53 | /// On a new line one level deeper than the box indentation. |
| 54 | Indent, |
| 55 | /// `Inline` when the box is flat, otherwise `Line`. |
| 56 | SoftLine(Gap), |
| 57 | /// `Inline` when the box is flat, otherwise `Indent`. |
| 58 | SoftIndent(Gap), |
| 59 | } |
| 60 | |
| 61 | /// Child of a box with its placement. |
| 62 | export record Child: Copy { |
| 63 | /// Where the child goes relative to the previous sibling. |
| 64 | placement: Placement, |
| 65 | /// Child layout. |
| 66 | layout: *Layout, |
| 67 | } |
| 68 | |
| 69 | /// Layout element. |
| 70 | export union Item: Copy { |
| 71 | /// Text fragment without line breaks. |
| 72 | Text { |
| 73 | /// Text bytes. |
| 74 | text: *[u8], |
| 75 | /// Purpose of the text. |
| 76 | role: Role, |
| 77 | }, |
| 78 | /// Box of children whose soft placements are all flat or all broken. |
| 79 | /// |
| 80 | /// A box has the indentation of its parent, plus one level when it is |
| 81 | /// placed with `Indent`, or with `SoftIndent` in a broken parent. |
| 82 | Box(*[Child]), |
| 83 | /// Text that is present only when the parent box is broken, eg. the |
| 84 | /// trailing comma of a vertical list. |
| 85 | BrokenText { |
| 86 | /// Text bytes. |
| 87 | text: *[u8], |
| 88 | /// Purpose of the text. |
| 89 | role: Role, |
| 90 | }, |
| 91 | /// Empty line. Placed with `Line` or `Indent`. |
| 92 | Blank, |
| 93 | /// Alternative layouts of the same content, in order of preference. |
| 94 | /// |
| 95 | /// A renderer takes the first alternative that fits, and the last one when |
| 96 | /// none does. Content before a choice can break where its alternative that |
| 97 | /// breaks first does. Alternatives may share layouts. |
| 98 | Choice(*[*Layout]), |
| 99 | /// Tab stop of a column, eg. after the pattern of a match prong or after |
| 100 | /// the code that a trailing comment follows. |
| 101 | /// |
| 102 | /// A row is a child that starts a line and the children after it on the |
| 103 | /// same line, with everything inside them. A renderer takes consecutive |
| 104 | /// rows that each fit on one line as a run, and pads each row at its tab |
| 105 | /// stop of a column to the widest column in the run, so that the content |
| 106 | /// after the stops starts in one column. It leaves a run unpadded if the |
| 107 | /// padding would put content at or past the line width. A row that does |
| 108 | /// not fit on one line, a blank line, and a line on its own end a run. A |
| 109 | /// stop takes no space itself. A row has at most one stop of a column, |
| 110 | /// and the stops of lower columns come first. |
| 111 | Tab(u32), |
| 112 | /// Box that is broken whether or not it fits, eg. the last argument of a |
| 113 | /// call that opens on the line of the call. A choice measures it up to |
| 114 | /// its earliest possible line break. |
| 115 | Broken(*Layout), |
| 116 | } |
| 117 | |
| 118 | /// Layout tree node. |
| 119 | export record Layout: Copy { |
| 120 | /// AST node that this layout presents, if any. |
| 121 | source: ?*ast::Node, |
| 122 | /// Layout element. |
| 123 | item: Item, |
| 124 | /// Set of the columns of the tab stops in this layout and in the boxes |
| 125 | /// nested in it, as a bit mask. Set by [`new`]. |
| 126 | tabs: u32, |
| 127 | /// Whether a renderer that measures the layout flat finds a broken part |
| 128 | /// in it: a [`Item::Broken`] in it or in the boxes nested in it, or in the |
| 129 | /// first alternative of a choice. Set by [`new`]. |
| 130 | broken: bool, |
| 131 | /// Whether the layout is only text and boxes whose children are all on the |
| 132 | /// line of the previous sibling when the box is flat. If a renderer puts |
| 133 | /// such a layout flat on one line, so is every box in it. Set by [`new`]. |
| 134 | plain: bool, |
| 135 | } |
| 136 | |
| 137 | /// Allocate a layout node with the given source node and item. |
| 138 | export fn new( |
| 139 | a: &heap::Heap, source: ?*ast::Node, item: Item |
| 140 | ) -> *Layout throws (alloc::AllocError) { |
| 141 | let mut nodes: *mut [Layout] = &mut []; |
| 142 | try nodes.append( |
| 143 | Layout { |
| 144 | source, |
| 145 | item, |
| 146 | tabs: tabStops(item), |
| 147 | broken: hasBroken(item), |
| 148 | plain: isPlain(item), |
| 149 | }, |
| 150 | a |
| 151 | ); |
| 152 | |
| 153 | return &nodes[0]; |
| 154 | } |
| 155 | |
| 156 | /// Whether an item holds a broken part, in the form that a renderer measures. |
| 157 | fn hasBroken(item: Item) -> bool { |
| 158 | match item { |
| 159 | case Item::Broken(_) => return true, |
| 160 | case Item::Box(children) => { |
| 161 | for child in children { |
| 162 | if child.layout.broken { |
| 163 | return true; |
| 164 | } |
| 165 | } |
| 166 | return false; |
| 167 | } |
| 168 | case Item::Choice(alts) => return alts[0].broken, |
| 169 | else => return false, |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /// Whether an item is plain: see [`Layout::plain`]. |
| 174 | fn isPlain(item: Item) -> bool { |
| 175 | match item { |
| 176 | case Item::Text { .. }, Item::BrokenText { .. } => return true, |
| 177 | case Item::Box(children) => { |
| 178 | for child in children { |
| 179 | match child.placement { |
| 180 | case Placement::Line, Placement::Indent => return false, |
| 181 | else => {} |
| 182 | } |
| 183 | if not child.layout.plain { |
| 184 | return false; |
| 185 | } |
| 186 | } |
| 187 | return true; |
| 188 | } |
| 189 | else => return false, |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /// Columns of the tab stops of an item, as a bit mask. Only a stop and the |
| 194 | /// boxes that hold one count, as a renderer pads rows only by those. |
| 195 | fn tabStops(item: Item) -> u32 { |
| 196 | match item { |
| 197 | case Item::Tab(column) => return 1 << column, |
| 198 | case Item::Box(children) => { |
| 199 | let mut tabs: u32 = 0; |
| 200 | for child in children { |
| 201 | set tabs |= child.layout.tabs; |
| 202 | } |
| 203 | return tabs; |
| 204 | } |
| 205 | else => return 0, |
| 206 | } |
| 207 | } |