mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:09:22 +00:00
Rename ruff_python_whitespace
to ruff_python_trivia
(#5886)
## Summary This crate now contains utilities for dealing with trivia more broadly: whitespace, newlines, "simple" trivia lexing, etc. So renaming it to reflect its increased responsibilities. To avoid conflicts, I've also renamed `Token` and `TokenKind` to `SimpleToken` and `SimpleTokenKind`.
This commit is contained in:
parent
a75a6de577
commit
5f3da9955a
86 changed files with 360 additions and 353 deletions
|
@ -133,8 +133,8 @@ At time of writing, the repository includes the following crates:
|
||||||
refer to?"
|
refer to?"
|
||||||
- `crates/ruff_python_stdlib`: library crate containing Python-specific standard library data, e.g.
|
- `crates/ruff_python_stdlib`: library crate containing Python-specific standard library data, e.g.
|
||||||
the names of all built-in exceptions and which standard library types are immutable.
|
the names of all built-in exceptions and which standard library types are immutable.
|
||||||
- `crates/ruff_python_whitespace`: library crate containing Python-specific whitespace analysis
|
- `crates/ruff_python_trivia`: library crate containing Python-specific trivia utilities (e.g.,
|
||||||
logic (indentation and newlines).
|
for analyzing indentation, newlines, etc.).
|
||||||
- `crates/ruff_rustpython`: library crate containing `RustPython`-specific utilities.
|
- `crates/ruff_rustpython`: library crate containing `RustPython`-specific utilities.
|
||||||
- `crates/ruff_textwrap`: library crate to indent and dedent Python source code.
|
- `crates/ruff_textwrap`: library crate to indent and dedent Python source code.
|
||||||
- `crates/ruff_wasm`: library crate for exposing Ruff as a WebAssembly module. Powers the
|
- `crates/ruff_wasm`: library crate for exposing Ruff as a WebAssembly module. Powers the
|
||||||
|
|
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -1928,7 +1928,7 @@ dependencies = [
|
||||||
"ruff_python_ast",
|
"ruff_python_ast",
|
||||||
"ruff_python_semantic",
|
"ruff_python_semantic",
|
||||||
"ruff_python_stdlib",
|
"ruff_python_stdlib",
|
||||||
"ruff_python_whitespace",
|
"ruff_python_trivia",
|
||||||
"ruff_rustpython",
|
"ruff_rustpython",
|
||||||
"ruff_text_size",
|
"ruff_text_size",
|
||||||
"ruff_textwrap",
|
"ruff_textwrap",
|
||||||
|
@ -2124,7 +2124,7 @@ dependencies = [
|
||||||
"num-bigint",
|
"num-bigint",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"ruff_python_whitespace",
|
"ruff_python_trivia",
|
||||||
"ruff_text_size",
|
"ruff_text_size",
|
||||||
"rustc-hash",
|
"rustc-hash",
|
||||||
"rustpython-ast",
|
"rustpython-ast",
|
||||||
|
@ -2148,7 +2148,7 @@ dependencies = [
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"ruff_formatter",
|
"ruff_formatter",
|
||||||
"ruff_python_ast",
|
"ruff_python_ast",
|
||||||
"ruff_python_whitespace",
|
"ruff_python_trivia",
|
||||||
"ruff_text_size",
|
"ruff_text_size",
|
||||||
"rustc-hash",
|
"rustc-hash",
|
||||||
"rustpython-parser",
|
"rustpython-parser",
|
||||||
|
@ -2191,7 +2191,7 @@ name = "ruff_python_stdlib"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ruff_python_whitespace"
|
name = "ruff_python_trivia"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"insta",
|
"insta",
|
||||||
|
@ -2237,7 +2237,7 @@ dependencies = [
|
||||||
name = "ruff_textwrap"
|
name = "ruff_textwrap"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ruff_python_whitespace",
|
"ruff_python_trivia",
|
||||||
"ruff_text_size",
|
"ruff_text_size",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ ruff_cache = { path = "../ruff_cache" }
|
||||||
ruff_diagnostics = { path = "../ruff_diagnostics", features = ["serde"] }
|
ruff_diagnostics = { path = "../ruff_diagnostics", features = ["serde"] }
|
||||||
ruff_index = { path = "../ruff_index" }
|
ruff_index = { path = "../ruff_index" }
|
||||||
ruff_macros = { path = "../ruff_macros" }
|
ruff_macros = { path = "../ruff_macros" }
|
||||||
ruff_python_whitespace = { path = "../ruff_python_whitespace" }
|
ruff_python_trivia = { path = "../ruff_python_trivia" }
|
||||||
ruff_python_ast = { path = "../ruff_python_ast", features = ["serde"] }
|
ruff_python_ast = { path = "../ruff_python_ast", features = ["serde"] }
|
||||||
ruff_python_semantic = { path = "../ruff_python_semantic" }
|
ruff_python_semantic = { path = "../ruff_python_semantic" }
|
||||||
ruff_python_stdlib = { path = "../ruff_python_stdlib" }
|
ruff_python_stdlib = { path = "../ruff_python_stdlib" }
|
||||||
|
|
|
@ -7,7 +7,7 @@ use rustpython_parser::{lexer, Mode};
|
||||||
use ruff_diagnostics::Edit;
|
use ruff_diagnostics::Edit;
|
||||||
use ruff_python_ast::helpers;
|
use ruff_python_ast::helpers;
|
||||||
use ruff_python_ast::source_code::{Indexer, Locator, Stylist};
|
use ruff_python_ast::source_code::{Indexer, Locator, Stylist};
|
||||||
use ruff_python_whitespace::{is_python_whitespace, NewlineWithTrailingNewline, PythonWhitespace};
|
use ruff_python_trivia::{is_python_whitespace, NewlineWithTrailingNewline, PythonWhitespace};
|
||||||
|
|
||||||
use crate::autofix::codemods;
|
use crate::autofix::codemods;
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ use ruff_text_size::TextSize;
|
||||||
|
|
||||||
use ruff_diagnostics::Diagnostic;
|
use ruff_diagnostics::Diagnostic;
|
||||||
use ruff_python_ast::source_code::{Indexer, Locator, Stylist};
|
use ruff_python_ast::source_code::{Indexer, Locator, Stylist};
|
||||||
use ruff_python_whitespace::UniversalNewlines;
|
use ruff_python_trivia::UniversalNewlines;
|
||||||
|
|
||||||
use crate::comments::shebang::ShebangDirective;
|
use crate::comments::shebang::ShebangDirective;
|
||||||
use crate::registry::Rule;
|
use crate::registry::Rule;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use ruff_python_whitespace::{is_python_whitespace, Cursor};
|
use ruff_python_trivia::{is_python_whitespace, Cursor};
|
||||||
use ruff_text_size::{TextLen, TextSize};
|
use ruff_text_size::{TextLen, TextSize};
|
||||||
|
|
||||||
/// A shebang directive (e.g., `#!/usr/bin/env python3`).
|
/// A shebang directive (e.g., `#!/usr/bin/env python3`).
|
||||||
|
|
|
@ -10,7 +10,7 @@ use rustpython_parser::Tok;
|
||||||
|
|
||||||
use ruff_python_ast::source_code::Locator;
|
use ruff_python_ast::source_code::Locator;
|
||||||
use ruff_python_ast::statement_visitor::{walk_stmt, StatementVisitor};
|
use ruff_python_ast::statement_visitor::{walk_stmt, StatementVisitor};
|
||||||
use ruff_python_whitespace::UniversalNewlineIterator;
|
use ruff_python_trivia::UniversalNewlineIterator;
|
||||||
|
|
||||||
/// Extract doc lines (standalone comments) from a token sequence.
|
/// Extract doc lines (standalone comments) from a token sequence.
|
||||||
pub(crate) fn doc_lines_from_tokens(lxr: &[LexResult]) -> DocLines {
|
pub(crate) fn doc_lines_from_tokens(lxr: &[LexResult]) -> DocLines {
|
||||||
|
|
|
@ -5,7 +5,7 @@ use ruff_python_ast::docstrings::{leading_space, leading_words};
|
||||||
use ruff_text_size::{TextLen, TextRange, TextSize};
|
use ruff_text_size::{TextLen, TextRange, TextSize};
|
||||||
use strum_macros::EnumIter;
|
use strum_macros::EnumIter;
|
||||||
|
|
||||||
use ruff_python_whitespace::{Line, UniversalNewlineIterator, UniversalNewlines};
|
use ruff_python_trivia::{Line, UniversalNewlineIterator, UniversalNewlines};
|
||||||
|
|
||||||
use crate::docstrings::styles::SectionStyle;
|
use crate::docstrings::styles::SectionStyle;
|
||||||
use crate::docstrings::{Docstring, DocstringBody};
|
use crate::docstrings::{Docstring, DocstringBody};
|
||||||
|
|
|
@ -8,7 +8,7 @@ use rustpython_parser::{lexer, Mode, Tok};
|
||||||
use ruff_diagnostics::Edit;
|
use ruff_diagnostics::Edit;
|
||||||
use ruff_python_ast::helpers::is_docstring_stmt;
|
use ruff_python_ast::helpers::is_docstring_stmt;
|
||||||
use ruff_python_ast::source_code::{Locator, Stylist};
|
use ruff_python_ast::source_code::{Locator, Stylist};
|
||||||
use ruff_python_whitespace::{PythonWhitespace, UniversalNewlineIterator};
|
use ruff_python_trivia::{PythonWhitespace, UniversalNewlineIterator};
|
||||||
use ruff_textwrap::indent;
|
use ruff_textwrap::indent;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
@ -305,7 +305,7 @@ mod tests {
|
||||||
use rustpython_parser::Parse;
|
use rustpython_parser::Parse;
|
||||||
|
|
||||||
use ruff_python_ast::source_code::{Locator, Stylist};
|
use ruff_python_ast::source_code::{Locator, Stylist};
|
||||||
use ruff_python_whitespace::LineEnding;
|
use ruff_python_trivia::LineEnding;
|
||||||
|
|
||||||
use super::Insertion;
|
use super::Insertion;
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ use serde::Serialize;
|
||||||
use serde_json::error::Category;
|
use serde_json::error::Category;
|
||||||
|
|
||||||
use ruff_diagnostics::Diagnostic;
|
use ruff_diagnostics::Diagnostic;
|
||||||
use ruff_python_whitespace::{NewlineWithTrailingNewline, UniversalNewlineIterator};
|
use ruff_python_trivia::{NewlineWithTrailingNewline, UniversalNewlineIterator};
|
||||||
use ruff_text_size::{TextRange, TextSize};
|
use ruff_text_size::{TextRange, TextSize};
|
||||||
|
|
||||||
use crate::autofix::source_map::{SourceMap, SourceMarker};
|
use crate::autofix::source_map::{SourceMap, SourceMarker};
|
||||||
|
|
|
@ -13,7 +13,7 @@ use rustpython_parser::ast::Ranged;
|
||||||
|
|
||||||
use ruff_diagnostics::Diagnostic;
|
use ruff_diagnostics::Diagnostic;
|
||||||
use ruff_python_ast::source_code::Locator;
|
use ruff_python_ast::source_code::Locator;
|
||||||
use ruff_python_whitespace::LineEnding;
|
use ruff_python_trivia::LineEnding;
|
||||||
|
|
||||||
use crate::codes::NoqaCode;
|
use crate::codes::NoqaCode;
|
||||||
use crate::fs::relativize_path;
|
use crate::fs::relativize_path;
|
||||||
|
@ -772,7 +772,7 @@ mod tests {
|
||||||
|
|
||||||
use ruff_diagnostics::Diagnostic;
|
use ruff_diagnostics::Diagnostic;
|
||||||
use ruff_python_ast::source_code::Locator;
|
use ruff_python_ast::source_code::Locator;
|
||||||
use ruff_python_whitespace::LineEnding;
|
use ruff_python_trivia::LineEnding;
|
||||||
|
|
||||||
use crate::noqa::{add_noqa_inner, Directive, NoqaMapping, ParsedFileExemption};
|
use crate::noqa::{add_noqa_inner, Directive, NoqaMapping, ParsedFileExemption};
|
||||||
use crate::rules::pycodestyle::rules::AmbiguousVariableName;
|
use crate::rules::pycodestyle::rules::AmbiguousVariableName;
|
||||||
|
|
|
@ -3,7 +3,7 @@ use rustpython_parser::ast::{self, Constant, Decorator, Expr, Keyword};
|
||||||
use ruff_python_ast::call_path::{collect_call_path, CallPath};
|
use ruff_python_ast::call_path::{collect_call_path, CallPath};
|
||||||
use ruff_python_ast::helpers::map_callable;
|
use ruff_python_ast::helpers::map_callable;
|
||||||
use ruff_python_semantic::SemanticModel;
|
use ruff_python_semantic::SemanticModel;
|
||||||
use ruff_python_whitespace::PythonWhitespace;
|
use ruff_python_trivia::PythonWhitespace;
|
||||||
|
|
||||||
pub(super) fn get_mark_decorators(
|
pub(super) fn get_mark_decorators(
|
||||||
decorators: &[Decorator],
|
decorators: &[Decorator],
|
||||||
|
|
|
@ -3,7 +3,7 @@ use rustpython_parser::ast;
|
||||||
use rustpython_parser::ast::{Expr, Ranged, Stmt};
|
use rustpython_parser::ast::{Expr, Ranged, Stmt};
|
||||||
|
|
||||||
use ruff_python_ast::source_code::Locator;
|
use ruff_python_ast::source_code::Locator;
|
||||||
use ruff_python_whitespace::UniversalNewlines;
|
use ruff_python_trivia::UniversalNewlines;
|
||||||
|
|
||||||
/// Return `true` if a function's return statement include at least one
|
/// Return `true` if a function's return statement include at least one
|
||||||
/// non-`None` value.
|
/// non-`None` value.
|
||||||
|
|
|
@ -12,7 +12,7 @@ use ruff_python_ast::helpers::{any_over_expr, contains_effect, first_colon_range
|
||||||
use ruff_python_ast::source_code::Locator;
|
use ruff_python_ast::source_code::Locator;
|
||||||
use ruff_python_ast::stmt_if::if_elif_branches;
|
use ruff_python_ast::stmt_if::if_elif_branches;
|
||||||
use ruff_python_semantic::SemanticModel;
|
use ruff_python_semantic::SemanticModel;
|
||||||
use ruff_python_whitespace::UniversalNewlines;
|
use ruff_python_trivia::UniversalNewlines;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::line_width::LineWidth;
|
use crate::line_width::LineWidth;
|
||||||
|
|
|
@ -6,7 +6,7 @@ use ruff_diagnostics::{AutofixKind, Violation};
|
||||||
use ruff_diagnostics::{Diagnostic, Fix};
|
use ruff_diagnostics::{Diagnostic, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::helpers::first_colon_range;
|
use ruff_python_ast::helpers::first_colon_range;
|
||||||
use ruff_python_whitespace::UniversalNewlines;
|
use ruff_python_trivia::UniversalNewlines;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::line_width::LineWidth;
|
use crate::line_width::LineWidth;
|
||||||
|
|
|
@ -2,7 +2,7 @@ use rustpython_parser::ast::{Ranged, Stmt};
|
||||||
use rustpython_parser::{lexer, Mode, Tok};
|
use rustpython_parser::{lexer, Mode, Tok};
|
||||||
|
|
||||||
use ruff_python_ast::source_code::Locator;
|
use ruff_python_ast::source_code::Locator;
|
||||||
use ruff_python_whitespace::{PythonWhitespace, UniversalNewlines};
|
use ruff_python_trivia::{PythonWhitespace, UniversalNewlines};
|
||||||
|
|
||||||
use crate::rules::isort::types::TrailingComma;
|
use crate::rules::isort::types::TrailingComma;
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ use ruff_python_ast::helpers::{
|
||||||
followed_by_multi_statement_line, preceded_by_multi_statement_line, trailing_lines_end,
|
followed_by_multi_statement_line, preceded_by_multi_statement_line, trailing_lines_end,
|
||||||
};
|
};
|
||||||
use ruff_python_ast::source_code::{Indexer, Locator, Stylist};
|
use ruff_python_ast::source_code::{Indexer, Locator, Stylist};
|
||||||
use ruff_python_whitespace::{leading_indentation, PythonWhitespace, UniversalNewlines};
|
use ruff_python_trivia::{leading_indentation, PythonWhitespace, UniversalNewlines};
|
||||||
use ruff_textwrap::indent;
|
use ruff_textwrap::indent;
|
||||||
|
|
||||||
use crate::line_width::LineWidth;
|
use crate::line_width::LineWidth;
|
||||||
|
|
|
@ -3,7 +3,7 @@ use rustpython_parser::ast::{CmpOp, Expr, Ranged};
|
||||||
use unicode_width::UnicodeWidthStr;
|
use unicode_width::UnicodeWidthStr;
|
||||||
|
|
||||||
use ruff_python_ast::source_code::Locator;
|
use ruff_python_ast::source_code::Locator;
|
||||||
use ruff_python_whitespace::Line;
|
use ruff_python_trivia::Line;
|
||||||
|
|
||||||
use crate::line_width::{LineLength, LineWidth, TabSize};
|
use crate::line_width::{LineLength, LineWidth, TabSize};
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_whitespace::Line;
|
use ruff_python_trivia::Line;
|
||||||
|
|
||||||
use crate::rules::pycodestyle::helpers::is_overlong;
|
use crate::rules::pycodestyle::helpers::is_overlong;
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
|
|
|
@ -8,7 +8,7 @@ use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::helpers::{has_leading_content, has_trailing_content};
|
use ruff_python_ast::helpers::{has_leading_content, has_trailing_content};
|
||||||
use ruff_python_ast::source_code::Generator;
|
use ruff_python_ast::source_code::Generator;
|
||||||
use ruff_python_semantic::SemanticModel;
|
use ruff_python_semantic::SemanticModel;
|
||||||
use ruff_python_whitespace::{leading_indentation, UniversalNewlines};
|
use ruff_python_trivia::{leading_indentation, UniversalNewlines};
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::registry::AsRule;
|
use crate::registry::AsRule;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_whitespace::Line;
|
use ruff_python_trivia::Line;
|
||||||
|
|
||||||
use crate::rules::pycodestyle::helpers::is_overlong;
|
use crate::rules::pycodestyle::helpers::is_overlong;
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
|
|
|
@ -18,7 +18,7 @@ use rustpython_parser::lexer::LexResult;
|
||||||
|
|
||||||
use ruff_python_ast::source_code::Locator;
|
use ruff_python_ast::source_code::Locator;
|
||||||
use ruff_python_ast::token_kind::TokenKind;
|
use ruff_python_ast::token_kind::TokenKind;
|
||||||
use ruff_python_whitespace::is_python_whitespace;
|
use ruff_python_trivia::is_python_whitespace;
|
||||||
|
|
||||||
mod extraneous_whitespace;
|
mod extraneous_whitespace;
|
||||||
mod indentation;
|
mod indentation;
|
||||||
|
|
|
@ -4,7 +4,7 @@ use ruff_diagnostics::Violation;
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::source_code::Locator;
|
use ruff_python_ast::source_code::Locator;
|
||||||
use ruff_python_ast::token_kind::TokenKind;
|
use ruff_python_ast::token_kind::TokenKind;
|
||||||
use ruff_python_whitespace::PythonWhitespace;
|
use ruff_python_trivia::PythonWhitespace;
|
||||||
|
|
||||||
use crate::checkers::logical_lines::LogicalLinesContext;
|
use crate::checkers::logical_lines::LogicalLinesContext;
|
||||||
use crate::rules::pycodestyle::rules::logical_lines::LogicalLine;
|
use crate::rules::pycodestyle::rules::logical_lines::LogicalLine;
|
||||||
|
|
|
@ -2,7 +2,7 @@ use ruff_text_size::{TextLen, TextRange};
|
||||||
|
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_whitespace::{leading_indentation, Line};
|
use ruff_python_trivia::{leading_indentation, Line};
|
||||||
|
|
||||||
/// ## What it does
|
/// ## What it does
|
||||||
/// Checks for mixed tabs and spaces in indentation.
|
/// Checks for mixed tabs and spaces in indentation.
|
||||||
|
|
|
@ -3,7 +3,7 @@ use ruff_text_size::{TextLen, TextRange, TextSize};
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::source_code::Indexer;
|
use ruff_python_ast::source_code::Indexer;
|
||||||
use ruff_python_whitespace::{leading_indentation, Line};
|
use ruff_python_trivia::{leading_indentation, Line};
|
||||||
|
|
||||||
#[violation]
|
#[violation]
|
||||||
pub struct TabIndentation;
|
pub struct TabIndentation;
|
||||||
|
|
|
@ -4,7 +4,7 @@ use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::helpers;
|
use ruff_python_ast::helpers;
|
||||||
use ruff_python_ast::source_code::{Indexer, Locator};
|
use ruff_python_ast::source_code::{Indexer, Locator};
|
||||||
use ruff_python_whitespace::Line;
|
use ruff_python_trivia::Line;
|
||||||
|
|
||||||
use crate::registry::Rule;
|
use crate::registry::Rule;
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
|
|
|
@ -5,7 +5,7 @@ use ruff_python_ast::cast;
|
||||||
use ruff_python_ast::helpers::map_callable;
|
use ruff_python_ast::helpers::map_callable;
|
||||||
use ruff_python_ast::str::is_implicit_concatenation;
|
use ruff_python_ast::str::is_implicit_concatenation;
|
||||||
use ruff_python_semantic::{Definition, Member, MemberKind, SemanticModel};
|
use ruff_python_semantic::{Definition, Member, MemberKind, SemanticModel};
|
||||||
use ruff_python_whitespace::UniversalNewlines;
|
use ruff_python_trivia::UniversalNewlines;
|
||||||
|
|
||||||
/// Return the index of the first logical line in a string.
|
/// Return the index of the first logical line in a string.
|
||||||
pub(super) fn logical_line(content: &str) -> Option<usize> {
|
pub(super) fn logical_line(content: &str) -> Option<usize> {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
|
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_whitespace::{UniversalNewlineIterator, UniversalNewlines};
|
use ruff_python_trivia::{UniversalNewlineIterator, UniversalNewlines};
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::docstrings::Docstring;
|
use crate::docstrings::Docstring;
|
||||||
|
|
|
@ -4,7 +4,7 @@ use rustpython_parser::ast::Ranged;
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_semantic::{Definition, Member, MemberKind};
|
use ruff_python_semantic::{Definition, Member, MemberKind};
|
||||||
use ruff_python_whitespace::{PythonWhitespace, UniversalNewlineIterator, UniversalNewlines};
|
use ruff_python_trivia::{PythonWhitespace, UniversalNewlineIterator, UniversalNewlines};
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::docstrings::Docstring;
|
use crate::docstrings::Docstring;
|
||||||
|
|
|
@ -6,7 +6,7 @@ use rustpython_parser::ast::Ranged;
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_semantic::{Definition, Member, MemberKind};
|
use ruff_python_semantic::{Definition, Member, MemberKind};
|
||||||
use ruff_python_whitespace::{PythonWhitespace, UniversalNewlineIterator, UniversalNewlines};
|
use ruff_python_trivia::{PythonWhitespace, UniversalNewlineIterator, UniversalNewlines};
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::docstrings::Docstring;
|
use crate::docstrings::Docstring;
|
||||||
|
|
|
@ -3,7 +3,7 @@ use strum::IntoEnumIterator;
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_whitespace::{UniversalNewlineIterator, UniversalNewlines};
|
use ruff_python_trivia::{UniversalNewlineIterator, UniversalNewlines};
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::docstrings::sections::SectionKind;
|
use crate::docstrings::sections::SectionKind;
|
||||||
|
|
|
@ -3,7 +3,7 @@ use strum::IntoEnumIterator;
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_whitespace::{UniversalNewlineIterator, UniversalNewlines};
|
use ruff_python_trivia::{UniversalNewlineIterator, UniversalNewlines};
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::docstrings::sections::SectionKind;
|
use crate::docstrings::sections::SectionKind;
|
||||||
|
|
|
@ -4,7 +4,7 @@ use ruff_diagnostics::{AlwaysAutofixableViolation, Violation};
|
||||||
use ruff_diagnostics::{Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::docstrings::{clean_space, leading_space};
|
use ruff_python_ast::docstrings::{clean_space, leading_space};
|
||||||
use ruff_python_whitespace::NewlineWithTrailingNewline;
|
use ruff_python_trivia::NewlineWithTrailingNewline;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::docstrings::Docstring;
|
use crate::docstrings::Docstring;
|
||||||
|
|
|
@ -5,7 +5,7 @@ use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::str::{is_triple_quote, leading_quote};
|
use ruff_python_ast::str::{is_triple_quote, leading_quote};
|
||||||
use ruff_python_semantic::{Definition, Member};
|
use ruff_python_semantic::{Definition, Member};
|
||||||
use ruff_python_whitespace::{NewlineWithTrailingNewline, UniversalNewlineIterator};
|
use ruff_python_trivia::{NewlineWithTrailingNewline, UniversalNewlineIterator};
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::docstrings::Docstring;
|
use crate::docstrings::Docstring;
|
||||||
|
|
|
@ -4,7 +4,7 @@ use rustpython_parser::ast::Ranged;
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::docstrings::clean_space;
|
use ruff_python_ast::docstrings::clean_space;
|
||||||
use ruff_python_whitespace::{NewlineWithTrailingNewline, UniversalNewlines};
|
use ruff_python_trivia::{NewlineWithTrailingNewline, UniversalNewlines};
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::docstrings::Docstring;
|
use crate::docstrings::Docstring;
|
||||||
|
|
|
@ -3,7 +3,7 @@ use rustpython_parser::ast::{self, Stmt};
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_semantic::{Definition, Member, MemberKind};
|
use ruff_python_semantic::{Definition, Member, MemberKind};
|
||||||
use ruff_python_whitespace::UniversalNewlines;
|
use ruff_python_trivia::UniversalNewlines;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::docstrings::Docstring;
|
use crate::docstrings::Docstring;
|
||||||
|
|
|
@ -2,7 +2,7 @@ use ruff_text_size::{TextLen, TextRange};
|
||||||
|
|
||||||
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
|
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_whitespace::NewlineWithTrailingNewline;
|
use ruff_python_trivia::NewlineWithTrailingNewline;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::docstrings::Docstring;
|
use crate::docstrings::Docstring;
|
||||||
|
|
|
@ -9,7 +9,7 @@ use ruff_python_ast::call_path::{from_qualified_name, CallPath};
|
||||||
use ruff_python_ast::cast;
|
use ruff_python_ast::cast;
|
||||||
use ruff_python_semantic::analyze::visibility::{is_property, is_test};
|
use ruff_python_semantic::analyze::visibility::{is_property, is_test};
|
||||||
use ruff_python_semantic::{Definition, Member, MemberKind};
|
use ruff_python_semantic::{Definition, Member, MemberKind};
|
||||||
use ruff_python_whitespace::UniversalNewlines;
|
use ruff_python_trivia::UniversalNewlines;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::docstrings::Docstring;
|
use crate::docstrings::Docstring;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
|
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::str::{leading_quote, trailing_quote};
|
use ruff_python_ast::str::{leading_quote, trailing_quote};
|
||||||
use ruff_python_whitespace::NewlineWithTrailingNewline;
|
use ruff_python_trivia::NewlineWithTrailingNewline;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::docstrings::Docstring;
|
use crate::docstrings::Docstring;
|
||||||
|
|
|
@ -13,7 +13,7 @@ use ruff_python_ast::docstrings::{clean_space, leading_space};
|
||||||
use ruff_python_ast::identifier::Identifier;
|
use ruff_python_ast::identifier::Identifier;
|
||||||
use ruff_python_semantic::analyze::visibility::is_staticmethod;
|
use ruff_python_semantic::analyze::visibility::is_staticmethod;
|
||||||
use ruff_python_semantic::{Definition, Member, MemberKind};
|
use ruff_python_semantic::{Definition, Member, MemberKind};
|
||||||
use ruff_python_whitespace::{NewlineWithTrailingNewline, PythonWhitespace};
|
use ruff_python_trivia::{NewlineWithTrailingNewline, PythonWhitespace};
|
||||||
use ruff_textwrap::dedent;
|
use ruff_textwrap::dedent;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
|
|
@ -5,7 +5,7 @@ use rustpython_parser::ast::{Expr, Ranged};
|
||||||
use ruff_diagnostics::Edit;
|
use ruff_diagnostics::Edit;
|
||||||
use ruff_python_ast::source_code::{Locator, Stylist};
|
use ruff_python_ast::source_code::{Locator, Stylist};
|
||||||
use ruff_python_semantic::Binding;
|
use ruff_python_semantic::Binding;
|
||||||
use ruff_python_whitespace::{SimpleTokenizer, TokenKind};
|
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
|
||||||
|
|
||||||
use crate::autofix::codemods::CodegenStylist;
|
use crate::autofix::codemods::CodegenStylist;
|
||||||
use crate::cst::matchers::{match_call_mut, match_dict, match_expression};
|
use crate::cst::matchers::{match_call_mut, match_dict, match_expression};
|
||||||
|
@ -102,7 +102,7 @@ pub(crate) fn remove_exception_handler_assignment(
|
||||||
let preceding = tokenizer
|
let preceding = tokenizer
|
||||||
.next_back()
|
.next_back()
|
||||||
.context("expected the exception name to be preceded by `as`")?;
|
.context("expected the exception name to be preceded by `as`")?;
|
||||||
debug_assert!(matches!(preceding.kind, TokenKind::As));
|
debug_assert!(matches!(preceding.kind, SimpleTokenKind::As));
|
||||||
|
|
||||||
// Lex to the end of the preceding token, which should be the exception value.
|
// Lex to the end of the preceding token, which should be the exception value.
|
||||||
let preceding = tokenizer
|
let preceding = tokenizer
|
||||||
|
@ -113,7 +113,7 @@ pub(crate) fn remove_exception_handler_assignment(
|
||||||
let following = SimpleTokenizer::starts_at(bound_exception.range.end(), locator.contents())
|
let following = SimpleTokenizer::starts_at(bound_exception.range.end(), locator.contents())
|
||||||
.next()
|
.next()
|
||||||
.context("expected the exception name to be followed by a colon")?;
|
.context("expected the exception name to be followed by a colon")?;
|
||||||
debug_assert!(matches!(following.kind, TokenKind::Colon));
|
debug_assert!(matches!(following.kind, SimpleTokenKind::Colon));
|
||||||
|
|
||||||
Ok(Edit::deletion(preceding.end(), following.start()))
|
Ok(Edit::deletion(preceding.end(), following.start()))
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ use ruff_text_size::{TextLen, TextRange, TextSize};
|
||||||
|
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_whitespace::Line;
|
use ruff_python_trivia::Line;
|
||||||
|
|
||||||
/// ## What it does
|
/// ## What it does
|
||||||
/// Check for `noqa` annotations that suppress all diagnostics, as opposed to
|
/// Check for `noqa` annotations that suppress all diagnostics, as opposed to
|
||||||
|
|
|
@ -5,7 +5,7 @@ use ruff_text_size::{TextLen, TextRange, TextSize};
|
||||||
|
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_whitespace::Line;
|
use ruff_python_trivia::Line;
|
||||||
|
|
||||||
/// ## What it does
|
/// ## What it does
|
||||||
/// Check for `type: ignore` annotations that suppress all type warnings, as
|
/// Check for `type: ignore` annotations that suppress all type warnings, as
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_whitespace::Line;
|
use ruff_python_trivia::Line;
|
||||||
|
|
||||||
const BIDI_UNICODE: [char; 10] = [
|
const BIDI_UNICODE: [char; 10] = [
|
||||||
'\u{202A}', //{LEFT-TO-RIGHT EMBEDDING}
|
'\u{202A}', //{LEFT-TO-RIGHT EMBEDDING}
|
||||||
|
|
|
@ -3,7 +3,7 @@ use regex::Regex;
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_whitespace::Line;
|
use ruff_python_trivia::Line;
|
||||||
|
|
||||||
/// ## What it does
|
/// ## What it does
|
||||||
/// Checks for unnecessary UTF-8 encoding declarations.
|
/// Checks for unnecessary UTF-8 encoding declarations.
|
||||||
|
|
|
@ -13,7 +13,7 @@ license = { workspace = true }
|
||||||
[lib]
|
[lib]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
ruff_python_whitespace = { path = "../ruff_python_whitespace" }
|
ruff_python_trivia = { path = "../ruff_python_trivia" }
|
||||||
ruff_text_size = { workspace = true }
|
ruff_text_size = { workspace = true }
|
||||||
|
|
||||||
anyhow = { workspace = true }
|
anyhow = { workspace = true }
|
||||||
|
|
|
@ -12,7 +12,7 @@ use rustpython_parser::ast::{
|
||||||
use rustpython_parser::{lexer, Mode, Tok};
|
use rustpython_parser::{lexer, Mode, Tok};
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
|
|
||||||
use ruff_python_whitespace::{is_python_whitespace, PythonWhitespace, UniversalNewlineIterator};
|
use ruff_python_trivia::{is_python_whitespace, PythonWhitespace, UniversalNewlineIterator};
|
||||||
|
|
||||||
use crate::call_path::CallPath;
|
use crate::call_path::CallPath;
|
||||||
use crate::source_code::{Indexer, Locator};
|
use crate::source_code::{Indexer, Locator};
|
||||||
|
|
|
@ -14,7 +14,7 @@ use ruff_text_size::{TextLen, TextRange, TextSize};
|
||||||
use rustpython_ast::{Alias, Arg, ArgWithDefault};
|
use rustpython_ast::{Alias, Arg, ArgWithDefault};
|
||||||
use rustpython_parser::ast::{self, ExceptHandler, Ranged, Stmt};
|
use rustpython_parser::ast::{self, ExceptHandler, Ranged, Stmt};
|
||||||
|
|
||||||
use ruff_python_whitespace::{is_python_whitespace, Cursor};
|
use ruff_python_trivia::{is_python_whitespace, Cursor};
|
||||||
|
|
||||||
use crate::source_code::Locator;
|
use crate::source_code::Locator;
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ use rustpython_parser::ast::{
|
||||||
TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, WithItem,
|
TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, WithItem,
|
||||||
};
|
};
|
||||||
|
|
||||||
use ruff_python_whitespace::LineEnding;
|
use ruff_python_trivia::LineEnding;
|
||||||
|
|
||||||
use crate::source_code::stylist::{Indentation, Quote, Stylist};
|
use crate::source_code::stylist::{Indentation, Quote, Stylist};
|
||||||
|
|
||||||
|
@ -1470,7 +1470,7 @@ mod tests {
|
||||||
use rustpython_ast::Stmt;
|
use rustpython_ast::Stmt;
|
||||||
use rustpython_parser::Parse;
|
use rustpython_parser::Parse;
|
||||||
|
|
||||||
use ruff_python_whitespace::LineEnding;
|
use ruff_python_trivia::LineEnding;
|
||||||
|
|
||||||
use crate::source_code::stylist::{Indentation, Quote};
|
use crate::source_code::stylist::{Indentation, Quote};
|
||||||
use crate::source_code::Generator;
|
use crate::source_code::Generator;
|
||||||
|
|
|
@ -6,7 +6,7 @@ use memchr::{memchr2, memrchr2};
|
||||||
use once_cell::unsync::OnceCell;
|
use once_cell::unsync::OnceCell;
|
||||||
use ruff_text_size::{TextLen, TextRange, TextSize};
|
use ruff_text_size::{TextLen, TextRange, TextSize};
|
||||||
|
|
||||||
use ruff_python_whitespace::find_newline;
|
use ruff_python_trivia::find_newline;
|
||||||
|
|
||||||
use crate::source_code::{LineIndex, OneIndexed, SourceCode, SourceLocation};
|
use crate::source_code::{LineIndex, OneIndexed, SourceCode, SourceLocation};
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ use std::fmt;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
use once_cell::unsync::OnceCell;
|
use once_cell::unsync::OnceCell;
|
||||||
use ruff_python_whitespace::{find_newline, LineEnding};
|
use ruff_python_trivia::{find_newline, LineEnding};
|
||||||
use rustpython_literal::escape::Quote as StrQuote;
|
use rustpython_literal::escape::Quote as StrQuote;
|
||||||
use rustpython_parser::lexer::LexResult;
|
use rustpython_parser::lexer::LexResult;
|
||||||
use rustpython_parser::Tok;
|
use rustpython_parser::Tok;
|
||||||
|
@ -166,7 +166,7 @@ mod tests {
|
||||||
use rustpython_parser::lexer::lex;
|
use rustpython_parser::lexer::lex;
|
||||||
use rustpython_parser::Mode;
|
use rustpython_parser::Mode;
|
||||||
|
|
||||||
use ruff_python_whitespace::{find_newline, LineEnding};
|
use ruff_python_trivia::{find_newline, LineEnding};
|
||||||
|
|
||||||
use crate::source_code::stylist::{Indentation, Quote};
|
use crate::source_code::stylist::{Indentation, Quote};
|
||||||
use crate::source_code::{Locator, Stylist};
|
use crate::source_code::{Locator, Stylist};
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use ruff_text_size::{TextRange, TextSize};
|
use ruff_text_size::{TextRange, TextSize};
|
||||||
use rustpython_parser::ast::Ranged;
|
use rustpython_parser::ast::Ranged;
|
||||||
|
|
||||||
use ruff_python_whitespace::is_python_whitespace;
|
use ruff_python_trivia::is_python_whitespace;
|
||||||
|
|
||||||
use crate::source_code::Locator;
|
use crate::source_code::Locator;
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ license = { workspace = true }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
ruff_formatter = { path = "../ruff_formatter" }
|
ruff_formatter = { path = "../ruff_formatter" }
|
||||||
ruff_python_whitespace = { path = "../ruff_python_whitespace" }
|
ruff_python_trivia = { path = "../ruff_python_trivia" }
|
||||||
ruff_python_ast = { path = "../ruff_python_ast" }
|
ruff_python_ast = { path = "../ruff_python_ast" }
|
||||||
ruff_text_size = { workspace = true }
|
ruff_text_size = { workspace = true }
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,8 @@ use ruff_text_size::{TextRange, TextSize};
|
||||||
use rustpython_parser::ast::Ranged;
|
use rustpython_parser::ast::Ranged;
|
||||||
|
|
||||||
use ruff_formatter::{format_args, write, Argument, Arguments};
|
use ruff_formatter::{format_args, write, Argument, Arguments};
|
||||||
use ruff_python_whitespace::{
|
use ruff_python_trivia::{
|
||||||
lines_after, skip_trailing_trivia, SimpleTokenizer, Token, TokenKind,
|
lines_after, skip_trailing_trivia, SimpleToken, SimpleTokenKind, SimpleTokenizer,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::context::NodeLevel;
|
use crate::context::NodeLevel;
|
||||||
|
@ -294,12 +294,12 @@ impl<'fmt, 'ast, 'buf> JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> {
|
||||||
)
|
)
|
||||||
.skip_trivia()
|
.skip_trivia()
|
||||||
// Skip over any closing parentheses belonging to the expression
|
// Skip over any closing parentheses belonging to the expression
|
||||||
.find(|token| token.kind() != TokenKind::RParen);
|
.find(|token| token.kind() != SimpleTokenKind::RParen);
|
||||||
|
|
||||||
matches!(
|
matches!(
|
||||||
first_token,
|
first_token,
|
||||||
Some(Token {
|
Some(SimpleToken {
|
||||||
kind: TokenKind::Comma,
|
kind: SimpleTokenKind::Comma,
|
||||||
..
|
..
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
|
@ -3,7 +3,7 @@ use rustpython_parser::ast::Ranged;
|
||||||
|
|
||||||
use ruff_formatter::{format_args, write, FormatError, SourceCode};
|
use ruff_formatter::{format_args, write, FormatError, SourceCode};
|
||||||
use ruff_python_ast::node::{AnyNodeRef, AstNode};
|
use ruff_python_ast::node::{AnyNodeRef, AstNode};
|
||||||
use ruff_python_whitespace::{lines_after, lines_before, skip_trailing_trivia};
|
use ruff_python_trivia::{lines_after, lines_before, skip_trailing_trivia};
|
||||||
|
|
||||||
use crate::comments::SourceComment;
|
use crate::comments::SourceComment;
|
||||||
use crate::context::NodeLevel;
|
use crate::context::NodeLevel;
|
||||||
|
|
|
@ -7,8 +7,8 @@ use rustpython_parser::ast::{Expr, ExprIfExp, ExprSlice, Ranged};
|
||||||
use ruff_python_ast::node::{AnyNodeRef, AstNode};
|
use ruff_python_ast::node::{AnyNodeRef, AstNode};
|
||||||
use ruff_python_ast::source_code::Locator;
|
use ruff_python_ast::source_code::Locator;
|
||||||
use ruff_python_ast::whitespace;
|
use ruff_python_ast::whitespace;
|
||||||
use ruff_python_whitespace::{
|
use ruff_python_trivia::{
|
||||||
first_non_trivia_token_rev, PythonWhitespace, SimpleTokenizer, Token, TokenKind,
|
first_non_trivia_token_rev, PythonWhitespace, SimpleToken, SimpleTokenKind, SimpleTokenizer,
|
||||||
UniversalNewlines,
|
UniversalNewlines,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -756,7 +756,7 @@ fn handle_trailing_end_of_line_condition_comment<'a>(
|
||||||
|
|
||||||
for token in tokens {
|
for token in tokens {
|
||||||
match token.kind() {
|
match token.kind() {
|
||||||
TokenKind::Colon => {
|
SimpleTokenKind::Colon => {
|
||||||
if comment.slice().start() > token.start() {
|
if comment.slice().start() > token.start() {
|
||||||
// Comment comes after the colon
|
// Comment comes after the colon
|
||||||
// ```python
|
// ```python
|
||||||
|
@ -775,10 +775,10 @@ fn handle_trailing_end_of_line_condition_comment<'a>(
|
||||||
// ```
|
// ```
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
TokenKind::RParen => {
|
SimpleTokenKind::RParen => {
|
||||||
// Skip over any closing parentheses
|
// Skip over any closing parentheses
|
||||||
}
|
}
|
||||||
TokenKind::Comma => {
|
SimpleTokenKind::Comma => {
|
||||||
// Skip over any trailing comma
|
// Skip over any trailing comma
|
||||||
}
|
}
|
||||||
kind => {
|
kind => {
|
||||||
|
@ -884,8 +884,8 @@ fn handle_trailing_binary_expression_left_or_operator_comment<'a>(
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut tokens = SimpleTokenizer::new(locator.contents(), between_operands_range).skip_trivia();
|
let mut tokens = SimpleTokenizer::new(locator.contents(), between_operands_range).skip_trivia();
|
||||||
let operator_offset = if let Some(non_r_paren) = tokens.find(|t| t.kind() != TokenKind::RParen)
|
let operator_offset =
|
||||||
{
|
if let Some(non_r_paren) = tokens.find(|t| t.kind() != SimpleTokenKind::RParen) {
|
||||||
non_r_paren.start()
|
non_r_paren.start()
|
||||||
} else {
|
} else {
|
||||||
return CommentPlacement::Default(comment);
|
return CommentPlacement::Default(comment);
|
||||||
|
@ -1061,8 +1061,8 @@ fn handle_slice_comments<'a>(
|
||||||
// Check for `foo[ # comment`, but only if they are on the same line
|
// Check for `foo[ # comment`, but only if they are on the same line
|
||||||
let after_lbracket = matches!(
|
let after_lbracket = matches!(
|
||||||
first_non_trivia_token_rev(comment.slice().start(), locator.contents()),
|
first_non_trivia_token_rev(comment.slice().start(), locator.contents()),
|
||||||
Some(Token {
|
Some(SimpleToken {
|
||||||
kind: TokenKind::LBracket,
|
kind: SimpleTokenKind::LBracket,
|
||||||
..
|
..
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
@ -1182,11 +1182,11 @@ fn handle_dict_unpacking_comment<'a>(
|
||||||
// we start from the preceding node but we skip its token
|
// we start from the preceding node but we skip its token
|
||||||
for token in tokens.by_ref() {
|
for token in tokens.by_ref() {
|
||||||
// Skip closing parentheses that are not part of the node range
|
// Skip closing parentheses that are not part of the node range
|
||||||
if token.kind == TokenKind::RParen {
|
if token.kind == SimpleTokenKind::RParen {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// The Keyword case
|
// The Keyword case
|
||||||
if token.kind == TokenKind::Star {
|
if token.kind == SimpleTokenKind::Star {
|
||||||
count += 1;
|
count += 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1194,8 +1194,8 @@ fn handle_dict_unpacking_comment<'a>(
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
matches!(
|
matches!(
|
||||||
token,
|
token,
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: TokenKind::LBrace | TokenKind::Comma | TokenKind::Colon,
|
kind: SimpleTokenKind::LBrace | SimpleTokenKind::Comma | SimpleTokenKind::Colon,
|
||||||
..
|
..
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
|
@ -1205,7 +1205,7 @@ fn handle_dict_unpacking_comment<'a>(
|
||||||
}
|
}
|
||||||
|
|
||||||
for token in tokens {
|
for token in tokens {
|
||||||
if token.kind != TokenKind::Star {
|
if token.kind != SimpleTokenKind::Star {
|
||||||
return CommentPlacement::Default(comment);
|
return CommentPlacement::Default(comment);
|
||||||
}
|
}
|
||||||
count += 1;
|
count += 1;
|
||||||
|
@ -1302,12 +1302,12 @@ fn handle_expr_if_comment<'a>(
|
||||||
let if_token = find_only_token_in_range(
|
let if_token = find_only_token_in_range(
|
||||||
TextRange::new(body.end(), test.start()),
|
TextRange::new(body.end(), test.start()),
|
||||||
locator,
|
locator,
|
||||||
TokenKind::If,
|
SimpleTokenKind::If,
|
||||||
);
|
);
|
||||||
let else_token = find_only_token_in_range(
|
let else_token = find_only_token_in_range(
|
||||||
TextRange::new(test.end(), orelse.start()),
|
TextRange::new(test.end(), orelse.start()),
|
||||||
locator,
|
locator,
|
||||||
TokenKind::Else,
|
SimpleTokenKind::Else,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Between `if` and `test`
|
// Between `if` and `test`
|
||||||
|
@ -1369,7 +1369,7 @@ fn handle_with_item_comment<'a>(
|
||||||
let as_token = find_only_token_in_range(
|
let as_token = find_only_token_in_range(
|
||||||
TextRange::new(context_expr.end(), optional_vars.start()),
|
TextRange::new(context_expr.end(), optional_vars.start()),
|
||||||
locator,
|
locator,
|
||||||
TokenKind::As,
|
SimpleTokenKind::As,
|
||||||
);
|
);
|
||||||
|
|
||||||
// If before the `as` keyword, then it must be a trailing comment of the context expression.
|
// If before the `as` keyword, then it must be a trailing comment of the context expression.
|
||||||
|
@ -1386,13 +1386,17 @@ fn handle_with_item_comment<'a>(
|
||||||
|
|
||||||
/// Looks for a token in the range that contains no other tokens except for parentheses outside
|
/// Looks for a token in the range that contains no other tokens except for parentheses outside
|
||||||
/// the expression ranges
|
/// the expression ranges
|
||||||
fn find_only_token_in_range(range: TextRange, locator: &Locator, token_kind: TokenKind) -> Token {
|
fn find_only_token_in_range(
|
||||||
|
range: TextRange,
|
||||||
|
locator: &Locator,
|
||||||
|
token_kind: SimpleTokenKind,
|
||||||
|
) -> SimpleToken {
|
||||||
let mut tokens = SimpleTokenizer::new(locator.contents(), range)
|
let mut tokens = SimpleTokenizer::new(locator.contents(), range)
|
||||||
.skip_trivia()
|
.skip_trivia()
|
||||||
.skip_while(|token| token.kind == TokenKind::RParen);
|
.skip_while(|token| token.kind == SimpleTokenKind::RParen);
|
||||||
let token = tokens.next().expect("Expected a token");
|
let token = tokens.next().expect("Expected a token");
|
||||||
debug_assert_eq!(token.kind(), token_kind);
|
debug_assert_eq!(token.kind(), token_kind);
|
||||||
let mut tokens = tokens.skip_while(|token| token.kind == TokenKind::LParen);
|
let mut tokens = tokens.skip_while(|token| token.kind == SimpleTokenKind::LParen);
|
||||||
debug_assert_eq!(tokens.next(), None);
|
debug_assert_eq!(tokens.next(), None);
|
||||||
token
|
token
|
||||||
}
|
}
|
||||||
|
@ -1446,7 +1450,7 @@ fn handle_comprehension_comment<'a>(
|
||||||
comprehension.iter.range().start(),
|
comprehension.iter.range().start(),
|
||||||
),
|
),
|
||||||
locator,
|
locator,
|
||||||
TokenKind::In,
|
SimpleTokenKind::In,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Comments between the target and the `in`
|
// Comments between the target and the `in`
|
||||||
|
@ -1509,7 +1513,7 @@ fn handle_comprehension_comment<'a>(
|
||||||
let if_token = find_only_token_in_range(
|
let if_token = find_only_token_in_range(
|
||||||
TextRange::new(last_end, if_node.range().start()),
|
TextRange::new(last_end, if_node.range().start()),
|
||||||
locator,
|
locator,
|
||||||
TokenKind::If,
|
SimpleTokenKind::If,
|
||||||
);
|
);
|
||||||
if is_own_line {
|
if is_own_line {
|
||||||
if last_end < comment.slice().start() && comment.slice().start() < if_token.start() {
|
if last_end < comment.slice().start() && comment.slice().start() < if_token.start() {
|
||||||
|
|
|
@ -13,7 +13,7 @@ use ruff_python_ast::source_code::{CommentRanges, Locator};
|
||||||
// pre-order.
|
// pre-order.
|
||||||
#[allow(clippy::wildcard_imports)]
|
#[allow(clippy::wildcard_imports)]
|
||||||
use ruff_python_ast::visitor::preorder::*;
|
use ruff_python_ast::visitor::preorder::*;
|
||||||
use ruff_python_whitespace::is_python_whitespace;
|
use ruff_python_trivia::is_python_whitespace;
|
||||||
|
|
||||||
use crate::comments::node_key::NodeRefEqualityKey;
|
use crate::comments::node_key::NodeRefEqualityKey;
|
||||||
use crate::comments::placement::place_comment;
|
use crate::comments::placement::place_comment;
|
||||||
|
|
|
@ -3,7 +3,7 @@ use rustpython_parser::ast::{Expr, ExprCall, Ranged};
|
||||||
|
|
||||||
use ruff_formatter::write;
|
use ruff_formatter::write;
|
||||||
use ruff_python_ast::node::AnyNodeRef;
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use ruff_python_whitespace::{SimpleTokenizer, TokenKind};
|
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
|
||||||
|
|
||||||
use crate::comments::dangling_comments;
|
use crate::comments::dangling_comments;
|
||||||
use crate::expression::expr_generator_exp::GeneratorExpParentheses;
|
use crate::expression::expr_generator_exp::GeneratorExpParentheses;
|
||||||
|
@ -132,14 +132,14 @@ fn is_single_argument_parenthesized(argument: &Expr, call_end: TextSize, source:
|
||||||
SimpleTokenizer::new(source, TextRange::new(argument.end(), call_end)).skip_trivia()
|
SimpleTokenizer::new(source, TextRange::new(argument.end(), call_end)).skip_trivia()
|
||||||
{
|
{
|
||||||
match token.kind() {
|
match token.kind() {
|
||||||
TokenKind::RParen => {
|
SimpleTokenKind::RParen => {
|
||||||
if has_seen_r_paren {
|
if has_seen_r_paren {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
has_seen_r_paren = true;
|
has_seen_r_paren = true;
|
||||||
}
|
}
|
||||||
// Skip over any trailing comma
|
// Skip over any trailing comma
|
||||||
TokenKind::Comma => continue,
|
SimpleTokenKind::Comma => continue,
|
||||||
_ => {
|
_ => {
|
||||||
// Passed the arguments
|
// Passed the arguments
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -5,7 +5,7 @@ use rustpython_parser::ast::{Expr, Ranged};
|
||||||
use ruff_formatter::prelude::{hard_line_break, line_suffix_boundary, space, text};
|
use ruff_formatter::prelude::{hard_line_break, line_suffix_boundary, space, text};
|
||||||
use ruff_formatter::{write, Buffer, Format, FormatError, FormatResult};
|
use ruff_formatter::{write, Buffer, Format, FormatError, FormatResult};
|
||||||
use ruff_python_ast::node::{AnyNodeRef, AstNode};
|
use ruff_python_ast::node::{AnyNodeRef, AstNode};
|
||||||
use ruff_python_whitespace::{SimpleTokenizer, Token, TokenKind};
|
use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer};
|
||||||
|
|
||||||
use crate::comments::{dangling_comments, SourceComment};
|
use crate::comments::{dangling_comments, SourceComment};
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
|
@ -158,17 +158,17 @@ pub(crate) fn find_colons(
|
||||||
range: TextRange,
|
range: TextRange,
|
||||||
lower: &Option<Box<Expr>>,
|
lower: &Option<Box<Expr>>,
|
||||||
upper: &Option<Box<Expr>>,
|
upper: &Option<Box<Expr>>,
|
||||||
) -> FormatResult<(Token, Option<Token>)> {
|
) -> FormatResult<(SimpleToken, Option<SimpleToken>)> {
|
||||||
let after_lower = lower
|
let after_lower = lower
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or(range.start(), |lower| lower.range().end());
|
.map_or(range.start(), |lower| lower.range().end());
|
||||||
let mut tokens = SimpleTokenizer::new(contents, TextRange::new(after_lower, range.end()))
|
let mut tokens = SimpleTokenizer::new(contents, TextRange::new(after_lower, range.end()))
|
||||||
.skip_trivia()
|
.skip_trivia()
|
||||||
.skip_while(|token| token.kind == TokenKind::RParen);
|
.skip_while(|token| token.kind == SimpleTokenKind::RParen);
|
||||||
let first_colon = tokens.next().ok_or(FormatError::syntax_error(
|
let first_colon = tokens.next().ok_or(FormatError::syntax_error(
|
||||||
"Din't find any token for slice first colon",
|
"Din't find any token for slice first colon",
|
||||||
))?;
|
))?;
|
||||||
if first_colon.kind != TokenKind::Colon {
|
if first_colon.kind != SimpleTokenKind::Colon {
|
||||||
return Err(FormatError::syntax_error(
|
return Err(FormatError::syntax_error(
|
||||||
"slice first colon token was not a colon",
|
"slice first colon token was not a colon",
|
||||||
));
|
));
|
||||||
|
@ -179,9 +179,9 @@ pub(crate) fn find_colons(
|
||||||
.map_or(first_colon.end(), |upper| upper.range().end());
|
.map_or(first_colon.end(), |upper| upper.range().end());
|
||||||
let mut tokens = SimpleTokenizer::new(contents, TextRange::new(after_upper, range.end()))
|
let mut tokens = SimpleTokenizer::new(contents, TextRange::new(after_upper, range.end()))
|
||||||
.skip_trivia()
|
.skip_trivia()
|
||||||
.skip_while(|token| token.kind == TokenKind::RParen);
|
.skip_while(|token| token.kind == SimpleTokenKind::RParen);
|
||||||
let second_colon = if let Some(token) = tokens.next() {
|
let second_colon = if let Some(token) = tokens.next() {
|
||||||
if token.kind != TokenKind::Colon {
|
if token.kind != SimpleTokenKind::Colon {
|
||||||
return Err(FormatError::syntax_error(
|
return Err(FormatError::syntax_error(
|
||||||
"Expected a colon for the second colon token",
|
"Expected a colon for the second colon token",
|
||||||
));
|
));
|
||||||
|
|
|
@ -5,7 +5,7 @@ use rustpython_parser::ast::{ExprUnaryOp, Ranged};
|
||||||
use ruff_formatter::prelude::{hard_line_break, space, text};
|
use ruff_formatter::prelude::{hard_line_break, space, text};
|
||||||
use ruff_formatter::{Format, FormatContext, FormatResult};
|
use ruff_formatter::{Format, FormatContext, FormatResult};
|
||||||
use ruff_python_ast::node::AnyNodeRef;
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use ruff_python_whitespace::{SimpleTokenizer, TokenKind};
|
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
|
||||||
|
|
||||||
use crate::comments::trailing_comments;
|
use crate::comments::trailing_comments;
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
|
@ -97,7 +97,7 @@ fn is_operand_parenthesized(unary: &ExprUnaryOp, source: &str) -> bool {
|
||||||
.skip_trivia()
|
.skip_trivia()
|
||||||
.next()
|
.next()
|
||||||
{
|
{
|
||||||
debug_assert_eq!(token.kind(), TokenKind::LParen);
|
debug_assert_eq!(token.kind(), SimpleTokenKind::LParen);
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
|
|
|
@ -3,7 +3,7 @@ use rustpython_parser::ast::Ranged;
|
||||||
use ruff_formatter::prelude::tag::Condition;
|
use ruff_formatter::prelude::tag::Condition;
|
||||||
use ruff_formatter::{format_args, write, Argument, Arguments};
|
use ruff_formatter::{format_args, write, Argument, Arguments};
|
||||||
use ruff_python_ast::node::AnyNodeRef;
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use ruff_python_whitespace::{first_non_trivia_token, SimpleTokenizer, Token, TokenKind};
|
use ruff_python_trivia::{first_non_trivia_token, SimpleToken, SimpleTokenKind, SimpleTokenizer};
|
||||||
|
|
||||||
use crate::context::NodeLevel;
|
use crate::context::NodeLevel;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
@ -77,8 +77,8 @@ pub(crate) fn is_expression_parenthesized(expr: AnyNodeRef, contents: &str) -> b
|
||||||
// First test if there's a closing parentheses because it tends to be cheaper.
|
// First test if there's a closing parentheses because it tends to be cheaper.
|
||||||
if matches!(
|
if matches!(
|
||||||
first_non_trivia_token(expr.end(), contents),
|
first_non_trivia_token(expr.end(), contents),
|
||||||
Some(Token {
|
Some(SimpleToken {
|
||||||
kind: TokenKind::RParen,
|
kind: SimpleTokenKind::RParen,
|
||||||
..
|
..
|
||||||
})
|
})
|
||||||
) {
|
) {
|
||||||
|
@ -87,8 +87,8 @@ pub(crate) fn is_expression_parenthesized(expr: AnyNodeRef, contents: &str) -> b
|
||||||
|
|
||||||
matches!(
|
matches!(
|
||||||
tokenizer.next_back(),
|
tokenizer.next_back(),
|
||||||
Some(Token {
|
Some(SimpleToken {
|
||||||
kind: TokenKind::LParen,
|
kind: SimpleTokenKind::LParen,
|
||||||
..
|
..
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
|
@ -5,7 +5,7 @@ use rustpython_parser::ast::{Arguments, Ranged};
|
||||||
|
|
||||||
use ruff_formatter::{format_args, write, FormatRuleWithOptions};
|
use ruff_formatter::{format_args, write, FormatRuleWithOptions};
|
||||||
use ruff_python_ast::node::{AnyNodeRef, AstNode};
|
use ruff_python_ast::node::{AnyNodeRef, AstNode};
|
||||||
use ruff_python_whitespace::{first_non_trivia_token, SimpleTokenizer, Token, TokenKind};
|
use ruff_python_trivia::{first_non_trivia_token, SimpleToken, SimpleTokenKind, SimpleTokenizer};
|
||||||
|
|
||||||
use crate::comments::{
|
use crate::comments::{
|
||||||
dangling_comments, leading_comments, leading_node_comments, trailing_comments,
|
dangling_comments, leading_comments, leading_node_comments, trailing_comments,
|
||||||
|
@ -166,17 +166,17 @@ impl FormatNodeRule<Arguments> for FormatArguments {
|
||||||
.skip_trivia();
|
.skip_trivia();
|
||||||
|
|
||||||
let comma = tokens.next();
|
let comma = tokens.next();
|
||||||
assert!(matches!(comma, Some(Token { kind: TokenKind::Comma, .. })), "The last positional only argument must be separated by a `,` from the positional only arguments separator `/` but found '{comma:?}'.");
|
assert!(matches!(comma, Some(SimpleToken { kind: SimpleTokenKind::Comma, .. })), "The last positional only argument must be separated by a `,` from the positional only arguments separator `/` but found '{comma:?}'.");
|
||||||
|
|
||||||
let slash = tokens.next();
|
let slash = tokens.next();
|
||||||
assert!(matches!(slash, Some(Token { kind: TokenKind::Slash, .. })), "The positional argument separator must be present for a function that has positional only arguments but found '{slash:?}'.");
|
assert!(matches!(slash, Some(SimpleToken { kind: SimpleTokenKind::Slash, .. })), "The positional argument separator must be present for a function that has positional only arguments but found '{slash:?}'.");
|
||||||
|
|
||||||
tokens.next()
|
tokens.next()
|
||||||
} else {
|
} else {
|
||||||
first_non_trivia_token(last_node.end(), f.context().source())
|
first_non_trivia_token(last_node.end(), f.context().source())
|
||||||
};
|
};
|
||||||
|
|
||||||
if maybe_comma_token.map_or(false, |token| token.kind() == TokenKind::Comma) {
|
if maybe_comma_token.map_or(false, |token| token.kind() == SimpleTokenKind::Comma) {
|
||||||
write!(f, [hard_line_break()])?;
|
write!(f, [hard_line_break()])?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -298,11 +298,11 @@ pub(crate) fn find_argument_separators(
|
||||||
let comma = tokens
|
let comma = tokens
|
||||||
.next()
|
.next()
|
||||||
.expect("The function definition can't end here");
|
.expect("The function definition can't end here");
|
||||||
debug_assert!(comma.kind() == TokenKind::Comma, "{comma:?}");
|
debug_assert!(comma.kind() == SimpleTokenKind::Comma, "{comma:?}");
|
||||||
let slash = tokens
|
let slash = tokens
|
||||||
.next()
|
.next()
|
||||||
.expect("The function definition can't end here");
|
.expect("The function definition can't end here");
|
||||||
debug_assert!(slash.kind() == TokenKind::Slash, "{slash:?}");
|
debug_assert!(slash.kind() == SimpleTokenKind::Slash, "{slash:?}");
|
||||||
|
|
||||||
Some((preceding_end, slash.range))
|
Some((preceding_end, slash.range))
|
||||||
} else {
|
} else {
|
||||||
|
@ -331,11 +331,11 @@ pub(crate) fn find_argument_separators(
|
||||||
let comma = tokens
|
let comma = tokens
|
||||||
.next()
|
.next()
|
||||||
.expect("The function definition can't end here");
|
.expect("The function definition can't end here");
|
||||||
debug_assert!(comma.kind() == TokenKind::Comma, "{comma:?}");
|
debug_assert!(comma.kind() == SimpleTokenKind::Comma, "{comma:?}");
|
||||||
let star = tokens
|
let star = tokens
|
||||||
.next()
|
.next()
|
||||||
.expect("The function definition can't end here");
|
.expect("The function definition can't end here");
|
||||||
debug_assert!(star.kind() == TokenKind::Star, "{star:?}");
|
debug_assert!(star.kind() == SimpleTokenKind::Star, "{star:?}");
|
||||||
|
|
||||||
Some(ArgumentSeparator {
|
Some(ArgumentSeparator {
|
||||||
preceding_end,
|
preceding_end,
|
||||||
|
@ -348,11 +348,11 @@ pub(crate) fn find_argument_separators(
|
||||||
let lparen = tokens
|
let lparen = tokens
|
||||||
.next()
|
.next()
|
||||||
.expect("The function definition can't end here");
|
.expect("The function definition can't end here");
|
||||||
debug_assert!(lparen.kind() == TokenKind::LParen, "{lparen:?}");
|
debug_assert!(lparen.kind() == SimpleTokenKind::LParen, "{lparen:?}");
|
||||||
let star = tokens
|
let star = tokens
|
||||||
.next()
|
.next()
|
||||||
.expect("The function definition can't end here");
|
.expect("The function definition can't end here");
|
||||||
debug_assert!(star.kind() == TokenKind::Star, "{star:?}");
|
debug_assert!(star.kind() == SimpleTokenKind::Star, "{star:?}");
|
||||||
Some(ArgumentSeparator {
|
Some(ArgumentSeparator {
|
||||||
preceding_end: arguments.range.start(),
|
preceding_end: arguments.range.start(),
|
||||||
separator: star.range,
|
separator: star.range,
|
||||||
|
|
|
@ -2,7 +2,7 @@ use ruff_text_size::TextRange;
|
||||||
use rustpython_parser::ast::{Ranged, StmtClassDef};
|
use rustpython_parser::ast::{Ranged, StmtClassDef};
|
||||||
|
|
||||||
use ruff_formatter::write;
|
use ruff_formatter::write;
|
||||||
use ruff_python_whitespace::{SimpleTokenizer, TokenKind};
|
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
|
||||||
|
|
||||||
use crate::comments::trailing_comments;
|
use crate::comments::trailing_comments;
|
||||||
use crate::expression::parentheses::{parenthesized, Parentheses};
|
use crate::expression::parentheses::{parenthesized, Parentheses};
|
||||||
|
@ -97,7 +97,7 @@ impl Format<PyFormatContext<'_>> for FormatInheritanceClause<'_> {
|
||||||
.skip_trivia();
|
.skip_trivia();
|
||||||
|
|
||||||
let left_paren_count = tokenizer
|
let left_paren_count = tokenizer
|
||||||
.take_while(|token| token.kind() == TokenKind::LParen)
|
.take_while(|token| token.kind() == SimpleTokenKind::LParen)
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
// Ignore the first parentheses count
|
// Ignore the first parentheses count
|
||||||
|
|
|
@ -2,7 +2,7 @@ use rustpython_parser::ast::{Ranged, StmtFunctionDef};
|
||||||
|
|
||||||
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule};
|
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule};
|
||||||
use ruff_python_ast::function::AnyFunctionDefinition;
|
use ruff_python_ast::function::AnyFunctionDefinition;
|
||||||
use ruff_python_whitespace::{lines_after, skip_trailing_trivia};
|
use ruff_python_trivia::{lines_after, skip_trailing_trivia};
|
||||||
|
|
||||||
use crate::comments::{leading_comments, trailing_comments};
|
use crate::comments::{leading_comments, trailing_comments};
|
||||||
use crate::context::NodeLevel;
|
use crate::context::NodeLevel;
|
||||||
|
|
|
@ -3,7 +3,7 @@ use rustpython_parser::ast::{Ranged, StmtAsyncWith, StmtWith, Suite, WithItem};
|
||||||
|
|
||||||
use ruff_formatter::{format_args, write, FormatError};
|
use ruff_formatter::{format_args, write, FormatError};
|
||||||
use ruff_python_ast::node::AnyNodeRef;
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use ruff_python_whitespace::{SimpleTokenizer, TokenKind};
|
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
|
||||||
|
|
||||||
use crate::comments::trailing_comments;
|
use crate::comments::trailing_comments;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{
|
||||||
|
@ -125,7 +125,7 @@ fn are_with_items_parenthesized(
|
||||||
|
|
||||||
let mut tokenizer = SimpleTokenizer::new(context.source(), before_first_with_item)
|
let mut tokenizer = SimpleTokenizer::new(context.source(), before_first_with_item)
|
||||||
.skip_trivia()
|
.skip_trivia()
|
||||||
.skip_while(|t| t.kind() == TokenKind::Async);
|
.skip_while(|t| t.kind() == SimpleTokenKind::Async);
|
||||||
|
|
||||||
let with_keyword = tokenizer.next().ok_or(FormatError::syntax_error(
|
let with_keyword = tokenizer.next().ok_or(FormatError::syntax_error(
|
||||||
"Expected a with keyword, didn't find any token",
|
"Expected a with keyword, didn't find any token",
|
||||||
|
@ -133,13 +133,13 @@ fn are_with_items_parenthesized(
|
||||||
|
|
||||||
debug_assert_eq!(
|
debug_assert_eq!(
|
||||||
with_keyword.kind(),
|
with_keyword.kind(),
|
||||||
TokenKind::With,
|
SimpleTokenKind::With,
|
||||||
"Expected with keyword but at {with_keyword:?}"
|
"Expected with keyword but at {with_keyword:?}"
|
||||||
);
|
);
|
||||||
|
|
||||||
match tokenizer.next() {
|
match tokenizer.next() {
|
||||||
Some(left_paren) => {
|
Some(left_paren) => {
|
||||||
debug_assert_eq!(left_paren.kind(), TokenKind::LParen);
|
debug_assert_eq!(left_paren.kind(), SimpleTokenKind::LParen);
|
||||||
Ok(true)
|
Ok(true)
|
||||||
}
|
}
|
||||||
None => Ok(false),
|
None => Ok(false),
|
||||||
|
|
|
@ -3,7 +3,7 @@ use rustpython_parser::ast::{Ranged, Stmt, Suite};
|
||||||
use ruff_formatter::{
|
use ruff_formatter::{
|
||||||
format_args, write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions,
|
format_args, write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions,
|
||||||
};
|
};
|
||||||
use ruff_python_whitespace::lines_before;
|
use ruff_python_trivia::lines_before;
|
||||||
|
|
||||||
use crate::context::NodeLevel;
|
use crate::context::NodeLevel;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
[package]
|
[package]
|
||||||
name = "ruff_python_whitespace"
|
name = "ruff_python_trivia"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
publish = false
|
publish = false
|
||||||
authors = { workspace = true }
|
authors = { workspace = true }
|
|
@ -22,7 +22,7 @@ impl UniversalNewlines for str {
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use ruff_text_size::TextSize;
|
/// # use ruff_text_size::TextSize;
|
||||||
/// # use ruff_python_whitespace::{Line, UniversalNewlineIterator};
|
/// # use ruff_python_trivia::{Line, UniversalNewlineIterator};
|
||||||
/// let mut lines = UniversalNewlineIterator::from("foo\nbar\n\r\nbaz\rbop");
|
/// let mut lines = UniversalNewlineIterator::from("foo\nbar\n\r\nbaz\rbop");
|
||||||
///
|
///
|
||||||
/// assert_eq!(lines.next_back(), Some(Line::new("bop", TextSize::from(14))));
|
/// assert_eq!(lines.next_back(), Some(Line::new("bop", TextSize::from(14))));
|
|
@ -1,217 +1,217 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_whitespace/src/tokenizer.rs
|
source: crates/ruff_python_trivia/src/tokenizer.rs
|
||||||
expression: test_case.tokenize_reverse()
|
expression: test_case.tokenize_reverse()
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: RParen,
|
kind: RParen,
|
||||||
range: 52..53,
|
range: 52..53,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Other,
|
kind: Other,
|
||||||
range: 51..52,
|
range: 51..52,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 50..51,
|
range: 50..51,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 49..50,
|
range: 49..50,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 48..49,
|
range: 48..49,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 47..48,
|
range: 47..48,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 46..47,
|
range: 46..47,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 45..46,
|
range: 45..46,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 44..45,
|
range: 44..45,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 43..44,
|
range: 43..44,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 42..43,
|
range: 42..43,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 41..42,
|
range: 41..42,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 40..41,
|
range: 40..41,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 39..40,
|
range: 39..40,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 38..39,
|
range: 38..39,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 37..38,
|
range: 37..38,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 36..37,
|
range: 36..37,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 35..36,
|
range: 35..36,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 34..35,
|
range: 34..35,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 33..34,
|
range: 33..34,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 32..33,
|
range: 32..33,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 31..32,
|
range: 31..32,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 30..31,
|
range: 30..31,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 29..30,
|
range: 29..30,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 28..29,
|
range: 28..29,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 27..28,
|
range: 27..28,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 26..27,
|
range: 26..27,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 25..26,
|
range: 25..26,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 24..25,
|
range: 24..25,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 23..24,
|
range: 23..24,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 22..23,
|
range: 22..23,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 21..22,
|
range: 21..22,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 20..21,
|
range: 20..21,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 19..20,
|
range: 19..20,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 18..19,
|
range: 18..19,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 17..18,
|
range: 17..18,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 16..17,
|
range: 16..17,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 15..16,
|
range: 15..16,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 14..15,
|
range: 14..15,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 13..14,
|
range: 13..14,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 12..13,
|
range: 12..13,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 11..12,
|
range: 11..12,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 10..11,
|
range: 10..11,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 9..10,
|
range: 9..10,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 8..9,
|
range: 8..9,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 7..8,
|
range: 7..8,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 6..7,
|
range: 6..7,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 5..6,
|
range: 5..6,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 4..5,
|
range: 4..5,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 3..4,
|
range: 3..4,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 2..3,
|
range: 2..3,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 1..2,
|
range: 1..2,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 0..1,
|
range: 0..1,
|
||||||
},
|
},
|
|
@ -1,9 +1,9 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_whitespace/src/tokenizer.rs
|
source: crates/ruff_python_trivia/src/tokenizer.rs
|
||||||
expression: test_case.tokens()
|
expression: test_case.tokens()
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Other,
|
kind: Other,
|
||||||
range: 0..2,
|
range: 0..2,
|
||||||
},
|
},
|
|
@ -1,17 +1,17 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_whitespace/src/tokenizer.rs
|
source: crates/ruff_python_trivia/src/tokenizer.rs
|
||||||
expression: test_case.tokens()
|
expression: test_case.tokens()
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Other,
|
kind: Other,
|
||||||
range: 0..1,
|
range: 0..1,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 1..2,
|
range: 1..2,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 2..3,
|
range: 2..3,
|
||||||
},
|
},
|
|
@ -1,125 +1,125 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_whitespace/src/tokenizer.rs
|
source: crates/ruff_python_trivia/src/tokenizer.rs
|
||||||
expression: test_case.tokens()
|
expression: test_case.tokens()
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Comment,
|
kind: Comment,
|
||||||
range: 0..17,
|
range: 0..17,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Newline,
|
kind: Newline,
|
||||||
range: 17..18,
|
range: 17..18,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Whitespace,
|
kind: Whitespace,
|
||||||
range: 18..26,
|
range: 18..26,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Other,
|
kind: Other,
|
||||||
range: 26..27,
|
range: 26..27,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 27..28,
|
range: 27..28,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 28..29,
|
range: 28..29,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 29..30,
|
range: 29..30,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 30..31,
|
range: 30..31,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 31..32,
|
range: 31..32,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 32..33,
|
range: 32..33,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 33..34,
|
range: 33..34,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 34..35,
|
range: 34..35,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 35..36,
|
range: 35..36,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 36..37,
|
range: 36..37,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 37..38,
|
range: 37..38,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 38..39,
|
range: 38..39,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 39..40,
|
range: 39..40,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 40..41,
|
range: 40..41,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 41..42,
|
range: 41..42,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 42..43,
|
range: 42..43,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 43..44,
|
range: 43..44,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 44..45,
|
range: 44..45,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 45..46,
|
range: 45..46,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 46..47,
|
range: 46..47,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 47..48,
|
range: 47..48,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 48..49,
|
range: 48..49,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 49..50,
|
range: 49..50,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 50..51,
|
range: 50..51,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 51..52,
|
range: 51..52,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Bogus,
|
kind: Bogus,
|
||||||
range: 52..53,
|
range: 52..53,
|
||||||
},
|
},
|
|
@ -1,21 +1,21 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_whitespace/src/tokenizer.rs
|
source: crates/ruff_python_trivia/src/tokenizer.rs
|
||||||
expression: test_case.tokens()
|
expression: test_case.tokens()
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Comma,
|
kind: Comma,
|
||||||
range: 0..1,
|
range: 0..1,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Comma,
|
kind: Comma,
|
||||||
range: 1..2,
|
range: 1..2,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Comma,
|
kind: Comma,
|
||||||
range: 2..3,
|
range: 2..3,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Comma,
|
kind: Comma,
|
||||||
range: 3..4,
|
range: 3..4,
|
||||||
},
|
},
|
|
@ -1,29 +1,29 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_whitespace/src/tokenizer.rs
|
source: crates/ruff_python_trivia/src/tokenizer.rs
|
||||||
expression: test_case.tokens()
|
expression: test_case.tokens()
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: LParen,
|
kind: LParen,
|
||||||
range: 0..1,
|
range: 0..1,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Whitespace,
|
kind: Whitespace,
|
||||||
range: 1..2,
|
range: 1..2,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Continuation,
|
kind: Continuation,
|
||||||
range: 2..3,
|
range: 2..3,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Newline,
|
kind: Newline,
|
||||||
range: 3..4,
|
range: 3..4,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Whitespace,
|
kind: Whitespace,
|
||||||
range: 4..5,
|
range: 4..5,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: RParen,
|
kind: RParen,
|
||||||
range: 5..6,
|
range: 5..6,
|
||||||
},
|
},
|
|
@ -1,33 +1,33 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_whitespace/src/tokenizer.rs
|
source: crates/ruff_python_trivia/src/tokenizer.rs
|
||||||
expression: test_case.tokens()
|
expression: test_case.tokens()
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: If,
|
kind: If,
|
||||||
range: 0..2,
|
range: 0..2,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Whitespace,
|
kind: Whitespace,
|
||||||
range: 2..3,
|
range: 2..3,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: In,
|
kind: In,
|
||||||
range: 3..5,
|
range: 3..5,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Whitespace,
|
kind: Whitespace,
|
||||||
range: 5..6,
|
range: 5..6,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Else,
|
kind: Else,
|
||||||
range: 6..10,
|
range: 6..10,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Whitespace,
|
kind: Whitespace,
|
||||||
range: 10..11,
|
range: 10..11,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Match,
|
kind: Match,
|
||||||
range: 11..16,
|
range: 11..16,
|
||||||
},
|
},
|
|
@ -1,29 +1,29 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_whitespace/src/tokenizer.rs
|
source: crates/ruff_python_trivia/src/tokenizer.rs
|
||||||
expression: test_case.tokens()
|
expression: test_case.tokens()
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: LParen,
|
kind: LParen,
|
||||||
range: 0..1,
|
range: 0..1,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: LBracket,
|
kind: LBracket,
|
||||||
range: 1..2,
|
range: 1..2,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: LBrace,
|
kind: LBrace,
|
||||||
range: 2..3,
|
range: 2..3,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: RBrace,
|
kind: RBrace,
|
||||||
range: 3..4,
|
range: 3..4,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: RBracket,
|
kind: RBracket,
|
||||||
range: 4..5,
|
range: 4..5,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: RParen,
|
kind: RParen,
|
||||||
range: 5..6,
|
range: 5..6,
|
||||||
},
|
},
|
|
@ -1,41 +1,41 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_whitespace/src/tokenizer.rs
|
source: crates/ruff_python_trivia/src/tokenizer.rs
|
||||||
expression: test_case.tokens()
|
expression: test_case.tokens()
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Whitespace,
|
kind: Whitespace,
|
||||||
range: 0..1,
|
range: 0..1,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Comment,
|
kind: Comment,
|
||||||
range: 1..30,
|
range: 1..30,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Newline,
|
kind: Newline,
|
||||||
range: 30..31,
|
range: 30..31,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Whitespace,
|
kind: Whitespace,
|
||||||
range: 31..39,
|
range: 31..39,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Comment,
|
kind: Comment,
|
||||||
range: 39..77,
|
range: 39..77,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Newline,
|
kind: Newline,
|
||||||
range: 77..78,
|
range: 77..78,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Whitespace,
|
kind: Whitespace,
|
||||||
range: 78..86,
|
range: 78..86,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Comma,
|
kind: Comma,
|
||||||
range: 86..87,
|
range: 86..87,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Slash,
|
kind: Slash,
|
||||||
range: 87..88,
|
range: 87..88,
|
||||||
},
|
},
|
|
@ -1,17 +1,17 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_whitespace/src/tokenizer.rs
|
source: crates/ruff_python_trivia/src/tokenizer.rs
|
||||||
expression: test_case.tokens()
|
expression: test_case.tokens()
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: RParen,
|
kind: RParen,
|
||||||
range: 14..15,
|
range: 14..15,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Whitespace,
|
kind: Whitespace,
|
||||||
range: 15..16,
|
range: 15..16,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Comment,
|
kind: Comment,
|
||||||
range: 16..25,
|
range: 16..25,
|
||||||
},
|
},
|
|
@ -1,21 +1,21 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_whitespace/src/tokenizer.rs
|
source: crates/ruff_python_trivia/src/tokenizer.rs
|
||||||
expression: test_case.tokens()
|
expression: test_case.tokens()
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Comment,
|
kind: Comment,
|
||||||
range: 0..9,
|
range: 0..9,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Newline,
|
kind: Newline,
|
||||||
range: 9..10,
|
range: 9..10,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Whitespace,
|
kind: Whitespace,
|
||||||
range: 10..14,
|
range: 10..14,
|
||||||
},
|
},
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Comment,
|
kind: Comment,
|
||||||
range: 14..23,
|
range: 14..23,
|
||||||
},
|
},
|
|
@ -1,9 +1,9 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_whitespace/src/tokenizer.rs
|
source: crates/ruff_python_trivia/src/tokenizer.rs
|
||||||
expression: test_case.tokens()
|
expression: test_case.tokens()
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
Token {
|
SimpleToken {
|
||||||
kind: Other,
|
kind: Other,
|
||||||
range: 0..6,
|
range: 0..6,
|
||||||
},
|
},
|
|
@ -12,7 +12,7 @@ use crate::{is_python_whitespace, Cursor};
|
||||||
/// of the character, the second item the non-trivia character.
|
/// of the character, the second item the non-trivia character.
|
||||||
///
|
///
|
||||||
/// Returns `None` if the range is empty or only contains trivia (whitespace or comments).
|
/// Returns `None` if the range is empty or only contains trivia (whitespace or comments).
|
||||||
pub fn first_non_trivia_token(offset: TextSize, code: &str) -> Option<Token> {
|
pub fn first_non_trivia_token(offset: TextSize, code: &str) -> Option<SimpleToken> {
|
||||||
SimpleTokenizer::starts_at(offset, code)
|
SimpleTokenizer::starts_at(offset, code)
|
||||||
.skip_trivia()
|
.skip_trivia()
|
||||||
.next()
|
.next()
|
||||||
|
@ -24,7 +24,7 @@ pub fn first_non_trivia_token(offset: TextSize, code: &str) -> Option<Token> {
|
||||||
/// ## Notes
|
/// ## Notes
|
||||||
///
|
///
|
||||||
/// Prefer [`first_non_trivia_token`] whenever possible because reverse lookup is expensive because of comments.
|
/// Prefer [`first_non_trivia_token`] whenever possible because reverse lookup is expensive because of comments.
|
||||||
pub fn first_non_trivia_token_rev(offset: TextSize, code: &str) -> Option<Token> {
|
pub fn first_non_trivia_token_rev(offset: TextSize, code: &str) -> Option<SimpleToken> {
|
||||||
SimpleTokenizer::up_to(offset, code)
|
SimpleTokenizer::up_to(offset, code)
|
||||||
.skip_trivia()
|
.skip_trivia()
|
||||||
.next_back()
|
.next_back()
|
||||||
|
@ -37,10 +37,10 @@ pub fn lines_before(offset: TextSize, code: &str) -> u32 {
|
||||||
|
|
||||||
for token in tokens.rev() {
|
for token in tokens.rev() {
|
||||||
match token.kind() {
|
match token.kind() {
|
||||||
TokenKind::Newline => {
|
SimpleTokenKind::Newline => {
|
||||||
newlines += 1;
|
newlines += 1;
|
||||||
}
|
}
|
||||||
TokenKind::Whitespace => {
|
SimpleTokenKind::Whitespace => {
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -59,10 +59,10 @@ pub fn lines_after(offset: TextSize, code: &str) -> u32 {
|
||||||
|
|
||||||
for token in tokens {
|
for token in tokens {
|
||||||
match token.kind() {
|
match token.kind() {
|
||||||
TokenKind::Newline => {
|
SimpleTokenKind::Newline => {
|
||||||
newlines += 1;
|
newlines += 1;
|
||||||
}
|
}
|
||||||
TokenKind::Whitespace => {
|
SimpleTokenKind::Whitespace => {
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -80,7 +80,9 @@ pub fn skip_trailing_trivia(offset: TextSize, code: &str) -> TextSize {
|
||||||
|
|
||||||
for token in tokenizer {
|
for token in tokenizer {
|
||||||
match token.kind() {
|
match token.kind() {
|
||||||
TokenKind::Whitespace | TokenKind::Comment | TokenKind::Continuation => {
|
SimpleTokenKind::Whitespace
|
||||||
|
| SimpleTokenKind::Comment
|
||||||
|
| SimpleTokenKind::Continuation => {
|
||||||
// No op
|
// No op
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -111,13 +113,13 @@ fn is_non_ascii_identifier_start(c: char) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
||||||
pub struct Token {
|
pub struct SimpleToken {
|
||||||
pub kind: TokenKind,
|
pub kind: SimpleTokenKind,
|
||||||
pub range: TextRange,
|
pub range: TextRange,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Token {
|
impl SimpleToken {
|
||||||
pub const fn kind(&self) -> TokenKind {
|
pub const fn kind(&self) -> SimpleTokenKind {
|
||||||
self.kind
|
self.kind
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,7 +138,7 @@ impl Token {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
||||||
pub enum TokenKind {
|
pub enum SimpleTokenKind {
|
||||||
/// A comment, not including the trailing new line.
|
/// A comment, not including the trailing new line.
|
||||||
Comment,
|
Comment,
|
||||||
|
|
||||||
|
@ -209,35 +211,35 @@ pub enum TokenKind {
|
||||||
/// Any other non trivia token.
|
/// Any other non trivia token.
|
||||||
Other,
|
Other,
|
||||||
|
|
||||||
/// Returned for each character after [`TokenKind::Other`] has been returned once.
|
/// Returned for each character after [`SimpleTokenKind::Other`] has been returned once.
|
||||||
Bogus,
|
Bogus,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TokenKind {
|
impl SimpleTokenKind {
|
||||||
const fn from_non_trivia_char(c: char) -> TokenKind {
|
const fn from_non_trivia_char(c: char) -> SimpleTokenKind {
|
||||||
match c {
|
match c {
|
||||||
'(' => TokenKind::LParen,
|
'(' => SimpleTokenKind::LParen,
|
||||||
')' => TokenKind::RParen,
|
')' => SimpleTokenKind::RParen,
|
||||||
'[' => TokenKind::LBracket,
|
'[' => SimpleTokenKind::LBracket,
|
||||||
']' => TokenKind::RBracket,
|
']' => SimpleTokenKind::RBracket,
|
||||||
'{' => TokenKind::LBrace,
|
'{' => SimpleTokenKind::LBrace,
|
||||||
'}' => TokenKind::RBrace,
|
'}' => SimpleTokenKind::RBrace,
|
||||||
',' => TokenKind::Comma,
|
',' => SimpleTokenKind::Comma,
|
||||||
':' => TokenKind::Colon,
|
':' => SimpleTokenKind::Colon,
|
||||||
'/' => TokenKind::Slash,
|
'/' => SimpleTokenKind::Slash,
|
||||||
'*' => TokenKind::Star,
|
'*' => SimpleTokenKind::Star,
|
||||||
'.' => TokenKind::Dot,
|
'.' => SimpleTokenKind::Dot,
|
||||||
_ => TokenKind::Other,
|
_ => SimpleTokenKind::Other,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const fn is_trivia(self) -> bool {
|
const fn is_trivia(self) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
self,
|
self,
|
||||||
TokenKind::Whitespace
|
SimpleTokenKind::Whitespace
|
||||||
| TokenKind::Newline
|
| SimpleTokenKind::Newline
|
||||||
| TokenKind::Comment
|
| SimpleTokenKind::Comment
|
||||||
| TokenKind::Continuation
|
| SimpleTokenKind::Continuation
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -246,8 +248,8 @@ impl TokenKind {
|
||||||
///
|
///
|
||||||
/// The tokenizer must start at an offset that is trivia (e.g. not inside of a multiline string).
|
/// The tokenizer must start at an offset that is trivia (e.g. not inside of a multiline string).
|
||||||
///
|
///
|
||||||
/// The tokenizer doesn't guarantee any correctness after it returned a [`TokenKind::Other`]. That's why it
|
/// The tokenizer doesn't guarantee any correctness after it returned a [`SimpleTokenKind::Other`]. That's why it
|
||||||
/// will return [`TokenKind::Bogus`] for every character after until it reaches the end of the file.
|
/// will return [`SimpleTokenKind::Bogus`] for every character after until it reaches the end of the file.
|
||||||
pub struct SimpleTokenizer<'a> {
|
pub struct SimpleTokenizer<'a> {
|
||||||
offset: TextSize,
|
offset: TextSize,
|
||||||
back_offset: TextSize,
|
back_offset: TextSize,
|
||||||
|
@ -289,34 +291,34 @@ impl<'a> SimpleTokenizer<'a> {
|
||||||
tokenizer
|
tokenizer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_keyword_or_other(&self, range: TextRange) -> TokenKind {
|
fn to_keyword_or_other(&self, range: TextRange) -> SimpleTokenKind {
|
||||||
let source = &self.source[range];
|
let source = &self.source[range];
|
||||||
match source {
|
match source {
|
||||||
"as" => TokenKind::As,
|
"as" => SimpleTokenKind::As,
|
||||||
"async" => TokenKind::Async,
|
"async" => SimpleTokenKind::Async,
|
||||||
"else" => TokenKind::Else,
|
"else" => SimpleTokenKind::Else,
|
||||||
"if" => TokenKind::If,
|
"if" => SimpleTokenKind::If,
|
||||||
"in" => TokenKind::In,
|
"in" => SimpleTokenKind::In,
|
||||||
"match" => TokenKind::Match, // Match is a soft keyword that depends on the context but we can always lex it as a keyword and leave it to the caller (parser) to decide if it should be handled as an identifier or keyword.
|
"match" => SimpleTokenKind::Match, // Match is a soft keyword that depends on the context but we can always lex it as a keyword and leave it to the caller (parser) to decide if it should be handled as an identifier or keyword.
|
||||||
"with" => TokenKind::With,
|
"with" => SimpleTokenKind::With,
|
||||||
// ...,
|
// ...,
|
||||||
_ => TokenKind::Other, // Potentially an identifier, but only if it isn't a string prefix. We can ignore this for now https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
|
_ => SimpleTokenKind::Other, // Potentially an identifier, but only if it isn't a string prefix. We can ignore this for now https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_token(&mut self) -> Token {
|
fn next_token(&mut self) -> SimpleToken {
|
||||||
self.cursor.start_token();
|
self.cursor.start_token();
|
||||||
|
|
||||||
let Some(first) = self.cursor.bump() else {
|
let Some(first) = self.cursor.bump() else {
|
||||||
return Token {
|
return SimpleToken {
|
||||||
kind: TokenKind::EndOfFile,
|
kind: SimpleTokenKind::EndOfFile,
|
||||||
range: TextRange::empty(self.offset),
|
range: TextRange::empty(self.offset),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
if self.bogus {
|
if self.bogus {
|
||||||
let token = Token {
|
let token = SimpleToken {
|
||||||
kind: TokenKind::Bogus,
|
kind: SimpleTokenKind::Bogus,
|
||||||
range: TextRange::at(self.offset, first.text_len()),
|
range: TextRange::at(self.offset, first.text_len()),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -327,22 +329,22 @@ impl<'a> SimpleTokenizer<'a> {
|
||||||
let kind = match first {
|
let kind = match first {
|
||||||
' ' | '\t' => {
|
' ' | '\t' => {
|
||||||
self.cursor.eat_while(|c| matches!(c, ' ' | '\t'));
|
self.cursor.eat_while(|c| matches!(c, ' ' | '\t'));
|
||||||
TokenKind::Whitespace
|
SimpleTokenKind::Whitespace
|
||||||
}
|
}
|
||||||
|
|
||||||
'\n' => TokenKind::Newline,
|
'\n' => SimpleTokenKind::Newline,
|
||||||
|
|
||||||
'\r' => {
|
'\r' => {
|
||||||
self.cursor.eat_char('\n');
|
self.cursor.eat_char('\n');
|
||||||
TokenKind::Newline
|
SimpleTokenKind::Newline
|
||||||
}
|
}
|
||||||
|
|
||||||
'#' => {
|
'#' => {
|
||||||
self.cursor.eat_while(|c| !matches!(c, '\n' | '\r'));
|
self.cursor.eat_while(|c| !matches!(c, '\n' | '\r'));
|
||||||
TokenKind::Comment
|
SimpleTokenKind::Comment
|
||||||
}
|
}
|
||||||
|
|
||||||
'\\' => TokenKind::Continuation,
|
'\\' => SimpleTokenKind::Continuation,
|
||||||
|
|
||||||
c => {
|
c => {
|
||||||
let kind = if is_identifier_start(c) {
|
let kind = if is_identifier_start(c) {
|
||||||
|
@ -352,10 +354,10 @@ impl<'a> SimpleTokenizer<'a> {
|
||||||
let range = TextRange::at(self.offset, token_len);
|
let range = TextRange::at(self.offset, token_len);
|
||||||
self.to_keyword_or_other(range)
|
self.to_keyword_or_other(range)
|
||||||
} else {
|
} else {
|
||||||
TokenKind::from_non_trivia_char(c)
|
SimpleTokenKind::from_non_trivia_char(c)
|
||||||
};
|
};
|
||||||
|
|
||||||
if kind == TokenKind::Other {
|
if kind == SimpleTokenKind::Other {
|
||||||
self.bogus = true;
|
self.bogus = true;
|
||||||
}
|
}
|
||||||
kind
|
kind
|
||||||
|
@ -364,7 +366,7 @@ impl<'a> SimpleTokenizer<'a> {
|
||||||
|
|
||||||
let token_len = self.cursor.token_len();
|
let token_len = self.cursor.token_len();
|
||||||
|
|
||||||
let token = Token {
|
let token = SimpleToken {
|
||||||
kind,
|
kind,
|
||||||
range: TextRange::at(self.offset, token_len),
|
range: TextRange::at(self.offset, token_len),
|
||||||
};
|
};
|
||||||
|
@ -376,19 +378,19 @@ impl<'a> SimpleTokenizer<'a> {
|
||||||
|
|
||||||
/// Returns the next token from the back. Prefer iterating forwards. Iterating backwards is significantly more expensive
|
/// Returns the next token from the back. Prefer iterating forwards. Iterating backwards is significantly more expensive
|
||||||
/// because it needs to check if the line has any comments when encountering any non-trivia token.
|
/// because it needs to check if the line has any comments when encountering any non-trivia token.
|
||||||
pub fn next_token_back(&mut self) -> Token {
|
pub fn next_token_back(&mut self) -> SimpleToken {
|
||||||
self.cursor.start_token();
|
self.cursor.start_token();
|
||||||
|
|
||||||
let Some(last) = self.cursor.bump_back() else {
|
let Some(last) = self.cursor.bump_back() else {
|
||||||
return Token {
|
return SimpleToken {
|
||||||
kind: TokenKind::EndOfFile,
|
kind: SimpleTokenKind::EndOfFile,
|
||||||
range: TextRange::empty(self.back_offset),
|
range: TextRange::empty(self.back_offset),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
if self.bogus {
|
if self.bogus {
|
||||||
let token = Token {
|
let token = SimpleToken {
|
||||||
kind: TokenKind::Bogus,
|
kind: SimpleTokenKind::Bogus,
|
||||||
range: TextRange::at(self.back_offset - last.text_len(), last.text_len()),
|
range: TextRange::at(self.back_offset - last.text_len(), last.text_len()),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -401,22 +403,22 @@ impl<'a> SimpleTokenizer<'a> {
|
||||||
// as whitespace rather than being part of the token. This shouldn't matter for what we use the lexer for.
|
// as whitespace rather than being part of the token. This shouldn't matter for what we use the lexer for.
|
||||||
' ' | '\t' => {
|
' ' | '\t' => {
|
||||||
self.cursor.eat_back_while(|c| matches!(c, ' ' | '\t'));
|
self.cursor.eat_back_while(|c| matches!(c, ' ' | '\t'));
|
||||||
TokenKind::Whitespace
|
SimpleTokenKind::Whitespace
|
||||||
}
|
}
|
||||||
|
|
||||||
'\r' => {
|
'\r' => {
|
||||||
self.back_line_has_no_comment = false;
|
self.back_line_has_no_comment = false;
|
||||||
TokenKind::Newline
|
SimpleTokenKind::Newline
|
||||||
}
|
}
|
||||||
|
|
||||||
'\n' => {
|
'\n' => {
|
||||||
self.back_line_has_no_comment = false;
|
self.back_line_has_no_comment = false;
|
||||||
self.cursor.eat_char_back('\r');
|
self.cursor.eat_char_back('\r');
|
||||||
TokenKind::Newline
|
SimpleTokenKind::Newline
|
||||||
}
|
}
|
||||||
|
|
||||||
// Empty comment (could also be a comment nested in another comment, but this shouldn't matter for what we use the lexer for)
|
// Empty comment (could also be a comment nested in another comment, but this shouldn't matter for what we use the lexer for)
|
||||||
'#' => TokenKind::Comment,
|
'#' => SimpleTokenKind::Comment,
|
||||||
|
|
||||||
// For all other tokens, test if the character isn't part of a comment.
|
// For all other tokens, test if the character isn't part of a comment.
|
||||||
c => {
|
c => {
|
||||||
|
@ -447,7 +449,8 @@ impl<'a> SimpleTokenizer<'a> {
|
||||||
|
|
||||||
before_comment.chars().all(|c| {
|
before_comment.chars().all(|c| {
|
||||||
is_python_whitespace(c)
|
is_python_whitespace(c)
|
||||||
|| TokenKind::from_non_trivia_char(c) != TokenKind::Other
|
|| SimpleTokenKind::from_non_trivia_char(c)
|
||||||
|
!= SimpleTokenKind::Other
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
@ -462,9 +465,9 @@ impl<'a> SimpleTokenizer<'a> {
|
||||||
self.cursor.bump_back().unwrap();
|
self.cursor.bump_back().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
TokenKind::Comment
|
SimpleTokenKind::Comment
|
||||||
} else if c == '\\' {
|
} else if c == '\\' {
|
||||||
TokenKind::Continuation
|
SimpleTokenKind::Continuation
|
||||||
} else {
|
} else {
|
||||||
let kind = if is_identifier_continuation(c) {
|
let kind = if is_identifier_continuation(c) {
|
||||||
// if we only have identifier continuations but no start (e.g. 555) we
|
// if we only have identifier continuations but no start (e.g. 555) we
|
||||||
|
@ -484,13 +487,13 @@ impl<'a> SimpleTokenizer<'a> {
|
||||||
self.to_keyword_or_other(range)
|
self.to_keyword_or_other(range)
|
||||||
} else {
|
} else {
|
||||||
self.cursor = savepoint;
|
self.cursor = savepoint;
|
||||||
TokenKind::Other
|
SimpleTokenKind::Other
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
TokenKind::from_non_trivia_char(c)
|
SimpleTokenKind::from_non_trivia_char(c)
|
||||||
};
|
};
|
||||||
|
|
||||||
if kind == TokenKind::Other {
|
if kind == SimpleTokenKind::Other {
|
||||||
self.bogus = true;
|
self.bogus = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -503,7 +506,7 @@ impl<'a> SimpleTokenizer<'a> {
|
||||||
|
|
||||||
let start = self.back_offset - token_len;
|
let start = self.back_offset - token_len;
|
||||||
|
|
||||||
let token = Token {
|
let token = SimpleToken {
|
||||||
kind,
|
kind,
|
||||||
range: TextRange::at(start, token_len),
|
range: TextRange::at(start, token_len),
|
||||||
};
|
};
|
||||||
|
@ -513,18 +516,18 @@ impl<'a> SimpleTokenizer<'a> {
|
||||||
token
|
token
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn skip_trivia(self) -> impl Iterator<Item = Token> + DoubleEndedIterator + 'a {
|
pub fn skip_trivia(self) -> impl Iterator<Item = SimpleToken> + DoubleEndedIterator + 'a {
|
||||||
self.filter(|t| !t.kind().is_trivia())
|
self.filter(|t| !t.kind().is_trivia())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Iterator for SimpleTokenizer<'_> {
|
impl Iterator for SimpleTokenizer<'_> {
|
||||||
type Item = Token;
|
type Item = SimpleToken;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let token = self.next_token();
|
let token = self.next_token();
|
||||||
|
|
||||||
if token.kind == TokenKind::EndOfFile {
|
if token.kind == SimpleTokenKind::EndOfFile {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(token)
|
Some(token)
|
||||||
|
@ -536,7 +539,7 @@ impl DoubleEndedIterator for SimpleTokenizer<'_> {
|
||||||
fn next_back(&mut self) -> Option<Self::Item> {
|
fn next_back(&mut self) -> Option<Self::Item> {
|
||||||
let token = self.next_token_back();
|
let token = self.next_token_back();
|
||||||
|
|
||||||
if token.kind == TokenKind::EndOfFile {
|
if token.kind == SimpleTokenKind::EndOfFile {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(token)
|
Some(token)
|
||||||
|
@ -549,12 +552,12 @@ mod tests {
|
||||||
use insta::assert_debug_snapshot;
|
use insta::assert_debug_snapshot;
|
||||||
use ruff_text_size::{TextLen, TextRange, TextSize};
|
use ruff_text_size::{TextLen, TextRange, TextSize};
|
||||||
|
|
||||||
use crate::tokenizer::{lines_after, lines_before, SimpleTokenizer, Token};
|
use crate::tokenizer::{lines_after, lines_before, SimpleToken, SimpleTokenizer};
|
||||||
|
|
||||||
struct TokenizationTestCase {
|
struct TokenizationTestCase {
|
||||||
source: &'static str,
|
source: &'static str,
|
||||||
range: TextRange,
|
range: TextRange,
|
||||||
tokens: Vec<Token>,
|
tokens: Vec<SimpleToken>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TokenizationTestCase {
|
impl TokenizationTestCase {
|
||||||
|
@ -567,13 +570,13 @@ mod tests {
|
||||||
assert_eq!(&backwards, &self.tokens);
|
assert_eq!(&backwards, &self.tokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tokenize_reverse(&self) -> Vec<Token> {
|
fn tokenize_reverse(&self) -> Vec<SimpleToken> {
|
||||||
SimpleTokenizer::new(self.source, self.range)
|
SimpleTokenizer::new(self.source, self.range)
|
||||||
.rev()
|
.rev()
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tokens(&self) -> &[Token] {
|
fn tokens(&self) -> &[SimpleToken] {
|
||||||
&self.tokens
|
&self.tokens
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -11,5 +11,5 @@ repository = { workspace = true }
|
||||||
license = { workspace = true }
|
license = { workspace = true }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
ruff_python_whitespace = { path = "../ruff_python_whitespace" }
|
ruff_python_trivia = { path = "../ruff_python_trivia" }
|
||||||
ruff_text_size = { workspace = true }
|
ruff_text_size = { workspace = true }
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
|
|
||||||
use ruff_python_whitespace::{PythonWhitespace, UniversalNewlines};
|
use ruff_python_trivia::{PythonWhitespace, UniversalNewlines};
|
||||||
|
|
||||||
/// Indent each line by the given prefix.
|
/// Indent each line by the given prefix.
|
||||||
///
|
///
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue