Skip to content

Commit

Permalink
Move shared_traits to ruff_formatter (#4632)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored May 24, 2023
1 parent 4233f6e commit edc6c40
Show file tree
Hide file tree
Showing 24 changed files with 127 additions and 163 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(clippy::all)]

/// Used to get an object that knows how to format this object.
pub trait AsFormat<Context> {
type Format<'a>: ruff_formatter::Format<Context>
Expand Down
104 changes: 50 additions & 54 deletions crates/ruff_python_formatter/src/cst/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ pub(crate) mod visitor;
type Ident = String;

#[derive(Clone, Debug, PartialEq)]
pub struct Attributed<T> {
pub range: TextRange,
pub node: T,
pub trivia: Vec<Trivia>,
pub parentheses: Parenthesize,
pub(crate) struct Attributed<T> {
pub(crate) range: TextRange,
pub(crate) node: T,
pub(crate) trivia: Vec<Trivia>,
pub(crate) parentheses: Parenthesize,
}

impl<T> Attributed<T> {
pub fn new(range: TextRange, node: T) -> Self {
pub(crate) fn new(range: TextRange, node: T) -> Self {
Self {
range,
node,
Expand All @@ -36,23 +36,19 @@ impl<T> Attributed<T> {
}
}

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
}
}
Expand All @@ -65,7 +61,7 @@ impl<T> Deref for Attributed<T> {
}

#[derive(Clone, Debug, PartialEq)]
pub enum ExprContext {
pub(crate) enum ExprContext {
Load,
Store,
Del,
Expand All @@ -82,7 +78,7 @@ impl From<ast::ExprContext> for ExprContext {
}

#[derive(Clone, Debug, PartialEq)]
pub enum BoolOpKind {
pub(crate) enum BoolOpKind {
And,
Or,
}
Expand All @@ -99,7 +95,7 @@ impl From<&ast::Boolop> for BoolOpKind {
pub(crate) type BoolOp = Attributed<BoolOpKind>;

#[derive(Clone, Debug, PartialEq)]
pub enum OperatorKind {
pub(crate) enum OperatorKind {
Add,
Sub,
Mult,
Expand Down Expand Up @@ -138,7 +134,7 @@ impl From<&ast::Operator> for OperatorKind {
}

#[derive(Clone, Debug, PartialEq)]
pub enum UnaryOpKind {
pub(crate) enum UnaryOpKind {
Invert,
Not,
UAdd,
Expand All @@ -159,7 +155,7 @@ impl From<&ast::Unaryop> for UnaryOpKind {
}

#[derive(Clone, Debug, PartialEq)]
pub enum CmpOpKind {
pub(crate) enum CmpOpKind {
Eq,
NotEq,
Lt,
Expand Down Expand Up @@ -208,7 +204,7 @@ impl From<(Vec<ast::Stmt>, &Locator<'_>)> for Body {
}

#[derive(Clone, Debug, PartialEq)]
pub enum StmtKind {
pub(crate) enum StmtKind {
FunctionDef {
name: Ident,
args: Box<Arguments>,
Expand Down Expand Up @@ -338,7 +334,7 @@ pub enum StmtKind {
pub(crate) type Stmt = Attributed<StmtKind>;

#[derive(Clone, Debug, PartialEq)]
pub enum ExprKind {
pub(crate) enum ExprKind {
BoolOp {
ops: Vec<BoolOp>,
values: Vec<Expr>,
Expand Down Expand Up @@ -456,15 +452,15 @@ pub enum ExprKind {
pub(crate) type Expr = Attributed<ExprKind>;

#[derive(Clone, Debug, PartialEq)]
pub struct Comprehension {
pub target: Expr,
pub iter: Expr,
pub ifs: Vec<Expr>,
pub is_async: usize,
pub(crate) struct Comprehension {
pub(crate) target: Expr,
pub(crate) iter: Expr,
pub(crate) ifs: Vec<Expr>,
pub(crate) is_async: usize,
}

#[derive(Clone, Debug, PartialEq)]
pub enum ExcepthandlerKind {
pub(crate) enum ExcepthandlerKind {
ExceptHandler {
type_: Option<Box<Expr>>,
name: Option<Ident>,
Expand All @@ -475,7 +471,7 @@ pub enum ExcepthandlerKind {
pub(crate) type Excepthandler = Attributed<ExcepthandlerKind>;

#[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.
Expand All @@ -485,57 +481,57 @@ pub enum SliceIndexKind {
pub(crate) type SliceIndex = Attributed<SliceIndexKind>;

#[derive(Clone, Debug, PartialEq)]
pub struct Arguments {
pub posonlyargs: Vec<Arg>,
pub args: Vec<Arg>,
pub vararg: Option<Box<Arg>>,
pub kwonlyargs: Vec<Arg>,
pub kw_defaults: Vec<Expr>,
pub kwarg: Option<Box<Arg>>,
pub defaults: Vec<Expr>,
pub(crate) struct Arguments {
pub(crate) posonlyargs: Vec<Arg>,
pub(crate) args: Vec<Arg>,
pub(crate) vararg: Option<Box<Arg>>,
pub(crate) kwonlyargs: Vec<Arg>,
pub(crate) kw_defaults: Vec<Expr>,
pub(crate) kwarg: Option<Box<Arg>>,
pub(crate) defaults: Vec<Expr>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct ArgData {
pub arg: Ident,
pub annotation: Option<Box<Expr>>,
pub type_comment: Option<String>,
pub(crate) struct ArgData {
pub(crate) arg: Ident,
pub(crate) annotation: Option<Box<Expr>>,
pub(crate) type_comment: Option<String>,
}

pub(crate) type Arg = Attributed<ArgData>;

#[derive(Clone, Debug, PartialEq)]
pub struct KeywordData {
pub arg: Option<Ident>,
pub value: Expr,
pub(crate) struct KeywordData {
pub(crate) arg: Option<Ident>,
pub(crate) value: Expr,
}

pub(crate) type Keyword = Attributed<KeywordData>;

#[derive(Clone, Debug, PartialEq)]
pub struct AliasData {
pub name: Ident,
pub asname: Option<Ident>,
pub(crate) struct AliasData {
pub(crate) name: Ident,
pub(crate) asname: Option<Ident>,
}

pub(crate) type Alias = Attributed<AliasData>;

#[derive(Clone, Debug, PartialEq)]
pub struct Withitem {
pub context_expr: Expr,
pub optional_vars: Option<Box<Expr>>,
pub(crate) struct Withitem {
pub(crate) context_expr: Expr,
pub(crate) optional_vars: Option<Box<Expr>>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct MatchCase {
pub pattern: Pattern,
pub guard: Option<Box<Expr>>,
pub body: Body,
pub(crate) struct MatchCase {
pub(crate) pattern: Pattern,
pub(crate) guard: Option<Box<Expr>>,
pub(crate) body: Body,
}

#[allow(clippy::enum_variant_names)]
#[derive(Clone, Debug, PartialEq)]
pub enum PatternKind {
pub(crate) enum PatternKind {
MatchValue {
value: Box<Expr>,
},
Expand Down
6 changes: 2 additions & 4 deletions crates/ruff_python_formatter/src/format/alias.rs
Original file line number Diff line number Diff line change
@@ -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,
}

Expand Down
6 changes: 2 additions & 4 deletions crates/ruff_python_formatter/src/format/arg.rs
Original file line number Diff line number Diff line change
@@ -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,
}

Expand Down
6 changes: 2 additions & 4 deletions crates/ruff_python_formatter/src/format/arguments.rs
Original file line number Diff line number Diff line change
@@ -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,
}

Expand Down
6 changes: 2 additions & 4 deletions crates/ruff_python_formatter/src/format/bool_op.rs
Original file line number Diff line number Diff line change
@@ -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,
}

Expand Down
4 changes: 1 addition & 3 deletions crates/ruff_python_formatter/src/format/builders.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
6 changes: 2 additions & 4 deletions crates/ruff_python_formatter/src/format/cmp_op.rs
Original file line number Diff line number Diff line change
@@ -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,
}

Expand Down
3 changes: 1 addition & 2 deletions crates/ruff_python_formatter/src/format/comments.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 2 additions & 4 deletions crates/ruff_python_formatter/src/format/comprehension.rs
Original file line number Diff line number Diff line change
@@ -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,
}

Expand Down
6 changes: 2 additions & 4 deletions crates/ruff_python_formatter/src/format/excepthandler.rs
Original file line number Diff line number Diff line change
@@ -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,
}

Expand Down
6 changes: 2 additions & 4 deletions crates/ruff_python_formatter/src/format/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}

Expand Down
6 changes: 2 additions & 4 deletions crates/ruff_python_formatter/src/format/keyword.rs
Original file line number Diff line number Diff line change
@@ -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,
}

Expand Down
Loading

0 comments on commit edc6c40

Please sign in to comment.