Generate verbatim nodes

This commit is contained in:
Charlie Marsh
2024-01-28 21:10:24 -05:00
parent 7329bf459c
commit 00b3663b2d
14 changed files with 80 additions and 49 deletions

View File

@@ -17,6 +17,7 @@ ruff_python_ast = { path = "../ruff_python_ast" }
ruff_python_literal = { path = "../ruff_python_literal" }
ruff_python_parser = { path = "../ruff_python_parser" }
ruff_source_file = { path = "../ruff_source_file" }
ruff_text_size = { path = "../ruff_text_size" }
once_cell = { workspace = true }

View File

@@ -10,7 +10,8 @@ use ruff_python_ast::{
};
use ruff_python_ast::{ParameterWithDefault, TypeParams};
use ruff_python_literal::escape::{AsciiEscape, Escape, UnicodeEscape};
use ruff_source_file::LineEnding;
use ruff_source_file::{LineEnding, Locator};
use ruff_text_size::{Ranged, TextRange};
use super::stylist::{Indentation, Quote, Stylist};
@@ -61,6 +62,11 @@ mod precedence {
pub(crate) const MAX: u8 = 63;
}
pub struct Verbatim<'a> {
locator: &'a Locator<'a>,
nodes: Vec<TextRange>,
}
pub struct Generator<'a> {
/// The indentation style to use.
indent: &'a Indentation,
@@ -72,6 +78,7 @@ pub struct Generator<'a> {
indent_depth: usize,
num_newlines: usize,
initial: bool,
locator: Option<&'a Locator<'a>>,
}
impl<'a> From<&'a Stylist<'a>> for Generator<'a> {
@@ -84,6 +91,7 @@ impl<'a> From<&'a Stylist<'a>> for Generator<'a> {
indent_depth: 0,
num_newlines: 0,
initial: true,
locator: None,
}
}
}
@@ -100,6 +108,15 @@ impl<'a> Generator<'a> {
indent_depth: 0,
num_newlines: 0,
initial: true,
locator: None,
}
}
#[must_use]
pub fn with_locator(self, locator: &'a Locator<'a>) -> Self {
Self {
locator: Some(locator),
..self
}
}
@@ -796,6 +813,14 @@ impl<'a> Generator<'a> {
ret
}};
}
if let Some(locator) = &self.locator {
if !ast.range().is_empty() {
self.p(locator.slice(ast));
return;
}
}
match ast {
Expr::BoolOp(ast::ExprBoolOp {
op,