| @@ -12,6 +12,8 @@ |
| 12 | 12 | //! comment anywhere in a group therefore forces it to break. The soft |
| 13 | 13 | //! one coalesces with a following break (it ends a trailing comment's |
| 14 | 14 | //! line; a separator after it must not make a blank line). |
| 15 | +//! - [`Doc::Flat`] renders its contents on one line whatever the width: the |
| 16 | +//! groups inside it do not break (an interpolation hole, issue #315). |
| 15 | 17 | |
| 16 | 18 | use std::borrow::Cow; |
| 17 | 19 | |
| @@ -40,6 +42,10 @@ pub enum Doc<'a> { |
| 40 | 42 | /// needs no room for its whole flat width. |
| 41 | 43 | Group(Box<Doc<'a>>, bool), |
| 42 | 44 | Cat(Vec<Doc<'a>>), |
| 45 | + /// Contents rendered flat, however long the line: groups inside it |
| 46 | + /// neither probe nor break. Only for contents without a hard break |
| 47 | + /// ([`has_hard_break`]), which would still end the line. |
| 48 | + Flat(Box<Doc<'a>>), |
| 43 | 49 | } |
| 44 | 50 | |
| 45 | 51 | #[cfg(test)] |
| @@ -69,6 +75,23 @@ pub fn group_opaque<'a>(d: Doc<'a>) -> Doc<'a> { |
| 69 | 75 | Doc::Group(Box::new(d), true) |
| 70 | 76 | } |
| 71 | 77 | |
| 78 | +/// `d` on one line, whatever the width (see [`Doc::Flat`]). |
| 79 | +pub fn flat<'a>(d: Doc<'a>) -> Doc<'a> { |
| 80 | + Doc::Flat(Box::new(d)) |
| 81 | +} |
| 82 | + |
| 83 | +/// Whether `d` holds a break that no width removes: a hard line, or text |
| 84 | +/// spanning lines. |
| 85 | +pub fn has_hard_break(d: &Doc) -> bool { |
| 86 | + match d { |
| 87 | + Doc::HardLine | Doc::HardLineSoft => true, |
| 88 | + Doc::Text(s) => s.contains('\n'), |
| 89 | + Doc::Nil | Doc::Line | Doc::SoftLine | Doc::LeadComma => false, |
| 90 | + Doc::Nest(_, d) | Doc::Group(d, _) | Doc::Flat(d) => has_hard_break(d), |
| 91 | + Doc::Cat(docs) => docs.iter().any(has_hard_break), |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 72 | 95 | pub fn nest<'a>(levels: u32, d: Doc<'a>) -> Doc<'a> { |
| 73 | 96 | Doc::Nest(levels, Box::new(d)) |
| 74 | 97 | } |
| @@ -89,6 +112,14 @@ pub fn join<'a>(sep: Doc<'a>, docs: Vec<Doc<'a>>) -> Doc<'a> { |
| 89 | 112 | enum Mode { |
| 90 | 113 | Flat, |
| 91 | 114 | Break, |
| 115 | + /// Flat, and so is every group inside: under a [`Doc::Flat`]. |
| 116 | + Forced, |
| 117 | +} |
| 118 | + |
| 119 | +impl Mode { |
| 120 | + fn breaks(self) -> bool { |
| 121 | + self == Mode::Break |
| 122 | + } |
| 92 | 123 | } |
| 93 | 124 | |
| 94 | 125 | /// Column after appending `s` at column `col`: the width of its last line. |
| @@ -131,30 +162,28 @@ where |
| 131 | 162 | } |
| 132 | 163 | } |
| 133 | 164 | } |
| 134 | | − Doc::Line => match mode { |
| 135 | | − Mode::Flat => width -= 1, |
| 136 | | − Mode::Break => return true, |
| 137 | | − }, |
| 138 | | − Doc::SoftLine => match mode { |
| 139 | | − Mode::Flat => {} |
| 140 | | − Mode::Break => return true, |
| 141 | | − }, |
| 142 | | − Doc::LeadComma => match mode { |
| 143 | | − Mode::Flat => width -= 2, |
| 144 | | − Mode::Break => return true, |
| 145 | | − }, |
| 165 | + Doc::Line if mode.breaks() => return true, |
| 166 | + Doc::Line => width -= 1, |
| 167 | + Doc::SoftLine if mode.breaks() => return true, |
| 168 | + Doc::SoftLine => {} |
| 169 | + Doc::LeadComma if mode.breaks() => return true, |
| 170 | + Doc::LeadComma => width -= 2, |
| 146 | 171 | Doc::HardLine | Doc::HardLineSoft => return !content, |
| 147 | 172 | Doc::Nest(n, d) => stack.push((ind + n, mode, content, d)), |
| 148 | 173 | // Groups are flattened for measurement; in the rest of the |
| 149 | | − // line an opaque group counts only up to its first break. |
| 174 | + // line an opaque group counts only up to its first break, |
| 175 | + // unless it is under a `Flat`, which never breaks. |
| 150 | 176 | Doc::Group(d, opaque) => { |
| 151 | | − let mode = if content || !opaque { |
| 177 | + let mode = if mode == Mode::Forced { |
| 178 | + Mode::Forced |
| 179 | + } else if content || !opaque { |
| 152 | 180 | Mode::Flat |
| 153 | 181 | } else { |
| 154 | 182 | Mode::Break |
| 155 | 183 | }; |
| 156 | 184 | stack.push((ind, mode, content, d)); |
| 157 | 185 | } |
| 186 | + Doc::Flat(d) => stack.push((ind, Mode::Forced, content, d)), |
| 158 | 187 | Doc::Cat(docs) => { |
| 159 | 188 | for d in docs.iter().rev() { |
| 160 | 189 | stack.push((ind, mode, content, d)); |
| @@ -192,21 +221,20 @@ pub fn render(doc: &Doc, width: usize) -> String { |
| 192 | 221 | out.push_str(s); |
| 193 | 222 | col = advance(col, s); |
| 194 | 223 | } |
| 195 | | − Doc::Line => match mode { |
| 196 | | − Mode::Flat => { |
| 197 | | − out.push(' '); |
| 198 | | − col += 1; |
| 199 | | − } |
| 200 | | − Mode::Break => { |
| 224 | + Doc::Line => { |
| 225 | + if mode.breaks() { |
| 201 | 226 | if pending.is_none() { |
| 202 | 227 | push_newline(&mut out); |
| 203 | 228 | } |
| 204 | 229 | pending = Some(ind); |
| 205 | 230 | col = ind as usize * INDENT; |
| 231 | + } else { |
| 232 | + out.push(' '); |
| 233 | + col += 1; |
| 206 | 234 | } |
| 207 | | − }, |
| 235 | + } |
| 208 | 236 | Doc::SoftLine => { |
| 209 | | − if mode == Mode::Break { |
| 237 | + if mode.breaks() { |
| 210 | 238 | if pending.is_none() { |
| 211 | 239 | push_newline(&mut out); |
| 212 | 240 | } |
| @@ -214,12 +242,8 @@ pub fn render(doc: &Doc, width: usize) -> String { |
| 214 | 242 | col = ind as usize * INDENT; |
| 215 | 243 | } |
| 216 | 244 | } |
| 217 | | − Doc::LeadComma => match mode { |
| 218 | | − Mode::Flat => { |
| 219 | | − out.push_str(", "); |
| 220 | | − col += 2; |
| 221 | | − } |
| 222 | | − Mode::Break => { |
| 245 | + Doc::LeadComma => { |
| 246 | + if mode.breaks() { |
| 223 | 247 | // The comma aligns with the brackets (one level out |
| 224 | 248 | // from the items, which are nested one in). |
| 225 | 249 | if pending.is_none() { |
| @@ -232,8 +256,11 @@ pub fn render(doc: &Doc, width: usize) -> String { |
| 232 | 256 | out.push_str(", "); |
| 233 | 257 | col = ind as usize * INDENT + 2; |
| 234 | 258 | pending = None; |
| 259 | + } else { |
| 260 | + out.push_str(", "); |
| 261 | + col += 2; |
| 235 | 262 | } |
| 236 | | − }, |
| 263 | + } |
| 237 | 264 | Doc::HardLine => { |
| 238 | 265 | push_newline(&mut out); |
| 239 | 266 | pending = Some(ind); |
| @@ -247,6 +274,8 @@ pub fn render(doc: &Doc, width: usize) -> String { |
| 247 | 274 | col = ind as usize * INDENT; |
| 248 | 275 | } |
| 249 | 276 | Doc::Nest(n, d) => stack.push((ind + n, mode, d)), |
| 277 | + Doc::Group(d, _) if mode == Mode::Forced => stack.push((ind, Mode::Forced, d)), |
| 278 | + Doc::Flat(d) => stack.push((ind, Mode::Forced, d)), |
| 250 | 279 | Doc::Group(d, _) => { |
| 251 | 280 | // `fits` pops from the end: the probed content goes last, |
| 252 | 281 | // under it the rest of the line, top of stack first. |
| @@ -322,4 +351,29 @@ mod tests { |
| 322 | 351 | let out = r(&doc, 80); |
| 323 | 352 | assert!(out.starts_with("[\n aa\n, {"), "{out}"); |
| 324 | 353 | } |
| 354 | + |
| 355 | + #[test] |
| 356 | + fn a_flat_doc_never_breaks() { |
| 357 | + let doc = cat([text("x = "), flat(list(vec![text("aaaa"), text("bbbb")]))]); |
| 358 | + assert_eq!(r(&doc, 5), "x = [aaaa, bbbb]"); |
| 359 | + // Groups inside it don't break either, however narrow the line. |
| 360 | + let nested = flat(list(vec![list(vec![text("aa"), text("bb")]), text("cc")])); |
| 361 | + assert_eq!(r(&nested, 1), "[[aa, bb], cc]"); |
| 362 | + } |
| 363 | + |
| 364 | + #[test] |
| 365 | + fn a_flat_doc_counts_its_whole_width_for_what_precedes_it() { |
| 366 | + let before = group(cat([text("a"), Doc::Line, text("b")])); |
| 367 | + let doc = cat([before, flat(list(vec![text("cc"), text("dd")]))]); |
| 368 | + assert_eq!(r(&doc, 20), "a b[cc, dd]"); |
| 369 | + assert_eq!(r(&doc, 8), "a\nb[cc, dd]"); |
| 370 | + } |
| 371 | + |
| 372 | + #[test] |
| 373 | + fn hard_breaks_are_found_anywhere_in_a_doc() { |
| 374 | + assert!(!has_hard_break(&list(vec![text("a"), text("b")]))); |
| 375 | + assert!(has_hard_break(&nest(1, cat([text("a"), Doc::HardLine])))); |
| 376 | + assert!(has_hard_break(&group(Doc::HardLineSoft))); |
| 377 | + assert!(has_hard_break(&text("two\nlines"))); |
| 378 | + } |
| 325 | 379 | } |