difftreelog
feat(fmt) reformat text block
in: master
6 files changed
crates/jrsonnet-formatter/src/lib.rsdiffbeforeafterboth1use std::{any::type_name, rc::Rc};23use children::{children_between, trivia_before};4use dprint_core::formatting::{5 condition_helpers::is_multiple_lines,6 condition_resolvers::true_resolver,7 ir_helpers::{new_line_group, with_indent},8 ConditionResolver, ConditionResolverContext, LineNumber, PrintItems, PrintOptions,9};10use hi_doc::{Formatting, SnippetBuilder};11use jrsonnet_rowan_parser::{12 nodes::{13 Arg, ArgsDesc, Assertion, BinaryOperator, Bind, CompSpec, Destruct, DestructArrayPart,14 DestructRest, Expr, ExprBase, FieldName, ForSpec, IfSpec, ImportKind, Literal, Member,15 Name, Number, ObjBody, ObjLocal, ParamsDesc, SliceDesc, SourceFile, Stmt, Suffix, Text,16 TextKind, UnaryOperator, Visibility,17 },18 AstNode, AstToken as _, SyntaxToken,19};2021use crate::{22 children::{trivia_after, Child, EndingComments},23 comments::{format_comments, CommentLocation},24};2526mod children;27mod comments;28mod tests;2930fn with_indent_eoi(cond: ConditionResolver, o: PrintItems, e: EndingComments) -> PrintItems {31 let end_comments_items = {32 let mut items = PrintItems::new();33 if e.should_start_with_newline {34 p!(&mut items, nl);35 }36 format_comments(&e.trivia, CommentLocation::EndOfItems, &mut items);37 items.into_rc_path()38 };39 let items =40 new_line_group(pi!(@i; items(o.into()) items(end_comments_items.into()))).into_rc_path();4142 let indented = with_indent(pi!(@i; nl items(items.into())));4344 pi!(@i; if_else("indented body", cond, items(indented))(str(" ") items(items.into())))45}4647pub trait Printable {48 fn print(&self, out: &mut PrintItems);49}5051macro_rules! pi {52 (@i; $($t:tt)*) => {{53 #[allow(unused_mut)]54 let mut o = dprint_core::formatting::PrintItems::new();55 pi!(@s; o: $($t)*);56 o57 }};58 (@s; $o:ident: str($e:expr $(,)?) $($t:tt)*) => {{59 $o.push_string($e.to_owned());60 pi!(@s; $o: $($t)*);61 }};62 (@s; $o:ident: string($e:expr $(,)?) $($t:tt)*) => {{63 $o.push_string($e);64 pi!(@s; $o: $($t)*);65 }};66 (@s; $o:ident: nl $($t:tt)*) => {{67 $o.push_signal(dprint_core::formatting::Signal::NewLine);68 pi!(@s; $o: $($t)*);69 }};70 (@s; $o:ident: sonl $($t:tt)*) => {{71 $o.push_signal(dprint_core::formatting::Signal::SpaceOrNewLine);72 pi!(@s; $o: $($t)*);73 }};74 (@s; $o:ident: tab $($t:tt)*) => {{75 $o.push_signal(dprint_core::formatting::Signal::Tab);76 pi!(@s; $o: $($t)*);77 }};78 (@s; $o:ident: >i $($t:tt)*) => {{79 $o.push_signal(dprint_core::formatting::Signal::StartIndent);80 pi!(@s; $o: $($t)*);81 }};82 (@s; $o:ident: <i $($t:tt)*) => {{83 $o.push_signal(dprint_core::formatting::Signal::FinishIndent);84 pi!(@s; $o: $($t)*);85 }};86 (@s; $o:ident: info($v:expr) $($t:tt)*) => {{87 $o.push_info($v);88 pi!(@s; $o: $($t)*);89 }};90 (@s; $o:ident: ln_anchor($v:expr) $($t:tt)*) => {{91 $o.push_anchor(LineNumberAnchor::new($v));92 pi!(@s; $o: $($t)*);93 }};94 (@s; $o:ident: if($s:literal, $cond:expr, $($i:tt)*) $($t:tt)*) => {{95 $o.push_condition(dprint_core::formatting::conditions::if_true(96 $s,97 $cond.clone(),98 {99 let mut o = PrintItems::new();100 p!(o, $($i)*);101 o102 },103 ));104 pi!(@s; $o: $($t)*);105 }};106 (@s; $o:ident: if_else($s:literal, $cond:expr, $($i:tt)*)($($e:tt)+) $($t:tt)*) => {{107 $o.push_condition(dprint_core::formatting::conditions::if_true_or(108 $s,109 $cond.clone(),110 {111 let mut o = PrintItems::new();112 p!(o, $($i)*);113 o114 },115 {116 let mut o = PrintItems::new();117 p!(o, $($e)*);118 o119 },120 ));121 pi!(@s; $o: $($t)*);122 }};123 (@s; $o:ident: if_not($s:literal, $cond:expr, $($e:tt)*) $($t:tt)*) => {{124 $o.push_condition(dprint_core::formatting::conditions::if_true_or(125 $s,126 $cond.clone(),127 {128 let o = PrintItems::new();129 o130 },131 {132 let mut o = PrintItems::new();133 p!(o, $($e)*);134 o135 },136 ));137 pi!(@s; $o: $($t)*);138 }};139 (@s; $o:ident: {$expr:expr} $($t:tt)*) => {{140 $expr.print($o);141 pi!(@s; $o: $($t)*);142 }};143 (@s; $o:ident: items($expr:expr) $($t:tt)*) => {{144 $o.extend($expr);145 pi!(@s; $o: $($t)*);146 }};147 (@s; $o:ident: if ($e:expr)($($then:tt)*) $($t:tt)*) => {{148 if $e {149 pi!(@s; $o: $($then)*);150 }151 pi!(@s; $o: $($t)*);152 }};153 (@s; $o:ident: ifelse ($e:expr)($($then:tt)*)($($else:tt)*) $($t:tt)*) => {{154 if $e {155 pi!(@s; $o: $($then)*);156 } else {157 pi!(@s; $o: $($else)*);158 }159 pi!(@s; $o: $($t)*);160 }};161 (@s; $i:ident:) => {}162}163macro_rules! p {164 ($o:ident, $($t:tt)*) => {165 pi!(@s; $o: $($t)*)166 };167 (&mut $o:ident, $($t:tt)*) => {168 let om = &mut $o;169 pi!(@s; om: $($t)*)170 };171}172pub(crate) use p;173pub(crate) use pi;174175impl<P> Printable for Option<P>176where177 P: Printable,178{179 fn print(&self, out: &mut PrintItems) {180 if let Some(v) = self {181 v.print(out);182 } else {183 p!(184 out,185 string(format!(186 "/*missing {}*/",187 type_name::<P>().replace("jrsonnet_rowan_parser::generated::nodes::", "")188 ),)189 );190 }191 }192}193194impl Printable for SyntaxToken {195 fn print(&self, out: &mut PrintItems) {196 p!(out, string(self.to_string()));197 }198}199200impl Printable for Text {201 fn print(&self, out: &mut PrintItems) {202 if matches!(self.kind(), TextKind::StringBlock) {203 let text = self.text();204205 for (i, ele) in text.split("\n").enumerate() {206 if i != 0 {207 p!(out, nl);208 }209 // TODO: Trim and recreate whitespace210 p!(out, string(ele.to_string()));211 }212 return;213 }214 p!(out, string(format!("{}", self)));215 }216}217impl Printable for Number {218 fn print(&self, out: &mut PrintItems) {219 p!(out, string(format!("{}", self)));220 }221}222223impl Printable for Name {224 fn print(&self, out: &mut PrintItems) {225 p!(out, { self.ident_lit() });226 }227}228229impl Printable for DestructRest {230 fn print(&self, out: &mut PrintItems) {231 p!(out, str("..."));232 if let Some(name) = self.into() {233 p!(out, { name });234 }235 }236}237238impl Printable for Destruct {239 fn print(&self, out: &mut PrintItems) {240 match self {241 Self::DestructFull(f) => {242 p!(out, { f.name() });243 }244 Self::DestructSkip(_) => p!(out, str("?")),245 Self::DestructArray(a) => {246 p!(out, str("[") >i nl);247 for el in a.destruct_array_parts() {248 match el {249 DestructArrayPart::DestructArrayElement(e) => {250 p!(out, {e.destruct()} str(",") nl);251 }252 DestructArrayPart::DestructRest(d) => {253 p!(out, {d} str(",") nl);254 }255 }256 }257 p!(out, <i str("]"));258 }259 Self::DestructObject(o) => {260 p!(out, str("{") >i nl);261 for item in o.destruct_object_fields() {262 p!(out, { item.field() });263 if let Some(des) = item.destruct() {264 p!(out, str(": ") {des});265 }266 if let Some(def) = item.expr() {267 p!(out, str(" = ") {def});268 }269 p!(out, str(",") nl);270 }271 if let Some(rest) = o.destruct_rest() {272 p!(out, {rest} nl);273 }274 p!(out, <i str("}"));275 }276 }277 }278}279280impl Printable for FieldName {281 fn print(&self, out: &mut PrintItems) {282 match self {283 Self::FieldNameFixed(f) => {284 if let Some(id) = f.id() {285 p!(out, { id });286 } else if let Some(str) = f.text() {287 p!(out, { str });288 } else {289 p!(out, str("/*missing FieldName*/"));290 }291 }292 Self::FieldNameDynamic(d) => {293 p!(out, str("[") {d.expr()} str("]"));294 }295 }296 }297}298299impl Printable for Visibility {300 fn print(&self, out: &mut PrintItems) {301 p!(out, string(self.to_string()));302 }303}304305impl Printable for ObjLocal {306 fn print(&self, out: &mut PrintItems) {307 p!(out, str("local ") {self.bind()});308 }309}310311impl Printable for Assertion {312 fn print(&self, out: &mut PrintItems) {313 p!(out, str("assert ") {self.condition()});314 if self.colon_token().is_some() || self.message().is_some() {315 p!(out, str(": ") {self.message()});316 }317 }318}319320impl Printable for ParamsDesc {321 fn print(&self, out: &mut PrintItems) {322 p!(out, str("(") >i nl);323 for param in self.params() {324 p!(out, { param.destruct() });325 if param.assign_token().is_some() || param.expr().is_some() {326 p!(out, str(" = ") {param.expr()});327 }328 p!(out, str(",") nl);329 }330 p!(out, <i str(")"));331 }332}333impl Printable for ArgsDesc {334 fn print(&self, out: &mut PrintItems) {335 let start = LineNumber::new("args start line");336 let end = LineNumber::new("args end line");337 let multi_line = Rc::new(move |condition_context: &mut ConditionResolverContext| {338 is_multiple_lines(condition_context, start, end)339 });340341 let (children, end_comments) = children_between::<Arg>(342 self.syntax().clone(),343 self.l_paren_token().map(Into::into).as_ref(),344 self.r_paren_token().map(Into::into).as_ref(),345 None,346 );347348 fn gen_args(children: Vec<Child<Arg>>, multi_line: ConditionResolver) -> PrintItems {349 let mut _out = PrintItems::new();350 let out = &mut _out;351352 let mut args = children.into_iter().peekable();353 while let Some(ele) = args.next() {354 if ele.should_start_with_newline {355 p!(out, nl);356 }357 format_comments(&ele.before_trivia, CommentLocation::AboveItem, out);358 let arg = ele.value;359 if arg.name().is_some() || arg.assign_token().is_some() {360 p!(out, {arg.name()} str(" = "));361 }362 p!(out, { arg.expr() });363 let has_more = args.peek().is_some();364 if has_more {365 p!(out, str(","));366 } else {367 p!(out, if("trailing comma", multi_line, str(",")));368 }369 format_comments(&ele.inline_trivia, CommentLocation::ItemInline, out);370 if has_more {371 p!(out, if_else("arg separator", multi_line, nl)(sonl));372 }373 }374 _out375 }376377 let args_items = new_line_group(gen_args(children, multi_line.clone())).into_rc_path();378 let args_indented = with_indent(pi!(@i; nl items(args_items.into())));379380 p!(out, str("(") info(start));381 p!(out, if_else("args body", multi_line, items(args_indented) nl)(items(args_items.into())));382 if end_comments.should_start_with_newline {383 p!(out, nl);384 }385 format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);386 p!(out, str(")") info(end));387 }388}389impl Printable for SliceDesc {390 fn print(&self, out: &mut PrintItems) {391 p!(out, str("["));392 if self.from().is_some() {393 p!(out, { self.from() });394 }395 p!(out, str(":"));396 if self.end().is_some() {397 p!(out, { self.end().map(|e| e.expr()) });398 }399 // Keep only one : in case if we don't need step400 if self.step().is_some() {401 p!(out, str(":") {self.step().map(|e|e.expr())});402 }403 p!(out, str("]"));404 }405}406407impl Printable for Member {408 fn print(&self, out: &mut PrintItems) {409 match self {410 Self::MemberBindStmt(b) => {411 p!(out, { b.obj_local() });412 }413 Self::MemberAssertStmt(ass) => {414 p!(out, { ass.assertion() });415 }416 Self::MemberFieldNormal(n) => {417 p!(out, {n.field_name()} if(n.plus_token().is_some())({n.plus_token()}) {n.visibility()} str(" ") {n.expr()});418 }419 Self::MemberFieldMethod(m) => {420 p!(out, {m.field_name()} {m.params_desc()} {m.visibility()} str(" ") {m.expr()});421 }422 }423 }424}425426impl Printable for ObjBody {427 fn print(&self, out: &mut PrintItems) {428 match self {429 Self::ObjBodyComp(l) => {430 let (children, mut end_comments) = children_between::<Member>(431 l.syntax().clone(),432 l.l_brace_token().map(Into::into).as_ref(),433 Some(434 &(l.comp_specs()435 .next()436 .expect("at least one spec is defined")437 .syntax()438 .clone())439 .into(),440 ),441 None,442 );443 let trailing_for_comp = end_comments.extract_trailing();444 p!(out, str("{") >i nl);445 for mem in children {446 if mem.should_start_with_newline {447 p!(out, nl);448 }449 format_comments(&mem.before_trivia, CommentLocation::AboveItem, out);450 p!(out, {mem.value} str(","));451 format_comments(&mem.inline_trivia, CommentLocation::ItemInline, out);452 p!(out, nl);453 }454455 if end_comments.should_start_with_newline {456 p!(out, nl);457 }458 format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);459460 let (compspecs, end_comments) = children_between::<CompSpec>(461 l.syntax().clone(),462 l.member_comps()463 .last()464 .map(|m| m.syntax().clone())465 .map(Into::into)466 .or_else(|| l.l_brace_token().map(Into::into))467 .as_ref(),468 l.r_brace_token().map(Into::into).as_ref(),469 Some(trailing_for_comp),470 );471 for mem in compspecs {472 if mem.should_start_with_newline {473 p!(out, nl);474 }475 format_comments(&mem.before_trivia, CommentLocation::AboveItem, out);476 p!(out, { mem.value });477 format_comments(&mem.inline_trivia, CommentLocation::ItemInline, out);478 }479 if end_comments.should_start_with_newline {480 p!(out, nl);481 }482 format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);483484 p!(out, nl <i str("}"));485 }486 Self::ObjBodyMemberList(l) => {487 let (children, end_comments) = children_between::<Member>(488 l.syntax().clone(),489 l.l_brace_token().map(Into::into).as_ref(),490 l.r_brace_token().map(Into::into).as_ref(),491 None,492 );493 if children.is_empty() && end_comments.is_empty() {494 p!(out, str("{ }"));495 return;496 }497498 let source_is_multiline = children.iter().any(|c| c.triggers_multiline)499 || end_comments.should_start_with_newline;500501 let start = LineNumber::new("obj start line");502 let end = LineNumber::new("obj end line");503 let multi_line: ConditionResolver = if source_is_multiline {504 true_resolver()505 } else {506 Rc::new(move |ctx: &mut ConditionResolverContext| {507 is_multiple_lines(ctx, start, end)508 })509 };510511 fn gen_members(512 children: Vec<Child<Member>>,513 multi_line: ConditionResolver,514 ) -> PrintItems {515 let mut _out = PrintItems::new();516 let out = &mut _out;517 let mut members = children.into_iter().peekable();518 while let Some(mem) = members.next() {519 if mem.should_start_with_newline {520 p!(out, nl);521 }522 format_comments(&mem.before_trivia, CommentLocation::AboveItem, out);523 p!(out, { mem.value });524 let has_more = members.peek().is_some();525 if has_more {526 p!(out, str(","));527 } else {528 p!(out, if("trailing comma", multi_line, str(",")));529 }530 format_comments(&mem.inline_trivia, CommentLocation::ItemInline, out);531 p!(out, if_else("member separator", multi_line, nl)(sonl));532 }533 _out534 }535536 let members_items =537 new_line_group(gen_members(children, multi_line.clone())).into_rc_path();538539 let members = with_indent_eoi(multi_line, members_items.into(), end_comments);540541 p!(out, str("{") info(start));542 p!(out, items(members));543 p!(out, str("}") info(end));544 }545 }546 }547}548impl Printable for UnaryOperator {549 fn print(&self, out: &mut PrintItems) {550 p!(out, string(self.text().to_string()));551 }552}553impl Printable for BinaryOperator {554 fn print(&self, out: &mut PrintItems) {555 p!(out, string(self.text().to_string()));556 }557}558impl Printable for Bind {559 fn print(&self, out: &mut PrintItems) {560 match self {561 Self::BindDestruct(d) => {562 p!(out, {d.into()} str(" = ") {d.value()});563 }564 Self::BindFunction(f) => {565 p!(out, {f.name()} {f.params()} str(" = ") {f.value()});566 }567 }568 }569}570impl Printable for Literal {571 fn print(&self, out: &mut PrintItems) {572 p!(out, string(self.syntax().to_string()));573 }574}575impl Printable for ImportKind {576 fn print(&self, out: &mut PrintItems) {577 p!(out, string(self.syntax().to_string()));578 }579}580impl Printable for ForSpec {581 fn print(&self, out: &mut PrintItems) {582 p!(out, str("for ") {self.bind()} str(" in ") {self.expr()});583 }584}585impl Printable for IfSpec {586 fn print(&self, out: &mut PrintItems) {587 p!(out, str("if ") {self.expr()});588 }589}590impl Printable for CompSpec {591 fn print(&self, out: &mut PrintItems) {592 match self {593 Self::ForSpec(f) => f.print(out),594 Self::IfSpec(i) => i.print(out),595 }596 }597}598impl Printable for Expr {599 fn print(&self, out: &mut PrintItems) {600 let (stmts, _ending) = children_between::<Stmt>(601 self.syntax().clone(),602 None,603 self.expr_base()604 .as_ref()605 .map(ExprBase::syntax)606 .cloned()607 .map(Into::into)608 .as_ref(),609 None,610 );611 for stmt in stmts {612 p!(out, { stmt.value });613 }614 p!(out, { self.expr_base() });615 let (suffixes, _ending) = children_between::<Suffix>(616 self.syntax().clone(),617 self.expr_base()618 .as_ref()619 .map(ExprBase::syntax)620 .cloned()621 .map(Into::into)622 .as_ref(),623 None,624 None,625 );626 for suffix in suffixes {627 p!(out, { suffix.value });628 }629 }630}631impl Printable for Suffix {632 fn print(&self, out: &mut PrintItems) {633 match self {634 Self::SuffixIndex(i) => {635 if i.question_mark_token().is_some() {636 p!(out, str("?"));637 }638 p!(out, str(".") {i.index()});639 }640 Self::SuffixIndexExpr(e) => {641 if e.question_mark_token().is_some() {642 p!(out, str(".?"));643 }644 p!(out, str("[") {e.index()} str("]"));645 }646 Self::SuffixSlice(d) => {647 p!(out, { d.slice_desc() });648 }649 Self::SuffixApply(a) => {650 p!(out, { a.args_desc() });651 }652 }653 }654}655impl Printable for Stmt {656 fn print(&self, out: &mut PrintItems) {657 match self {658 Self::StmtLocal(l) => {659 let (binds, end_comments) = children_between::<Bind>(660 l.syntax().clone(),661 l.local_kw_token().map(Into::into).as_ref(),662 l.semi_token().map(Into::into).as_ref(),663 None,664 );665 if binds.len() == 1 {666 let bind = &binds[0];667 format_comments(&bind.before_trivia, CommentLocation::AboveItem, out);668 p!(out, str("local ") {bind.value});669 // TODO: keep end_comments, child.inline_trivia somehow, force multiple locals formatting in case of presence?670 } else {671 p!(out,str("local") >i nl);672 for bind in binds {673 if bind.should_start_with_newline {674 p!(out, nl);675 }676 format_comments(&bind.before_trivia, CommentLocation::AboveItem, out);677 p!(out, {bind.value} str(","));678 format_comments(&bind.inline_trivia, CommentLocation::ItemInline, out);679 p!(out, nl);680 }681 if end_comments.should_start_with_newline {682 p!(out, nl);683 }684 format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);685 p!(out,<i);686 }687 p!(out,str(";") nl);688 }689 Self::StmtAssert(a) => {690 p!(out, {a.assertion()} str(";") nl);691 }692 }693 }694}695impl Printable for ExprBase {696 fn print(&self, out: &mut PrintItems) {697 match self {698 Self::ExprBinary(b) => {699 p!(out, {b.lhs()} str(" ") {b.binary_operator()} str(" ") {b.rhs()});700 }701 Self::ExprUnary(u) => p!(out, {u.unary_operator()} {u.rhs()}),702 // Self::ExprSlice(s) => {703 // p!(new: {s.expr()} {s.slice_desc()})704 // }705 // Self::ExprIndex(i) => {706 // p!(new: {i.expr()} str(".") {i.index()})707 // }708 // Self::ExprIndexExpr(i) => p!(new: {i.base()} str("[") {i.index()} str("]")),709 // Self::ExprApply(a) => {710 // let mut pi = p!(new: {a.expr()} {a.args_desc()});711 // if a.tailstrict_kw_token().is_some() {712 // p!(out,str(" tailstrict"));713 // }714 // pi715 // }716 Self::ExprObjExtend(ex) => {717 p!(out, {ex.lhs_work()} str(" ") {ex.rhs_work()});718 }719 Self::ExprParened(p) => {720 p!(out, str("(") {p.expr()} str(")"));721 }722 Self::ExprString(s) => p!(out, { s.text() }),723 Self::ExprNumber(n) => p!(out, { n.number() }),724 Self::ExprArray(a) => {725 p!(out, str("[") >i nl);726 for el in a.exprs() {727 p!(out, {el} str(",") nl);728 }729 p!(out, <i str("]"));730 }731 Self::ExprObject(obj) => {732 p!(out, { obj.obj_body() });733 }734 Self::ExprArrayComp(arr) => {735 p!(out, str("[") {arr.expr()});736 for spec in arr.comp_specs() {737 p!(out, str(" ") {spec});738 }739 p!(out, str("]"));740 }741 Self::ExprImport(v) => {742 p!(out, {v.import_kind()} str(" ") {v.text()});743 }744 Self::ExprVar(n) => p!(out, { n.name() }),745 // Self::ExprLocal(l) => {746 // }747 Self::ExprIfThenElse(ite) => {748 p!(out, str("if ") {ite.cond()} str(" then ") {ite.then().map(|t| t.expr())});749 if ite.else_kw_token().is_some() || ite.else_().is_some() {750 p!(out, str(" else ") {ite.else_().map(|t| t.expr())});751 }752 }753 Self::ExprFunction(f) => p!(out, str("function") {f.params_desc()} nl {f.expr()}),754 // Self::ExprAssert(a) => p!(new: {a.assertion()} str("; ") {a.expr()}),755 Self::ExprError(e) => p!(out, str("error ") {e.expr()}),756 Self::ExprLiteral(l) => {757 p!(out, { l.literal() });758 }759 }760 }761}762763impl Printable for SourceFile {764 fn print(&self, out: &mut PrintItems) {765 let before = trivia_before(766 self.syntax().clone(),767 self.expr()768 .map(|e| e.syntax().clone())769 .map(Into::into)770 .as_ref(),771 );772 let after = trivia_after(773 self.syntax().clone(),774 self.expr()775 .map(|e| e.syntax().clone())776 .map(Into::into)777 .as_ref(),778 );779 format_comments(&before, CommentLocation::AboveItem, out);780 p!(out, {self.expr()} nl);781 format_comments(&after, CommentLocation::EndOfItems, out);782 }783}784785pub struct FormatOptions {786 // 0 for hard tabs787 pub indent: u8,788}789pub fn format(input: &str, opts: &FormatOptions) -> Result<String, SnippetBuilder> {790 let (parsed, errors) = jrsonnet_rowan_parser::parse(input);791 if !errors.is_empty() {792 let mut builder = hi_doc::SnippetBuilder::new(input);793 for error in errors {794 builder795 .error(hi_doc::Text::fragment(796 format!("{:?}", error.error),797 Formatting::default(),798 ))799 .range(800 error.range.start().into()801 ..=(usize::from(error.range.end()) - 1).max(error.range.start().into()),802 )803 .build();804 }805 // let snippet = builder.build();806 return Err(builder);807 // It is possible to recover from this failure, but the output may be broken, as formatter is free to skip808 // ERROR rowan nodes.809 // Recovery needs to be enabled for LSP, though.810 }811 Ok(dprint_core::formatting::format(812 || {813 let mut out = PrintItems::new();814 parsed.print(&mut out);815 out816 },817 PrintOptions {818 indent_width: if opts.indent == 0 {819 // Reasonable max length for both 2 and 4 space sized tabs.820 3821 } else {822 opts.indent823 },824 max_width: 100,825 use_tabs: opts.indent == 0,826 new_line_text: "\n",827 },828 ))829}1use std::{any::type_name, rc::Rc};23use children::{children_between, trivia_before};4use dprint_core::formatting::{5 condition_helpers::is_multiple_lines,6 condition_resolvers::true_resolver,7 ir_helpers::{new_line_group, with_indent},8 ConditionResolver, ConditionResolverContext, LineNumber, PrintItems, PrintOptions,9};10use hi_doc::{Formatting, SnippetBuilder};11use jrsonnet_rowan_parser::{12 collect_lexed_str_block,13 nodes::{14 Arg, ArgsDesc, Assertion, BinaryOperator, Bind, CompSpec, Destruct, DestructArrayPart,15 DestructRest, Expr, ExprBase, FieldName, ForSpec, IfSpec, ImportKind, Literal, Member,16 Name, Number, ObjBody, ObjLocal, ParamsDesc, SliceDesc, SourceFile, Stmt, Suffix, Text,17 TextKind, UnaryOperator, Visibility,18 },19 AstNode, AstToken as _, SyntaxToken,20};2122use crate::{23 children::{trivia_after, Child, EndingComments},24 comments::{format_comments, CommentLocation},25};2627mod children;28mod comments;29mod tests;3031fn with_indent_eoi(cond: ConditionResolver, o: PrintItems, e: EndingComments) -> PrintItems {32 let end_comments_items = {33 let mut items = PrintItems::new();34 if e.should_start_with_newline {35 p!(&mut items, nl);36 }37 format_comments(&e.trivia, CommentLocation::EndOfItems, &mut items);38 items.into_rc_path()39 };40 let items =41 new_line_group(pi!(@i; items(o.into()) items(end_comments_items.into()))).into_rc_path();4243 let indented = with_indent(pi!(@i; nl items(items.into())));4445 pi!(@i; if_else("indented body", cond, items(indented))(str(" ") items(items.into())))46}4748pub trait Printable {49 fn print(&self, out: &mut PrintItems);50}5152macro_rules! pi {53 (@i; $($t:tt)*) => {{54 #[allow(unused_mut)]55 let mut o = dprint_core::formatting::PrintItems::new();56 pi!(@s; o: $($t)*);57 o58 }};59 (@s; $o:ident: str($e:expr $(,)?) $($t:tt)*) => {{60 $o.push_string($e.to_owned());61 pi!(@s; $o: $($t)*);62 }};63 (@s; $o:ident: string($e:expr $(,)?) $($t:tt)*) => {{64 $o.push_string($e);65 pi!(@s; $o: $($t)*);66 }};67 (@s; $o:ident: nl $($t:tt)*) => {{68 $o.push_signal(dprint_core::formatting::Signal::NewLine);69 pi!(@s; $o: $($t)*);70 }};71 (@s; $o:ident: sonl $($t:tt)*) => {{72 $o.push_signal(dprint_core::formatting::Signal::SpaceOrNewLine);73 pi!(@s; $o: $($t)*);74 }};75 (@s; $o:ident: tab $($t:tt)*) => {{76 $o.push_signal(dprint_core::formatting::Signal::Tab);77 pi!(@s; $o: $($t)*);78 }};79 (@s; $o:ident: >i $($t:tt)*) => {{80 $o.push_signal(dprint_core::formatting::Signal::StartIndent);81 pi!(@s; $o: $($t)*);82 }};83 (@s; $o:ident: <i $($t:tt)*) => {{84 $o.push_signal(dprint_core::formatting::Signal::FinishIndent);85 pi!(@s; $o: $($t)*);86 }};87 (@s; $o:ident: >ii $($t:tt)*) => {{88 $o.push_signal(dprint_core::formatting::Signal::StartIgnoringIndent);89 pi!(@s; $o: $($t)*);90 }};91 (@s; $o:ident: <ii $($t:tt)*) => {{92 $o.push_signal(dprint_core::formatting::Signal::FinishIgnoringIndent);93 pi!(@s; $o: $($t)*);94 }};95 (@s; $o:ident: info($v:expr) $($t:tt)*) => {{96 $o.push_info($v);97 pi!(@s; $o: $($t)*);98 }};99 (@s; $o:ident: ln_anchor($v:expr) $($t:tt)*) => {{100 $o.push_anchor(LineNumberAnchor::new($v));101 pi!(@s; $o: $($t)*);102 }};103 (@s; $o:ident: if($s:literal, $cond:expr, $($i:tt)*) $($t:tt)*) => {{104 $o.push_condition(dprint_core::formatting::conditions::if_true(105 $s,106 $cond.clone(),107 {108 let mut o = PrintItems::new();109 p!(o, $($i)*);110 o111 },112 ));113 pi!(@s; $o: $($t)*);114 }};115 (@s; $o:ident: if_else($s:literal, $cond:expr, $($i:tt)*)($($e:tt)+) $($t:tt)*) => {{116 $o.push_condition(dprint_core::formatting::conditions::if_true_or(117 $s,118 $cond.clone(),119 {120 let mut o = PrintItems::new();121 p!(o, $($i)*);122 o123 },124 {125 let mut o = PrintItems::new();126 p!(o, $($e)*);127 o128 },129 ));130 pi!(@s; $o: $($t)*);131 }};132 (@s; $o:ident: if_not($s:literal, $cond:expr, $($e:tt)*) $($t:tt)*) => {{133 $o.push_condition(dprint_core::formatting::conditions::if_true_or(134 $s,135 $cond.clone(),136 {137 let o = PrintItems::new();138 o139 },140 {141 let mut o = PrintItems::new();142 p!(o, $($e)*);143 o144 },145 ));146 pi!(@s; $o: $($t)*);147 }};148 (@s; $o:ident: {$expr:expr} $($t:tt)*) => {{149 $expr.print($o);150 pi!(@s; $o: $($t)*);151 }};152 (@s; $o:ident: items($expr:expr) $($t:tt)*) => {{153 $o.extend($expr);154 pi!(@s; $o: $($t)*);155 }};156 (@s; $o:ident: if ($e:expr)($($then:tt)*) $($t:tt)*) => {{157 if $e {158 pi!(@s; $o: $($then)*);159 }160 pi!(@s; $o: $($t)*);161 }};162 (@s; $o:ident: ifelse ($e:expr)($($then:tt)*)($($else:tt)*) $($t:tt)*) => {{163 if $e {164 pi!(@s; $o: $($then)*);165 } else {166 pi!(@s; $o: $($else)*);167 }168 pi!(@s; $o: $($t)*);169 }};170 (@s; $i:ident:) => {}171}172macro_rules! p {173 ($o:ident, $($t:tt)*) => {174 pi!(@s; $o: $($t)*)175 };176 (&mut $o:ident, $($t:tt)*) => {177 let om = &mut $o;178 pi!(@s; om: $($t)*)179 };180}181pub(crate) use p;182pub(crate) use pi;183184impl<P> Printable for Option<P>185where186 P: Printable,187{188 fn print(&self, out: &mut PrintItems) {189 if let Some(v) = self {190 v.print(out);191 } else {192 p!(193 out,194 string(format!(195 "/*missing {}*/",196 type_name::<P>().replace("jrsonnet_rowan_parser::generated::nodes::", "")197 ),)198 );199 }200 }201}202203impl Printable for SyntaxToken {204 fn print(&self, out: &mut PrintItems) {205 p!(out, string(self.to_string()));206 }207}208209impl Printable for Text {210 fn print(&self, out: &mut PrintItems) {211 if matches!(self.kind(), TextKind::StringBlock) {212 let text = self.text();213 let mut text = collect_lexed_str_block(&text[3..])214 .expect("formatting is not performed on code with parsing errors");215216 if text.truncate && text.lines.ends_with(&[""]) {217 text.truncate = false;218 text.lines.pop();219 }220221 p!(out, str("|||"));222 if text.truncate {223 p!(out, str("-"));224 }225 p!(out, nl > i);226 for ele in text.lines {227 if ele.is_empty() {228 p!(out, >ii nl <ii);229 } else {230 p!(out, string(ele.to_string()) nl);231 }232 }233 p!(out, <i str("|||"));234235 return;236 }237 p!(out, string(format!("{}", self)));238 }239}240impl Printable for Number {241 fn print(&self, out: &mut PrintItems) {242 p!(out, string(format!("{}", self)));243 }244}245246impl Printable for Name {247 fn print(&self, out: &mut PrintItems) {248 p!(out, { self.ident_lit() });249 }250}251252impl Printable for DestructRest {253 fn print(&self, out: &mut PrintItems) {254 p!(out, str("..."));255 if let Some(name) = self.into() {256 p!(out, { name });257 }258 }259}260261impl Printable for Destruct {262 fn print(&self, out: &mut PrintItems) {263 match self {264 Self::DestructFull(f) => {265 p!(out, { f.name() });266 }267 Self::DestructSkip(_) => p!(out, str("?")),268 Self::DestructArray(a) => {269 p!(out, str("[") >i nl);270 for el in a.destruct_array_parts() {271 match el {272 DestructArrayPart::DestructArrayElement(e) => {273 p!(out, {e.destruct()} str(",") nl);274 }275 DestructArrayPart::DestructRest(d) => {276 p!(out, {d} str(",") nl);277 }278 }279 }280 p!(out, <i str("]"));281 }282 Self::DestructObject(o) => {283 p!(out, str("{") >i nl);284 for item in o.destruct_object_fields() {285 p!(out, { item.field() });286 if let Some(des) = item.destruct() {287 p!(out, str(": ") {des});288 }289 if let Some(def) = item.expr() {290 p!(out, str(" = ") {def});291 }292 p!(out, str(",") nl);293 }294 if let Some(rest) = o.destruct_rest() {295 p!(out, {rest} nl);296 }297 p!(out, <i str("}"));298 }299 }300 }301}302303impl Printable for FieldName {304 fn print(&self, out: &mut PrintItems) {305 match self {306 Self::FieldNameFixed(f) => {307 if let Some(id) = f.id() {308 p!(out, { id });309 } else if let Some(str) = f.text() {310 p!(out, { str });311 } else {312 p!(out, str("/*missing FieldName*/"));313 }314 }315 Self::FieldNameDynamic(d) => {316 p!(out, str("[") {d.expr()} str("]"));317 }318 }319 }320}321322impl Printable for Visibility {323 fn print(&self, out: &mut PrintItems) {324 p!(out, string(self.to_string()));325 }326}327328impl Printable for ObjLocal {329 fn print(&self, out: &mut PrintItems) {330 p!(out, str("local ") {self.bind()});331 }332}333334impl Printable for Assertion {335 fn print(&self, out: &mut PrintItems) {336 p!(out, str("assert ") {self.condition()});337 if self.colon_token().is_some() || self.message().is_some() {338 p!(out, str(": ") {self.message()});339 }340 }341}342343impl Printable for ParamsDesc {344 fn print(&self, out: &mut PrintItems) {345 p!(out, str("(") >i nl);346 for param in self.params() {347 p!(out, { param.destruct() });348 if param.assign_token().is_some() || param.expr().is_some() {349 p!(out, str(" = ") {param.expr()});350 }351 p!(out, str(",") nl);352 }353 p!(out, <i str(")"));354 }355}356impl Printable for ArgsDesc {357 fn print(&self, out: &mut PrintItems) {358 let start = LineNumber::new("args start line");359 let end = LineNumber::new("args end line");360 let multi_line = Rc::new(move |condition_context: &mut ConditionResolverContext| {361 is_multiple_lines(condition_context, start, end)362 });363364 let (children, end_comments) = children_between::<Arg>(365 self.syntax().clone(),366 self.l_paren_token().map(Into::into).as_ref(),367 self.r_paren_token().map(Into::into).as_ref(),368 None,369 );370371 fn gen_args(children: Vec<Child<Arg>>, multi_line: ConditionResolver) -> PrintItems {372 let mut _out = PrintItems::new();373 let out = &mut _out;374375 let mut args = children.into_iter().peekable();376 while let Some(ele) = args.next() {377 if ele.should_start_with_newline {378 p!(out, nl);379 }380 format_comments(&ele.before_trivia, CommentLocation::AboveItem, out);381 let arg = ele.value;382 if arg.name().is_some() || arg.assign_token().is_some() {383 p!(out, {arg.name()} str(" = "));384 }385 p!(out, { arg.expr() });386 let has_more = args.peek().is_some();387 if has_more {388 p!(out, str(","));389 } else {390 p!(out, if("trailing comma", multi_line, str(",")));391 }392 format_comments(&ele.inline_trivia, CommentLocation::ItemInline, out);393 if has_more {394 p!(out, if_else("arg separator", multi_line, nl)(sonl));395 }396 }397 _out398 }399400 let args_items = new_line_group(gen_args(children, multi_line.clone())).into_rc_path();401 let args_indented = with_indent(pi!(@i; nl items(args_items.into())));402403 p!(out, str("(") info(start));404 p!(out, if_else("args body", multi_line, items(args_indented) nl)(items(args_items.into())));405 if end_comments.should_start_with_newline {406 p!(out, nl);407 }408 format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);409 p!(out, str(")") info(end));410 }411}412impl Printable for SliceDesc {413 fn print(&self, out: &mut PrintItems) {414 p!(out, str("["));415 if self.from().is_some() {416 p!(out, { self.from() });417 }418 p!(out, str(":"));419 if self.end().is_some() {420 p!(out, { self.end().map(|e| e.expr()) });421 }422 // Keep only one : in case if we don't need step423 if self.step().is_some() {424 p!(out, str(":") {self.step().map(|e|e.expr())});425 }426 p!(out, str("]"));427 }428}429430impl Printable for Member {431 fn print(&self, out: &mut PrintItems) {432 match self {433 Self::MemberBindStmt(b) => {434 p!(out, { b.obj_local() });435 }436 Self::MemberAssertStmt(ass) => {437 p!(out, { ass.assertion() });438 }439 Self::MemberFieldNormal(n) => {440 p!(out, {n.field_name()} if(n.plus_token().is_some())({n.plus_token()}) {n.visibility()} str(" ") {n.expr()});441 }442 Self::MemberFieldMethod(m) => {443 p!(out, {m.field_name()} {m.params_desc()} {m.visibility()} str(" ") {m.expr()});444 }445 }446 }447}448449impl Printable for ObjBody {450 fn print(&self, out: &mut PrintItems) {451 match self {452 Self::ObjBodyComp(l) => {453 let (children, mut end_comments) = children_between::<Member>(454 l.syntax().clone(),455 l.l_brace_token().map(Into::into).as_ref(),456 Some(457 &(l.comp_specs()458 .next()459 .expect("at least one spec is defined")460 .syntax()461 .clone())462 .into(),463 ),464 None,465 );466 let trailing_for_comp = end_comments.extract_trailing();467 p!(out, str("{") >i nl);468 for mem in children {469 if mem.should_start_with_newline {470 p!(out, nl);471 }472 format_comments(&mem.before_trivia, CommentLocation::AboveItem, out);473 p!(out, {mem.value} str(","));474 format_comments(&mem.inline_trivia, CommentLocation::ItemInline, out);475 p!(out, nl);476 }477478 if end_comments.should_start_with_newline {479 p!(out, nl);480 }481 format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);482483 let (compspecs, end_comments) = children_between::<CompSpec>(484 l.syntax().clone(),485 l.member_comps()486 .last()487 .map(|m| m.syntax().clone())488 .map(Into::into)489 .or_else(|| l.l_brace_token().map(Into::into))490 .as_ref(),491 l.r_brace_token().map(Into::into).as_ref(),492 Some(trailing_for_comp),493 );494 for mem in compspecs {495 if mem.should_start_with_newline {496 p!(out, nl);497 }498 format_comments(&mem.before_trivia, CommentLocation::AboveItem, out);499 p!(out, { mem.value });500 format_comments(&mem.inline_trivia, CommentLocation::ItemInline, out);501 }502 if end_comments.should_start_with_newline {503 p!(out, nl);504 }505 format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);506507 p!(out, nl <i str("}"));508 }509 Self::ObjBodyMemberList(l) => {510 let (children, end_comments) = children_between::<Member>(511 l.syntax().clone(),512 l.l_brace_token().map(Into::into).as_ref(),513 l.r_brace_token().map(Into::into).as_ref(),514 None,515 );516 if children.is_empty() && end_comments.is_empty() {517 p!(out, str("{ }"));518 return;519 }520521 let source_is_multiline = children.iter().any(|c| c.triggers_multiline)522 || end_comments.should_start_with_newline;523524 let start = LineNumber::new("obj start line");525 let end = LineNumber::new("obj end line");526 let multi_line: ConditionResolver = if source_is_multiline {527 true_resolver()528 } else {529 Rc::new(move |ctx: &mut ConditionResolverContext| {530 is_multiple_lines(ctx, start, end)531 })532 };533534 fn gen_members(535 children: Vec<Child<Member>>,536 multi_line: ConditionResolver,537 ) -> PrintItems {538 let mut _out = PrintItems::new();539 let out = &mut _out;540 let mut members = children.into_iter().peekable();541 while let Some(mem) = members.next() {542 if mem.should_start_with_newline {543 p!(out, nl);544 }545 format_comments(&mem.before_trivia, CommentLocation::AboveItem, out);546 p!(out, { mem.value });547 let has_more = members.peek().is_some();548 if has_more {549 p!(out, str(","));550 } else {551 p!(out, if("trailing comma", multi_line, str(",")));552 }553 format_comments(&mem.inline_trivia, CommentLocation::ItemInline, out);554 p!(out, if_else("member separator", multi_line, nl)(sonl));555 }556 _out557 }558559 let members_items =560 new_line_group(gen_members(children, multi_line.clone())).into_rc_path();561562 let members = with_indent_eoi(multi_line, members_items.into(), end_comments);563564 p!(out, str("{") info(start));565 p!(out, items(members));566 p!(out, str("}") info(end));567 }568 }569 }570}571impl Printable for UnaryOperator {572 fn print(&self, out: &mut PrintItems) {573 p!(out, string(self.text().to_string()));574 }575}576impl Printable for BinaryOperator {577 fn print(&self, out: &mut PrintItems) {578 p!(out, string(self.text().to_string()));579 }580}581impl Printable for Bind {582 fn print(&self, out: &mut PrintItems) {583 match self {584 Self::BindDestruct(d) => {585 p!(out, {d.into()} str(" = ") {d.value()});586 }587 Self::BindFunction(f) => {588 p!(out, {f.name()} {f.params()} str(" = ") {f.value()});589 }590 }591 }592}593impl Printable for Literal {594 fn print(&self, out: &mut PrintItems) {595 p!(out, string(self.syntax().to_string()));596 }597}598impl Printable for ImportKind {599 fn print(&self, out: &mut PrintItems) {600 p!(out, string(self.syntax().to_string()));601 }602}603impl Printable for ForSpec {604 fn print(&self, out: &mut PrintItems) {605 p!(out, str("for ") {self.bind()} str(" in ") {self.expr()});606 }607}608impl Printable for IfSpec {609 fn print(&self, out: &mut PrintItems) {610 p!(out, str("if ") {self.expr()});611 }612}613impl Printable for CompSpec {614 fn print(&self, out: &mut PrintItems) {615 match self {616 Self::ForSpec(f) => f.print(out),617 Self::IfSpec(i) => i.print(out),618 }619 }620}621impl Printable for Expr {622 fn print(&self, out: &mut PrintItems) {623 let (stmts, _ending) = children_between::<Stmt>(624 self.syntax().clone(),625 None,626 self.expr_base()627 .as_ref()628 .map(ExprBase::syntax)629 .cloned()630 .map(Into::into)631 .as_ref(),632 None,633 );634 for stmt in stmts {635 p!(out, { stmt.value });636 }637 p!(out, { self.expr_base() });638 let (suffixes, _ending) = children_between::<Suffix>(639 self.syntax().clone(),640 self.expr_base()641 .as_ref()642 .map(ExprBase::syntax)643 .cloned()644 .map(Into::into)645 .as_ref(),646 None,647 None,648 );649 for suffix in suffixes {650 p!(out, { suffix.value });651 }652 }653}654impl Printable for Suffix {655 fn print(&self, out: &mut PrintItems) {656 match self {657 Self::SuffixIndex(i) => {658 if i.question_mark_token().is_some() {659 p!(out, str("?"));660 }661 p!(out, str(".") {i.index()});662 }663 Self::SuffixIndexExpr(e) => {664 if e.question_mark_token().is_some() {665 p!(out, str(".?"));666 }667 p!(out, str("[") {e.index()} str("]"));668 }669 Self::SuffixSlice(d) => {670 p!(out, { d.slice_desc() });671 }672 Self::SuffixApply(a) => {673 p!(out, { a.args_desc() });674 }675 }676 }677}678impl Printable for Stmt {679 fn print(&self, out: &mut PrintItems) {680 match self {681 Self::StmtLocal(l) => {682 let (binds, end_comments) = children_between::<Bind>(683 l.syntax().clone(),684 l.local_kw_token().map(Into::into).as_ref(),685 l.semi_token().map(Into::into).as_ref(),686 None,687 );688 if binds.len() == 1 {689 let bind = &binds[0];690 format_comments(&bind.before_trivia, CommentLocation::AboveItem, out);691 p!(out, str("local ") {bind.value});692 // TODO: keep end_comments, child.inline_trivia somehow, force multiple locals formatting in case of presence?693 } else {694 p!(out,str("local") >i nl);695 for bind in binds {696 if bind.should_start_with_newline {697 p!(out, nl);698 }699 format_comments(&bind.before_trivia, CommentLocation::AboveItem, out);700 p!(out, {bind.value} str(","));701 format_comments(&bind.inline_trivia, CommentLocation::ItemInline, out);702 p!(out, nl);703 }704 if end_comments.should_start_with_newline {705 p!(out, nl);706 }707 format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);708 p!(out,<i);709 }710 p!(out,str(";") nl);711 }712 Self::StmtAssert(a) => {713 p!(out, {a.assertion()} str(";") nl);714 }715 }716 }717}718impl Printable for ExprBase {719 fn print(&self, out: &mut PrintItems) {720 match self {721 Self::ExprBinary(b) => {722 p!(out, {b.lhs()} str(" ") {b.binary_operator()} str(" ") {b.rhs()});723 }724 Self::ExprUnary(u) => p!(out, {u.unary_operator()} {u.rhs()}),725 // Self::ExprSlice(s) => {726 // p!(new: {s.expr()} {s.slice_desc()})727 // }728 // Self::ExprIndex(i) => {729 // p!(new: {i.expr()} str(".") {i.index()})730 // }731 // Self::ExprIndexExpr(i) => p!(new: {i.base()} str("[") {i.index()} str("]")),732 // Self::ExprApply(a) => {733 // let mut pi = p!(new: {a.expr()} {a.args_desc()});734 // if a.tailstrict_kw_token().is_some() {735 // p!(out,str(" tailstrict"));736 // }737 // pi738 // }739 Self::ExprObjExtend(ex) => {740 p!(out, {ex.lhs_work()} str(" ") {ex.rhs_work()});741 }742 Self::ExprParened(p) => {743 p!(out, str("(") {p.expr()} str(")"));744 }745 Self::ExprString(s) => p!(out, { s.text() }),746 Self::ExprNumber(n) => p!(out, { n.number() }),747 Self::ExprArray(a) => {748 p!(out, str("[") >i nl);749 for el in a.exprs() {750 p!(out, {el} str(",") nl);751 }752 p!(out, <i str("]"));753 }754 Self::ExprObject(obj) => {755 p!(out, { obj.obj_body() });756 }757 Self::ExprArrayComp(arr) => {758 p!(out, str("[") {arr.expr()});759 for spec in arr.comp_specs() {760 p!(out, str(" ") {spec});761 }762 p!(out, str("]"));763 }764 Self::ExprImport(v) => {765 p!(out, {v.import_kind()} str(" ") {v.text()});766 }767 Self::ExprVar(n) => p!(out, { n.name() }),768 // Self::ExprLocal(l) => {769 // }770 Self::ExprIfThenElse(ite) => {771 p!(out, str("if ") {ite.cond()} str(" then ") {ite.then().map(|t| t.expr())});772 if ite.else_kw_token().is_some() || ite.else_().is_some() {773 p!(out, str(" else ") {ite.else_().map(|t| t.expr())});774 }775 }776 Self::ExprFunction(f) => p!(out, str("function") {f.params_desc()} nl {f.expr()}),777 // Self::ExprAssert(a) => p!(new: {a.assertion()} str("; ") {a.expr()}),778 Self::ExprError(e) => p!(out, str("error ") {e.expr()}),779 Self::ExprLiteral(l) => {780 p!(out, { l.literal() });781 }782 }783 }784}785786impl Printable for SourceFile {787 fn print(&self, out: &mut PrintItems) {788 let before = trivia_before(789 self.syntax().clone(),790 self.expr()791 .map(|e| e.syntax().clone())792 .map(Into::into)793 .as_ref(),794 );795 let after = trivia_after(796 self.syntax().clone(),797 self.expr()798 .map(|e| e.syntax().clone())799 .map(Into::into)800 .as_ref(),801 );802 format_comments(&before, CommentLocation::AboveItem, out);803 p!(out, {self.expr()} nl);804 format_comments(&after, CommentLocation::EndOfItems, out);805 }806}807808pub struct FormatOptions {809 // 0 for hard tabs810 pub indent: u8,811}812pub fn format(input: &str, opts: &FormatOptions) -> Result<String, SnippetBuilder> {813 let (parsed, errors) = jrsonnet_rowan_parser::parse(input);814 if !errors.is_empty() {815 let mut builder = hi_doc::SnippetBuilder::new(input);816 for error in errors {817 builder818 .error(hi_doc::Text::fragment(819 format!("{:?}", error.error),820 Formatting::default(),821 ))822 .range(823 error.range.start().into()824 ..=(usize::from(error.range.end()) - 1).max(error.range.start().into()),825 )826 .build();827 }828 // let snippet = builder.build();829 return Err(builder);830 // It is possible to recover from this failure, but the output may be broken, as formatter is free to skip831 // ERROR rowan nodes.832 // Recovery needs to be enabled for LSP, though.833 }834 Ok(dprint_core::formatting::format(835 || {836 let mut out = PrintItems::new();837 parsed.print(&mut out);838 out839 },840 PrintOptions {841 indent_width: if opts.indent == 0 {842 // Reasonable max length for both 2 and 4 space sized tabs.843 3844 } else {845 opts.indent846 },847 max_width: 100,848 use_tabs: opts.indent == 0,849 new_line_text: "\n",850 },851 ))852}crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@string_styles.jsonnet.snapdiffbeforeafterboth--- a/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@string_styles.jsonnet.snap
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@string_styles.jsonnet.snap
@@ -8,7 +8,18 @@
single_quote: 'hello world',
escaped: 'line1\nline2',
multiline: |||
- This is a
- multiline string
- |||,
+ This is a
+
+ multiline string
+ |||,
+ multiline_truncated: |||-
+ This is a
+
+ multiline string with truncated newline
+ |||,
+ multiline_to_truncated: |||
+ This is a
+
+ multiline string with to-be truncated newline
+ |||,
}
crates/jrsonnet-formatter/src/tests.rsdiffbeforeafterboth--- a/crates/jrsonnet-formatter/src/tests.rs
+++ b/crates/jrsonnet-formatter/src/tests.rs
@@ -3,7 +3,6 @@
use std::fs;
use dprint_core::formatting::{PrintItems, PrintOptions};
-use indoc::indoc;
use insta::{assert_snapshot, glob};
use crate::Printable;
crates/jrsonnet-formatter/src/tests/string_styles.jsonnetdiffbeforeafterboth--- a/crates/jrsonnet-formatter/src/tests/string_styles.jsonnet
+++ b/crates/jrsonnet-formatter/src/tests/string_styles.jsonnet
@@ -4,6 +4,18 @@
escaped: 'line1\nline2',
multiline: |||
This is a
+
multiline string
|||,
+ multiline_truncated: |||-
+ This is a
+
+ multiline string with truncated newline
+ |||,
+ multiline_to_truncated: |||-
+ This is a
+
+ multiline string with to-be truncated newline
+
+ |||,
}
crates/jrsonnet-rowan-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/src/lib.rs
+++ b/crates/jrsonnet-rowan-parser/src/lib.rs
@@ -22,6 +22,7 @@
pub use generated::{nodes, syntax_kinds::SyntaxKind};
pub use language::*;
pub use token_set::SyntaxKindSet;
+pub use string_block::{collect_lexed_str_block, CollectStrBlock};
use self::{
ast::support,
crates/jrsonnet-rowan-parser/src/string_block.rsdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/src/string_block.rs
+++ b/crates/jrsonnet-rowan-parser/src/string_block.rs
@@ -6,142 +6,193 @@
MissingIndent,
}
-use std::ops::Range;
-
use logos::Lexer;
use StringBlockError::*;
use crate::SyntaxKind;
-pub fn lex_str_block_test(lex: &mut Lexer<SyntaxKind>) {
+pub(crate) fn lex_str_block_test<'d>(lex: &mut Lexer<'d, SyntaxKind>) {
let _ = lex_str_block(lex);
}
-#[allow(clippy::too_many_lines)]
-pub fn lex_str_block(lex: &mut Lexer<SyntaxKind>) -> Result<(), StringBlockError> {
- struct Context<'a> {
- source: &'a str,
- index: usize,
- offset: usize,
+pub(crate) struct Context<'a> {
+ source: &'a str,
+ index: usize,
+}
+
+impl<'a> Context<'a> {
+ fn rest(&self) -> &'a str {
+ &self.source[self.index..]
}
- impl<'a> Context<'a> {
- fn rest(&self) -> &'a str {
- &self.source[self.index..]
+ fn next(&mut self) -> Option<char> {
+ if self.index == self.source.len() {
+ return None;
}
- fn next(&mut self) -> Option<char> {
- if self.index == self.source.len() {
- return None;
+ match self.rest().chars().next() {
+ None => None,
+ Some(c) => {
+ self.index += c.len_utf8();
+ Some(c)
}
+ }
+ }
- match self.rest().chars().next() {
- None => None,
- Some(c) => {
- self.index += c.len_utf8();
- Some(c)
- }
- }
+ fn peek(&self) -> Option<char> {
+ if self.index == self.source.len() {
+ return None;
}
- fn peek(&self) -> Option<char> {
- if self.index == self.source.len() {
- return None;
- }
+ self.rest().chars().next()
+ }
- self.rest().chars().next()
+ fn eat_if(&mut self, f: impl Fn(char) -> bool) -> usize {
+ if self.peek().map(f).unwrap_or(false) {
+ self.index += 1;
+ return 1;
}
+ 0
+ }
- fn eat_if(&mut self, f: impl Fn(char) -> bool) -> usize {
- if self.peek().map(f).unwrap_or(false) {
- self.index += 1;
- return 1;
- }
- 0
+ fn eat_while(&mut self, f: impl Fn(char) -> bool) -> usize {
+ if self.index == self.source.len() {
+ return 0;
}
- fn eat_while(&mut self, f: impl Fn(char) -> bool) -> usize {
- if self.index == self.source.len() {
- return 0;
- }
+ let next_char = self.rest().char_indices().find(|(_, c)| !f(*c));
- let next_char = self.rest().char_indices().find(|(_, c)| !f(*c));
-
- match next_char {
- None => {
- let diff = self.source.len() - self.index;
- self.index = self.source.len();
- diff
- }
- Some((idx, _)) => {
- self.index += idx;
- idx
- }
+ match next_char {
+ None => {
+ let diff = self.source.len() - self.index;
+ self.index = self.source.len();
+ diff
}
- }
-
- fn skip(&mut self, len: usize) {
- self.index = match self.index + len {
- n if n > self.source.len() => self.source.len(),
- n => n,
- };
- }
-
- #[allow(clippy::range_plus_one)]
- fn pos(&self) -> Range<usize> {
- if self.index == self.source.len() {
- self.offset + self.index..self.offset + self.index
- } else {
- // TODO: char size
- self.offset + self.index..self.offset + self.index + 1
+ Some((idx, _)) => {
+ self.index += idx;
+ idx
}
}
}
- // Check that b has at least the same whitespace prefix as a and returns the
- // amount of this whitespace, otherwise returns 0. If a has no whitespace
- // prefix than return 0.
- fn check_whitespace(a: &str, b: &str) -> usize {
- let a = a.as_bytes();
- let b = b.as_bytes();
+ fn skip(&mut self, len: usize) {
+ self.index = match self.index + len {
+ n if n > self.source.len() => self.source.len(),
+ n => n,
+ };
+ }
+}
- for i in 0..a.len() {
- if a[i] != b' ' && a[i] != b'\t' {
- // a has run out of whitespace and b matched up to this point. Return result.
- return i;
- }
+// Check that b has at least the same whitespace prefix as a and returns the
+// amount of this whitespace, otherwise returns 0. If a has no whitespace
+// prefix than return 0.
+fn check_whitespace(a: &str, b: &str) -> usize {
+ let a = a.as_bytes();
+ let b = b.as_bytes();
- if i >= b.len() {
- // We ran off the edge of b while a still has whitespace. Return 0 as failure.
- return 0;
- }
+ for i in 0..a.len() {
+ if a[i] != b' ' && a[i] != b'\t' {
+ // a has run out of whitespace and b matched up to this point. Return result.
+ return i;
+ }
- if a[i] != b[i] {
- // a has whitespace but b does not. Return 0 as failure.
- return 0;
- }
+ if i >= b.len() {
+ // We ran off the edge of b while a still has whitespace. Return 0 as failure.
+ return 0;
}
- // We ran off the end of a and b kept up
- a.len()
+ if a[i] != b[i] {
+ // a has whitespace but b does not. Return 0 as failure.
+ return 0;
+ }
}
- fn guess_token_end_and_bump<'a>(lex: &mut Lexer<'a, SyntaxKind>, ctx: &Context<'a>) {
+ // We ran off the end of a and b kept up
+ a.len()
+}
+
+pub(crate) trait StrBlockLexCtx<'d> {
+ fn remainder(&self) -> &'d str;
+ fn eat_error(&mut self, ctx: &Context<'d>);
+ fn bump_pos(&mut self, s: usize);
+ fn mark_truncating(&mut self);
+ fn mark_line(&mut self, line: &'d str);
+}
+
+impl<'d> StrBlockLexCtx<'d> for Lexer<'d, SyntaxKind> {
+ fn remainder(&self) -> &'d str {
+ self.remainder()
+ }
+ fn eat_error(&mut self, ctx: &Context<'d>) {
let end_index = ctx
.rest()
.find("|||")
.map_or_else(|| ctx.rest().len(), |v| v + 3);
- lex.bump(ctx.index + end_index);
+ self.bump(ctx.index + end_index);
}
+ fn bump_pos(&mut self, s: usize) {
+ self.bump(s);
+ }
+ fn mark_truncating(&mut self) {
+ // Lexer test doesn't collect anything
+ }
+ fn mark_line(&mut self, _line: &'d str) {
+ // Lexer test doesn't collect anything
+ }
+}
- debug_assert_eq!(lex.slice(), "|||");
- let mut ctx = Context {
+pub fn collect_lexed_str_block<'s>(
+ input: &'s str,
+) -> Result<CollectStrBlock<'s>, StringBlockError> {
+ let mut collect = CollectStrBlock {
+ truncate: false,
+ lines: vec![],
+ input,
+ offset: 0,
+ };
+ lex_str_block(&mut collect)?;
+ Ok(collect)
+}
+
+pub struct CollectStrBlock<'s> {
+ pub truncate: bool,
+ pub lines: Vec<&'s str>,
+ input: &'s str,
+ offset: usize,
+}
+
+impl<'d> StrBlockLexCtx<'d> for CollectStrBlock<'d> {
+ fn remainder(&self) -> &'d str {
+ self.input
+ }
+
+ fn eat_error(&mut self, _ctx: &Context<'d>) {
+ // Error will be returned, no need to record it here
+ }
+
+ fn bump_pos(&mut self, s: usize) {
+ self.offset += s;
+ }
+
+ fn mark_truncating(&mut self) {
+ self.truncate = true;
+ }
+
+ fn mark_line(&mut self, line: &'d str) {
+ self.lines.push(line)
+ }
+}
+
+pub(crate) fn lex_str_block<'a>(lex: &mut impl StrBlockLexCtx<'a>) -> Result<(), StringBlockError> {
+ // debug_assert_eq!(lex.slice(), "|||");
+ let mut ctx = Context::<'a> {
source: lex.remainder(),
index: 0,
- offset: lex.span().end,
};
- ctx.eat_if(|v| v == '-');
+ if ctx.eat_if(|v| v == '-') != 0 {
+ lex.mark_truncating();
+ }
// Skip whitespaces
ctx.eat_while(|r| r == ' ' || r == '\t' || r == '\r');
@@ -150,12 +201,12 @@
match ctx.next() {
Some('\n') => (),
None => {
- guess_token_end_and_bump(lex, &ctx);
+ lex.eat_error(&ctx);
return Err(UnexpectedEnd);
}
// Text block requires new line after |||.
Some(_) => {
- guess_token_end_and_bump(lex, &ctx);
+ lex.eat_error(&ctx);
return Err(MissingNewLine);
}
}
@@ -170,7 +221,7 @@
if num_whitespace == 0 {
// Text block's first line must start with whitespace
- guess_token_end_and_bump(lex, &ctx);
+ lex.eat_error(&ctx);
return Err(MissingIndent);
}
@@ -178,19 +229,27 @@
debug_assert_ne!(num_whitespace, 0, "Unexpected value for num_whitespace");
ctx.skip(num_whitespace);
+ let line_start = ctx.index;
+ let mut line_size = 0;
loop {
match ctx.next() {
None => {
- guess_token_end_and_bump(lex, &ctx);
+ lex.eat_error(&ctx);
return Err(UnexpectedEnd);
}
- Some('\n') => break,
- Some(_) => (),
+ Some('\n') => {
+ lex.mark_line(&ctx.source[line_start..line_start + line_size]);
+ break;
+ }
+ Some(c) => {
+ line_size += c.len_utf8();
+ }
}
}
// Skip any blank lines
while ctx.peek() == Some('\n') {
+ lex.mark_line("");
ctx.next();
}
@@ -206,15 +265,11 @@
}
if !ctx.rest().starts_with("|||") {
- // Text block not terminated with |||
- let pos = ctx.pos();
- if pos.is_empty() {
- // eof
- lex.bump(ctx.index);
+ if ctx.rest().is_empty() {
+ lex.bump_pos(ctx.index);
return Err(UnexpectedEnd);
}
-
- guess_token_end_and_bump(lex, &ctx);
+ lex.eat_error(&ctx);
return Err(MissingTermination);
}
@@ -224,6 +279,6 @@
}
}
- lex.bump(ctx.index);
+ lex.bump_pos(ctx.index);
Ok(())
}