difftreelog
fix various parsing fixes
in: master
4 files changed
cmds/jrsonnet-fmt/src/main.rsdiffbeforeafterboth4 io::{self, Write},4 io::{self, Write},5 path::PathBuf,5 path::PathBuf,6 process,6 process,7 rc::Rc,7};8};899use children::{children_between, trivia_before};10use children::{children_between, trivia_before};10use clap::Parser;11use clap::Parser;11use dprint_core::formatting::{PrintItems, PrintOptions};12use dprint_core::formatting::{13 condition_helpers::is_multiple_lines, condition_resolvers::true_resolver,14 ConditionResolverContext, LineNumber, PrintItems, PrintOptions,15};12use jrsonnet_rowan_parser::{16use jrsonnet_rowan_parser::{13 nodes::{17 nodes::{14 ArgsDesc, Assertion, BinaryOperator, Bind, CompSpec, Destruct, DestructArrayPart,18 Arg, ArgsDesc, Assertion, BinaryOperator, Bind, CompSpec, Destruct, DestructArrayPart,15 DestructRest, Expr, ExprBase, FieldName, ForSpec, IfSpec, ImportKind, Literal, Member,19 DestructRest, Expr, ExprBase, FieldName, ForSpec, IfSpec, ImportKind, Literal, Member,16 Name, Number, ObjBody, ObjLocal, ParamsDesc, SliceDesc, SourceFile, Stmt, Suffix, Text,20 Name, Number, ObjBody, ObjLocal, ParamsDesc, SliceDesc, SourceFile, Stmt, Suffix, Text,17 UnaryOperator, Visibility,21 UnaryOperator, Visibility,64 $o.push_signal(dprint_core::formatting::Signal::FinishIndent);68 $o.push_signal(dprint_core::formatting::Signal::FinishIndent);65 pi!(@s; $o: $($t)*);69 pi!(@s; $o: $($t)*);66 }};70 }};71 (@s; $o:ident: info($v:expr) $($t:tt)*) => {{72 $o.push_info($v);73 pi!(@s; $o: $($t)*);74 }};75 (@s; $o:ident: if($s:literal, $cond:expr, $($i:tt)*) $($t:tt)*) => {{76 $o.push_condition(dprint_core::formatting::conditions::if_true(77 $s,78 $cond.clone(),79 {80 let mut o = PrintItems::new();81 p!(o, $($i)*);82 o83 },84 ));85 pi!(@s; $o: $($t)*);86 }};87 (@s; $o:ident: if_else($s:literal, $cond:expr, $($i:tt)*)($($e:tt)+) $($t:tt)*) => {{88 $o.push_condition(dprint_core::formatting::conditions::if_true_or(89 $s,90 $cond.clone(),91 {92 let mut o = PrintItems::new();93 p!(o, $($i)*);94 o95 },96 {97 let mut o = PrintItems::new();98 p!(o, $($e)*);99 o100 },101 ));102 pi!(@s; $o: $($t)*);103 }};104 (@s; $o:ident: if_not($s:literal, $cond:expr, $($e:tt)*) $($t:tt)*) => {{105 $o.push_condition(dprint_core::formatting::conditions::if_true_or(106 $s,107 $cond.clone(),108 {109 let o = PrintItems::new();110 o111 },112 {113 let mut o = PrintItems::new();114 p!(o, $($e)*);115 o116 },117 ));118 pi!(@s; $o: $($t)*);119 }};67 (@s; $o:ident: {$expr:expr} $($t:tt)*) => {{120 (@s; $o:ident: {$expr:expr} $($t:tt)*) => {{68 $expr.print($o);121 $expr.print($o);69 pi!(@s; $o: $($t)*);122 pi!(@s; $o: $($t)*);244}297}245impl Printable for ArgsDesc {298impl Printable for ArgsDesc {246 fn print(&self, out: &mut PrintItems) {299 fn print(&self, out: &mut PrintItems) {300 let start = LineNumber::new("start");301 let end = LineNumber::new("end");302 let multi_line = Rc::new(move |condition_context: &mut ConditionResolverContext| {303 is_multiple_lines(condition_context, start, end).map(|v| !v)304 });247 p!(out, str("(") >i nl);305 p!(out, str("(") info(start) if("start args", multi_line, >i nl));306 let (children, end_comments) = children_between::<Arg>(307 self.syntax().clone(),308 self.l_paren_token().map(Into::into).as_ref(),309 self.r_paren_token().map(Into::into).as_ref(),310 None,311 );312 let mut args = children.into_iter().peekable();248 for arg in self.args() {313 while let Some(ele) = args.next() {314 if ele.should_start_with_newline {315 p!(out, nl);316 }317 format_comments(&ele.before_trivia, CommentLocation::AboveItem, out);318 let arg = ele.value;249 if arg.name().is_some() || arg.assign_token().is_some() {319 if arg.name().is_some() || arg.assign_token().is_some() {250 p!(out, {arg.name()} str(" = "));320 p!(out, {arg.name()} str(" = "));251 }321 }322 let comma_between = if args.peek().is_some() {323 true_resolver()324 } else {325 multi_line.clone()326 };252 p!(out, {arg.expr()} str(",") nl)327 p!(out, {arg.expr()} if("arg comma", comma_between, str(",") if_not("between args", multi_line, str(" "))));328 format_comments(&ele.inline_trivia, CommentLocation::ItemInline, out);329 p!(out, if("between args", multi_line, nl));253 }330 }254 p!(out, <i str(")"));331 p!(out, if("end args", multi_line, <i info(end)) str(")"));255 }332 }256}333}257impl Printable for SliceDesc {334impl Printable for SliceDesc {513 format_comments(&bind.before_trivia, CommentLocation::AboveItem, out);590 format_comments(&bind.before_trivia, CommentLocation::AboveItem, out);514 p!(out, {bind.value} str(","));591 p!(out, {bind.value} str(","));515 format_comments(&bind.inline_trivia, CommentLocation::ItemInline, out);592 format_comments(&bind.inline_trivia, CommentLocation::ItemInline, out);593 p!(out, nl)516 }594 }517 if end_comments.should_start_with_newline {595 if end_comments.should_start_with_newline {518 p!(out, nl)596 p!(out, nl)cmds/jrsonnet-fmt/src/tests.rsdiffbeforeafterboth21 )21 )22}22}2324macro_rules! assert_formatted {25 ($input:literal, $output:literal) => {26 let formatted = reformat(indoc!($input));27 let mut expected = indoc!($output).to_owned();28 expected.push('\n');29 if formatted != expected {30 panic!(31 "bad formatting, expected\n```\n{formatted}\n```\nto be equal to\n```\n{expected}\n```",32 )33 }34 };35}3637#[test]38fn padding_stripped_for_multiline_comment() {39 assert_formatted!(40 "{41 /*42 Hello43 World44 */45 _: null,46 }",47 "{48 /*49 Hello50 World51 */52 _: null,53 }"54 );55}5657#[test]58fn last_comment_respects_spacing_with_inline_comment_above() {59 assert_formatted!(60 "{61 a: '', // Inline6263 // Comment64 }",65 "{66 a: '', // Inline6768 // Comment69 }"70 );71}722373#[test]24#[test]74fn complex_comments_snapshot() {25fn complex_comments_snapshot() {crates/jrsonnet-rowan-parser/src/parser.rsdiffbeforeafterboth450 p.bump();450 p.bump();451 break;451 break;452 }452 }453 if p.at_ts(COMPSPEC) {453 if p.at_ts(TS![for]) {454 if elems == 0 {454 if elems == 0 {455 let m = p.start();455 let m = p.start();456 m.complete_missing(p, ExpectedSyntax::Named("field definition"));456 m.complete_missing(p, ExpectedSyntax::Named("field definition"));612 p.bump();612 p.bump();613 break;613 break;614 }614 }615 if elems != 0 && p.at_ts(COMPSPEC) {615 if elems != 0 && p.at_ts(TS![for]) {616 while p.at_ts(COMPSPEC) {616 while p.at_ts(COMPSPEC) {617 compspecs.push(compspec(p));617 compspecs.push(compspec(p));618 }618 }crates/jrsonnet-rowan-parser/src/tests.rsdiffbeforeafterboth247#[test]247#[test]248fn eval_simple() {248fn eval_simple() {249 let src = "local a = 1, b = 2; a + local c = 1; c";249 let src = "local a = 1, b = 2; a + local c = 1; c";250 let (node, errors) = parse(src);250 let (node, _errors) = parse(src);251 assert!(errors.is_empty());252251253 dbg!(node);252 dbg!(node);254}253}