|
132 | 132 | //! methods called `Printer::scan_*`, and the 'PRINT' process is the |
133 | 133 | //! method called `Printer::print`. |
134 | 134 |
|
135 | | -mod convenience; |
136 | 135 | mod ring; |
137 | 136 |
|
138 | 137 | use std::borrow::Cow; |
@@ -188,6 +187,12 @@ pub(crate) enum Token { |
188 | 187 | End, |
189 | 188 | } |
190 | 189 |
|
| 190 | +impl Token { |
| 191 | + pub(crate) fn is_hardbreak_tok(&self) -> bool { |
| 192 | + *self == Printer::hardbreak_tok_offset(0) |
| 193 | + } |
| 194 | +} |
| 195 | + |
191 | 196 | #[derive(Copy, Clone)] |
192 | 197 | enum PrintFrame { |
193 | 198 | Fits, |
@@ -479,4 +484,132 @@ impl Printer { |
479 | 484 | self.out.push_str(string); |
480 | 485 | self.space -= string.len() as isize; |
481 | 486 | } |
| 487 | + |
| 488 | + /// Synthesizes a comment that was not textually present in the original |
| 489 | + /// source file. |
| 490 | + pub fn synth_comment(&mut self, text: impl Into<Cow<'static, str>>) { |
| 491 | + self.word("/*"); |
| 492 | + self.space(); |
| 493 | + self.word(text); |
| 494 | + self.space(); |
| 495 | + self.word("*/") |
| 496 | + } |
| 497 | + |
| 498 | + /// "raw box" |
| 499 | + pub fn rbox(&mut self, indent: isize, breaks: Breaks) -> BoxMarker { |
| 500 | + self.scan_begin(BeginToken { indent: IndentStyle::Block { offset: indent }, breaks }) |
| 501 | + } |
| 502 | + |
| 503 | + /// Inconsistent breaking box |
| 504 | + pub fn ibox(&mut self, indent: isize) -> BoxMarker { |
| 505 | + self.rbox(indent, Breaks::Inconsistent) |
| 506 | + } |
| 507 | + |
| 508 | + /// Consistent breaking box |
| 509 | + pub fn cbox(&mut self, indent: isize) -> BoxMarker { |
| 510 | + self.rbox(indent, Breaks::Consistent) |
| 511 | + } |
| 512 | + |
| 513 | + pub fn visual_align(&mut self) -> BoxMarker { |
| 514 | + self.scan_begin(BeginToken { indent: IndentStyle::Visual, breaks: Breaks::Consistent }) |
| 515 | + } |
| 516 | + |
| 517 | + pub fn break_offset(&mut self, n: usize, off: isize) { |
| 518 | + self.scan_break(BreakToken { |
| 519 | + offset: off, |
| 520 | + blank_space: n as isize, |
| 521 | + ..BreakToken::default() |
| 522 | + }); |
| 523 | + } |
| 524 | + |
| 525 | + pub fn end(&mut self, b: BoxMarker) { |
| 526 | + self.scan_end(b) |
| 527 | + } |
| 528 | + |
| 529 | + pub fn eof(mut self) -> String { |
| 530 | + self.scan_eof(); |
| 531 | + self.out |
| 532 | + } |
| 533 | + |
| 534 | + pub fn word<S: Into<Cow<'static, str>>>(&mut self, wrd: S) { |
| 535 | + let string = wrd.into(); |
| 536 | + self.scan_string(string) |
| 537 | + } |
| 538 | + |
| 539 | + pub fn word_space<W: Into<Cow<'static, str>>>(&mut self, w: W) { |
| 540 | + self.word(w); |
| 541 | + self.space(); |
| 542 | + } |
| 543 | + |
| 544 | + pub fn nbsp(&mut self) { |
| 545 | + self.word(" ") |
| 546 | + } |
| 547 | + |
| 548 | + pub fn word_nbsp<S: Into<Cow<'static, str>>>(&mut self, w: S) { |
| 549 | + self.word(w); |
| 550 | + self.nbsp() |
| 551 | + } |
| 552 | + |
| 553 | + fn spaces(&mut self, n: usize) { |
| 554 | + self.break_offset(n, 0) |
| 555 | + } |
| 556 | + |
| 557 | + pub fn zerobreak(&mut self) { |
| 558 | + self.spaces(0) |
| 559 | + } |
| 560 | + |
| 561 | + pub fn space(&mut self) { |
| 562 | + self.spaces(1) |
| 563 | + } |
| 564 | + |
| 565 | + pub fn popen(&mut self) { |
| 566 | + self.word("("); |
| 567 | + } |
| 568 | + |
| 569 | + pub fn pclose(&mut self) { |
| 570 | + self.word(")"); |
| 571 | + } |
| 572 | + |
| 573 | + pub fn hardbreak(&mut self) { |
| 574 | + self.spaces(SIZE_INFINITY as usize) |
| 575 | + } |
| 576 | + |
| 577 | + pub fn is_beginning_of_line(&self) -> bool { |
| 578 | + match self.last_token() { |
| 579 | + Some(last_token) => last_token.is_hardbreak_tok(), |
| 580 | + None => true, |
| 581 | + } |
| 582 | + } |
| 583 | + |
| 584 | + pub fn hardbreak_if_not_bol(&mut self) { |
| 585 | + if !self.is_beginning_of_line() { |
| 586 | + self.hardbreak() |
| 587 | + } |
| 588 | + } |
| 589 | + |
| 590 | + pub fn space_if_not_bol(&mut self) { |
| 591 | + if !self.is_beginning_of_line() { |
| 592 | + self.space(); |
| 593 | + } |
| 594 | + } |
| 595 | + |
| 596 | + pub(crate) fn hardbreak_tok_offset(off: isize) -> Token { |
| 597 | + Token::Break(BreakToken { |
| 598 | + offset: off, |
| 599 | + blank_space: SIZE_INFINITY, |
| 600 | + ..BreakToken::default() |
| 601 | + }) |
| 602 | + } |
| 603 | + |
| 604 | + pub fn trailing_comma(&mut self) { |
| 605 | + self.scan_break(BreakToken { pre_break: Some(','), ..BreakToken::default() }); |
| 606 | + } |
| 607 | + |
| 608 | + pub fn trailing_comma_or_space(&mut self) { |
| 609 | + self.scan_break(BreakToken { |
| 610 | + blank_space: 1, |
| 611 | + pre_break: Some(','), |
| 612 | + ..BreakToken::default() |
| 613 | + }); |
| 614 | + } |
482 | 615 | } |
0 commit comments