Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
Rename Signal to Tag
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Sep 23, 2022
1 parent e159f4c commit fc06d58
Show file tree
Hide file tree
Showing 17 changed files with 353 additions and 379 deletions.
6 changes: 3 additions & 3 deletions crates/rome_formatter/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl<'fmt, Context> From<&'fmt Argument<'fmt, Context>> for Arguments<'fmt, Cont

#[cfg(test)]
mod tests {
use crate::format_element::signal::Signal;
use crate::format_element::tag::Tag;
use crate::prelude::*;
use crate::{format_args, write, FormatState, VecBuffer};

Expand Down Expand Up @@ -150,10 +150,10 @@ mod tests {
FormatElement::Text(Text::Static { text: "a" }),
FormatElement::Space,
// Group
FormatElement::Signal(Signal::StartGroup(None)),
FormatElement::Tag(Tag::StartGroup(None)),
FormatElement::Text(Text::Static { text: "(" }),
FormatElement::Text(Text::Static { text: ")" }),
FormatElement::Signal(Signal::EndGroup)
FormatElement::Tag(Tag::EndGroup)
]
);
}
Expand Down
66 changes: 32 additions & 34 deletions crates/rome_formatter/src/builders.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::format_element::signal::{Condition, Signal};
use crate::prelude::signal::{DedentMode, LabelId};
use crate::format_element::tag::{Condition, Tag};
use crate::prelude::tag::{DedentMode, LabelId};
use crate::prelude::*;
use crate::{format_element, write, Argument, Arguments, GroupId, TextRange, TextSize};
use crate::{Buffer, VecBuffer};
Expand All @@ -8,7 +8,7 @@ use std::borrow::Cow;
use std::cell::Cell;
use std::marker::PhantomData;
use std::num::NonZeroU8;
use Signal::*;
use Tag::*;

/// A line break that only gets printed if the enclosing `Group` doesn't fit on a single line.
/// It's omitted if the enclosing `Group` fits on a single line.
Expand Down Expand Up @@ -433,9 +433,9 @@ pub struct LineSuffix<'a, Context> {

impl<Context> Format<Context> for LineSuffix<'_, Context> {
fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
f.write_element(FormatElement::Signal(StartLineSuffix))?;
f.write_element(FormatElement::Tag(StartLineSuffix))?;
Arguments::from(&self.content).fmt(f)?;
f.write_element(FormatElement::Signal(EndLineSuffix))
f.write_element(FormatElement::Tag(EndLineSuffix))
}
}

Expand Down Expand Up @@ -487,7 +487,7 @@ impl<Context> Format<Context> for LineSuffixBoundary {
/// Marks some content with a label.
///
/// This does not directly influence how this content will be printed, but some
/// parts of the formatter may inspect the [labelled element](Signal::Label)
/// parts of the formatter may inspect the [labelled element](Tag::StartLabelled)
/// using [FormatElements::has_label].
///
/// ## Examples
Expand Down Expand Up @@ -550,9 +550,9 @@ pub struct FormatLabelled<'a, Context> {

impl<Context> Format<Context> for FormatLabelled<'_, Context> {
fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
f.write_element(FormatElement::Signal(StartLabelled(self.label_id)))?;
f.write_element(FormatElement::Tag(StartLabelled(self.label_id)))?;
Arguments::from(&self.content).fmt(f)?;
f.write_element(FormatElement::Signal(EndLabelled))
f.write_element(FormatElement::Tag(EndLabelled))
}
}

Expand Down Expand Up @@ -647,9 +647,9 @@ pub struct Indent<'a, Context> {

impl<Context> Format<Context> for Indent<'_, Context> {
fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
f.write_element(FormatElement::Signal(StartIndent))?;
f.write_element(FormatElement::Tag(StartIndent))?;
Arguments::from(&self.content).fmt(f)?;
f.write_element(FormatElement::Signal(EndIndent))
f.write_element(FormatElement::Tag(EndIndent))
}
}

Expand Down Expand Up @@ -718,9 +718,9 @@ pub struct Dedent<'a, Context> {

impl<Context> Format<Context> for Dedent<'_, Context> {
fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
f.write_element(FormatElement::Signal(StartDedent(self.mode)))?;
f.write_element(FormatElement::Tag(StartDedent(self.mode)))?;
Arguments::from(&self.content).fmt(f)?;
f.write_element(FormatElement::Signal(EndDedent))
f.write_element(FormatElement::Tag(EndDedent))
}
}

Expand Down Expand Up @@ -903,9 +903,9 @@ pub struct Align<'a, Context> {

impl<Context> Format<Context> for Align<'_, Context> {
fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
f.write_element(FormatElement::Signal(StartAlign(signal::Align(self.count))))?;
f.write_element(FormatElement::Tag(StartAlign(tag::Align(self.count))))?;
Arguments::from(&self.content).fmt(f)?;
f.write_element(FormatElement::Signal(EndAlign))
f.write_element(FormatElement::Tag(EndAlign))
}
}

Expand Down Expand Up @@ -1123,7 +1123,7 @@ impl<Context> Format<Context> for BlockIndent<'_, Context> {
fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
let snapshot = f.snapshot();

f.write_element(FormatElement::Signal(StartIndent))?;
f.write_element(FormatElement::Tag(StartIndent))?;

match self.mode {
IndentMode::Soft => write!(f, [soft_line_break()])?,
Expand All @@ -1144,7 +1144,7 @@ impl<Context> Format<Context> for BlockIndent<'_, Context> {
return Ok(());
}

f.write_element(FormatElement::Signal(EndIndent))?;
f.write_element(FormatElement::Tag(EndIndent))?;

match self.mode {
IndentMode::Soft => write!(f, [soft_line_break()]),
Expand Down Expand Up @@ -1336,8 +1336,8 @@ impl<Context> Group<'_, Context> {
/// line or contains any hard line breaks.
///
/// The formatter writes a [FormatElement::ExpandParent], forcing any enclosing group to expand, if `should_expand` is `true`.
/// It also omits the [`start`](Signal::StartGroup) and [`end`](Signal::EndGroup) signals because the group would be forced to expand anyway.
/// The [`start`](Signal::StartGroup) and [`end`](Signal::EndGroup) signals are only written if the `group_id` specified with [Group::with_group_id] isn't [None]
/// It also omits the [`start`](Tag::StartGroup) and [`end`](Tag::EndGroup) tags because the group would be forced to expand anyway.
/// The [`start`](Tag::StartGroup) and [`end`](Tag::EndGroup) tags are only written if the `group_id` specified with [Group::with_group_id] isn't [None]
/// because other IR elements may reference the group with that group id and the printer may panic
/// if no group with the given id is present in the document.
pub fn should_expand(mut self, should_expand: bool) -> Self {
Expand All @@ -1356,7 +1356,7 @@ impl<Context> Format<Context> for Group<'_, Context> {
let write_group = !self.should_expand || self.group_id.is_some();

if write_group {
f.write_element(FormatElement::Signal(Signal::StartGroup(self.group_id)))?;
f.write_element(FormatElement::Tag(Tag::StartGroup(self.group_id)))?;
}

if self.should_expand {
Expand All @@ -1366,7 +1366,7 @@ impl<Context> Format<Context> for Group<'_, Context> {
Arguments::from(&self.content).fmt(f)?;

if write_group {
f.write_element(FormatElement::Signal(Signal::EndGroup))?;
f.write_element(FormatElement::Tag(Tag::EndGroup))?;
}

Ok(())
Expand Down Expand Up @@ -1667,11 +1667,11 @@ impl<Context> IfGroupBreaks<'_, Context> {

impl<Context> Format<Context> for IfGroupBreaks<'_, Context> {
fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
f.write_element(FormatElement::Signal(StartConditionalContent(
f.write_element(FormatElement::Tag(StartConditionalContent(
Condition::new(self.mode).with_group_id(self.group_id),
)))?;
Arguments::from(&self.content).fmt(f)?;
f.write_element(FormatElement::Signal(EndConditionalContent))
f.write_element(FormatElement::Tag(EndConditionalContent))
}
}

Expand Down Expand Up @@ -1791,11 +1791,9 @@ pub struct IndentIfGroupBreaks<'a, Context> {

impl<Context> Format<Context> for IndentIfGroupBreaks<'_, Context> {
fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
f.write_element(FormatElement::Signal(StartIndentIfGroupBreaks(
self.group_id,
)))?;
f.write_element(FormatElement::Tag(StartIndentIfGroupBreaks(self.group_id)))?;
Arguments::from(&self.content).fmt(f)?;
f.write_element(FormatElement::Signal(EndIndentIfGroupBreaks))
f.write_element(FormatElement::Tag(EndIndentIfGroupBreaks))
}
}

Expand Down Expand Up @@ -2146,7 +2144,7 @@ pub struct FillBuilder<'fmt, 'buf, Context> {

impl<'a, 'buf, Context> FillBuilder<'a, 'buf, Context> {
pub(crate) fn new(fmt: &'a mut Formatter<'buf, Context>) -> Self {
let result = fmt.write_element(FormatElement::Signal(StartFill));
let result = fmt.write_element(FormatElement::Tag(StartFill));

Self {
result,
Expand Down Expand Up @@ -2178,14 +2176,14 @@ impl<'a, 'buf, Context> FillBuilder<'a, 'buf, Context> {
if self.empty {
self.empty = false;
} else {
self.fmt.write_element(FormatElement::Signal(StartEntry))?;
self.fmt.write_element(FormatElement::Tag(StartEntry))?;
separator.fmt(self.fmt)?;
self.fmt.write_element(FormatElement::Signal(EndEntry))?;
self.fmt.write_element(FormatElement::Tag(EndEntry))?;
}

self.fmt.write_element(FormatElement::Signal(StartEntry))?;
self.fmt.write_element(FormatElement::Tag(StartEntry))?;
entry.fmt(self.fmt)?;
self.fmt.write_element(FormatElement::Signal(EndEntry))
self.fmt.write_element(FormatElement::Tag(EndEntry))
});

self
Expand All @@ -2194,7 +2192,7 @@ impl<'a, 'buf, Context> FillBuilder<'a, 'buf, Context> {
/// Finishes the output and returns any error encountered
pub fn finish(&mut self) -> FormatResult<()> {
self.result
.and_then(|_| self.fmt.write_element(FormatElement::Signal(EndFill)))
.and_then(|_| self.fmt.write_element(FormatElement::Tag(EndFill)))
}
}

Expand Down Expand Up @@ -2232,9 +2230,9 @@ impl<Context> Format<Context> for BestFitting<'_, Context> {
let mut formatted_variants = Vec::with_capacity(variants.len());

for variant in variants {
buffer.write_element(FormatElement::Signal(StartEntry))?;
buffer.write_element(FormatElement::Tag(StartEntry))?;
buffer.write_fmt(Arguments::from(variant))?;
buffer.write_element(FormatElement::Signal(EndEntry))?;
buffer.write_element(FormatElement::Tag(EndEntry))?;

formatted_variants.push(buffer.take_vec().into_boxed_slice());
}
Expand Down
60 changes: 29 additions & 31 deletions crates/rome_formatter/src/format_element.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub mod document;
pub mod signal;
pub mod tag;

use crate::format_element::signal::{LabelId, Signal};
use crate::format_element::tag::{LabelId, Tag};

use crate::{SignalKind, TextSize};
use crate::{TagKind, TextSize};
#[cfg(target_pointer_width = "64")]
use rome_rowan::static_assert;
use rome_rowan::SyntaxTokenText;
Expand Down Expand Up @@ -41,8 +41,8 @@ pub enum FormatElement {
/// Line breaks inside of a best fitting don't propagate to parent groups.
BestFitting(BestFitting),

/// A signal that marks the start/end of some content to which some special formatting is applied.
Signal(Signal),
/// A [Tag] that marks the start/end of some content to which some special formatting is applied.
Tag(Tag),
}

impl std::fmt::Debug for FormatElement {
Expand All @@ -59,7 +59,7 @@ impl std::fmt::Debug for FormatElement {
FormatElement::Interned(interned) => {
fmt.debug_list().entries(interned.deref()).finish()
}
FormatElement::Signal(signal) => fmt.debug_tuple("Signal").field(signal).finish(),
FormatElement::Tag(tag) => fmt.debug_tuple("Tag").field(tag).finish(),
}
}
}
Expand Down Expand Up @@ -248,23 +248,23 @@ impl Deref for Text {
}

impl FormatElement {
/// Returns `true` if self is a [FormatElement::Signal]
pub const fn is_signal(&self) -> bool {
matches!(self, FormatElement::Signal(_))
/// Returns `true` if self is a [FormatElement::Tag]
pub const fn is_tag(&self) -> bool {
matches!(self, FormatElement::Tag(_))
}

/// Returns `true` if self is a [FormatElement::Signal] and `Signal::is_start` is `true`.
pub const fn is_start_signal(&self) -> bool {
/// Returns `true` if self is a [FormatElement::Tag] and [Tag::is_start] is `true`.
pub const fn is_start_tag(&self) -> bool {
match self {
FormatElement::Signal(signal) => signal.is_start(),
FormatElement::Tag(tag) => tag.is_start(),
_ => false,
}
}

/// Returns `true` if self is a [FormatElement::Signal] and `Signal::is_end` is `true`.
pub const fn is_end_signal(&self) -> bool {
/// Returns `true` if self is a [FormatElement::Tag] and [Tag::is_end] is `true`.
pub const fn is_end_tag(&self) -> bool {
match self {
FormatElement::Signal(signal) => signal.is_end(),
FormatElement::Tag(tag) => tag.is_end(),
_ => false,
}
}
Expand All @@ -279,28 +279,26 @@ impl FormatElements for FormatElement {
FormatElement::Interned(interned) => interned.will_break(),
FormatElement::LineSuffixBoundary
| FormatElement::Space
| FormatElement::Signal(_)
| FormatElement::Tag(_)
| FormatElement::BestFitting(_) => false,
}
}

fn has_label(&self, label_id: LabelId) -> bool {
match self {
FormatElement::Signal(Signal::StartLabelled(actual)) => *actual == label_id,
FormatElement::Tag(Tag::StartLabelled(actual)) => *actual == label_id,
FormatElement::Interned(interned) => interned.deref().has_label(label_id),
_ => false,
}
}

fn start_signal(&self, _: SignalKind) -> Option<&Signal> {
fn start_tag(&self, _: TagKind) -> Option<&Tag> {
None
}

fn end_signal(&self, kind: SignalKind) -> Option<&Signal> {
fn end_tag(&self, kind: TagKind) -> Option<&Tag> {
match self {
FormatElement::Signal(signal) if signal.kind() == kind && signal.is_end() => {
Some(signal)
}
FormatElement::Tag(tag) if tag.kind() == kind && tag.is_end() => Some(tag),
_ => None,
}
}
Expand Down Expand Up @@ -383,14 +381,14 @@ pub trait FormatElements {
/// Returns true if the element has the given label.
fn has_label(&self, label: LabelId) -> bool;

/// Returns the start signal of `kind` if:
/// * the last element is an end signal of `kind`.
/// * there's a matching start signal in this document (may not be true if this slice is an interned element and the `start` is in the document storing the interned element).
fn start_signal(&self, kind: SignalKind) -> Option<&Signal>;
/// Returns the start tag of `kind` if:
/// * the last element is an end tag of `kind`.
/// * there's a matching start tag in this document (may not be true if this slice is an interned element and the `start` is in the document storing the interned element).
fn start_tag(&self, kind: TagKind) -> Option<&Tag>;

/// Returns the end signal if:
/// * the last element is an end signal of `kind`
fn end_signal(&self, kind: SignalKind) -> Option<&Signal>;
/// Returns the end tag if:
/// * the last element is an end tag of `kind`
fn end_tag(&self, kind: TagKind) -> Option<&Tag>;
}

#[cfg(test)]
Expand All @@ -412,14 +410,14 @@ mod tests {
static_assert!(std::mem::size_of::<rome_rowan::TextRange>() == 8usize);

#[cfg(target_pointer_width = "64")]
static_assert!(std::mem::size_of::<crate::format_element::signal::VerbatimKind>() == 8usize);
static_assert!(std::mem::size_of::<crate::format_element::tag::VerbatimKind>() == 8usize);

#[cfg(target_pointer_width = "64")]
static_assert!(std::mem::size_of::<crate::format_element::Text>() == 24usize);

#[cfg(not(debug_assertions))]
#[cfg(target_pointer_width = "64")]
static_assert!(std::mem::size_of::<crate::format_element::Signal>() == 16usize);
static_assert!(std::mem::size_of::<crate::format_element::Tag>() == 16usize);

// Increasing the size of FormatElement has serious consequences on runtime performance and memory footprint.
// Is there a more efficient way to encode the data to avoid increasing its size? Can the information
Expand Down
Loading

0 comments on commit fc06d58

Please sign in to comment.