From 059757a8c8caa0ba899bff381c9bd056d9298270 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 27 Aug 2023 15:03:08 -0400 Subject: [PATCH] Implement `Ranged` on more structs (#6921) Now that it's in `ruff_text_size`, we can use it in a few places that we couldn't before. --- Cargo.lock | 1 + crates/ruff/src/autofix/mod.rs | 4 +-- crates/ruff/src/autofix/source_map.rs | 2 +- crates/ruff/src/docstrings/sections.rs | 10 +++++-- crates/ruff/src/linter.rs | 1 + crates/ruff/src/message/diff.rs | 2 +- crates/ruff/src/message/json.rs | 1 + crates/ruff/src/message/mod.rs | 18 ++++++------ crates/ruff/src/noqa.rs | 9 +++++- .../flake8_commas/rules/trailing_commas.rs | 6 ++-- crates/ruff/src/rules/isort/mod.rs | 20 ++++++------- .../rules/invalid_escape_sequence.rs | 2 +- crates/ruff/src/rules/pyflakes/mod.rs | 4 +-- crates/ruff/src/test.rs | 1 + crates/ruff_diagnostics/src/diagnostic.rs | 20 +++++-------- crates/ruff_diagnostics/src/edit.rs | 22 +++++---------- crates/ruff_diagnostics/src/fix.rs | 8 +++--- crates/ruff_formatter/src/printer/mod.rs | 23 ++++++++------- crates/ruff_formatter/src/source_code.rs | 15 ++++------ .../src/comments/placement.rs | 28 ++++++++----------- crates/ruff_python_trivia/src/tokenizer.rs | 15 +++------- crates/ruff_wasm/Cargo.toml | 1 + crates/ruff_wasm/src/lib.rs | 6 ++-- 23 files changed, 104 insertions(+), 115 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 476b919209..28b9f9beb9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2531,6 +2531,7 @@ dependencies = [ "ruff_python_index", "ruff_python_parser", "ruff_source_file", + "ruff_text_size", "serde", "serde-wasm-bindgen", "wasm-bindgen", diff --git a/crates/ruff/src/autofix/mod.rs b/crates/ruff/src/autofix/mod.rs index f402e7cb7d..b85f1b54d2 100644 --- a/crates/ruff/src/autofix/mod.rs +++ b/crates/ruff/src/autofix/mod.rs @@ -1,7 +1,7 @@ use itertools::Itertools; use std::collections::BTreeSet; -use ruff_text_size::{TextLen, TextRange, TextSize}; +use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; use rustc_hash::{FxHashMap, FxHashSet}; use ruff_diagnostics::{Diagnostic, Edit, Fix, IsolationLevel}; @@ -138,7 +138,7 @@ fn cmp_fix(rule1: Rule, rule2: Rule, fix1: &Fix, fix2: &Fix) -> std::cmp::Orderi #[cfg(test)] mod tests { - use ruff_text_size::TextSize; + use ruff_text_size::{Ranged, TextSize}; use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_source_file::Locator; diff --git a/crates/ruff/src/autofix/source_map.rs b/crates/ruff/src/autofix/source_map.rs index 9b6ea21e17..7871e51d9c 100644 --- a/crates/ruff/src/autofix/source_map.rs +++ b/crates/ruff/src/autofix/source_map.rs @@ -1,4 +1,4 @@ -use ruff_text_size::TextSize; +use ruff_text_size::{Ranged, TextSize}; use ruff_diagnostics::Edit; diff --git a/crates/ruff/src/docstrings/sections.rs b/crates/ruff/src/docstrings/sections.rs index 603173e1c1..73a42a443d 100644 --- a/crates/ruff/src/docstrings/sections.rs +++ b/crates/ruff/src/docstrings/sections.rs @@ -164,7 +164,7 @@ impl<'a> SectionContexts<'a> { lines.peek(), ) { if let Some(mut last) = last.take() { - last.range = TextRange::new(last.range.start(), line.start()); + last.range = TextRange::new(last.start(), line.start()); contexts.push(last); } @@ -181,7 +181,7 @@ impl<'a> SectionContexts<'a> { } if let Some(mut last) = last.take() { - last.range = TextRange::new(last.range.start(), contents.text_len()); + last.range = TextRange::new(last.start(), contents.text_len()); contexts.push(last); } @@ -267,6 +267,12 @@ struct SectionContextData { summary_full_end: TextSize, } +impl Ranged for SectionContextData { + fn range(&self) -> TextRange { + self.range + } +} + pub(crate) struct SectionContext<'a> { data: &'a SectionContextData, docstring_body: DocstringBody<'a>, diff --git a/crates/ruff/src/linter.rs b/crates/ruff/src/linter.rs index 9831615483..ae294045cb 100644 --- a/crates/ruff/src/linter.rs +++ b/crates/ruff/src/linter.rs @@ -17,6 +17,7 @@ use ruff_python_codegen::Stylist; use ruff_python_index::Indexer; use ruff_source_file::{Locator, SourceFileBuilder}; +use ruff_text_size::Ranged; use crate::autofix::{fix_file, FixResult}; use crate::checkers::ast::check_ast; diff --git a/crates/ruff/src/message/diff.rs b/crates/ruff/src/message/diff.rs index ca113b9969..645d0797f5 100644 --- a/crates/ruff/src/message/diff.rs +++ b/crates/ruff/src/message/diff.rs @@ -3,7 +3,7 @@ use std::num::NonZeroUsize; use colored::{Color, ColoredString, Colorize, Styles}; -use ruff_text_size::{TextRange, TextSize}; +use ruff_text_size::{Ranged, TextRange, TextSize}; use similar::{ChangeTag, TextDiff}; use ruff_diagnostics::{Applicability, Fix}; diff --git a/crates/ruff/src/message/json.rs b/crates/ruff/src/message/json.rs index 6d17df0fcc..e1a2e3e36d 100644 --- a/crates/ruff/src/message/json.rs +++ b/crates/ruff/src/message/json.rs @@ -6,6 +6,7 @@ use serde_json::{json, Value}; use ruff_diagnostics::Edit; use ruff_source_file::SourceCode; +use ruff_text_size::Ranged; use crate::message::{Emitter, EmitterContext, Message}; use crate::registry::AsRule; diff --git a/crates/ruff/src/message/mod.rs b/crates/ruff/src/message/mod.rs index bcafd6384e..ea89872600 100644 --- a/crates/ruff/src/message/mod.rs +++ b/crates/ruff/src/message/mod.rs @@ -15,7 +15,7 @@ pub use junit::JunitEmitter; pub use pylint::PylintEmitter; use ruff_diagnostics::{Diagnostic, DiagnosticKind, Fix}; use ruff_source_file::{SourceFile, SourceLocation}; -use ruff_text_size::{TextRange, TextSize}; +use ruff_text_size::{Ranged, TextRange, TextSize}; pub use text::TextEmitter; use crate::jupyter::Notebook; @@ -66,14 +66,6 @@ impl Message { pub fn compute_end_location(&self) -> SourceLocation { self.file.to_source_code().source_location(self.end()) } - - pub const fn start(&self) -> TextSize { - self.range.start() - } - - pub const fn end(&self) -> TextSize { - self.range.end() - } } impl Ord for Message { @@ -88,6 +80,12 @@ impl PartialOrd for Message { } } +impl Ranged for Message { + fn range(&self) -> TextRange { + self.range + } +} + struct MessageWithLocation<'a> { message: &'a Message, start_location: SourceLocation, @@ -154,7 +152,7 @@ mod tests { use ruff_diagnostics::{Diagnostic, DiagnosticKind, Edit, Fix}; use ruff_source_file::SourceFileBuilder; - use ruff_text_size::{TextRange, TextSize}; + use ruff_text_size::{Ranged, TextRange, TextSize}; use crate::message::{Emitter, EmitterContext, Message}; diff --git a/crates/ruff/src/noqa.rs b/crates/ruff/src/noqa.rs index f2be0b7073..36cf5105a9 100644 --- a/crates/ruff/src/noqa.rs +++ b/crates/ruff/src/noqa.rs @@ -536,7 +536,7 @@ fn add_noqa_inner( let rule = diagnostic.kind.rule(); if !includes(rule, codes) { matches_by_line - .entry(directive_line.range.start()) + .entry(directive_line.start()) .or_insert_with(|| { (RuleSet::default(), Some(&directive_line.directive)) }) @@ -644,6 +644,13 @@ pub(crate) struct NoqaDirectiveLine<'a> { pub(crate) matches: Vec, } +impl Ranged for NoqaDirectiveLine<'_> { + /// The range of the `noqa` directive. + fn range(&self) -> TextRange { + self.range + } +} + #[derive(Debug, Default)] pub(crate) struct NoqaDirectives<'a> { inner: Vec>, diff --git a/crates/ruff/src/rules/flake8_commas/rules/trailing_commas.rs b/crates/ruff/src/rules/flake8_commas/rules/trailing_commas.rs index 7e0b2f7247..7c2f253c85 100644 --- a/crates/ruff/src/rules/flake8_commas/rules/trailing_commas.rs +++ b/crates/ruff/src/rules/flake8_commas/rules/trailing_commas.rs @@ -1,12 +1,12 @@ use itertools::Itertools; -use ruff_python_parser::lexer::{LexResult, Spanned}; -use ruff_python_parser::Tok; -use ruff_text_size::TextRange; use ruff_diagnostics::{AlwaysAutofixableViolation, Violation}; use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; +use ruff_python_parser::lexer::{LexResult, Spanned}; +use ruff_python_parser::Tok; use ruff_source_file::Locator; +use ruff_text_size::{Ranged, TextRange}; use crate::registry::Rule; use crate::settings::Settings; diff --git a/crates/ruff/src/rules/isort/mod.rs b/crates/ruff/src/rules/isort/mod.rs index 074f59be15..4cbd61c1fd 100644 --- a/crates/ruff/src/rules/isort/mod.rs +++ b/crates/ruff/src/rules/isort/mod.rs @@ -317,11 +317,11 @@ mod tests { use std::path::Path; use anyhow::Result; + use ruff_text_size::Ranged; use rustc_hash::FxHashMap; use test_case::test_case; use crate::assert_messages; - use crate::message::Message; use crate::registry::Rule; use crate::rules::isort::categorize::{ImportSection, KnownModules}; use crate::settings::Settings; @@ -660,7 +660,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(Message::start); + diagnostics.sort_by_key(Ranged::start); assert_messages!(snapshot, diagnostics); Ok(()) } @@ -688,7 +688,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(Message::start); + diagnostics.sort_by_key(Ranged::start); assert_messages!(snapshot, diagnostics); Ok(()) } @@ -718,7 +718,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(Message::start); + diagnostics.sort_by_key(Ranged::start); assert_messages!(snapshot, diagnostics); Ok(()) } @@ -746,7 +746,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(Message::start); + diagnostics.sort_by_key(Ranged::start); assert_messages!(snapshot, diagnostics); Ok(()) } @@ -766,7 +766,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(Message::start); + diagnostics.sort_by_key(Ranged::start); assert_messages!(snapshot, diagnostics); Ok(()) } @@ -937,7 +937,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(Message::start); + diagnostics.sort_by_key(Ranged::start); assert_messages!(snapshot, diagnostics); Ok(()) } @@ -962,7 +962,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(Message::start); + diagnostics.sort_by_key(Ranged::start); assert_messages!(snapshot, diagnostics); Ok(()) } @@ -983,7 +983,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(Message::start); + diagnostics.sort_by_key(Ranged::start); assert_messages!(snapshot, diagnostics); Ok(()) } @@ -1002,7 +1002,7 @@ mod tests { ..Settings::for_rule(Rule::UnsortedImports) }, )?; - diagnostics.sort_by_key(Message::start); + diagnostics.sort_by_key(Ranged::start); assert_messages!(snapshot, diagnostics); Ok(()) } diff --git a/crates/ruff/src/rules/pycodestyle/rules/invalid_escape_sequence.rs b/crates/ruff/src/rules/pycodestyle/rules/invalid_escape_sequence.rs index 2cea1ad2f1..286344a7ca 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/invalid_escape_sequence.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/invalid_escape_sequence.rs @@ -1,5 +1,5 @@ use memchr::memchr_iter; -use ruff_text_size::{TextLen, TextRange, TextSize}; +use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; diff --git a/crates/ruff/src/rules/pyflakes/mod.rs b/crates/ruff/src/rules/pyflakes/mod.rs index ea7288d5a6..8268fc11bf 100644 --- a/crates/ruff/src/rules/pyflakes/mod.rs +++ b/crates/ruff/src/rules/pyflakes/mod.rs @@ -15,13 +15,13 @@ mod tests { use test_case::test_case; - use ruff_diagnostics::Diagnostic; use ruff_python_ast::PySourceType; use ruff_python_codegen::Stylist; use ruff_python_index::Indexer; use ruff_python_parser::AsMode; use ruff_python_trivia::textwrap::dedent; use ruff_source_file::Locator; + use ruff_text_size::Ranged; use crate::linter::{check_path, LinterResult}; use crate::registry::{AsRule, Linter, Rule}; @@ -535,7 +535,7 @@ mod tests { None, source_type, ); - diagnostics.sort_by_key(Diagnostic::start); + diagnostics.sort_by_key(Ranged::start); let actual = diagnostics .iter() .map(|diagnostic| diagnostic.kind.rule()) diff --git a/crates/ruff/src/test.rs b/crates/ruff/src/test.rs index 4c17d19e08..b322d5b919 100644 --- a/crates/ruff/src/test.rs +++ b/crates/ruff/src/test.rs @@ -17,6 +17,7 @@ use ruff_python_parser::lexer::LexResult; use ruff_python_parser::AsMode; use ruff_python_trivia::textwrap::dedent; use ruff_source_file::{Locator, SourceFileBuilder}; +use ruff_text_size::Ranged; use crate::autofix::{fix_file, FixResult}; use crate::directives; diff --git a/crates/ruff_diagnostics/src/diagnostic.rs b/crates/ruff_diagnostics/src/diagnostic.rs index 19a5d3ec45..006df34636 100644 --- a/crates/ruff_diagnostics/src/diagnostic.rs +++ b/crates/ruff_diagnostics/src/diagnostic.rs @@ -3,7 +3,7 @@ use log::error; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use ruff_text_size::{TextRange, TextSize}; +use ruff_text_size::{Ranged, TextRange, TextSize}; use crate::Fix; @@ -71,21 +71,15 @@ impl Diagnostic { } } - pub const fn range(&self) -> TextRange { - self.range - } - - pub const fn start(&self) -> TextSize { - self.range.start() - } - - pub const fn end(&self) -> TextSize { - self.range.end() - } - /// Set the location of the diagnostic's parent node. #[inline] pub fn set_parent(&mut self, parent: TextSize) { self.parent = Some(parent); } } + +impl Ranged for Diagnostic { + fn range(&self) -> TextRange { + self.range + } +} diff --git a/crates/ruff_diagnostics/src/edit.rs b/crates/ruff_diagnostics/src/edit.rs index c26f566de6..8c05474d21 100644 --- a/crates/ruff_diagnostics/src/edit.rs +++ b/crates/ruff_diagnostics/src/edit.rs @@ -3,7 +3,7 @@ use std::cmp::Ordering; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use ruff_text_size::{TextRange, TextSize}; +use ruff_text_size::{Ranged, TextRange, TextSize}; /// A text edit to be applied to a source file. Inserts, deletes, or replaces /// content at a given location. @@ -62,20 +62,6 @@ impl Edit { self.content.as_deref() } - /// Returns the start location of the edit in the source document. - pub const fn start(&self) -> TextSize { - self.range.start() - } - - pub const fn range(&self) -> TextRange { - self.range - } - - /// Returns the edit's end location in the source document. - pub const fn end(&self) -> TextSize { - self.range.end() - } - fn kind(&self) -> EditOperationKind { if self.content.is_none() { EditOperationKind::Deletion @@ -120,6 +106,12 @@ impl PartialOrd for Edit { } } +impl Ranged for Edit { + fn range(&self) -> TextRange { + self.range + } +} + #[derive(Copy, Clone, Eq, PartialEq, Debug)] enum EditOperationKind { /// Edit that inserts new content into the source document. diff --git a/crates/ruff_diagnostics/src/fix.rs b/crates/ruff_diagnostics/src/fix.rs index 8b37e0b8fe..d553e83d66 100644 --- a/crates/ruff_diagnostics/src/fix.rs +++ b/crates/ruff_diagnostics/src/fix.rs @@ -1,7 +1,7 @@ #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use ruff_text_size::TextSize; +use ruff_text_size::{Ranged, TextSize}; use crate::edit::Edit; @@ -88,7 +88,7 @@ impl Fix { /// Create a new [`Fix`] with [automatic applicability](Applicability::Automatic) from multiple [`Edit`] elements. pub fn automatic_edits(edit: Edit, rest: impl IntoIterator) -> Self { let mut edits: Vec = std::iter::once(edit).chain(rest).collect(); - edits.sort_by_key(Edit::start); + edits.sort_by_key(Ranged::start); Self { edits, applicability: Applicability::Automatic, @@ -108,7 +108,7 @@ impl Fix { /// Create a new [`Fix`] with [suggested applicability](Applicability::Suggested) from multiple [`Edit`] elements. pub fn suggested_edits(edit: Edit, rest: impl IntoIterator) -> Self { let mut edits: Vec = std::iter::once(edit).chain(rest).collect(); - edits.sort_by_key(Edit::start); + edits.sort_by_key(Ranged::start); Self { edits, applicability: Applicability::Suggested, @@ -128,7 +128,7 @@ impl Fix { /// Create a new [`Fix`] with [manual applicability](Applicability::Manual) from multiple [`Edit`] elements. pub fn manual_edits(edit: Edit, rest: impl IntoIterator) -> Self { let mut edits: Vec = std::iter::once(edit).chain(rest).collect(); - edits.sort_by_key(Edit::start); + edits.sort_by_key(Ranged::start); Self { edits, applicability: Applicability::Manual, diff --git a/crates/ruff_formatter/src/printer/mod.rs b/crates/ruff_formatter/src/printer/mod.rs index a096bd31fa..eef035faca 100644 --- a/crates/ruff_formatter/src/printer/mod.rs +++ b/crates/ruff_formatter/src/printer/mod.rs @@ -1,8 +1,10 @@ -mod call_stack; -mod line_suffixes; -mod printer_options; -mod queue; -mod stack; +use std::num::NonZeroU8; + +use drop_bomb::DebugDropBomb; +use unicode_width::UnicodeWidthChar; + +pub use printer_options::*; +use ruff_text_size::{Ranged, TextLen, TextSize}; use crate::format_element::document::Document; use crate::format_element::tag::{Condition, GroupMode}; @@ -21,11 +23,12 @@ use crate::{ ActualStart, FormatElement, GroupId, IndentStyle, InvalidDocumentError, PrintError, PrintResult, Printed, SourceMarker, TextRange, }; -use drop_bomb::DebugDropBomb; -pub use printer_options::*; -use ruff_text_size::{TextLen, TextSize}; -use std::num::NonZeroU8; -use unicode_width::UnicodeWidthChar; + +mod call_stack; +mod line_suffixes; +mod printer_options; +mod queue; +mod stack; /// Prints the format elements into a string #[derive(Debug, Default)] diff --git a/crates/ruff_formatter/src/source_code.rs b/crates/ruff_formatter/src/source_code.rs index 8f2104966f..cc81875aad 100644 --- a/crates/ruff_formatter/src/source_code.rs +++ b/crates/ruff_formatter/src/source_code.rs @@ -1,6 +1,7 @@ -use ruff_text_size::{TextRange, TextSize}; use std::fmt::{Debug, Formatter}; +use ruff_text_size::{Ranged, TextRange}; + /// The source code of a document that gets formatted #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default)] pub struct SourceCode<'a> { @@ -67,18 +68,12 @@ impl SourceCodeSlice { assert!(usize::from(self.range.end()) <= code.text.len(), "The range of this slice is out of bounds. Did you provide the correct source code for this slice?"); &code.text[self.range] } +} - pub const fn range(&self) -> TextRange { +impl Ranged for SourceCodeSlice { + fn range(&self) -> TextRange { self.range } - - pub const fn start(&self) -> TextSize { - self.range.start() - } - - pub const fn end(&self) -> TextSize { - self.range.end() - } } impl Debug for SourceCodeSlice { diff --git a/crates/ruff_python_formatter/src/comments/placement.rs b/crates/ruff_python_formatter/src/comments/placement.rs index 967a9c2bfe..0db12e13c8 100644 --- a/crates/ruff_python_formatter/src/comments/placement.rs +++ b/crates/ruff_python_formatter/src/comments/placement.rs @@ -434,7 +434,7 @@ fn handle_own_line_comment_around_body<'a>( // ``` let maybe_token = SimpleTokenizer::new( locator.contents(), - TextRange::new(preceding.end(), comment.slice().start()), + TextRange::new(preceding.end(), comment.start()), ) .skip_trivia() .next(); @@ -475,7 +475,7 @@ fn handle_own_line_comment_between_branches<'a>( // It depends on the indentation level of the comment if it is a leading comment for the // following branch or if it a trailing comment of the previous body's last statement. - let comment_indentation = indentation_at_offset(comment.slice().start(), locator) + let comment_indentation = indentation_at_offset(comment.start(), locator) .unwrap_or_default() .len(); @@ -559,7 +559,7 @@ fn handle_own_line_comment_after_branch<'a>( // We only care about the length because indentations with mixed spaces and tabs are only valid if // the indent-level doesn't depend on the tab width (the indent level must be the same if the tab width is 1 or 8). - let comment_indentation = indentation_at_offset(comment.slice().start(), locator) + let comment_indentation = indentation_at_offset(comment.start(), locator) .unwrap_or_default() .len(); @@ -848,7 +848,7 @@ fn handle_slice_comments<'a>( // Check for `foo[ # comment`, but only if they are on the same line let after_lbracket = matches!( - SimpleTokenizer::up_to_without_back_comment(comment.slice().start(), locator.contents()) + SimpleTokenizer::up_to_without_back_comment(comment.start(), locator.contents()) .skip_trivia() .next_back(), Some(SimpleToken { @@ -881,7 +881,7 @@ fn handle_slice_comments<'a>( }; if let Some(node) = node { - if comment.slice().start() < node.start() { + if comment.start() < node.start() { CommentPlacement::leading(node.as_ref(), comment) } else { // If a trailing comment is an end of line comment that's fine because we have a node @@ -977,7 +977,7 @@ fn handle_dict_unpacking_comment<'a>( }; let mut tokens = SimpleTokenizer::new( locator.contents(), - TextRange::new(preceding_end, comment.slice().start()), + TextRange::new(preceding_end, comment.start()), ) .skip_trivia() .skip_while(|token| token.kind == SimpleTokenKind::RParen); @@ -1138,7 +1138,7 @@ fn handle_expr_if_comment<'a>( locator.contents(), ); // Between `if` and `test` - if if_token.range.start() < comment.slice().start() && comment.slice().start() < test.start() { + if if_token.start() < comment.start() && comment.start() < test.start() { return CommentPlacement::leading(test.as_ref(), comment); } @@ -1148,9 +1148,7 @@ fn handle_expr_if_comment<'a>( locator.contents(), ); // Between `else` and `orelse` - if else_token.range.start() < comment.slice().start() - && comment.slice().start() < orelse.start() - { + if else_token.start() < comment.start() && comment.start() < orelse.start() { return CommentPlacement::leading(orelse.as_ref(), comment); } @@ -1585,7 +1583,7 @@ fn handle_comprehension_comment<'a>( // in c // ] // ``` - if comment.slice().start() < in_token.start() { + if comment.start() < in_token.start() { // attach as dangling comments on the target // (to be rendered as leading on the "in") return if is_own_line { @@ -1604,7 +1602,7 @@ fn handle_comprehension_comment<'a>( // c // ] // ``` - if comment.slice().start() < comprehension.iter.start() { + if comment.start() < comprehension.iter.start() { return if is_own_line { CommentPlacement::Default(comment) } else { @@ -1639,12 +1637,10 @@ fn handle_comprehension_comment<'a>( locator.contents(), ); if is_own_line { - if last_end < comment.slice().start() && comment.slice().start() < if_token.start() { + if last_end < comment.start() && comment.start() < if_token.start() { return CommentPlacement::dangling(if_node, comment); } - } else if if_token.start() < comment.slice().start() - && comment.slice().start() < if_node.start() - { + } else if if_token.start() < comment.start() && comment.start() < if_node.start() { return CommentPlacement::dangling(if_node, comment); } last_end = if_node.end(); diff --git a/crates/ruff_python_trivia/src/tokenizer.rs b/crates/ruff_python_trivia/src/tokenizer.rs index d0632abe4c..93aa9fb16a 100644 --- a/crates/ruff_python_trivia/src/tokenizer.rs +++ b/crates/ruff_python_trivia/src/tokenizer.rs @@ -1,7 +1,7 @@ use memchr::{memchr2, memchr3, memrchr3_iter}; use unic_ucd_ident::{is_xid_continue, is_xid_start}; -use ruff_text_size::{TextLen, TextRange, TextSize}; +use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; use crate::{is_python_whitespace, Cursor}; @@ -131,19 +131,12 @@ impl SimpleToken { pub const fn kind(&self) -> SimpleTokenKind { self.kind } +} - #[allow(unused)] - pub const fn range(&self) -> TextRange { +impl Ranged for SimpleToken { + fn range(&self) -> TextRange { self.range } - - pub const fn start(&self) -> TextSize { - self.range.start() - } - - pub const fn end(&self) -> TextSize { - self.range.end() - } } #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] diff --git a/crates/ruff_wasm/Cargo.toml b/crates/ruff_wasm/Cargo.toml index f3707a7362..45a1b64095 100644 --- a/crates/ruff_wasm/Cargo.toml +++ b/crates/ruff_wasm/Cargo.toml @@ -26,6 +26,7 @@ ruff_python_formatter = { path = "../ruff_python_formatter" } ruff_python_index = { path = "../ruff_python_index" } ruff_python_parser = { path = "../ruff_python_parser" } ruff_source_file = { path = "../ruff_source_file" } +ruff_text_size = { path = "../ruff_text_size" } console_error_panic_hook = { version = "0.1.7", optional = true } console_log = { version = "1.0.0" } diff --git a/crates/ruff_wasm/src/lib.rs b/crates/ruff_wasm/src/lib.rs index 602b18f552..5c0bca86e1 100644 --- a/crates/ruff_wasm/src/lib.rs +++ b/crates/ruff_wasm/src/lib.rs @@ -1,9 +1,6 @@ use std::path::Path; use js_sys::Error; - -use ruff_python_parser::lexer::LexResult; -use ruff_python_parser::{parse_tokens, Mode}; use serde::{Deserialize, Serialize}; use wasm_bindgen::prelude::*; @@ -26,8 +23,11 @@ use ruff_python_ast::PySourceType; use ruff_python_codegen::Stylist; use ruff_python_formatter::{format_module, format_node, PyFormatOptions}; use ruff_python_index::{CommentRangesBuilder, Indexer}; +use ruff_python_parser::lexer::LexResult; use ruff_python_parser::AsMode; +use ruff_python_parser::{parse_tokens, Mode}; use ruff_source_file::{Locator, SourceLocation}; +use ruff_text_size::Ranged; #[wasm_bindgen(typescript_custom_section)] const TYPES: &'static str = r#"