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

refactor(formatter): Introduce write, format, and format_args macros #2634

Merged
merged 38 commits into from
Jun 6, 2022
Merged
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
5a3e8df
refactor(formatter): `format` and `write` macros
MichaReiser May 30, 2022
1efbafb
refactor(formatter): Make Format `Options` a type parameter
MichaReiser May 30, 2022
9fe5507
refactor(formatter): Rewrite formatters
MichaReiser May 31, 2022
b98d4c2
refactor(formatter): Clippy
MichaReiser May 31, 2022
278f082
refactor(formatter): Fix memory layout issue
MichaReiser May 31, 2022
4c555f4
refactor(formatter): Fix formatting issues
MichaReiser May 31, 2022
994f5ae
Document `rome_formatter`
MichaReiser May 31, 2022
85c7c8c
Document macros, store owned content in builders, implement Debug
MichaReiser Jun 1, 2022
f1fa424
Update codegen to use `JsFormatContext`
MichaReiser Jun 1, 2022
f7c7e50
Formatting
MichaReiser Jun 1, 2022
1f4401d
Move JS builders to `builders.rs` (except separated)
MichaReiser Jun 1, 2022
acd1266
Move `format_separated` to its own file
MichaReiser Jun 1, 2022
8d4fddb
Add support for best fitting
MichaReiser Jun 1, 2022
6e0e0b4
Refine builders
MichaReiser Jun 1, 2022
68e31fe
Update formatter codegen
MichaReiser Jun 1, 2022
85e950d
Fix unknown assigment formatting
MichaReiser Jun 1, 2022
a3364c9
Apply format leading trivia optimisation
MichaReiser Jun 1, 2022
aa9c728
Merge branch 'main' into refactor/format-and-write-macros
MichaReiser Jun 2, 2022
aef0ef5
Clippy
MichaReiser Jun 2, 2022
2ac2775
Remove unnecessary clone
MichaReiser Jun 2, 2022
8ac52ad
Fix out of order printing for union/intersection type
MichaReiser Jun 2, 2022
1edde7b
Address first batch of review comments
MichaReiser Jun 2, 2022
5ee400b
Add debuging macro
MichaReiser Jun 2, 2022
9db75c2
Use alternative printing in debug
MichaReiser Jun 2, 2022
076b2ae
Small cleanups
MichaReiser Jun 3, 2022
988a8d3
Code review comments
MichaReiser Jun 3, 2022
4ea0982
Added a test that verifies that `format_once` panics if formatted twice
MichaReiser Jun 3, 2022
92d27bd
Some more options to context renaming
MichaReiser Jun 3, 2022
486a08c
Update documentation
MichaReiser Jun 3, 2022
f4185ab
Merge branch 'main' into refactor/format-and-write-macros
MichaReiser Jun 3, 2022
ae2c61c
Change builders to accept `&dyn Format` to improve compile time
Jun 4, 2022
6007d32
Rename `format_fields` to `fmt_fields`
Jun 4, 2022
2570732
Revert submodule changes
Jun 4, 2022
c6183b3
More documentation & use list formatting logic rather than re-impleme…
MichaReiser Jun 4, 2022
1196545
Simplify setter formatting
MichaReiser Jun 4, 2022
e45f7b4
refactor(formatter): Use argument instead of `dyn Format`
MichaReiser Jun 6, 2022
de2c274
Merge branch 'main' into refactor/format-and-write-macros
MichaReiser Jun 6, 2022
00e8740
Clippy
MichaReiser Jun 6, 2022
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
Prev Previous commit
Next Next commit
Some more options to context renaming
MichaReiser committed Jun 3, 2022
commit 92d27bd5574bb49b15b2680c9b1ef769cc6819ce
40 changes: 20 additions & 20 deletions crates/rome_formatter/src/builders.rs
Original file line number Diff line number Diff line change
@@ -54,12 +54,12 @@ impl<Context> Format<Context> for Empty {
/// use rome_formatter::{format, format_args, LineWidth};
/// use rome_formatter::prelude::*;
///
/// let options = SimpleFormatContext {
/// let context = SimpleFormatContext {
/// line_width: LineWidth::try_from(10).unwrap(),
/// ..SimpleFormatContext::default()
/// };
///
/// let elements = format!(options, [
/// let elements = format!(context, [
/// group_elements(format_args![
/// token("a long word,"),
/// soft_line_break(),
@@ -163,12 +163,12 @@ pub const fn empty_line() -> Line {
/// use rome_formatter::{format_args, format, LineWidth};
/// use rome_formatter::prelude::*;
///
/// let options = SimpleFormatContext {
/// let context = SimpleFormatContext {
/// line_width: LineWidth::try_from(10).unwrap(),
/// ..SimpleFormatContext::default()
/// };
///
/// let elements = format!(options, [
/// let elements = format!(context, [
/// group_elements(format_args![
/// token("a long word,"),
/// soft_line_break_or_space(),
@@ -680,12 +680,12 @@ pub const fn block_indent<Content>(content: Content) -> BlockIndent<Content> {
/// use rome_formatter::{format, format_args, LineWidth};
/// use rome_formatter::prelude::*;
///
/// let options = SimpleFormatContext {
/// let context = SimpleFormatContext {
/// line_width: LineWidth::try_from(10).unwrap(),
/// ..SimpleFormatContext::default()
/// };
///
/// let elements = format!(options, [
/// let elements = format!(context, [
/// group_elements(format_args![
/// token("["),
/// soft_block_indent(format_args![
@@ -748,12 +748,12 @@ pub const fn soft_block_indent<Content>(content: Content) -> BlockIndent<Content
/// use rome_formatter::{format, format_args, LineWidth};
/// use rome_formatter::prelude::*;
///
/// let options = SimpleFormatContext {
/// let context = SimpleFormatContext {
/// line_width: LineWidth::try_from(10).unwrap(),
/// ..SimpleFormatContext::default()
/// };
///
/// let elements = format!(options, [
/// let elements = format!(context, [
/// group_elements(format_args![
/// token("name"),
/// space_token(),
@@ -905,12 +905,12 @@ where
/// use rome_formatter::{format, format_args, LineWidth};
/// use rome_formatter::prelude::*;
///
/// let options = SimpleFormatContext {
/// let context = SimpleFormatContext {
/// line_width: LineWidth::try_from(20).unwrap(),
/// ..SimpleFormatContext::default()
/// };
///
/// let elements = format!(options, [
/// let elements = format!(context, [
/// group_elements(format_args![
/// token("["),
/// soft_block_indent(format_args![
@@ -1092,12 +1092,12 @@ impl<Context> Format<Context> for ExpandParent {
/// use rome_formatter::{format_args, format, LineWidth};
/// use rome_formatter::prelude::*;
///
/// let options = SimpleFormatContext {
/// let context = SimpleFormatContext {
/// line_width: LineWidth::try_from(20).unwrap(),
/// ..SimpleFormatContext::default()
/// };
///
/// let elements = format!(options, [
/// let elements = format!(context, [
/// group_elements(format_args![
/// token("["),
/// soft_block_indent(format_args![
@@ -1168,12 +1168,12 @@ pub const fn if_group_breaks<Content>(content: Content) -> IfGroupBreaks<Content
/// use rome_formatter::{format, format_args, LineWidth};
/// use rome_formatter::prelude::*;
///
/// let options = SimpleFormatContext {
/// let context = SimpleFormatContext {
/// line_width: LineWidth::try_from(20).unwrap(),
/// ..SimpleFormatContext::default()
/// };
///
/// let formatted = format!(options, [
/// let formatted = format!(context, [
/// group_elements(format_args![
/// token("["),
/// soft_block_indent(format_args![
@@ -1224,12 +1224,12 @@ impl<Content> IfGroupBreaks<Content> {
/// use rome_formatter::{format, format_args, write, LineWidth};
/// use rome_formatter::prelude::*;
///
/// let options = SimpleFormatContext {
/// let context = SimpleFormatContext {
/// line_width: LineWidth::try_from(20).unwrap(),
/// ..SimpleFormatContext::default()
/// };
///
/// let formatted = format!(options, [format_with(|f| {
/// let formatted = format!(context, [format_with(|f| {
/// let group_id = f.group_id("array");
///
/// write!(f, [
@@ -1311,7 +1311,7 @@ where
#[derive(Copy, Clone)]
pub struct FormatWith<Context, T> {
formatter: T,
options: PhantomData<Context>,
context: PhantomData<Context>,
}

impl<Context, T> Format<Context> for FormatWith<Context, T>
@@ -1370,7 +1370,7 @@ where
{
FormatWith {
formatter,
options: PhantomData,
context: PhantomData,
}
}

@@ -1444,13 +1444,13 @@ where
{
FormatOnce {
formatter: Cell::new(Some(formatter)),
options: PhantomData,
context: PhantomData,
}
}

pub struct FormatOnce<T, Context> {
formatter: Cell<Option<T>>,
options: PhantomData<Context>,
context: PhantomData<Context>,
}

impl<T, Context> Format<Context> for FormatOnce<T, Context>
2 changes: 1 addition & 1 deletion crates/rome_formatter/src/formatter.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ use crate::prelude::*;
use crate::printed_tokens::PrintedTokens;
use crate::{Arguments, Buffer, FormatState, GroupId};

/// Handles the formatting of a CST and stores the options how the CST should be formatted (user preferences).
/// Handles the formatting of a CST and stores the context how the CST should be formatted (user preferences).
/// The formatter is passed to the [Format] implementation of every node in the CST so that they
/// can use it to format their children.
pub struct Formatter<'buf, Context> {
16 changes: 8 additions & 8 deletions crates/rome_formatter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -651,13 +651,13 @@ pub fn write<Context>(
/// let formatted = format!(SimpleFormatContext::default(), [token("test")]).unwrap();
/// assert_eq!("test", formatted.print().as_code());
/// ```
pub fn format<Context>(options: Context, arguments: Arguments<Context>) -> FormatResult<Formatted>
pub fn format<Context>(context: Context, arguments: Arguments<Context>) -> FormatResult<Formatted>
where
Context: FormatContext,
{
let print_options = options.as_print_options();
let mut context = FormatState::new(options);
let mut buffer = VecBuffer::with_capacity(arguments.items().len(), &mut context);
let print_options = context.as_print_options();
let mut state = FormatState::new(context);
let mut buffer = VecBuffer::with_capacity(arguments.items().len(), &mut state);

buffer.write_fmt(arguments)?;

@@ -749,7 +749,7 @@ where
/// It returns a [Formatted] result with a range corresponding to the
/// range of the input that was effectively overwritten by the formatter
pub fn format_range<Context, L, R, P>(
options: Context,
context: Context,
root: &SyntaxNode<L>,
mut range: TextRange,
mut predicate: P,
@@ -899,7 +899,7 @@ where

// Perform the actual formatting of the root node with
// an appropriate indentation level
let formatted = format_sub_tree(options, &FormatRefWithRule::<_, R>::new(common_root))?;
let formatted = format_sub_tree(context, &FormatRefWithRule::<_, R>::new(common_root))?;

// This finds the closest marker to the beginning of the source
// starting before or at said starting point, and the closest
@@ -1082,8 +1082,8 @@ where
Context: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("FormatContext")
.field("options", &self.context)
f.debug_struct("FormatState")
.field("context", &self.context)
.finish()
}
}
2 changes: 1 addition & 1 deletion crates/rome_js_formatter/src/js/statements/if_statement.rs
Original file line number Diff line number Diff line change
@@ -57,7 +57,7 @@ fn write_if_element(
[
if_token.format(),
space_token(),
format_delimited(&l_paren_token?, test.format(), &r_paren_token?,).soft_block_indent(),
format_delimited(&l_paren_token?, test.format(), &r_paren_token?).soft_block_indent(),
]
]?;