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,6 +1,5 @@
use ruff_formatter::{format_args, write, Argument, Arguments};
use ruff_python_ast::Ranged;
use ruff_text_size::{TextRange, TextSize};
use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::context::{NodeLevel, WithNodeLevel};
use crate::other::commas::has_magic_trailing_comma;

View file

@ -1,9 +1,9 @@
use std::fmt::{Debug, Formatter, Write};
use itertools::Itertools;
use ruff_python_ast::Ranged;
use ruff_formatter::SourceCode;
use ruff_text_size::Ranged;
use crate::comments::node_key::NodeRefEqualityKey;
use crate::comments::{CommentsMap, SourceComment};
@ -178,11 +178,11 @@ impl Debug for DebugNodeCommentSlice<'_> {
#[cfg(test)]
mod tests {
use insta::assert_debug_snapshot;
use ruff_python_ast::{StmtBreak, StmtContinue};
use ruff_text_size::{TextRange, TextSize};
use ruff_formatter::SourceCode;
use ruff_python_ast::node::AnyNode;
use ruff_python_ast::{StmtBreak, StmtContinue};
use ruff_text_size::{TextRange, TextSize};
use crate::comments::map::MultiMap;
use crate::comments::{CommentLinePosition, Comments, CommentsMap, SourceComment};

View file

@ -1,5 +1,4 @@
use ruff_python_ast::Ranged;
use ruff_text_size::{TextLen, TextRange, TextSize};
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use ruff_formatter::{format_args, write, FormatError, SourceCode};
use ruff_python_ast::node::{AnyNodeRef, AstNode};

View file

@ -87,12 +87,12 @@
//!
//! It is possible to add an additional optional label to [`SourceComment`] If ever the need arises to distinguish two *dangling comments* in the formatting logic,
use ruff_text_size::TextRange;
use ruff_text_size::{Ranged, TextRange};
use std::cell::Cell;
use std::fmt::Debug;
use std::rc::Rc;
use ruff_python_ast::{Mod, Ranged};
use ruff_python_ast::Mod;
pub(crate) use format::{
dangling_comments, dangling_node_comments, dangling_open_parenthesis_comments,

View file

@ -2,12 +2,12 @@ use std::cmp::Ordering;
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::whitespace::indentation;
use ruff_python_ast::{self as ast, Comprehension, Expr, MatchCase, Parameters, Ranged};
use ruff_python_ast::{self as ast, Comprehension, Expr, MatchCase, Parameters};
use ruff_python_trivia::{
find_only_token_in_range, indentation_at_offset, SimpleToken, SimpleTokenKind, SimpleTokenizer,
};
use ruff_source_file::Locator;
use ruff_text_size::{TextLen, TextRange};
use ruff_text_size::{Ranged, TextLen, TextRange};
use crate::comments::visitor::{CommentPlacement, DecoratedComment};
use crate::expression::expr_slice::{assign_comment_in_slice, ExprSliceCommentSection};

View file

@ -1,7 +1,7 @@
use std::iter::Peekable;
use ruff_python_ast::{Mod, Ranged, Stmt};
use ruff_text_size::{TextRange, TextSize};
use ruff_python_ast::{Mod, Stmt};
use ruff_text_size::{Ranged, TextRange, TextSize};
use ruff_formatter::{SourceCode, SourceCodeSlice};
use ruff_python_ast::node::AnyNodeRef;

View file

@ -1,8 +1,8 @@
use ruff_formatter::{write, FormatRuleWithOptions};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::{Constant, Expr, ExprAttribute, ExprConstant, Ranged};
use ruff_python_ast::{Constant, Expr, ExprAttribute, ExprConstant};
use ruff_python_trivia::{find_only_token_in_range, SimpleTokenKind};
use ruff_text_size::TextRange;
use ruff_text_size::{Ranged, TextRange};
use crate::comments::{dangling_comments, SourceComment};
use crate::expression::parentheses::{

View file

@ -1,7 +1,7 @@
use ruff_formatter::FormatRuleWithOptions;
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::{Constant, ExprConstant, Ranged};
use ruff_text_size::{TextLen, TextRange};
use ruff_python_ast::{Constant, ExprConstant};
use ruff_text_size::{Ranged, TextLen, TextRange};
use crate::comments::SourceComment;
use crate::expression::number::{FormatComplex, FormatFloat, FormatInt};

View file

@ -1,8 +1,7 @@
use ruff_formatter::{format_args, write};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::Ranged;
use ruff_python_ast::{Expr, ExprDict};
use ruff_text_size::TextRange;
use ruff_text_size::{Ranged, TextRange};
use crate::comments::{leading_comments, SourceComment};
use crate::expression::parentheses::{

View file

@ -1,4 +1,5 @@
use ruff_python_ast::{ExprIpyEscapeCommand, Ranged};
use ruff_python_ast::ExprIpyEscapeCommand;
use ruff_text_size::Ranged;
use crate::prelude::*;

View file

@ -1,6 +1,7 @@
use ruff_formatter::prelude::format_with;
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::{ExprList, Ranged};
use ruff_python_ast::ExprList;
use ruff_text_size::Ranged;
use crate::comments::SourceComment;
use crate::expression::parentheses::{

View file

@ -50,9 +50,8 @@ impl NeedsParentheses for ExprName {
#[cfg(test)]
mod tests {
use ruff_python_ast::Ranged;
use ruff_python_parser::parse_program;
use ruff_text_size::{TextRange, TextSize};
use ruff_text_size::{Ranged, TextRange, TextSize};
#[test]
fn name_range_with_comments() {

View file

@ -1,5 +1,6 @@
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::{ExprSet, Ranged};
use ruff_python_ast::ExprSet;
use ruff_text_size::Ranged;
use crate::comments::SourceComment;
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};

View file

@ -2,7 +2,7 @@ use ruff_formatter::{write, FormatError};
use ruff_python_ast::node::{AnyNodeRef, AstNode};
use ruff_python_ast::{Expr, ExprSlice, ExprUnaryOp, UnaryOp};
use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::TextRange;
use ruff_text_size::{Ranged, TextRange};
use crate::comments::{dangling_comments, SourceComment};
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
@ -158,9 +158,7 @@ pub(crate) fn find_colons(
lower: Option<&Expr>,
upper: Option<&Expr>,
) -> FormatResult<(SimpleToken, Option<SimpleToken>)> {
let after_lower = lower
.as_ref()
.map_or(range.start(), ruff_python_ast::Ranged::end);
let after_lower = lower.as_ref().map_or(range.start(), Ranged::end);
let mut tokens = SimpleTokenizer::new(contents, TextRange::new(after_lower, range.end()))
.skip_trivia()
.skip_while(|token| token.kind == SimpleTokenKind::RParen);
@ -173,9 +171,7 @@ pub(crate) fn find_colons(
));
}
let after_upper = upper
.as_ref()
.map_or(first_colon.end(), ruff_python_ast::Ranged::end);
let after_upper = upper.as_ref().map_or(first_colon.end(), Ranged::end);
let mut tokens = SimpleTokenizer::new(contents, TextRange::new(after_upper, range.end()))
.skip_trivia()
.skip_while(|token| token.kind == SimpleTokenKind::RParen);

View file

@ -1,9 +1,8 @@
use ruff_formatter::{format_args, write, FormatRuleWithOptions};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::ExprTuple;
use ruff_python_ast::Ranged;
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;

View file

@ -1,8 +1,8 @@
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::ExprUnaryOp;
use ruff_python_ast::UnaryOp;
use ruff_python_ast::{ExprUnaryOp, Ranged};
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{TextLen, TextRange};
use ruff_text_size::{Ranged, TextLen, TextRange};
use crate::comments::trailing_comments;
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};

View file

@ -1,7 +1,7 @@
use ruff_formatter::write;
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::{Expr, ExprYield, ExprYieldFrom, Ranged};
use ruff_text_size::TextRange;
use ruff_python_ast::{Expr, ExprYield, ExprYieldFrom};
use ruff_text_size::{Ranged, TextRange};
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Parenthesize};

View file

@ -1,7 +1,7 @@
use std::borrow::Cow;
use ruff_python_ast::{ExprConstant, Ranged};
use ruff_text_size::TextSize;
use ruff_python_ast::ExprConstant;
use ruff_text_size::{Ranged, TextSize};
use crate::prelude::*;

View file

@ -1,8 +1,9 @@
use ruff_formatter::prelude::tag::Condition;
use ruff_formatter::{format_args, write, Argument, Arguments, FormatContext, FormatOptions};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::{ExpressionRef, Ranged};
use ruff_python_ast::ExpressionRef;
use ruff_python_trivia::{first_non_trivia_token, SimpleToken, SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::Ranged;
use crate::comments::{
dangling_comments, dangling_open_parenthesis_comments, trailing_comments, SourceComment,

View file

@ -4,11 +4,11 @@ use bitflags::bitflags;
use ruff_formatter::{format_args, write, FormatError, FormatOptions, TabWidth};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::{self as ast, ExprConstant, ExprFString, Ranged};
use ruff_python_ast::{self as ast, ExprConstant, ExprFString};
use ruff_python_parser::lexer::{lex_starts_at, LexicalError, LexicalErrorType};
use ruff_python_parser::{Mode, Tok};
use ruff_source_file::Locator;
use ruff_text_size::{TextLen, TextRange, TextSize};
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use crate::comments::{leading_comments, trailing_comments};
use crate::expression::parentheses::{

View file

@ -1,8 +1,8 @@
use ruff_formatter::write;
use ruff_python_ast::node::AstNode;
use ruff_python_ast::{Arguments, Expr, Ranged};
use ruff_python_ast::{Arguments, Expr};
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{TextRange, TextSize};
use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::comments::SourceComment;
use crate::expression::expr_generator_exp::GeneratorExpParentheses;

View file

@ -1,5 +1,6 @@
use ruff_formatter::{format_args, write, Buffer, FormatResult};
use ruff_python_ast::{Comprehension, Expr, Ranged};
use ruff_python_ast::{Comprehension, Expr};
use ruff_text_size::Ranged;
use crate::comments::{leading_comments, trailing_comments, SourceComment};
use crate::expression::expr_tuple::TupleParentheses;

View file

@ -1,7 +1,8 @@
use crate::prelude::*;
use ruff_formatter::{FormatOwnedWithRule, FormatRefWithRule};
use ruff_python_ast::{Identifier, Ranged};
use ruff_python_ast::Identifier;
use ruff_text_size::Ranged;
use crate::prelude::*;
pub struct FormatIdentifier;

View file

@ -2,9 +2,9 @@ use std::usize;
use ruff_formatter::{format_args, write, FormatRuleWithOptions};
use ruff_python_ast::node::{AnyNodeRef, AstNode};
use ruff_python_ast::{Parameters, Ranged};
use ruff_python_ast::Parameters;
use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{TextRange, TextSize};
use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::comments::{
dangling_comments, dangling_open_parenthesis_comments, leading_comments, leading_node_comments,

View file

@ -1,7 +1,8 @@
use ruff_formatter::{FormatOwnedWithRule, FormatRefWithRule, FormatRule, FormatRuleWithOptions};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::{Pattern, Ranged};
use ruff_python_ast::Pattern;
use ruff_python_trivia::{first_non_trivia_token, SimpleToken, SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::Ranged;
use crate::expression::parentheses::{
parenthesized, NeedsParentheses, OptionalParentheses, Parentheses,

View file

@ -1,8 +1,8 @@
use ruff_formatter::write;
use ruff_python_ast::node::AstNode;
use ruff_python_ast::{Pattern, PatternArguments, Ranged};
use ruff_python_ast::{Pattern, PatternArguments};
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::{TextRange, TextSize};
use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::comments::SourceComment;
use crate::expression::parentheses::{empty_parenthesized, parenthesized, Parentheses};

View file

@ -1,9 +1,9 @@
use ruff_formatter::{format_args, write};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::PatternMatchMapping;
use ruff_python_ast::{Expr, Identifier, Pattern, Ranged};
use ruff_python_ast::{Expr, Identifier, Pattern};
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::TextRange;
use ruff_text_size::{Ranged, TextRange};
use crate::comments::{leading_comments, trailing_comments, SourceComment};
use crate::expression::parentheses::{

View file

@ -1,8 +1,8 @@
use ruff_formatter::{Format, FormatResult};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::{PatternMatchSequence, Ranged};
use ruff_python_ast::PatternMatchSequence;
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
use ruff_text_size::TextRange;
use ruff_text_size::{Ranged, TextRange};
use crate::expression::parentheses::{
empty_parenthesized, optional_parentheses, parenthesized, NeedsParentheses, OptionalParentheses,

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};

View file

@ -1,12 +1,13 @@
use ruff_formatter::write;
use ruff_formatter::FormatResult;
use ruff_python_ast::node::AstNode;
use ruff_python_ast::TypeParams;
use ruff_text_size::Ranged;
use crate::builders::PyFormatterExtensions;
use crate::comments::{trailing_comments, SourceComment};
use crate::expression::parentheses::parenthesized;
use crate::prelude::*;
use ruff_formatter::write;
use ruff_formatter::FormatResult;
use ruff_python_ast::node::AstNode;
use ruff_python_ast::{Ranged, TypeParams};
#[derive(Default)]
pub struct FormatTypeParams;

View file

@ -5,12 +5,12 @@ use unicode_width::UnicodeWidthStr;
use ruff_formatter::{write, FormatError};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::{Ranged, Stmt};
use ruff_python_ast::Stmt;
use ruff_python_parser::lexer::{lex_starts_at, LexResult};
use ruff_python_parser::{Mode, Tok};
use ruff_python_trivia::lines_before;
use ruff_source_file::Locator;
use ruff_text_size::{TextRange, TextSize};
use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::comments::format::{empty_lines, format_comment};
use crate::comments::{leading_comments, trailing_comments, SourceComment};