From e17e60d35bf5de8f4b478c8d9a1bb989d9bfc4de Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Fri, 24 Feb 2023 19:12:50 +0000 Subject: [PATCH] feat(fmt): try to use ass-stroke --- --- a/cmds/jrsonnet-fmt/Cargo.toml +++ b/cmds/jrsonnet-fmt/Cargo.toml @@ -8,3 +8,4 @@ jrsonnet-rowan-parser.workspace = true insta = "1.15" indoc = "1.0" +ass-stroke = { path = "/home/lach/build/ass-stroke/crates/ass-stroke", version = "0.1.0" } --- a/cmds/jrsonnet-fmt/src/children.rs +++ b/cmds/jrsonnet-fmt/src/children.rs @@ -267,3 +267,8 @@ pub should_start_with_newline: bool, pub trivia: ChildTrivia, } +impl EndingComments { + pub fn is_empty(&self) -> bool { + !self.should_start_with_newline && self.trivia.is_empty() + } +} --- a/cmds/jrsonnet-fmt/src/main.rs +++ b/cmds/jrsonnet-fmt/src/main.rs @@ -296,7 +296,6 @@ fn print(&self) -> PrintItems { match self { ObjBody::ObjBodyComp(l) => { - let mut pi = p!(new: str("{") >i nl); let (children, end_comments) = children_between::( l.syntax().clone(), l.l_brace_token().map(Into::into).as_ref(), @@ -309,6 +308,7 @@ .into(), ), ); + let mut pi = p!(new: str("{") >i nl); for mem in children.into_iter() { if mem.should_start_with_newline { p!(pi: nl); @@ -341,7 +341,6 @@ p!(pi: items(format_comments(&mem.before_trivia, CommentLocation::AboveItem))); p!(pi: {mem.value}); p!(pi: items(format_comments(&mem.inline_trivia, CommentLocation::ItemInline))); - p!(pi: nl) } if end_comments.should_start_with_newline { p!(pi: nl); @@ -352,12 +351,15 @@ pi } ObjBody::ObjBodyMemberList(l) => { - let mut pi = p!(new: str("{") >i nl); let (children, end_comments) = children_between::( l.syntax().clone(), l.l_brace_token().map(Into::into).as_ref(), l.r_brace_token().map(Into::into).as_ref(), ); + if children.is_empty() && end_comments.is_empty() { + return p!(new: str("{ }")); + } + let mut pi = p!(new: str("{") >i nl); for mem in children.into_iter() { if mem.should_start_with_newline { p!(pi: nl); @@ -395,7 +397,7 @@ p!(new: {d.into()} str(" = ") {d.value()}) } Bind::BindFunction(f) => { - p!(new: str("function") {f.params()} str(" = ") {f.value()}) + p!(new: {f.name()} {f.params()} str(" = ") {f.value()}) } } } @@ -504,7 +506,7 @@ p!(pi: nl); } p!(pi: items(format_comments(&bind.before_trivia, CommentLocation::AboveItem))); - p!(pi: {bind.value} str(";")); + p!(pi: {bind.value} str(",")); p!(pi: items(format_comments(&bind.inline_trivia, CommentLocation::ItemInline)) nl); } if end_comments.should_start_with_newline { @@ -573,16 +575,45 @@ } } +fn format(input: &str) -> String { + let (parsed, errors) = jrsonnet_rowan_parser::parse(input); + if !errors.is_empty() { + let mut builder = ass_stroke::SnippetBuilder::new(input); + for error in errors { + builder + .error(ass_stroke::Text::single( + format!("{:?}", error.error).chars(), + Default::default(), + )) + .range( + error.range.start().into() + ..=(usize::from(error.range.end()) - 1).max(error.range.start().into()), + ) + .build(); + } + let snippet = builder.build(); + let ansi = ass_stroke::source_to_ansi(&snippet); + println!("{ansi}"); + } + dprint_core::formatting::format( + || parsed.print(), + PrintOptions { + indent_width: 2, + max_width: 100, + use_tabs: false, + new_line_text: "\n", + }, + ) +} fn main() { - let (parsed, _errors) = jrsonnet_rowan_parser::parse( - r#" + let input = r#" # Edit me! local b = import "b.libsonnet"; # comment local a = import "a.libsonnet"; - local f(x,y)=x+y; + local f(x,y)=x+y; local {a: [b, ..., c], d, ...e} = null; @@ -693,28 +724,29 @@ else Template {}, compspecs: { - obj_with_no_item: {for i in [1, 2, 3]}, - obj_with_2_items: {a:1, b:2, for i in [1,2,3]}, + obj_with_no_item: {a:1, for i in [1, 2, 3]}, + obj_with_2_items: {a:1, /*b:2,*/ for i in [1,2,3]}, } } + Template +"#; - - // Comment after everything -"#, - ); - - // dbg!(errors); - dbg!(&parsed); - - let o = dprint_core::formatting::format( - || parsed.print(), - PrintOptions { - indent_width: 2, - max_width: 100, - use_tabs: false, - new_line_text: "\n", - }, - ); - println!("{}", o); + let mut iteration = 0; + let mut a = input.to_string(); + let mut b; + // https://github.com/dprint/dprint/pull/423 + loop { + b = format(&a).trim().to_owned(); + if a == b { + break; + } + println!("{b}"); + a = b; + iteration += 1; + if iteration > 5 { + panic!("formatting not converged"); + break; + } + } + println!("{a}"); } --- a/cmds/jrsonnet/Cargo.toml +++ b/cmds/jrsonnet/Cargo.toml @@ -12,10 +12,7 @@ # Use mimalloc as allocator mimalloc = ["mimallocator"] # Experimental feature, which allows to preserve order of object fields -exp-preserve-order = [ - "jrsonnet-evaluator/exp-preserve-order", - "jrsonnet-cli/exp-preserve-order", -] +exp-preserve-order = ["jrsonnet-evaluator/exp-preserve-order", "jrsonnet-cli/exp-preserve-order"] # Destructuring of locals exp-destruct = ["jrsonnet-evaluator/exp-destruct"] # Iteration over objects yields [key, value] elements @@ -44,3 +41,4 @@ clap_complete = { version = "4.1" } serde_json = "1.0.104" serde = { workspace = true, features = ["derive"] } +ass-stroke = { git = "https://github.com/CertainLach/ass-stroke", version = "0.1.0" } -- gitstuff