mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-21 20:15:11 +00:00
Use shared leading_quote
implementation in ruff_python_formatter (#3396)
This commit is contained in:
parent
dfe1cad928
commit
ff2c0dd491
21 changed files with 114 additions and 123 deletions
|
@ -3,31 +3,6 @@ use rustpython_parser::ast::Location;
|
|||
use crate::core::locator::Locator;
|
||||
use crate::core::types::Range;
|
||||
|
||||
/// Return the leading quote for a string or byte literal (e.g., `"""`).
|
||||
pub fn leading_quote(content: &str) -> Option<&str> {
|
||||
if let Some(first_line) = content.lines().next() {
|
||||
for pattern in ruff_python_stdlib::str::TRIPLE_QUOTE_PREFIXES
|
||||
.iter()
|
||||
.chain(ruff_python_stdlib::bytes::TRIPLE_QUOTE_PREFIXES)
|
||||
.chain(ruff_python_stdlib::str::SINGLE_QUOTE_PREFIXES)
|
||||
.chain(ruff_python_stdlib::bytes::SINGLE_QUOTE_PREFIXES)
|
||||
{
|
||||
if first_line.starts_with(pattern) {
|
||||
return Some(pattern);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Return the trailing quote string for a string or byte literal (e.g., `"""`).
|
||||
pub fn trailing_quote(content: &str) -> Option<&&str> {
|
||||
ruff_python_stdlib::str::TRIPLE_QUOTE_SUFFIXES
|
||||
.iter()
|
||||
.chain(ruff_python_stdlib::str::SINGLE_QUOTE_SUFFIXES)
|
||||
.find(|&pattern| content.ends_with(pattern))
|
||||
}
|
||||
|
||||
/// Return `true` if the given string is a radix literal (e.g., `0b101`).
|
||||
pub fn is_radix_literal(content: &str) -> bool {
|
||||
content.starts_with("0b")
|
||||
|
@ -155,30 +130,3 @@ pub fn is_elif(orelse: &[rustpython_parser::ast::Stmt], locator: &Locator) -> bo
|
|||
}
|
||||
false
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn test_prefixes() {
|
||||
let prefixes = ruff_python_stdlib::str::TRIPLE_QUOTE_PREFIXES
|
||||
.iter()
|
||||
.chain(ruff_python_stdlib::bytes::TRIPLE_QUOTE_PREFIXES)
|
||||
.chain(ruff_python_stdlib::str::SINGLE_QUOTE_PREFIXES)
|
||||
.chain(ruff_python_stdlib::bytes::SINGLE_QUOTE_PREFIXES)
|
||||
.collect::<Vec<_>>();
|
||||
for i in 1..prefixes.len() {
|
||||
for j in 0..i - 1 {
|
||||
if i != j {
|
||||
if prefixes[i].starts_with(prefixes[j]) {
|
||||
assert!(
|
||||
!prefixes[i].starts_with(prefixes[j]),
|
||||
"Prefixes are not unique: {} starts with {}",
|
||||
prefixes[i],
|
||||
prefixes[j]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,10 +2,10 @@ use rustpython_parser::{Mode, Tok};
|
|||
|
||||
use ruff_formatter::prelude::*;
|
||||
use ruff_formatter::{write, Format};
|
||||
use ruff_python_ast::str::{leading_quote, trailing_quote};
|
||||
use ruff_text_size::TextSize;
|
||||
|
||||
use crate::context::ASTFormatContext;
|
||||
use crate::core::helpers::{leading_quote, trailing_quote};
|
||||
use crate::core::types::Range;
|
||||
use crate::cst::Expr;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue