Move Ranged into ruff_text_size (#6919)

## Summary

The motivation here is that this enables us to implement `Ranged` in
crates that don't depend on `ruff_python_ast`.

Largely a mechanical refactor with a lot of regex, Clippy help, and
manual fixups.

## Test Plan

`cargo test`
This commit is contained in:
Charlie Marsh 2023-08-27 14:12:51 -04:00 committed by GitHub
parent 88c8bece38
commit fc89976c24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
468 changed files with 940 additions and 706 deletions

View file

@ -1,11 +1,11 @@
use ruff_formatter::{write, Argument, Arguments, FormatError};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::{
ElifElseClause, ExceptHandlerExceptHandler, MatchCase, Ranged, StmtClassDef, StmtFor,
StmtFunctionDef, StmtIf, StmtMatch, StmtTry, StmtWhile, StmtWith, Suite,
ElifElseClause, ExceptHandlerExceptHandler, MatchCase, StmtClassDef, StmtFor, StmtFunctionDef,
StmtIf, StmtMatch, StmtTry, StmtWhile, StmtWith, Suite,
};
use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{TextRange, TextSize};
use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::comments::{
leading_alternate_branch_comments, trailing_comments, SourceComment, SuppressionKind,
@ -60,11 +60,9 @@ impl<'a> ClauseHeader<'a> {
| ClauseHeader::With(_)
| ClauseHeader::OrElse(_) => last_child_end,
ClauseHeader::ExceptHandler(handler) => handler
.name
.as_ref()
.map(ruff_python_ast::Ranged::end)
.or(last_child_end),
ClauseHeader::ExceptHandler(handler) => {
handler.name.as_ref().map(Ranged::end).or(last_child_end)
}
};
let colon = colon_range(end.unwrap_or(keyword_range.end()), source)?;

View file

@ -1,6 +1,7 @@
use ruff_formatter::write;
use ruff_python_ast::{Decorator, Ranged, StmtClassDef};
use ruff_python_ast::{Decorator, StmtClassDef};
use ruff_python_trivia::lines_after_ignoring_trivia;
use ruff_text_size::Ranged;
use crate::comments::{leading_comments, trailing_comments, SourceComment};
use crate::prelude::*;

View file

@ -1,5 +1,6 @@
use ruff_formatter::write;
use ruff_python_ast::{Ranged, StmtDelete};
use ruff_python_ast::StmtDelete;
use ruff_text_size::Ranged;
use crate::builders::{parenthesize_if_expands, PyFormatterExtensions};
use crate::comments::{dangling_node_comments, SourceComment, SuppressionKind};

View file

@ -1,5 +1,6 @@
use ruff_formatter::{format_args, write};
use ruff_python_ast::{Expr, Ranged, Stmt, StmtFor};
use ruff_python_ast::{Expr, Stmt, StmtFor};
use ruff_text_size::Ranged;
use crate::comments::SourceComment;
use crate::expression::expr_tuple::TupleParentheses;

View file

@ -1,6 +1,7 @@
use ruff_formatter::write;
use ruff_python_ast::{Parameters, Ranged, StmtFunctionDef};
use ruff_python_ast::{Parameters, StmtFunctionDef};
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::Ranged;
use crate::comments::SourceComment;
use crate::expression::maybe_parenthesize_expression;

View file

@ -1,6 +1,7 @@
use ruff_formatter::write;
use ruff_python_ast::node::AstNode;
use ruff_python_ast::{Ranged, StmtImportFrom};
use ruff_python_ast::StmtImportFrom;
use ruff_text_size::Ranged;
use crate::builders::{parenthesize_if_expands, PyFormatterExtensions, TrailingComma};
use crate::comments::{SourceComment, SuppressionKind};

View file

@ -1,4 +1,5 @@
use ruff_python_ast::{Ranged, StmtIpyEscapeCommand};
use ruff_python_ast::StmtIpyEscapeCommand;
use ruff_text_size::Ranged;
use crate::comments::{SourceComment, SuppressionKind};
use crate::prelude::*;

View file

@ -1,5 +1,6 @@
use ruff_formatter::{write, FormatRuleWithOptions};
use ruff_python_ast::{ExceptHandler, Ranged, StmtTry};
use ruff_python_ast::{ExceptHandler, StmtTry};
use ruff_text_size::Ranged;
use crate::comments;
use crate::comments::leading_alternate_branch_comments;

View file

@ -1,6 +1,7 @@
use ruff_formatter::{format_args, write};
use ruff_python_ast::node::AstNode;
use ruff_python_ast::{Ranged, Stmt, StmtWhile};
use ruff_python_ast::{Stmt, StmtWhile};
use ruff_text_size::Ranged;
use crate::comments::SourceComment;
use crate::expression::maybe_parenthesize_expression;

View file

@ -1,8 +1,8 @@
use ruff_formatter::{format_args, write, FormatError};
use ruff_python_ast::node::AstNode;
use ruff_python_ast::{Ranged, StmtWith};
use ruff_python_ast::StmtWith;
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::TextRange;
use ruff_text_size::{Ranged, TextRange};
use crate::builders::parenthesize_if_expands;
use crate::comments::SourceComment;
@ -12,7 +12,7 @@ use crate::expression::parentheses::{
use crate::other::commas;
use crate::prelude::*;
use crate::statement::clause::{clause_body, clause_header, ClauseHeader};
use crate::{FormatNodeRule, PyFormatOptions};
use crate::PyFormatOptions;
#[derive(Default)]
pub struct FormatStmtWith;

View file

@ -1,9 +1,9 @@
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions};
use ruff_python_ast::helpers::is_compound_statement;
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::{self as ast, Constant, Expr, ExprConstant, Ranged, Stmt, Suite};
use ruff_python_ast::{self as ast, Constant, Expr, ExprConstant, Stmt, Suite};
use ruff_python_trivia::{lines_after_ignoring_trivia, lines_before};
use ruff_text_size::TextRange;
use ruff_text_size::{Ranged, TextRange};
use crate::comments::{leading_comments, trailing_comments, Comments};
use crate::context::{NodeLevel, WithNodeLevel};