Skip to content

Commit 4976d1b

Browse files
refactor(formatter): add printer option to disable source-map generation (#8284)
Co-authored-by: Emanuele Stoppa <[email protected]>
1 parent 3710702 commit 4976d1b

File tree

11 files changed

+78
-13
lines changed

11 files changed

+78
-13
lines changed

.changeset/tough-buckets-write.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Improved the performance of the Biome Formatter by enabling the internal source maps only when needed.

crates/biome_css_semantic/src/format_semantic_model.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use biome_formatter::prelude::*;
44
use biome_formatter::write;
55
use biome_formatter::{
66
FormatContext, FormatOptions, IndentStyle, IndentWidth, LineEnding, LineWidth,
7-
TransformSourceMap,
7+
SourceMapGeneration, TransformSourceMap,
88
};
99
use biome_rowan::{AstNode, TextSize};
1010

@@ -34,6 +34,7 @@ impl FormatOptions for FormatSemanticModelOptions {
3434
print_width: self.line_width().into(),
3535
line_ending: self.line_ending(),
3636
indent_style: self.indent_style(),
37+
source_map_generation: SourceMapGeneration::default(),
3738
}
3839
}
3940
}

crates/biome_formatter/src/format_element/document.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use crate::prelude::tag::GroupMode;
66
use crate::prelude::*;
77
use crate::{
88
BufferExtensions, Format, FormatContext, FormatElement, FormatOptions, FormatResult, Formatter,
9-
IndentStyle, IndentWidth, LineEnding, LineWidth, PrinterOptions, TransformSourceMap,
9+
IndentStyle, IndentWidth, LineEnding, LineWidth, PrinterOptions, SourceMapGeneration,
10+
TransformSourceMap,
1011
};
1112
use crate::{format, write};
1213
use biome_rowan::TextSize;
@@ -266,6 +267,7 @@ impl FormatOptions for IrFormatOptions {
266267
print_width: self.line_width().into(),
267268
line_ending: LineEnding::Lf,
268269
indent_style: IndentStyle::Space,
270+
source_map_generation: SourceMapGeneration::default(),
269271
}
270272
}
271273
}

crates/biome_formatter/src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ pub use buffer::{
7474
pub use builders::BestFitting;
7575
pub use format_element::{FormatElement, LINE_TERMINATORS, normalize_newlines};
7676
pub use group_id::GroupId;
77+
pub use printer::SourceMapGeneration;
7778
pub use source_map::{TransformSourceMap, TransformSourceMapBuilder};
7879
use std::num::ParseIntError;
7980
use std::str::FromStr;
@@ -1021,8 +1022,16 @@ where
10211022
Ok(printed)
10221023
}
10231024

1024-
pub fn print_with_indent(&self, indent: u16) -> PrintResult<Printed> {
1025-
let print_options = self.context.options().as_print_options();
1025+
pub fn print_with_indent(
1026+
&self,
1027+
indent: u16,
1028+
source_map: SourceMapGeneration,
1029+
) -> PrintResult<Printed> {
1030+
let print_options = self
1031+
.context
1032+
.options()
1033+
.as_print_options()
1034+
.with_source_map_generation(source_map);
10261035
let printed = Printer::new(print_options).print_with_indent(&self.document, indent)?;
10271036

10281037
let printed = match self.context.source_map() {
@@ -2045,7 +2054,7 @@ pub fn format_sub_tree<L: FormatLanguage>(
20452054
};
20462055

20472056
let formatted = format_node(root, language, false)?;
2048-
let mut printed = formatted.print_with_indent(initial_indent)?;
2057+
let mut printed = formatted.print_with_indent(initial_indent, SourceMapGeneration::Enabled)?;
20492058
let sourcemap = printed.take_sourcemap();
20502059
let verbatim_ranges = printed.take_verbatim_ranges();
20512060

crates/biome_formatter/src/printer/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ impl<'a> Printer<'a> {
394394
}
395395

396396
fn push_marker(&mut self, marker: SourceMarker) {
397+
if self.options.source_map_generation().is_disabled() {
398+
return;
399+
}
400+
397401
if let Some(last) = self.state.source_markers.last() {
398402
if last != &marker {
399403
self.state.source_markers.push(marker)

crates/biome_formatter/src/printer/printer_options/mod.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ pub struct PrinterOptions {
1515

1616
/// Whether the printer should use tabs or spaces to indent code and if spaces, by how many.
1717
pub indent_style: IndentStyle,
18+
19+
/// Should the printer generate a source map that allows mapping positions in the source document
20+
/// to positions in the formatted document.
21+
pub source_map_generation: SourceMapGeneration,
1822
}
1923

2024
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
@@ -81,10 +85,20 @@ impl PrinterOptions {
8185
self
8286
}
8387

88+
pub fn with_source_map_generation(mut self, source_map: SourceMapGeneration) -> Self {
89+
self.source_map_generation = source_map;
90+
91+
self
92+
}
93+
8494
pub(crate) fn indent_style(&self) -> IndentStyle {
8595
self.indent_style
8696
}
8797

98+
pub const fn source_map_generation(&self) -> SourceMapGeneration {
99+
self.source_map_generation
100+
}
101+
88102
/// Width of an indent in characters.
89103
pub(super) const fn indent_width(&self) -> IndentWidth {
90104
self.indent_width
@@ -103,6 +117,32 @@ impl Default for PrinterOptions {
103117
print_width: PrintWidth::default(),
104118
indent_style: Default::default(),
105119
line_ending: LineEnding::Lf,
120+
source_map_generation: SourceMapGeneration::default(),
106121
}
107122
}
108123
}
124+
125+
/// Configures whether the printer generates a source map that allows mapping
126+
/// positions in the source document to positions in the formatted code.
127+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
128+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
129+
pub enum SourceMapGeneration {
130+
/// The printer generates no source map.
131+
#[default]
132+
Disabled,
133+
134+
/// The printer generates a source map that allows mapping positions in the source document
135+
/// to positions in the formatted document. The ability to map positions is useful for range formatting
136+
/// or when trying to identify where to move the cursor so that it matches its position in the source document.
137+
Enabled,
138+
}
139+
140+
impl SourceMapGeneration {
141+
pub const fn is_enabled(self) -> bool {
142+
matches!(self, Self::Enabled)
143+
}
144+
145+
pub const fn is_disabled(self) -> bool {
146+
matches!(self, Self::Disabled)
147+
}
148+
}

crates/biome_js_formatter/src/syntax_rewriter.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ fn has_type_cast_comment_or_skipped(trivia: &JsSyntaxTrivia) -> bool {
465465
mod tests {
466466
use super::JsFormatSyntaxRewriter;
467467
use crate::{JsFormatOptions, TextRange, format_node};
468-
use biome_formatter::{SourceMarker, TransformSourceMap};
468+
use biome_formatter::{SourceMapGeneration, SourceMarker, TransformSourceMap};
469469
use biome_js_parser::{JsParserOptions, parse, parse_module};
470470
use biome_js_syntax::{
471471
JsArrayExpression, JsBinaryExpression, JsExpressionStatement, JsFileSource,
@@ -847,7 +847,9 @@ mod tests {
847847

848848
let formatted =
849849
format_node(JsFormatOptions::new(JsFileSource::default()), &transformed).unwrap();
850-
let printed = formatted.print().unwrap();
850+
let printed = formatted
851+
.print_with_indent(0, SourceMapGeneration::Enabled)
852+
.unwrap();
851853

852854
assert_eq!(printed.as_code(), "(a * b * c) / 3;\n");
853855

crates/biome_js_semantic/src/format_semantic_model.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use biome_formatter::prelude::*;
22
use biome_formatter::{
33
FormatContext, FormatOptions, IndentStyle, IndentWidth, LineEnding, LineWidth,
4-
TransformSourceMap,
4+
SourceMapGeneration, TransformSourceMap,
55
};
66
use biome_formatter::{format_args, write};
77
use biome_js_syntax::TextSize;
@@ -33,6 +33,7 @@ impl FormatOptions for FormatSemanticModelOptions {
3333
print_width: self.line_width().into(),
3434
line_ending: self.line_ending(),
3535
indent_style: self.indent_style(),
36+
source_map_generation: SourceMapGeneration::default(),
3637
}
3738
}
3839
}

crates/biome_js_type_info/src/format_type_info.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
use biome_formatter::prelude::*;
1111
use biome_formatter::{
1212
FormatContext, FormatOptions, IndentStyle, IndentWidth, LineEnding, LineWidth,
13-
TransformSourceMap,
13+
SourceMapGeneration, TransformSourceMap,
1414
};
1515
use biome_formatter::{format_args, write};
1616
use biome_js_syntax::TextSize;
@@ -44,6 +44,7 @@ impl FormatOptions for FormatTypeOptions {
4444
print_width: self.line_width().into(),
4545
line_ending: self.line_ending(),
4646
indent_style: self.indent_style(),
47+
source_map_generation: SourceMapGeneration::default(),
4748
}
4849
}
4950
}

crates/biome_service/src/file_handlers/svelte.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::file_handlers::{
77
};
88
use crate::settings::Settings;
99
use crate::workspace::{DocumentFileSource, FixFileResult, PullActionsResult};
10-
use biome_formatter::Printed;
10+
use biome_formatter::{Printed, SourceMapGeneration};
1111
use biome_fs::BiomePath;
1212
use biome_html_syntax::HtmlLanguage;
1313
use biome_js_formatter::format_node;
@@ -157,7 +157,7 @@ fn format(
157157
};
158158
let tree = parse.syntax();
159159
let formatted = format_node(options, &tree)?;
160-
match formatted.print_with_indent(indent_amount) {
160+
match formatted.print_with_indent(indent_amount, SourceMapGeneration::Disabled) {
161161
Ok(printed) => Ok(printed),
162162
Err(error) => {
163163
error!("The file {} couldn't be formatted", biome_path.as_str());

0 commit comments

Comments
 (0)