mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-12 06:38:21 +00:00
Formatter: Detect line endings (#7054)
This commit is contained in:
parent
834566f34f
commit
93ca8ebbc0
6 changed files with 32 additions and 8 deletions
|
@ -1,4 +1,5 @@
|
|||
use thiserror::Error;
|
||||
use tracing::Level;
|
||||
|
||||
use ruff_formatter::prelude::*;
|
||||
use ruff_formatter::{format, FormatError, Formatted, PrintError, Printed, SourceCode};
|
||||
|
@ -119,6 +120,7 @@ impl From<ParseError> for FormatModuleError {
|
|||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(level=Level::TRACE, skip_all, err)]
|
||||
pub fn format_module(
|
||||
contents: &str,
|
||||
options: PyFormatOptions,
|
||||
|
|
|
@ -28,6 +28,8 @@ pub struct PyFormatOptions {
|
|||
#[cfg_attr(feature = "serde", serde(default = "default_tab_width"))]
|
||||
tab_width: TabWidth,
|
||||
|
||||
line_ending: LineEnding,
|
||||
|
||||
/// The preferred quote style to use (single vs double quotes).
|
||||
quote_style: QuoteStyle,
|
||||
|
||||
|
@ -59,6 +61,7 @@ impl Default for PyFormatOptions {
|
|||
line_width: default_line_width(),
|
||||
tab_width: default_tab_width(),
|
||||
quote_style: QuoteStyle::default(),
|
||||
line_ending: LineEnding::default(),
|
||||
magic_trailing_comma: MagicTrailingComma::default(),
|
||||
source_map_generation: SourceMapGeneration::default(),
|
||||
}
|
||||
|
@ -94,6 +97,10 @@ impl PyFormatOptions {
|
|||
self.source_map_generation
|
||||
}
|
||||
|
||||
pub fn line_ending(&self) -> LineEnding {
|
||||
self.line_ending
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_tab_width(mut self, tab_width: TabWidth) -> Self {
|
||||
self.tab_width = tab_width;
|
||||
|
@ -123,6 +130,12 @@ impl PyFormatOptions {
|
|||
self.line_width = line_width;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_line_ending(mut self, line_ending: LineEnding) -> Self {
|
||||
self.line_ending = line_ending;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl FormatOptions for PyFormatOptions {
|
||||
|
@ -142,7 +155,7 @@ impl FormatOptions for PyFormatOptions {
|
|||
PrinterOptions {
|
||||
tab_width: self.tab_width,
|
||||
line_width: self.line_width,
|
||||
line_ending: LineEnding::LineFeed,
|
||||
line_ending: self.line_ending,
|
||||
indent_style: self.indent_style,
|
||||
source_map_generation: self.source_map_generation,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue