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

feat(rome_js_formatter): Function parameter & return type grouping #2990

Merged
merged 2 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions crates/rome_formatter/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ pub trait BufferExtensions: Buffer + Sized {
///
/// ## Alternatives
///
/// Use `Memoized.inspect(f).will_break()` if you need to know if some content breaks that should
/// Use `Memoized.inspect(f)?.will_break()` if you need to know if some content breaks that should
/// only be written later.
fn inspect_will_break(&mut self) -> WillBreakBuffer<Self::Context> {
WillBreakBuffer::new(self)
Expand Down Expand Up @@ -541,7 +541,7 @@ pub trait BufferExtensions: Buffer + Sized {
///
/// /// ## Alternatives
///
/// Use `Memoized.inspect(f).will_break()` if you need to know if some content breaks that should
/// Use `Memoized.inspect(f)?.has_label(LabelId::of::<SomeLabelId>()` if you need to know if some content breaks that should
/// only be written later.
fn inspect_is_labelled<T: ?Sized + 'static>(&mut self) -> HasLabelBuffer<Self::Context> {
let label_id = LabelId::of::<T>();
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_formatter/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use crate::builders::*;
pub use crate::format_element::*;
pub use crate::format_extensions::{FormatOptional as _, MemoizeFormat};
pub use crate::format_extensions::{FormatOptional as _, MemoizeFormat, Memoized};
pub use crate::formatter::Formatter;
pub use crate::printer::PrinterOptions;
pub use crate::token::{
Expand Down
12 changes: 11 additions & 1 deletion crates/rome_js_formatter/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ pub fn format_delimited<'a, 'content>(
content: Argument::new(content),
close_token,
mode: DelimitedMode::SoftBlockIndent(None),
grouped: true,
}
}

Expand All @@ -329,6 +330,7 @@ pub struct FormatDelimited<'a, 'content> {
content: Argument<'content, JsFormatContext>,
close_token: &'a JsSyntaxToken,
mode: DelimitedMode,
grouped: bool,
}

impl FormatDelimited<'_, '_> {
Expand Down Expand Up @@ -359,6 +361,12 @@ impl FormatDelimited<'_, '_> {
pub fn soft_block_indent_with_group_id(self, group_id: Option<GroupId>) -> Self {
self.with_mode(DelimitedMode::SoftBlockIndent(group_id))
}

/// Prevents the formatter from grouping the content even in soft block or soft block spaces mode.
pub fn ungrouped(mut self) -> Self {
self.grouped = false;
self
}
}

impl Format<JsFormatContext> for FormatDelimited<'_, '_> {
Expand All @@ -368,6 +376,7 @@ impl Format<JsFormatContext> for FormatDelimited<'_, '_> {
close_token,
content,
mode,
grouped,
} = self;

let open_delimiter = format_open_delimiter(open_token);
Expand Down Expand Up @@ -427,8 +436,8 @@ impl Format<JsFormatContext> for FormatDelimited<'_, '_> {
});

match mode {
_ if !grouped => write!(f, [delimited])?,
// Group is useless, the block indent would expand it right anyway
DelimitedMode::BlockIndent => write!(f, [delimited])?,
DelimitedMode::SoftBlockIndent(group_id) | DelimitedMode::SoftBlockSpaces(group_id) => {
match group_id {
None => write!(f, [group(&delimited)])?,
Expand All @@ -437,6 +446,7 @@ impl Format<JsFormatContext> for FormatDelimited<'_, '_> {
}
}
}
DelimitedMode::BlockIndent => write!(f, [delimited])?,
};

write!(f, [format_trailing_trivia(close_token)])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::prelude::*;

use rome_formatter::write;
use rome_js_syntax::JsConstructorParameters;
use rome_js_syntax::JsConstructorParametersFields;

Expand All @@ -15,12 +14,9 @@ impl FormatNodeRule<JsConstructorParameters> for FormatJsConstructorParameters {
r_paren_token,
} = node.as_fields();

write!(
f,
[
format_delimited(&l_paren_token?, &parameters.format(), &r_paren_token?,)
.soft_block_indent()
]
)
format_delimited(&l_paren_token?, &parameters.format(), &r_paren_token?)
.soft_block_indent()
.ungrouped()
.fmt(f)
}
}
12 changes: 4 additions & 8 deletions crates/rome_js_formatter/src/js/bindings/parameters.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::prelude::*;

use rome_formatter::write;
use rome_js_syntax::JsParameters;
use rome_js_syntax::JsParametersFields;

Expand All @@ -15,12 +14,9 @@ impl FormatNodeRule<JsParameters> for FormatJsParameters {
r_paren_token,
} = node.as_fields();

write!(
f,
[
format_delimited(&l_paren_token?, &items.format(), &r_paren_token?,)
.soft_block_indent()
]
)
format_delimited(&l_paren_token?, &items.format(), &r_paren_token?)
.soft_block_indent()
.ungrouped()
.fmt(f)
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
use crate::prelude::*;

use crate::js::classes::method_class_member::FormatJsAnyMethodMember;
use rome_formatter::write;
use rome_js_syntax::JsConstructorClassMember;
use rome_js_syntax::JsConstructorClassMemberFields;

#[derive(Debug, Clone, Default)]
pub struct FormatJsConstructorClassMember;

impl FormatNodeRule<JsConstructorClassMember> for FormatJsConstructorClassMember {
fn fmt_fields(&self, node: &JsConstructorClassMember, f: &mut JsFormatter) -> FormatResult<()> {
let JsConstructorClassMemberFields {
modifiers,
name,
parameters,
body,
} = node.as_fields();

write![
f,
[
modifiers.format(),
space(),
name.format(),
parameters.format(),
node.modifiers().format(),
space(),
body.format()
FormatJsAnyMethodMember::from(node.clone())
]
]
}
Expand Down
Loading