mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-23 13:05:06 +00:00
Normalize newlines in verbatim_text
(#4850)
This commit is contained in:
parent
f9e82f2578
commit
913b9d1fcf
1 changed files with 23 additions and 5 deletions
|
@ -3,12 +3,15 @@ use ruff_text_size::TextRange;
|
|||
use rustpython_parser::ast::Mod;
|
||||
use rustpython_parser::lexer::lex;
|
||||
use rustpython_parser::{parse_tokens, Mode};
|
||||
use std::borrow::Cow;
|
||||
|
||||
use ruff_formatter::format_element::tag::VerbatimKind;
|
||||
use ruff_formatter::formatter::Formatter;
|
||||
use ruff_formatter::prelude::{source_position, source_text_slice, ContainsNewlines, Tag};
|
||||
use ruff_formatter::prelude::{
|
||||
dynamic_text, source_position, source_text_slice, ContainsNewlines, Tag,
|
||||
};
|
||||
use ruff_formatter::{
|
||||
format, write, Buffer, Format, FormatContext, FormatElement, FormatResult, Formatted,
|
||||
format, normalize_newlines, write, Buffer, Format, FormatElement, FormatResult, Formatted,
|
||||
IndentStyle, Printed, SimpleFormatOptions, SourceCode,
|
||||
};
|
||||
use ruff_python_ast::node::AstNode;
|
||||
|
@ -145,14 +148,29 @@ pub(crate) const fn verbatim_text(range: TextRange) -> VerbatimText {
|
|||
VerbatimText(range)
|
||||
}
|
||||
|
||||
impl<C: FormatContext> Format<C> for VerbatimText {
|
||||
fn fmt(&self, f: &mut Formatter<C>) -> FormatResult<()> {
|
||||
impl Format<PyFormatContext<'_>> for VerbatimText {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
f.write_element(FormatElement::Tag(Tag::StartVerbatim(
|
||||
VerbatimKind::Verbatim {
|
||||
length: self.0.len(),
|
||||
},
|
||||
)))?;
|
||||
write!(f, [source_text_slice(self.0, ContainsNewlines::Detect)])?;
|
||||
|
||||
match normalize_newlines(f.context().locator().slice(self.0), ['\r']) {
|
||||
Cow::Borrowed(_) => {
|
||||
write!(f, [source_text_slice(self.0, ContainsNewlines::Detect)])?;
|
||||
}
|
||||
Cow::Owned(cleaned) => {
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
dynamic_text(&cleaned, Some(self.0.start())),
|
||||
source_position(self.0.end())
|
||||
]
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
f.write_element(FormatElement::Tag(Tag::EndVerbatim))?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue