mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-11 22:28:27 +00:00
Optional source map generation (#6894)
This commit is contained in:
parent
15b73bdb8a
commit
eae59cf088
7 changed files with 117 additions and 73 deletions
|
@ -1,11 +1,8 @@
|
|||
use thiserror::Error;
|
||||
|
||||
use ruff_formatter::format_element::tag;
|
||||
use ruff_formatter::prelude::{source_position, text, Formatter, Tag};
|
||||
use ruff_formatter::{
|
||||
format, write, Buffer, Format, FormatElement, FormatError, FormatResult, PrintError,
|
||||
};
|
||||
use ruff_formatter::{Formatted, Printed, SourceCode};
|
||||
use ruff_formatter::prelude::*;
|
||||
use ruff_formatter::{format, FormatError, Formatted, PrintError, Printed, SourceCode};
|
||||
use ruff_python_ast::node::{AnyNodeRef, AstNode};
|
||||
use ruff_python_ast::Mod;
|
||||
use ruff_python_index::{CommentRanges, CommentRangesBuilder};
|
||||
|
@ -54,23 +51,22 @@ where
|
|||
if self.is_suppressed(node_comments.trailing, f.context()) {
|
||||
suppressed_node(node.as_any_node_ref()).fmt(f)
|
||||
} else {
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
leading_comments(node_comments.leading),
|
||||
source_position(node.start())
|
||||
]
|
||||
)?;
|
||||
leading_comments(node_comments.leading).fmt(f)?;
|
||||
|
||||
let is_source_map_enabled = f.options().source_map_generation().is_enabled();
|
||||
|
||||
if is_source_map_enabled {
|
||||
source_position(node.start()).fmt(f)?;
|
||||
}
|
||||
|
||||
self.fmt_fields(node, f)?;
|
||||
self.fmt_dangling_comments(node_comments.dangling, f)?;
|
||||
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
source_position(node.end()),
|
||||
trailing_comments(node_comments.trailing)
|
||||
]
|
||||
)
|
||||
if is_source_map_enabled {
|
||||
source_position(node.end()).fmt(f)?;
|
||||
}
|
||||
|
||||
trailing_comments(node_comments.trailing).fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue