mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +00:00
Playground: Fix escaped quotes handling (#5906)
Co-authored-by: konsti <konstin@mailbox.org>
This commit is contained in:
parent
9e32585cb1
commit
029fe05a5f
2 changed files with 44 additions and 1 deletions
|
@ -255,11 +255,35 @@ impl Format<IrFormatContext<'_>> for &[FormatElement] {
|
||||||
|
|
||||||
in_text = true;
|
in_text = true;
|
||||||
|
|
||||||
|
fn write_escaped(
|
||||||
|
element: &FormatElement,
|
||||||
|
f: &mut Formatter<IrFormatContext>,
|
||||||
|
) -> FormatResult<()> {
|
||||||
|
let text = match element {
|
||||||
|
FormatElement::StaticText { text } => text,
|
||||||
|
FormatElement::DynamicText { text } => text.as_ref(),
|
||||||
|
FormatElement::SourceCodeSlice { slice, .. } => {
|
||||||
|
slice.text(f.context().source_code())
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if text.contains('"') {
|
||||||
|
f.write_element(FormatElement::DynamicText {
|
||||||
|
text: text.replace('"', r#"\""#).into(),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
f.write_element(element.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
match element {
|
match element {
|
||||||
FormatElement::Space => {
|
FormatElement::Space => {
|
||||||
write!(f, [text(" ")])?;
|
write!(f, [text(" ")])?;
|
||||||
}
|
}
|
||||||
element if element.is_text() => f.write_element(element.clone())?,
|
element if element.is_text() => {
|
||||||
|
write_escaped(element, f)?;
|
||||||
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -787,6 +811,22 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn escapes_quotes() {
|
||||||
|
let formatted = format!(
|
||||||
|
SimpleFormatContext::default(),
|
||||||
|
[text(r#""""Python docstring""""#)]
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let document = formatted.into_document();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
&std::format!("{}", document.display(SourceCode::default())),
|
||||||
|
r#"["\"\"\"Python docstring\"\"\""]"#
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn display_elements_with_source_text_slice() {
|
fn display_elements_with_source_text_slice() {
|
||||||
let source_code = "Some longer content\nThat should ultimately break";
|
let source_code = "Some longer content\nThat should ultimately break";
|
||||||
|
|
|
@ -596,6 +596,7 @@ function defineRustPythonAstLanguage(monaco: Monaco) {
|
||||||
],
|
],
|
||||||
string: [
|
string: [
|
||||||
[/[^\\"]+/, "string"],
|
[/[^\\"]+/, "string"],
|
||||||
|
[/\\[\\"]/, "string.escape"],
|
||||||
[/"/, { token: "string.quote", bracket: "@close", next: "@pop" }],
|
[/"/, { token: "string.quote", bracket: "@close", next: "@pop" }],
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
@ -650,6 +651,7 @@ function defineRustPythonTokensLanguage(monaco: Monaco) {
|
||||||
],
|
],
|
||||||
string: [
|
string: [
|
||||||
[/[^\\"]+/, "string"],
|
[/[^\\"]+/, "string"],
|
||||||
|
[/\\[\\"]/, "string.escape"],
|
||||||
[/"/, { token: "string.quote", bracket: "@close", next: "@pop" }],
|
[/"/, { token: "string.quote", bracket: "@close", next: "@pop" }],
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
@ -695,6 +697,7 @@ function defineFirLanguage(monaco: Monaco) {
|
||||||
],
|
],
|
||||||
string: [
|
string: [
|
||||||
[/[^\\"]+/, "string"],
|
[/[^\\"]+/, "string"],
|
||||||
|
[/\\[\\"]/, "string.escape"],
|
||||||
[/"/, { token: "string.quote", bracket: "@close", next: "@pop" }],
|
[/"/, { token: "string.quote", bracket: "@close", next: "@pop" }],
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue