Optional source map generation (#6894)

This commit is contained in:
Micha Reiser 2023-08-26 18:00:43 +02:00 committed by GitHub
parent 15b73bdb8a
commit eae59cf088
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 117 additions and 73 deletions

View file

@ -1,4 +1,4 @@
use ruff_formatter::printer::{LineEnding, PrinterOptions};
use ruff_formatter::printer::{LineEnding, PrinterOptions, SourceMapGeneration};
use ruff_formatter::{FormatOptions, IndentStyle, LineWidth, TabWidth};
use ruff_python_ast::PySourceType;
use std::path::Path;
@ -33,6 +33,10 @@ pub struct PyFormatOptions {
/// Whether to expand lists or elements if they have a trailing comma such as `(a, b,)`.
magic_trailing_comma: MagicTrailingComma,
/// Should the formatter generate a source map that allows mapping source positions to positions
/// in the formatted document.
source_map_generation: SourceMapGeneration,
}
fn default_line_width() -> LineWidth {
@ -56,6 +60,7 @@ impl Default for PyFormatOptions {
tab_width: default_tab_width(),
quote_style: QuoteStyle::default(),
magic_trailing_comma: MagicTrailingComma::default(),
source_map_generation: SourceMapGeneration::default(),
}
}
}
@ -85,6 +90,10 @@ impl PyFormatOptions {
self.source_type
}
pub fn source_map_generation(&self) -> SourceMapGeneration {
self.source_map_generation
}
#[must_use]
pub fn with_quote_style(mut self, style: QuoteStyle) -> Self {
self.quote_style = style;
@ -129,6 +138,7 @@ impl FormatOptions for PyFormatOptions {
print_width: self.line_width.into(),
line_ending: LineEnding::LineFeed,
indent_style: self.indent_style,
source_map_generation: self.source_map_generation,
}
}
}