From edc6c4058f100c472e8165d2a915425696f34f53 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 24 May 2023 17:38:11 +0200 Subject: [PATCH] Move `shared_traits` to `ruff_formatter` (#4632) --- .../src => ruff_formatter}/shared_traits.rs | 2 - crates/ruff_python_formatter/src/cst/mod.rs | 104 +++++++++--------- .../ruff_python_formatter/src/format/alias.rs | 6 +- .../ruff_python_formatter/src/format/arg.rs | 6 +- .../src/format/arguments.rs | 6 +- .../src/format/bool_op.rs | 6 +- .../src/format/builders.rs | 4 +- .../src/format/cmp_op.rs | 6 +- .../src/format/comments.rs | 3 +- .../src/format/comprehension.rs | 6 +- .../src/format/excepthandler.rs | 6 +- .../ruff_python_formatter/src/format/expr.rs | 6 +- .../src/format/keyword.rs | 6 +- .../src/format/match_case.rs | 6 +- .../src/format/numbers.rs | 3 +- .../src/format/operator.rs | 6 +- .../src/format/pattern.rs | 6 +- .../ruff_python_formatter/src/format/stmt.rs | 6 +- .../src/format/strings.rs | 3 +- .../src/format/unary_op.rs | 6 +- .../src/format/withitem.rs | 6 +- crates/ruff_python_formatter/src/lib.rs | 8 +- crates/ruff_python_formatter/src/prelude.rs | 3 + crates/ruff_python_formatter/src/trivia.rs | 70 ++++++------ 24 files changed, 127 insertions(+), 163 deletions(-) rename crates/{ruff_python_formatter/src => ruff_formatter}/shared_traits.rs (99%) create mode 100644 crates/ruff_python_formatter/src/prelude.rs diff --git a/crates/ruff_python_formatter/src/shared_traits.rs b/crates/ruff_formatter/shared_traits.rs similarity index 99% rename from crates/ruff_python_formatter/src/shared_traits.rs rename to crates/ruff_formatter/shared_traits.rs index 421d1f6b1b11f..f427934f31a31 100644 --- a/crates/ruff_python_formatter/src/shared_traits.rs +++ b/crates/ruff_formatter/shared_traits.rs @@ -1,5 +1,3 @@ -#![allow(clippy::all)] - /// Used to get an object that knows how to format this object. pub trait AsFormat { type Format<'a>: ruff_formatter::Format diff --git a/crates/ruff_python_formatter/src/cst/mod.rs b/crates/ruff_python_formatter/src/cst/mod.rs index a6e353e91749d..239629bdd0cfd 100644 --- a/crates/ruff_python_formatter/src/cst/mod.rs +++ b/crates/ruff_python_formatter/src/cst/mod.rs @@ -19,15 +19,15 @@ pub(crate) mod visitor; type Ident = String; #[derive(Clone, Debug, PartialEq)] -pub struct Attributed { - pub range: TextRange, - pub node: T, - pub trivia: Vec, - pub parentheses: Parenthesize, +pub(crate) struct Attributed { + pub(crate) range: TextRange, + pub(crate) node: T, + pub(crate) trivia: Vec, + pub(crate) parentheses: Parenthesize, } impl Attributed { - pub fn new(range: TextRange, node: T) -> Self { + pub(crate) fn new(range: TextRange, node: T) -> Self { Self { range, node, @@ -36,23 +36,19 @@ impl Attributed { } } - pub const fn range(&self) -> TextRange { + pub(crate) const fn range(&self) -> TextRange { self.range } - pub const fn start(&self) -> TextSize { + pub(crate) const fn start(&self) -> TextSize { self.range.start() } - pub const fn end(&self) -> TextSize { + pub(crate) const fn end(&self) -> TextSize { self.range.end() } - pub fn add_trivia(&mut self, trivia: Trivia) { - self.trivia.push(trivia); - } - - pub fn id(&self) -> usize { + pub(crate) fn id(&self) -> usize { std::ptr::addr_of!(self.node) as usize } } @@ -65,7 +61,7 @@ impl Deref for Attributed { } #[derive(Clone, Debug, PartialEq)] -pub enum ExprContext { +pub(crate) enum ExprContext { Load, Store, Del, @@ -82,7 +78,7 @@ impl From for ExprContext { } #[derive(Clone, Debug, PartialEq)] -pub enum BoolOpKind { +pub(crate) enum BoolOpKind { And, Or, } @@ -99,7 +95,7 @@ impl From<&ast::Boolop> for BoolOpKind { pub(crate) type BoolOp = Attributed; #[derive(Clone, Debug, PartialEq)] -pub enum OperatorKind { +pub(crate) enum OperatorKind { Add, Sub, Mult, @@ -138,7 +134,7 @@ impl From<&ast::Operator> for OperatorKind { } #[derive(Clone, Debug, PartialEq)] -pub enum UnaryOpKind { +pub(crate) enum UnaryOpKind { Invert, Not, UAdd, @@ -159,7 +155,7 @@ impl From<&ast::Unaryop> for UnaryOpKind { } #[derive(Clone, Debug, PartialEq)] -pub enum CmpOpKind { +pub(crate) enum CmpOpKind { Eq, NotEq, Lt, @@ -208,7 +204,7 @@ impl From<(Vec, &Locator<'_>)> for Body { } #[derive(Clone, Debug, PartialEq)] -pub enum StmtKind { +pub(crate) enum StmtKind { FunctionDef { name: Ident, args: Box, @@ -338,7 +334,7 @@ pub enum StmtKind { pub(crate) type Stmt = Attributed; #[derive(Clone, Debug, PartialEq)] -pub enum ExprKind { +pub(crate) enum ExprKind { BoolOp { ops: Vec, values: Vec, @@ -456,15 +452,15 @@ pub enum ExprKind { pub(crate) type Expr = Attributed; #[derive(Clone, Debug, PartialEq)] -pub struct Comprehension { - pub target: Expr, - pub iter: Expr, - pub ifs: Vec, - pub is_async: usize, +pub(crate) struct Comprehension { + pub(crate) target: Expr, + pub(crate) iter: Expr, + pub(crate) ifs: Vec, + pub(crate) is_async: usize, } #[derive(Clone, Debug, PartialEq)] -pub enum ExcepthandlerKind { +pub(crate) enum ExcepthandlerKind { ExceptHandler { type_: Option>, name: Option, @@ -475,7 +471,7 @@ pub enum ExcepthandlerKind { pub(crate) type Excepthandler = Attributed; #[derive(Clone, Debug, PartialEq)] -pub enum SliceIndexKind { +pub(crate) enum SliceIndexKind { /// The index slot exists, but is empty. Empty, /// The index slot contains an expression. @@ -485,57 +481,57 @@ pub enum SliceIndexKind { pub(crate) type SliceIndex = Attributed; #[derive(Clone, Debug, PartialEq)] -pub struct Arguments { - pub posonlyargs: Vec, - pub args: Vec, - pub vararg: Option>, - pub kwonlyargs: Vec, - pub kw_defaults: Vec, - pub kwarg: Option>, - pub defaults: Vec, +pub(crate) struct Arguments { + pub(crate) posonlyargs: Vec, + pub(crate) args: Vec, + pub(crate) vararg: Option>, + pub(crate) kwonlyargs: Vec, + pub(crate) kw_defaults: Vec, + pub(crate) kwarg: Option>, + pub(crate) defaults: Vec, } #[derive(Clone, Debug, PartialEq)] -pub struct ArgData { - pub arg: Ident, - pub annotation: Option>, - pub type_comment: Option, +pub(crate) struct ArgData { + pub(crate) arg: Ident, + pub(crate) annotation: Option>, + pub(crate) type_comment: Option, } pub(crate) type Arg = Attributed; #[derive(Clone, Debug, PartialEq)] -pub struct KeywordData { - pub arg: Option, - pub value: Expr, +pub(crate) struct KeywordData { + pub(crate) arg: Option, + pub(crate) value: Expr, } pub(crate) type Keyword = Attributed; #[derive(Clone, Debug, PartialEq)] -pub struct AliasData { - pub name: Ident, - pub asname: Option, +pub(crate) struct AliasData { + pub(crate) name: Ident, + pub(crate) asname: Option, } pub(crate) type Alias = Attributed; #[derive(Clone, Debug, PartialEq)] -pub struct Withitem { - pub context_expr: Expr, - pub optional_vars: Option>, +pub(crate) struct Withitem { + pub(crate) context_expr: Expr, + pub(crate) optional_vars: Option>, } #[derive(Clone, Debug, PartialEq)] -pub struct MatchCase { - pub pattern: Pattern, - pub guard: Option>, - pub body: Body, +pub(crate) struct MatchCase { + pub(crate) pattern: Pattern, + pub(crate) guard: Option>, + pub(crate) body: Body, } #[allow(clippy::enum_variant_names)] #[derive(Clone, Debug, PartialEq)] -pub enum PatternKind { +pub(crate) enum PatternKind { MatchValue { value: Box, }, diff --git a/crates/ruff_python_formatter/src/format/alias.rs b/crates/ruff_python_formatter/src/format/alias.rs index 1432b0c1c5626..a6d3f1e189117 100644 --- a/crates/ruff_python_formatter/src/format/alias.rs +++ b/crates/ruff_python_formatter/src/format/alias.rs @@ -1,12 +1,10 @@ -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::write; -use crate::context::ASTFormatContext; use crate::cst::Alias; use crate::format::comments::end_of_line_comments; -use crate::shared_traits::AsFormat; -pub struct FormatAlias<'a> { +pub(crate) struct FormatAlias<'a> { item: &'a Alias, } diff --git a/crates/ruff_python_formatter/src/format/arg.rs b/crates/ruff_python_formatter/src/format/arg.rs index b321b0145e2de..f350a464dfa2e 100644 --- a/crates/ruff_python_formatter/src/format/arg.rs +++ b/crates/ruff_python_formatter/src/format/arg.rs @@ -1,12 +1,10 @@ -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::write; -use crate::context::ASTFormatContext; use crate::cst::Arg; use crate::format::comments::end_of_line_comments; -use crate::shared_traits::AsFormat; -pub struct FormatArg<'a> { +pub(crate) struct FormatArg<'a> { item: &'a Arg, } diff --git a/crates/ruff_python_formatter/src/format/arguments.rs b/crates/ruff_python_formatter/src/format/arguments.rs index 38a536d5fe7c1..fe8dbc5e5a636 100644 --- a/crates/ruff_python_formatter/src/format/arguments.rs +++ b/crates/ruff_python_formatter/src/format/arguments.rs @@ -1,11 +1,9 @@ -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::{format_args, write, Format}; -use crate::context::ASTFormatContext; use crate::cst::Arguments; -use crate::shared_traits::AsFormat; -pub struct FormatArguments<'a> { +pub(crate) struct FormatArguments<'a> { item: &'a Arguments, } diff --git a/crates/ruff_python_formatter/src/format/bool_op.rs b/crates/ruff_python_formatter/src/format/bool_op.rs index eb705f0d47402..4e278ce2c0a2a 100644 --- a/crates/ruff_python_formatter/src/format/bool_op.rs +++ b/crates/ruff_python_formatter/src/format/bool_op.rs @@ -1,12 +1,10 @@ -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::write; -use crate::context::ASTFormatContext; use crate::cst::{BoolOp, BoolOpKind}; use crate::format::comments::{end_of_line_comments, leading_comments, trailing_comments}; -use crate::shared_traits::AsFormat; -pub struct FormatBoolOp<'a> { +pub(crate) struct FormatBoolOp<'a> { item: &'a BoolOp, } diff --git a/crates/ruff_python_formatter/src/format/builders.rs b/crates/ruff_python_formatter/src/format/builders.rs index 3b2ec3c8da725..d22cf7bd278cf 100644 --- a/crates/ruff_python_formatter/src/format/builders.rs +++ b/crates/ruff_python_formatter/src/format/builders.rs @@ -1,10 +1,8 @@ -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::{write, Format}; use ruff_text_size::TextRange; -use crate::context::ASTFormatContext; use crate::cst::{Body, Stmt}; -use crate::shared_traits::AsFormat; use crate::trivia::{Relationship, TriviaKind}; #[derive(Copy, Clone)] diff --git a/crates/ruff_python_formatter/src/format/cmp_op.rs b/crates/ruff_python_formatter/src/format/cmp_op.rs index fe90676fc4441..5b32e0a4be1fc 100644 --- a/crates/ruff_python_formatter/src/format/cmp_op.rs +++ b/crates/ruff_python_formatter/src/format/cmp_op.rs @@ -1,12 +1,10 @@ -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::write; -use crate::context::ASTFormatContext; use crate::cst::{CmpOp, CmpOpKind}; use crate::format::comments::{end_of_line_comments, leading_comments, trailing_comments}; -use crate::shared_traits::AsFormat; -pub struct FormatCmpOp<'a> { +pub(crate) struct FormatCmpOp<'a> { item: &'a CmpOp, } diff --git a/crates/ruff_python_formatter/src/format/comments.rs b/crates/ruff_python_formatter/src/format/comments.rs index 352bc85f7e443..b6d13f021d443 100644 --- a/crates/ruff_python_formatter/src/format/comments.rs +++ b/crates/ruff_python_formatter/src/format/comments.rs @@ -1,7 +1,6 @@ -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::{write, Format}; -use crate::context::ASTFormatContext; use crate::cst::Attributed; use crate::format::builders::literal; use crate::trivia::TriviaKind; diff --git a/crates/ruff_python_formatter/src/format/comprehension.rs b/crates/ruff_python_formatter/src/format/comprehension.rs index 9b572edcb2db7..ccf981b99400b 100644 --- a/crates/ruff_python_formatter/src/format/comprehension.rs +++ b/crates/ruff_python_formatter/src/format/comprehension.rs @@ -1,11 +1,9 @@ -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::write; -use crate::context::ASTFormatContext; use crate::cst::Comprehension; -use crate::shared_traits::AsFormat; -pub struct FormatComprehension<'a> { +pub(crate) struct FormatComprehension<'a> { item: &'a Comprehension, } diff --git a/crates/ruff_python_formatter/src/format/excepthandler.rs b/crates/ruff_python_formatter/src/format/excepthandler.rs index 5619a951501c6..2662540249899 100644 --- a/crates/ruff_python_formatter/src/format/excepthandler.rs +++ b/crates/ruff_python_formatter/src/format/excepthandler.rs @@ -1,13 +1,11 @@ -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::write; -use crate::context::ASTFormatContext; use crate::cst::{Excepthandler, ExcepthandlerKind}; use crate::format::builders::block; use crate::format::comments::end_of_line_comments; -use crate::shared_traits::AsFormat; -pub struct FormatExcepthandler<'a> { +pub(crate) struct FormatExcepthandler<'a> { item: &'a Excepthandler, } diff --git a/crates/ruff_python_formatter/src/format/expr.rs b/crates/ruff_python_formatter/src/format/expr.rs index bc1bf911d6093..5a7ae3282412d 100644 --- a/crates/ruff_python_formatter/src/format/expr.rs +++ b/crates/ruff_python_formatter/src/format/expr.rs @@ -2,10 +2,9 @@ use rustpython_parser::ast::{Constant, ConversionFlag}; -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::{format_args, write}; -use crate::context::ASTFormatContext; use crate::cst::{ Arguments, BoolOp, CmpOp, Comprehension, Expr, ExprKind, Keyword, Operator, OperatorKind, SliceIndex, SliceIndexKind, UnaryOp, UnaryOpKind, @@ -15,10 +14,9 @@ use crate::format::comments::{dangling_comments, end_of_line_comments, leading_c use crate::format::helpers::{is_self_closing, is_simple_power, is_simple_slice}; use crate::format::numbers::{complex_literal, float_literal, int_literal}; use crate::format::strings::string_literal; -use crate::shared_traits::AsFormat; use crate::trivia::{Parenthesize, TriviaKind}; -pub struct FormatExpr<'a> { +pub(crate) struct FormatExpr<'a> { item: &'a Expr, } diff --git a/crates/ruff_python_formatter/src/format/keyword.rs b/crates/ruff_python_formatter/src/format/keyword.rs index 2f9a18be092a7..c277b7de57c38 100644 --- a/crates/ruff_python_formatter/src/format/keyword.rs +++ b/crates/ruff_python_formatter/src/format/keyword.rs @@ -1,12 +1,10 @@ -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::write; -use crate::context::ASTFormatContext; use crate::cst::Keyword; use crate::format::comments::{end_of_line_comments, leading_comments, trailing_comments}; -use crate::shared_traits::AsFormat; -pub struct FormatKeyword<'a> { +pub(crate) struct FormatKeyword<'a> { item: &'a Keyword, } diff --git a/crates/ruff_python_formatter/src/format/match_case.rs b/crates/ruff_python_formatter/src/format/match_case.rs index 6161e0f7645d9..d71ac3c919620 100644 --- a/crates/ruff_python_formatter/src/format/match_case.rs +++ b/crates/ruff_python_formatter/src/format/match_case.rs @@ -1,13 +1,11 @@ -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::write; -use crate::context::ASTFormatContext; use crate::cst::MatchCase; use crate::format::builders::block; use crate::format::comments::{end_of_line_comments, leading_comments}; -use crate::shared_traits::AsFormat; -pub struct FormatMatchCase<'a> { +pub(crate) struct FormatMatchCase<'a> { item: &'a MatchCase, } diff --git a/crates/ruff_python_formatter/src/format/numbers.rs b/crates/ruff_python_formatter/src/format/numbers.rs index a82d620c59a0f..dde5a1c5371d7 100644 --- a/crates/ruff_python_formatter/src/format/numbers.rs +++ b/crates/ruff_python_formatter/src/format/numbers.rs @@ -1,10 +1,9 @@ use std::ops::{Add, Sub}; -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::{write, Format}; use ruff_text_size::{TextRange, TextSize}; -use crate::context::ASTFormatContext; use crate::format::builders::literal; #[derive(Debug, Copy, Clone, Eq, PartialEq)] diff --git a/crates/ruff_python_formatter/src/format/operator.rs b/crates/ruff_python_formatter/src/format/operator.rs index 40c49893fc007..360cea69cbe45 100644 --- a/crates/ruff_python_formatter/src/format/operator.rs +++ b/crates/ruff_python_formatter/src/format/operator.rs @@ -1,12 +1,10 @@ -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::write; -use crate::context::ASTFormatContext; use crate::cst::{Operator, OperatorKind}; use crate::format::comments::{end_of_line_comments, leading_comments, trailing_comments}; -use crate::shared_traits::AsFormat; -pub struct FormatOperator<'a> { +pub(crate) struct FormatOperator<'a> { item: &'a Operator, } diff --git a/crates/ruff_python_formatter/src/format/pattern.rs b/crates/ruff_python_formatter/src/format/pattern.rs index 59093f4b61723..565312c92e9d2 100644 --- a/crates/ruff_python_formatter/src/format/pattern.rs +++ b/crates/ruff_python_formatter/src/format/pattern.rs @@ -1,13 +1,11 @@ use rustpython_parser::ast::Constant; -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::write; -use crate::context::ASTFormatContext; use crate::cst::{Pattern, PatternKind}; -use crate::shared_traits::AsFormat; -pub struct FormatPattern<'a> { +pub(crate) struct FormatPattern<'a> { item: &'a Pattern, } diff --git a/crates/ruff_python_formatter/src/format/stmt.rs b/crates/ruff_python_formatter/src/format/stmt.rs index 2b8045392105b..0e76023b01b59 100644 --- a/crates/ruff_python_formatter/src/format/stmt.rs +++ b/crates/ruff_python_formatter/src/format/stmt.rs @@ -1,9 +1,8 @@ #![allow(unused_variables, clippy::too_many_arguments)] -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::{format_args, write}; -use crate::context::ASTFormatContext; use crate::cst::{ Alias, Arguments, Body, Excepthandler, Expr, ExprKind, Keyword, MatchCase, Operator, Stmt, StmtKind, Withitem, @@ -11,7 +10,6 @@ use crate::cst::{ use crate::format::builders::{block, join_names}; use crate::format::comments::{end_of_line_comments, leading_comments, trailing_comments}; use crate::format::helpers::is_self_closing; -use crate::shared_traits::AsFormat; fn format_break(f: &mut Formatter, stmt: &Stmt) -> FormatResult<()> { write!(f, [text("break")])?; @@ -752,7 +750,7 @@ fn format_with_( Ok(()) } -pub struct FormatStmt<'a> { +pub(crate) struct FormatStmt<'a> { item: &'a Stmt, } diff --git a/crates/ruff_python_formatter/src/format/strings.rs b/crates/ruff_python_formatter/src/format/strings.rs index e5163cc53259a..2046608295807 100644 --- a/crates/ruff_python_formatter/src/format/strings.rs +++ b/crates/ruff_python_formatter/src/format/strings.rs @@ -1,11 +1,10 @@ use rustpython_parser::{Mode, Tok}; -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::{write, Format}; use ruff_python_ast::str::{leading_quote, trailing_quote}; use ruff_text_size::TextRange; -use crate::context::ASTFormatContext; use crate::cst::Expr; #[derive(Debug, Copy, Clone, Eq, PartialEq)] diff --git a/crates/ruff_python_formatter/src/format/unary_op.rs b/crates/ruff_python_formatter/src/format/unary_op.rs index 5e51d251c2a8b..b16220fee918e 100644 --- a/crates/ruff_python_formatter/src/format/unary_op.rs +++ b/crates/ruff_python_formatter/src/format/unary_op.rs @@ -1,11 +1,9 @@ -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::write; -use crate::context::ASTFormatContext; use crate::cst::{UnaryOp, UnaryOpKind}; -use crate::shared_traits::AsFormat; -pub struct FormatUnaryOp<'a> { +pub(crate) struct FormatUnaryOp<'a> { item: &'a UnaryOp, } diff --git a/crates/ruff_python_formatter/src/format/withitem.rs b/crates/ruff_python_formatter/src/format/withitem.rs index 50c309f86d576..6b48dcf6e0d3e 100644 --- a/crates/ruff_python_formatter/src/format/withitem.rs +++ b/crates/ruff_python_formatter/src/format/withitem.rs @@ -1,11 +1,9 @@ -use ruff_formatter::prelude::*; +use crate::prelude::*; use ruff_formatter::write; -use crate::context::ASTFormatContext; use crate::cst::Withitem; -use crate::shared_traits::AsFormat; -pub struct FormatWithitem<'a> { +pub(crate) struct FormatWithitem<'a> { item: &'a Withitem, } diff --git a/crates/ruff_python_formatter/src/lib.rs b/crates/ruff_python_formatter/src/lib.rs index bfa75f3ed52ba..90ee7eb27e8d1 100644 --- a/crates/ruff_python_formatter/src/lib.rs +++ b/crates/ruff_python_formatter/src/lib.rs @@ -17,8 +17,10 @@ mod cst; mod format; mod newlines; mod parentheses; -pub mod shared_traits; -pub mod trivia; +mod prelude; +mod trivia; + +include!("../../ruff_formatter/shared_traits.rs"); pub fn fmt(contents: &str) -> Result> { // Create a reusable locator. @@ -179,7 +181,7 @@ mod tests { #[test] fn string_processing() { - use ruff_formatter::prelude::*; + use crate::prelude::*; use ruff_formatter::{format, format_args, write}; struct FormatString<'a>(&'a str); diff --git a/crates/ruff_python_formatter/src/prelude.rs b/crates/ruff_python_formatter/src/prelude.rs new file mode 100644 index 0000000000000..01089927b27ca --- /dev/null +++ b/crates/ruff_python_formatter/src/prelude.rs @@ -0,0 +1,3 @@ +#[allow(unused_imports)] +pub(crate) use crate::{ASTFormatContext, AsFormat, FormattedIterExt as _, IntoFormat}; +pub(crate) use ruff_formatter::prelude::*; diff --git a/crates/ruff_python_formatter/src/trivia.rs b/crates/ruff_python_formatter/src/trivia.rs index 8bc00de5bd79a..cab42fd7b6d70 100644 --- a/crates/ruff_python_formatter/src/trivia.rs +++ b/crates/ruff_python_formatter/src/trivia.rs @@ -9,7 +9,7 @@ use crate::cst::{ }; #[derive(Clone, Copy, Debug)] -pub enum Node<'a> { +pub(crate) enum Node<'a> { Alias(&'a Alias), Arg(&'a Arg), Body(&'a Body), @@ -27,7 +27,7 @@ pub enum Node<'a> { } impl Node<'_> { - pub fn id(&self) -> usize { + pub(crate) fn id(&self) -> usize { match self { Node::Alias(node) => node.id(), Node::Arg(node) => node.id(), @@ -46,7 +46,7 @@ impl Node<'_> { } } - pub fn start(&self) -> TextSize { + pub(crate) fn start(&self) -> TextSize { match self { Node::Alias(node) => node.start(), Node::Arg(node) => node.start(), @@ -65,7 +65,7 @@ impl Node<'_> { } } - pub fn end(&self) -> TextSize { + pub(crate) fn end(&self) -> TextSize { match self { Node::Alias(node) => node.end(), Node::Arg(node) => node.end(), @@ -86,7 +86,7 @@ impl Node<'_> { } #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum TriviaTokenKind { +pub(crate) enum TriviaTokenKind { OwnLineComment, EndOfLineComment, MagicTrailingComma, @@ -95,23 +95,23 @@ pub enum TriviaTokenKind { } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct TriviaToken { - pub range: TextRange, - pub kind: TriviaTokenKind, +pub(crate) struct TriviaToken { + pub(crate) range: TextRange, + pub(crate) kind: TriviaTokenKind, } impl TriviaToken { - pub const fn start(&self) -> TextSize { + pub(crate) const fn start(&self) -> TextSize { self.range.start() } - pub const fn end(&self) -> TextSize { + pub(crate) const fn end(&self) -> TextSize { self.range.end() } } #[derive(Clone, Copy, Debug, PartialEq, Eq, is_macro::Is)] -pub enum TriviaKind { +pub(crate) enum TriviaKind { /// A Comment that is separated by at least one line break from the /// preceding token. /// @@ -140,14 +140,14 @@ pub enum TriviaKind { } #[derive(Clone, Copy, Debug, PartialEq, Eq, is_macro::Is)] -pub enum Relationship { +pub(crate) enum Relationship { Leading, Trailing, Dangling, } #[derive(Clone, Copy, Debug, PartialEq, Eq, is_macro::Is)] -pub enum Parenthesize { +pub(crate) enum Parenthesize { /// Always parenthesize the statement or expression. Always, /// Never parenthesize the statement or expression. @@ -157,13 +157,13 @@ pub enum Parenthesize { } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct Trivia { - pub kind: TriviaKind, - pub relationship: Relationship, +pub(crate) struct Trivia { + pub(crate) kind: TriviaKind, + pub(crate) relationship: Relationship, } impl Trivia { - pub fn from_token(token: &TriviaToken, relationship: Relationship) -> Self { + pub(crate) fn from_token(token: &TriviaToken, relationship: Relationship) -> Self { match token.kind { TriviaTokenKind::MagicTrailingComma => Self { kind: TriviaKind::MagicTrailingComma, @@ -189,7 +189,7 @@ impl Trivia { } } -pub fn extract_trivia_tokens(lxr: &[LexResult]) -> Vec { +pub(crate) fn extract_trivia_tokens(lxr: &[LexResult]) -> Vec { let mut tokens = vec![]; let mut prev_tok: Option<(&Tok, TextRange)> = None; let mut prev_semantic_tok: Option<(&Tok, TextRange)> = None; @@ -731,14 +731,14 @@ fn sorted_child_nodes_inner<'a>(node: Node<'a>, result: &mut Vec>) { } } -pub fn sorted_child_nodes(node: Node) -> Vec { +pub(crate) fn sorted_child_nodes(node: Node) -> Vec { let mut result = Vec::new(); sorted_child_nodes_inner(node, &mut result); result } -pub fn decorate_token<'a>( +pub(crate) fn decorate_token<'a>( token: &TriviaToken, node: Node<'a>, enclosing_node: Option>, @@ -818,20 +818,20 @@ pub fn decorate_token<'a>( } #[derive(Debug, Default)] -pub struct TriviaIndex { - pub alias: FxHashMap>, - pub arg: FxHashMap>, - pub body: FxHashMap>, - pub bool_op: FxHashMap>, - pub cmp_op: FxHashMap>, - pub excepthandler: FxHashMap>, - pub expr: FxHashMap>, - pub keyword: FxHashMap>, - pub operator: FxHashMap>, - pub pattern: FxHashMap>, - pub slice_index: FxHashMap>, - pub stmt: FxHashMap>, - pub unary_op: FxHashMap>, +pub(crate) struct TriviaIndex { + pub(crate) alias: FxHashMap>, + pub(crate) arg: FxHashMap>, + pub(crate) body: FxHashMap>, + pub(crate) bool_op: FxHashMap>, + pub(crate) cmp_op: FxHashMap>, + pub(crate) excepthandler: FxHashMap>, + pub(crate) expr: FxHashMap>, + pub(crate) keyword: FxHashMap>, + pub(crate) operator: FxHashMap>, + pub(crate) pattern: FxHashMap>, + pub(crate) slice_index: FxHashMap>, + pub(crate) stmt: FxHashMap>, + pub(crate) unary_op: FxHashMap>, } fn add_comment(comment: Trivia, node: &Node, trivia: &mut TriviaIndex) { @@ -931,7 +931,7 @@ fn add_comment(comment: Trivia, node: &Node, trivia: &mut TriviaIndex) { } } -pub fn decorate_trivia(tokens: Vec, python_ast: &[Stmt]) -> TriviaIndex { +pub(crate) fn decorate_trivia(tokens: Vec, python_ast: &[Stmt]) -> TriviaIndex { let mut stack = vec![]; let mut cache = FxHashMap::default(); for token in &tokens {