git.delta.rocks / jrsonnet / refs/commits / e17e60d35bf5

difftreelog

feat(fmt) try to use ass-stroke

Yaroslav Bolyukin2023-09-04parent: #fa1839d.patch.diff
in: master

4 files changed

modifiedcmds/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" }
modifiedcmds/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()
+	}
+}
modifiedcmds/jrsonnet-fmt/src/main.rsdiffbeforeafterboth
296 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 pi
353 }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}
575577
576fn 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#"
579
580
704"#,
705 );
706
707 // 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 builder
584 .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#"
610
611
612 # Edit me!
613 local b = import "b.libsonnet"; # comment
614 local a = import "a.libsonnet";
615
616 local f(x,y)=x+y;
617
618 local {a: [b, ..., c], d, ...e} = null;
619
620 local ass = assert false : false; false;
621
622 local fn = function(a, b, c = 3) 4;
623
624 local comp = [a for b in c if d == e];
625 local ocomp = {[k]: 1 for k in v};
626
627 local ? = skip;
628
629 local ie = a[expr];
630
631 local unary = !a;
632
633 local
634 // I am comment
635 singleLocalWithItemComment = 1,
636 ;
637
638 // Comment between local and expression
639
640 local
641 a = 1, // Inline
642 // Comment above b
643 b = 4,
644
645 // c needs some space
646 c = 5,
647
648 // Comment after everything
649 ;
650
651
652 local Template = {z: "foo"};
653
654 {
655 local
656
657 h = 3,
658 assert self.a == 1
659
660 : "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[::],
678
679 comments: {
680 _: '',
681 // Plain comment
682 a: '',
683
684 # Plain comment with empty line before
685 b: '',
686 /*Single-line multiline comment
687
688 */
689 c: '',
690
691 /**Single-line multiline doc comment
692
693 */
694 c: '',
695
696 /**multiline doc comment
697 s
698 */
699 c: '',
700
701 /*
702
703 Multi-line
704
705 comment
706 */
707 d: '',
708
709 e: '', // Inline comment
710
711 k: '',
712
713 // Text after everything
714 },
715 comments2: {
716 k: '',
717 // Text after everything, but no newline above
718 },
719 k: if a == b then
720
721
722 2
723
724 else Template {},
725
726 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 }
730
731 } + Template
732"#;
733
734 let mut iteration = 0;
735 let mut a = input.to_string();
736 let mut b;
737 // https://github.com/dprint/dprint/pull/423
738 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}
721753
modifiedcmds/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" }