Skip to content

Commit

Permalink
Add space_in_brackets option
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Nov 20, 2024
1 parent c0cccb2 commit 2d269c0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
7 changes: 7 additions & 0 deletions crates/biome_configuration/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use biome_deserialize::StringSet;
use biome_deserialize_macros::{Deserializable, Merge, Partial};
use biome_formatter::{
AttributePosition, BracketSpacing, IndentStyle, IndentWidth, LineEnding, LineWidth,
SpaceInBrackets,
};
use bpaf::Bpaf;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -55,6 +56,10 @@ pub struct FormatterConfiguration {
#[partial(bpaf(long("bracket-spacing"), argument("true|false"), optional))]
pub bracket_spacing: BracketSpacing,

/// @todo
#[partial(bpaf(long("bracket-spacing"), argument("true|false"), optional))]
pub space_in_brackets: SpaceInBrackets,

/// A list of Unix shell style patterns. The formatter will ignore files/folders that will
/// match these patterns.
#[partial(bpaf(hide))]
Expand Down Expand Up @@ -82,6 +87,7 @@ impl PartialFormatterConfiguration {
line_width: self.line_width.unwrap_or_default(),
attribute_position: self.attribute_position.unwrap_or_default(),
bracket_spacing: self.bracket_spacing.unwrap_or_default(),
space_in_brackets: self.space_in_brackets.unwrap_or_default(),
ignore: self.ignore.clone().unwrap_or_default(),
include: self.include.clone().unwrap_or_default(),
use_editorconfig: self.use_editorconfig.unwrap_or_default(),
Expand All @@ -101,6 +107,7 @@ impl Default for FormatterConfiguration {
line_width: LineWidth::default(),
attribute_position: AttributePosition::default(),
bracket_spacing: Default::default(),
space_in_brackets: Default::default(),
ignore: Default::default(),
include: Default::default(),
// TODO: Biome 2.0: change to true
Expand Down
7 changes: 6 additions & 1 deletion crates/biome_formatter/src/format_element/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use super::tag::Tag;
use crate::format_element::tag::DedentMode;
use crate::prelude::tag::GroupMode;
use crate::prelude::*;
use crate::{format, write, AttributePosition, BracketSpacing};
use crate::{prelude::*, SpaceInBrackets};
use crate::{
BufferExtensions, Format, FormatContext, FormatElement, FormatOptions, FormatResult, Formatter,
IndentStyle, IndentWidth, LineEnding, LineWidth, PrinterOptions, TransformSourceMap,
Expand Down Expand Up @@ -208,6 +208,10 @@ impl FormatOptions for IrFormatOptions {
BracketSpacing::default()
}

fn space_in_brackets(&self) -> SpaceInBrackets {
SpaceInBrackets::default()
}

fn as_print_options(&self) -> PrinterOptions {
PrinterOptions {
indent_width: self.indent_width(),
Expand All @@ -216,6 +220,7 @@ impl FormatOptions for IrFormatOptions {
indent_style: IndentStyle::Space,
attribute_position: self.attribute_position(),
bracket_spacing: self.bracket_spacing(),
space_in_brackets: self.space_in_brackets(),
}
}
}
Expand Down
56 changes: 56 additions & 0 deletions crates/biome_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,54 @@ impl FromStr for BracketSpacing {
}
}

#[derive(Clone, Copy, Debug, Deserializable, Eq, Hash, Merge, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema),
serde(rename_all = "camelCase")
)]
pub struct SpaceInBrackets(bool);

impl SpaceInBrackets {
/// Return the boolean value for this [SpaceInBrackets]
pub fn value(&self) -> bool {
self.0
}
}

impl Default for SpaceInBrackets {
fn default() -> Self {
Self(true)
}
}

impl From<bool> for SpaceInBrackets {
fn from(value: bool) -> Self {
Self(value)
}
}

impl std::fmt::Display for SpaceInBrackets {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::write!(f, "{}", self.value())
}
}

impl FromStr for SpaceInBrackets {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let value = bool::from_str(s);

match value {
Ok(value) => Ok(Self(value)),
Err(_) => Err(
"Value not supported for SpaceInBrackets. Supported values are 'true' and 'false'.",
),
}
}
}

#[derive(Clone, Copy, Debug, Default, Deserializable, Eq, Hash, Merge, PartialEq)]
#[cfg_attr(
feature = "serde",
Expand Down Expand Up @@ -668,6 +716,9 @@ pub trait FormatOptions {
/// Whether to insert spaces around brackets in object literals. Defaults to true.
fn bracket_spacing(&self) -> BracketSpacing;

/// @todo
fn space_in_brackets(&self) -> SpaceInBrackets;

/// Derives the print options from the these format options
fn as_print_options(&self) -> PrinterOptions;
}
Expand Down Expand Up @@ -718,6 +769,7 @@ pub struct SimpleFormatOptions {
pub line_ending: LineEnding,
pub attribute_position: AttributePosition,
pub bracket_spacing: BracketSpacing,
pub space_in_brackets: SpaceInBrackets,
}

impl FormatOptions for SimpleFormatOptions {
Expand Down Expand Up @@ -745,6 +797,10 @@ impl FormatOptions for SimpleFormatOptions {
self.bracket_spacing
}

fn space_in_brackets(&self) -> SpaceInBrackets {
self.space_in_brackets
}

fn as_print_options(&self) -> PrinterOptions {
PrinterOptions::default()
.with_indent_style(self.indent_style)
Expand Down
12 changes: 11 additions & 1 deletion crates/biome_formatter/src/printer/printer_options/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
AttributePosition, BracketSpacing, FormatOptions, IndentStyle, IndentWidth, LineEnding,
LineWidth,
LineWidth, SpaceInBrackets,
};

/// Options that affect how the [crate::Printer] prints the format tokens
Expand All @@ -24,6 +24,9 @@ pub struct PrinterOptions {

/// Whether to insert spaces around brackets in object literals. Defaults to true.
pub bracket_spacing: BracketSpacing,

/// @todo
pub space_in_brackets: SpaceInBrackets,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -103,6 +106,12 @@ impl PrinterOptions {
self
}

pub fn with_space_in_brackets(mut self, space_in_brackets: SpaceInBrackets) -> Self {
self.space_in_brackets = space_in_brackets;

self
}

pub(crate) fn indent_style(&self) -> IndentStyle {
self.indent_style
}
Expand Down Expand Up @@ -137,6 +146,7 @@ impl Default for PrinterOptions {
line_ending: LineEnding::Lf,
attribute_position: AttributePosition::default(),
bracket_spacing: BracketSpacing::default(),
space_in_brackets: SpaceInBrackets::default(),
}
}
}

0 comments on commit 2d269c0

Please sign in to comment.