| @@ -212,7 +212,7 @@ fn walked_import(node: &SyntaxNode, text: &str, out: &mut Vec<SyntaxToken>) { |
| 212 | 212 | } |
| 213 | 213 | if let Some(as_at) = toks.iter().position(|t| t.kind() == AS_KW) { |
| 214 | 214 | let alias = toks[as_at + 1..].iter().find(|t| t.kind() == IDENT); |
| 215 | | − let last_seg = toks[..as_at].iter().filter(|t| t.kind() == IDENT).next_back(); |
| 215 | + let last_seg = toks[..as_at].iter().rev().find(|t| t.kind() == IDENT); |
| 216 | 216 | if let (Some(alias), Some(seg)) = (alias, last_seg) |
| 217 | 217 | && alias.text() == seg.text() |
| 218 | 218 | { |
| @@ -265,21 +265,18 @@ fn walked_source_file(node: &SyntaxNode, text: &str, out: &mut Vec<SyntaxToken>) |
| 265 | 265 | if out.is_empty() && run.is_empty() { |
| 266 | 266 | out.append(&mut pending); |
| 267 | 267 | } |
| 268 | | − let key = { |
| 269 | | − let path = n |
| 270 | | − .children() |
| 271 | | − .find(|c| c.kind() == IMPORT_PATH) |
| 272 | | − .map(|p| { |
| 273 | | − p.descendants_with_tokens() |
| 274 | | − .filter_map(|e| e.into_token()) |
| 275 | | − .filter(|t| t.kind() == IDENT) |
| 276 | | − .map(|t| t.text().to_owned()) |
| 277 | | − .collect::<Vec<_>>() |
| 278 | | − .join("/") |
| 279 | | − }) |
| 280 | | − .unwrap_or_default(); |
| 281 | | − path |
| 282 | | − }; |
| 268 | + let key = n |
| 269 | + .children() |
| 270 | + .find(|c| c.kind() == IMPORT_PATH) |
| 271 | + .map(|p| { |
| 272 | + p.descendants_with_tokens() |
| 273 | + .filter_map(|e| e.into_token()) |
| 274 | + .filter(|t| t.kind() == IDENT) |
| 275 | + .map(|t| t.text().to_owned()) |
| 276 | + .collect::<Vec<_>>() |
| 277 | + .join("/") |
| 278 | + }) |
| 279 | + .unwrap_or_default(); |
| 283 | 280 | let mut toks = std::mem::take(&mut pending); |
| 284 | 281 | walked_tokens(&n, text, &mut toks); |
| 285 | 282 | run.push((key, toks)); |
| @@ -324,8 +321,7 @@ fn walked_import_list(node: &SyntaxNode, text: &str, out: &mut Vec<SyntaxToken>) |
| 324 | 321 | let is_type = toks.iter().any(|t| t.kind() == TYPE_KW); |
| 325 | 322 | let name = toks |
| 326 | 323 | .iter() |
| 327 | | − .filter(|t| t.kind() == IDENT) |
| 328 | | − .next() |
| 324 | + .find(|t| t.kind() == IDENT) |
| 329 | 325 | .map(|t| t.text().to_owned()) |
| 330 | 326 | .unwrap_or_default(); |
| 331 | 327 | let mut group = std::mem::take(&mut pending); |
| @@ -1111,12 +1107,11 @@ impl<'a> Printer<'a> { |
| 1111 | 1107 | /// comments kept. |
| 1112 | 1108 | fn import(&self, node: &SyntaxNode) -> Doc<'a> { |
| 1113 | 1109 | let Seq { items, .. } = self.seq(node); |
| 1114 | | − let redundant_alias = ast::Import::cast(node.clone()).is_some_and(|i| { |
| 1115 | | − match (i.alias(), i.path().last()) { |
| 1110 | + let redundant_alias = |
| 1111 | + ast::Import::cast(node.clone()).is_some_and(|i| match (i.alias(), i.path().last()) { |
| 1116 | 1112 | (Some(a), Some(last)) => a.text() == last.text(), |
| 1117 | 1113 | _ => false, |
| 1118 | | − } |
| 1119 | | − }); |
| 1114 | + }); |
| 1120 | 1115 | let mut head = vec![self.t("import ")]; |
| 1121 | 1116 | let mut list = None; |
| 1122 | 1117 | let mut tail: Vec<Doc> = Vec::new(); |
| @@ -1199,7 +1194,8 @@ impl<'a> Printer<'a> { |
| 1199 | 1194 | } |
| 1200 | 1195 | _ => { |
| 1201 | 1196 | let mut item = item; |
| 1202 | | − item.leading.splice(0..0, pending.drain(..).map(|c| (c, false))); |
| 1197 | + item.leading |
| 1198 | + .splice(0..0, pending.drain(..).map(|c| (c, false))); |
| 1203 | 1199 | let key = match &item.el { |
| 1204 | 1200 | NodeOrToken::Node(n) => ast::ImportItem::cast(n.clone()) |
| 1205 | 1201 | .map(|i| { |
| @@ -1835,10 +1831,7 @@ impl<'a> Printer<'a> { |
| 1835 | 1831 | // (rev. 6). |
| 1836 | 1832 | docs.push(pretty::group(pretty::cat([ |
| 1837 | 1833 | open, |
| 1838 | | − pretty::nest( |
| 1839 | | − 1, |
| 1840 | | − pretty::cat([Doc::Line, self.seq_soft(members, &end)]), |
| 1841 | | − ), |
| 1834 | + pretty::nest(1, pretty::cat([Doc::Line, self.seq_soft(members, &end)])), |
| 1842 | 1835 | Doc::Line, |
| 1843 | 1836 | self.t("}"), |
| 1844 | 1837 | ]))); |
| @@ -2511,8 +2504,7 @@ impl<'a> Printer<'a> { |
| 2511 | 2504 | _ => return self.raw(node), |
| 2512 | 2505 | } |
| 2513 | 2506 | } |
| 2514 | | − let (Some(body), Some(open), Some(close), Some(with)) = (body, open, close, with) |
| 2515 | | − else { |
| 2507 | + let (Some(body), Some(open), Some(close), Some(with)) = (body, open, close, with) else { |
| 2516 | 2508 | return self.raw(node); |
| 2517 | 2509 | }; |
| 2518 | 2510 | pretty::group(pretty::cat([ |
| @@ -2791,11 +2783,11 @@ fn flush_import_run_items(run: &mut Vec<Item>, out: &mut Vec<Item>) { |
| 2791 | 2783 | for i in run.iter_mut() { |
| 2792 | 2784 | i.blank_before = false; |
| 2793 | 2785 | } |
| 2794 | | − run.sort_by(|a, b| import_path_key(a).cmp(&import_path_key(b))); |
| 2786 | + run.sort_by_key(import_path_key); |
| 2795 | 2787 | if let Some(first) = run.first_mut() { |
| 2796 | 2788 | first.blank_before = blank; |
| 2797 | 2789 | } |
| 2798 | | − out.extend(run.drain(..)); |
| 2790 | + out.append(run); |
| 2799 | 2791 | } |
| 2800 | 2792 | |
| 2801 | 2793 | /// An import's sort key: its path (`std/process`); the `as` alias is not |
| @@ -2803,7 +2795,13 @@ fn flush_import_run_items(run: &mut Vec<Item>, out: &mut Vec<Item>) { |
| 2803 | 2795 | fn import_path_key(item: &Item) -> String { |
| 2804 | 2796 | match &item.el { |
| 2805 | 2797 | NodeOrToken::Node(n) => ast::Import::cast(n.clone()) |
| 2806 | | − .map(|i| i.path().iter().map(|s| s.text()).collect::<Vec<_>>().join("/")) |
| 2798 | + .map(|i| { |
| 2799 | + i.path() |
| 2800 | + .iter() |
| 2801 | + .map(|s| s.text()) |
| 2802 | + .collect::<Vec<_>>() |
| 2803 | + .join("/") |
| 2804 | + }) |
| 2807 | 2805 | .unwrap_or_default(), |
| 2808 | 2806 | NodeOrToken::Token(t) => t.text().to_owned(), |
| 2809 | 2807 | } |
| @@ -2913,7 +2911,9 @@ mod tests { |
| 2913 | 2911 | // Trailing commas move to the front when broken; the last one |
| 2914 | 2912 | // goes. |
| 2915 | 2913 | assert_eq!( |
| 2916 | | − narrow("fn f :: x {\n match x {\n Some(a_longer_name) -> a_longer_call x,\n None -> 0,\n }\n}\n"), |
| 2914 | + narrow( |
| 2915 | + "fn f :: x {\n match x {\n Some(a_longer_name) -> a_longer_call x,\n None -> 0,\n }\n}\n" |
| 2916 | + ), |
| 2917 | 2917 | "fn f :: x {\n match x\n { Some(a_longer_name) ->\n a_longer_call x\n , None -> 0\n }\n}\n" |
| 2918 | 2918 | ); |
| 2919 | 2919 | // An arm body broken after `->` rejoins when it fits. |
| @@ -2948,7 +2948,8 @@ mod tests { |
| 2948 | 2948 | ); |
| 2949 | 2949 | // ... and stay there once the comma leads (the comment and the |
| 2950 | 2950 | // blank line then belong to the comma, in the arm above). |
| 2951 | | − let led = "fn f :: x {\n match x\n { A -> 1\n\n // two\n , B -> 2\n\n , C -> 3\n }\n}\n"; |
| 2951 | + let led = |
| 2952 | + "fn f :: x {\n match x\n { A -> 1\n\n // two\n , B -> 2\n\n , C -> 3\n }\n}\n"; |
| 2952 | 2953 | assert_eq!(fmt(led), led); |
| 2953 | 2954 | } |
| 2954 | 2955 | |
| @@ -2982,7 +2983,9 @@ mod tests { |
| 2982 | 2983 | // Broken: the body stays with `handle`, the clauses move under |
| 2983 | 2984 | // the `with`, at its indent. |
| 2984 | 2985 | assert_eq!( |
| 2985 | | − narrow("fn f :: {\n handle { g () } with { ask -> resume a_longer_value, return x -> x } }\n"), |
| 2986 | + narrow( |
| 2987 | + "fn f :: {\n handle { g () } with { ask -> resume a_longer_value, return x -> x } }\n" |
| 2988 | + ), |
| 2986 | 2989 | "fn f :: {\n handle { g () }\n with\n { ask -> resume a_longer_value\n , return x -> x\n }\n}\n" |
| 2987 | 2990 | ); |
| 2988 | 2991 | } |
| @@ -3000,7 +3003,9 @@ mod tests { |
| 3000 | 3003 | // result and `where` under them; a `where`'s comma leads the next |
| 3001 | 3004 | // variant. |
| 3002 | 3005 | assert_eq!( |
| 3003 | | − narrow("type LongerNames<row> {\n Lit(a) -> LongerNames<a> where Show<a>, Eq<a>,\n Zero -> LongerNames<Int>\n}\n"), |
| 3006 | + narrow( |
| 3007 | + "type LongerNames<row> {\n Lit(a) -> LongerNames<a> where Show<a>, Eq<a>,\n Zero -> LongerNames<Int>\n}\n" |
| 3008 | + ), |
| 3004 | 3009 | "type LongerNames<row>\n{ Lit\n (a)\n -> LongerNames<a>\n where Show<a>, Eq<a>\n, Zero -> LongerNames<Int>\n}\n" |
| 3005 | 3010 | ); |
| 3006 | 3011 | } |
| @@ -3090,7 +3095,8 @@ mod tests { |
| 3090 | 3095 | fn template_holes_stay_on_one_line() { |
| 3091 | 3096 | // Issue #315: a hole on a template line too long for the width |
| 3092 | 3097 | // used to break, indented to the code instead of the template. |
| 3093 | | − let call = "fn f :: {\n pug`\n time(datetime={updated}) {string.slice updated 0 10}\n `\n}\n"; |
| 3098 | + let call = |
| 3099 | + "fn f :: {\n pug`\n time(datetime={updated}) {string.slice updated 0 10}\n `\n}\n"; |
| 3094 | 3100 | assert_eq!(narrow(call), call); |
| 3095 | 3101 | let cond = "fn f :: {\n pug`\n button(aria-pressed={if tagged { \"true\" } else { \"false\" }}) Tag\n `\n}\n"; |
| 3096 | 3102 | assert_eq!(narrow(cond), cond); |
| @@ -3105,8 +3111,7 @@ mod tests { |
| 3105 | 3111 | |
| 3106 | 3112 | #[test] |
| 3107 | 3113 | fn string_holes_stay_on_one_line() { |
| 3108 | | − let src = |
| 3109 | | − "fn f :: {\n \"a long string around {string.slice updated 0 10} its hole\"\n}\n"; |
| 3114 | + let src = "fn f :: {\n \"a long string around {string.slice updated 0 10} its hole\"\n}\n"; |
| 3110 | 3115 | assert_eq!(narrow(src), src); |
| 3111 | 3116 | } |
| 3112 | 3117 | |