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

feat(rome_js_formatter): Format Arrow Chains #3122

Merged
merged 6 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion crates/rome_formatter/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,28 +1231,53 @@ pub fn group<Context>(content: &impl Format<Context>) -> Group<Context> {
Group {
content: Argument::new(content),
group_id: None,
should_expand: false,
}
}

#[derive(Copy, Clone)]
pub struct Group<'a, Context> {
content: Argument<'a, Context>,
group_id: Option<GroupId>,
should_expand: bool,
}

impl<Context> Group<'_, Context> {
pub fn with_group_id(mut self, group_id: Option<GroupId>) -> Self {
self.group_id = group_id;
self
}

/// Setting the value to `true` forces the group and its enclosing group to expand regardless if it otherwise would fit on the
/// line or contains any hard line breaks.
///
/// The formatter writes a [FormatElement::ExpandParent], forcing any enclosing group to expand, if `should_expand` is `true``.
MichaReiser marked this conversation as resolved.
Show resolved Hide resolved
/// It also omits the enclosing [FormatElement::Group] because the group would be forced to expand anyway.
/// The [FormatElement:Group] only gets written if the `group id` specified with [Group::with_group_id] isn't [None]
MichaReiser marked this conversation as resolved.
Show resolved Hide resolved
/// 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 {
self.should_expand = should_expand;
self
}
}

impl<Context> Format<Context> for Group<'_, Context> {
fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
if self.group_id.is_none() && self.should_expand {
write!(f, [expand_parent()])?;
return f.write_fmt(Arguments::from(&self.content));
}

let mut buffer = GroupBuffer::new(f);

buffer.write_fmt(Arguments::from(&self.content))?;
let content = buffer.into_vec();

if self.should_expand {
write!(buffer, [expand_parent()])?;
}

let content = buffer.into_vec();
if content.is_empty() && self.group_id.is_none() {
return Ok(());
}
Expand All @@ -1269,6 +1294,7 @@ impl<Context> std::fmt::Debug for Group<'_, Context> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("GroupElements")
.field("group_id", &self.group_id)
.field("should_expand", &self.should_expand)
.field("content", &"{{content}}")
.finish()
}
Expand Down
30 changes: 27 additions & 3 deletions crates/rome_js_formatter/src/js/auxiliary/initializer_clause.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
use crate::prelude::*;

use rome_formatter::write;
use crate::utils::{with_assignment_layout, AssignmentLikeLayout};
use rome_formatter::{write, FormatRuleWithOptions};
use rome_js_syntax::JsInitializerClause;
use rome_js_syntax::JsInitializerClauseFields;

#[derive(Debug, Clone, Default)]
pub struct FormatJsInitializerClause;
pub struct FormatJsInitializerClause {
assignment_layout: Option<AssignmentLikeLayout>,
}

#[derive(Default, Debug)]
pub struct FormatJsInitializerClauseOptions {
pub(crate) assignment_layout: Option<AssignmentLikeLayout>,
}

impl FormatRuleWithOptions<JsInitializerClause> for FormatJsInitializerClause {
type Options = FormatJsInitializerClauseOptions;

fn with_options(mut self, options: Self::Options) -> Self {
self.assignment_layout = options.assignment_layout;
self
}
}

impl FormatNodeRule<JsInitializerClause> for FormatJsInitializerClause {
fn fmt_fields(&self, node: &JsInitializerClause, f: &mut JsFormatter) -> FormatResult<()> {
Expand All @@ -14,6 +31,13 @@ impl FormatNodeRule<JsInitializerClause> for FormatJsInitializerClause {
expression,
} = node.as_fields();

write![f, [eq_token.format(), space(), expression.format()]]
write![
f,
[
eq_token.format(),
space(),
with_assignment_layout(&expression?, self.assignment_layout)
]
]
}
}
Loading