| @@ -25,7 +25,9 @@ |
| 25 | 25 | //! their own line, trailing at the end of one) and force the group they |
| 26 | 26 | //! are in to break; blank lines between statements and items are kept (at |
| 27 | 27 | //! most one). String and template text is never touched, interpolation |
| 28 | | −//! holes are formatted as code, and HTML literals are only re-indented. |
| 28 | +//! holes are formatted as code but kept on one line (the text around them |
| 29 | +//! is verbatim, so a break could only follow the code's indentation; issue |
| 30 | +//! #315), and HTML literals are only re-indented. |
| 29 | 31 | //! Three normalizations go beyond whitespace: a trailing comma before a |
| 30 | 32 | //! closing bracket is dropped in leading-comma lists (a one-element tuple |
| 31 | 33 | //! keeps its significant comma), the optional commas between match arms, |
| @@ -1261,7 +1263,10 @@ impl<'a> Printer<'a> { |
| 1261 | 1263 | pretty::cat(items.iter().map(|i| self.item_doc(i))) |
| 1262 | 1264 | } |
| 1263 | 1265 | |
| 1264 | | − /// `{e}` or `{e:?}`. |
| 1266 | + /// `{e}` or `{e:?}`, on one line: the text around a hole is verbatim, |
| 1267 | + /// so a break inside it could only be indented to the code, not to the |
| 1268 | + /// string or template (issue #315). A hole that cannot be flat (a |
| 1269 | + /// `match`, a comment) is laid out as code. |
| 1265 | 1270 | fn interp_hole(&self, node: &SyntaxNode) -> Doc<'a> { |
| 1266 | 1271 | let Seq { items, .. } = self.seq(node); |
| 1267 | 1272 | let mut docs = vec![self.t("{")]; |
| @@ -1273,7 +1278,12 @@ impl<'a> Printer<'a> { |
| 1273 | 1278 | } |
| 1274 | 1279 | } |
| 1275 | 1280 | docs.push(self.t("}")); |
| 1276 | | − pretty::cat(docs) |
| 1281 | + let hole = pretty::cat(docs); |
| 1282 | + if pretty::has_hard_break(&hole) { |
| 1283 | + hole |
| 1284 | + } else { |
| 1285 | + pretty::flat(hole) |
| 1286 | + } |
| 1277 | 1287 | } |
| 1278 | 1288 | |
| 1279 | 1289 | /// `tag\`text {e} text\``: the tag, then text verbatim, holes as code. |
| @@ -2216,6 +2226,44 @@ mod tests { |
| 2216 | 2226 | } |
| 2217 | 2227 | |
| 2218 | 2228 | #[test] |
| 2229 | + fn template_holes_stay_on_one_line() { |
| 2230 | + // Issue #315: a hole on a template line too long for the width |
| 2231 | + // used to break, indented to the code instead of the template. |
| 2232 | + let call = "fn f() {\n pug`\n time(datetime={updated}) {string.slice(updated, 0, 10)}\n `\n}\n"; |
| 2233 | + assert_eq!(narrow(call), call); |
| 2234 | + let cond = "fn f() {\n pug`\n button(aria-pressed={if tagged { \"true\" } else { \"false\" }}) Tag\n `\n}\n"; |
| 2235 | + assert_eq!(narrow(cond), cond); |
| 2236 | + let lambda = "fn f() {\n pug`\n ul {list.map(labels, fn(label) { pug`li {label.name}` })}\n `\n}\n"; |
| 2237 | + assert_eq!(narrow(lambda), lambda); |
| 2238 | + // Spacing inside a hole is still the formatter's. |
| 2239 | + assert_eq!( |
| 2240 | + narrow("fn f() {\n pug`\n p {f(a,b)}\n `\n}\n"), |
| 2241 | + "fn f() {\n pug`\n p {f(a, b)}\n `\n}\n" |
| 2242 | + ); |
| 2243 | + } |
| 2244 | + |
| 2245 | + #[test] |
| 2246 | + fn string_holes_stay_on_one_line() { |
| 2247 | + let src = |
| 2248 | + "fn f() {\n \"a long string around {string.slice(updated, 0, 10)} its hole\"\n}\n"; |
| 2249 | + assert_eq!(narrow(src), src); |
| 2250 | + } |
| 2251 | + |
| 2252 | + #[test] |
| 2253 | + fn a_hole_that_cannot_be_flat_is_formatted_as_code() { |
| 2254 | + // A match puts one arm per line and a comment ends its line: such |
| 2255 | + // a hole keeps the layout code gets, and stays stable. |
| 2256 | + for src in [ |
| 2257 | + "fn f() {\n pug`\n p {match x { A -> 1, B -> 2 }}\n `\n}\n", |
| 2258 | + "fn f() {\n \"{x // why\n}\"\n}\n", |
| 2259 | + ] { |
| 2260 | + let once = narrow(src); |
| 2261 | + assert!(once.contains('\n'), "{once}"); |
| 2262 | + assert_eq!(narrow(&once), once); |
| 2263 | + } |
| 2264 | + } |
| 2265 | + |
| 2266 | + #[test] |
| 2219 | 2267 | fn trailing_commas_in_lists_are_dropped() { |
| 2220 | 2268 | assert_eq!(fmt("fn f() {\n f(a, b,)\n}\n"), "fn f() {\n f(a, b)\n}\n"); |
| 2221 | 2269 | } |