difftreelog
feat(fmt) try to use ass-stroke
in: master
4 files changed
cmds/jrsonnet-fmt/Cargo.tomldiffbeforeafterboth--- 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" }
cmds/jrsonnet-fmt/src/children.rsdiffbeforeafterboth--- 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()
+ }
+}
cmds/jrsonnet-fmt/src/main.rsdiffbeforeafterboth296 fn print(&self) -> PrintItems {296 fn print(&self) -> PrintItems {297 match self {297 match self {298 ObjBody::ObjBodyComp(l) => {298 ObjBody::ObjBodyComp(l) => {299 let mut pi = p!(new: str("{") >i nl);300 let (children, end_comments) = children_between::<Member>(299 let (children, end_comments) = children_between::<Member>(301 l.syntax().clone(),300 l.syntax().clone(),302 l.l_brace_token().map(Into::into).as_ref(),301 l.l_brace_token().map(Into::into).as_ref(),309 .into(),308 .into(),310 ),309 ),311 );310 );311 let mut pi = p!(new: str("{") >i nl);312 for mem in children.into_iter() {312 for mem in children.into_iter() {313 if mem.should_start_with_newline {313 if mem.should_start_with_newline {314 p!(pi: nl);314 p!(pi: nl);341 p!(pi: items(format_comments(&mem.before_trivia, CommentLocation::AboveItem)));341 p!(pi: items(format_comments(&mem.before_trivia, CommentLocation::AboveItem)));342 p!(pi: {mem.value});342 p!(pi: {mem.value});343 p!(pi: items(format_comments(&mem.inline_trivia, CommentLocation::ItemInline)));343 p!(pi: items(format_comments(&mem.inline_trivia, CommentLocation::ItemInline)));344 p!(pi: nl)345 }344 }346 if end_comments.should_start_with_newline {345 if end_comments.should_start_with_newline {347 p!(pi: nl);346 p!(pi: nl);352 pi351 pi353 }352 }354 ObjBody::ObjBodyMemberList(l) => {353 ObjBody::ObjBodyMemberList(l) => {355 let mut pi = p!(new: str("{") >i nl);356 let (children, end_comments) = children_between::<Member>(354 let (children, end_comments) = children_between::<Member>(357 l.syntax().clone(),355 l.syntax().clone(),358 l.l_brace_token().map(Into::into).as_ref(),356 l.l_brace_token().map(Into::into).as_ref(),359 l.r_brace_token().map(Into::into).as_ref(),357 l.r_brace_token().map(Into::into).as_ref(),360 );358 );359 if children.is_empty() && end_comments.is_empty() {360 return p!(new: str("{ }"));361 }362 let mut pi = p!(new: str("{") >i nl);361 for mem in children.into_iter() {363 for mem in children.into_iter() {362 if mem.should_start_with_newline {364 if mem.should_start_with_newline {363 p!(pi: nl);365 p!(pi: nl);395 p!(new: {d.into()} str(" = ") {d.value()})397 p!(new: {d.into()} str(" = ") {d.value()})396 }398 }397 Bind::BindFunction(f) => {399 Bind::BindFunction(f) => {398 p!(new: str("function") {f.params()} str(" = ") {f.value()})400 p!(new: {f.name()} {f.params()} str(" = ") {f.value()})399 }401 }400 }402 }401 }403 }504 p!(pi: nl);506 p!(pi: nl);505 }507 }506 p!(pi: items(format_comments(&bind.before_trivia, CommentLocation::AboveItem)));508 p!(pi: items(format_comments(&bind.before_trivia, CommentLocation::AboveItem)));507 p!(pi: {bind.value} str(";"));509 p!(pi: {bind.value} str(","));508 p!(pi: items(format_comments(&bind.inline_trivia, CommentLocation::ItemInline)) nl);510 p!(pi: items(format_comments(&bind.inline_trivia, CommentLocation::ItemInline)) nl);509 }511 }510 if end_comments.should_start_with_newline {512 if end_comments.should_start_with_newline {573 }575 }574}576}575577576fn main() {578fn format(input: &str) -> String {577 let (parsed, _errors) = jrsonnet_rowan_parser::parse(579 let (parsed, errors) = jrsonnet_rowan_parser::parse(input);578 r#"579580704"#,705 );706707 // dbg!(errors);708 dbg!(&parsed);580 if !errors.is_empty() {709581 let mut builder = ass_stroke::SnippetBuilder::new(input);582 for error in errors {583 builder584 .error(ass_stroke::Text::single(585 format!("{:?}", error.error).chars(),586 Default::default(),587 ))588 .range(589 error.range.start().into()590 ..=(usize::from(error.range.end()) - 1).max(error.range.start().into()),591 )592 .build();593 }710 let o = dprint_core::formatting::format(594 let snippet = builder.build();595 let ansi = ass_stroke::source_to_ansi(&snippet);596 println!("{ansi}");597 }598 dprint_core::formatting::format(711 || parsed.print(),599 || parsed.print(),712 PrintOptions {600 PrintOptions {715 use_tabs: false,603 use_tabs: false,716 new_line_text: "\n",604 new_line_text: "\n",717 },605 },718 );606 )719 println!("{}", o);720}607}608fn main() {609 let input = r#"610611612 # Edit me!613 local b = import "b.libsonnet"; # comment614 local a = import "a.libsonnet";615616 local f(x,y)=x+y;617618 local {a: [b, ..., c], d, ...e} = null;619620 local ass = assert false : false; false;621622 local fn = function(a, b, c = 3) 4;623624 local comp = [a for b in c if d == e];625 local ocomp = {[k]: 1 for k in v};626627 local ? = skip;628629 local ie = a[expr];630631 local unary = !a;632633 local634 // I am comment635 singleLocalWithItemComment = 1,636 ;637638 // Comment between local and expression639640 local641 a = 1, // Inline642 // Comment above b643 b = 4,644645 // c needs some space646 c = 5,647648 // Comment after everything649 ;650651652 local Template = {z: "foo"};653654 {655 local656657 h = 3,658 assert self.a == 1659660 : "error",661 "f": ((((((3)))))) ,662 "g g":663 f(4,2),664 arr: [[665 1, 2,666 ],667 3,668 {669 b: {670 c: {671 k: [16]672 }673 }674 }675 ],676 m: a[1::],677 m: b[::],678679 comments: {680 _: '',681 // Plain comment682 a: '',683684 # Plain comment with empty line before685 b: '',686 /*Single-line multiline comment687688 */689 c: '',690691 /**Single-line multiline doc comment692693 */694 c: '',695696 /**multiline doc comment697 s698 */699 c: '',700701 /*702703 Multi-line704705 comment706 */707 d: '',708709 e: '', // Inline comment710711 k: '',712713 // Text after everything714 },715 comments2: {716 k: '',717 // Text after everything, but no newline above718 },719 k: if a == b then720721722 2723724 else Template {},725726 compspecs: {727 obj_with_no_item: {a:1, for i in [1, 2, 3]},728 obj_with_2_items: {a:1, /*b:2,*/ for i in [1,2,3]},729 }730731 } + Template732"#;733734 let mut iteration = 0;735 let mut a = input.to_string();736 let mut b;737 // https://github.com/dprint/dprint/pull/423738 loop {739 b = format(&a).trim().to_owned();740 if a == b {741 break;742 }743 println!("{b}");744 a = b;745 iteration += 1;746 if iteration > 5 {747 panic!("formatting not converged");748 break;749 }750 }751 println!("{a}");752}721753cmds/jrsonnet/Cargo.tomldiffbeforeafterboth--- 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" }