From d3b18345c521dea0732f5ffff08ed9b4d5eecde3 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 18 May 2023 10:42:05 -0400 Subject: [PATCH] Move triple-quoted string detection into `Indexer` method (#4495) --- crates/ruff/src/checkers/physical_lines.rs | 3 +- .../pycodestyle/rules/tab_indentation.rs | 27 ++++----------- .../src/source_code/indexer.rs | 33 ++++++++++++------- 3 files changed, 30 insertions(+), 33 deletions(-) diff --git a/crates/ruff/src/checkers/physical_lines.rs b/crates/ruff/src/checkers/physical_lines.rs index 5712778624..47b647677d 100644 --- a/crates/ruff/src/checkers/physical_lines.rs +++ b/crates/ruff/src/checkers/physical_lines.rs @@ -55,7 +55,6 @@ pub(crate) fn check_physical_lines( let mut commented_lines_iter = indexer.comment_ranges().iter().peekable(); let mut doc_lines_iter = doc_lines.iter().peekable(); - let string_lines = indexer.triple_quoted_string_ranges(); for (index, line) in locator.contents().universal_newlines().enumerate() { while commented_lines_iter @@ -151,7 +150,7 @@ pub(crate) fn check_physical_lines( } if enforce_tab_indentation { - if let Some(diagnostic) = tab_indentation(&line, string_lines) { + if let Some(diagnostic) = tab_indentation(&line, indexer) { diagnostics.push(diagnostic); } } diff --git a/crates/ruff/src/rules/pycodestyle/rules/tab_indentation.rs b/crates/ruff/src/rules/pycodestyle/rules/tab_indentation.rs index 90c727b7ee..6532be1fcf 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/tab_indentation.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/tab_indentation.rs @@ -3,6 +3,7 @@ use ruff_text_size::{TextLen, TextRange, TextSize}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::newlines::Line; +use ruff_python_ast::source_code::Indexer; use ruff_python_ast::whitespace::leading_space; #[violation] @@ -16,31 +17,17 @@ impl Violation for TabIndentation { } /// W191 -pub(crate) fn tab_indentation(line: &Line, string_ranges: &[TextRange]) -> Option { +pub(crate) fn tab_indentation(line: &Line, indexer: &Indexer) -> Option { let indent = leading_space(line); if let Some(tab_index) = indent.find('\t') { - let tab_offset = line.start() + TextSize::try_from(tab_index).unwrap(); - - let string_range_index = string_ranges.binary_search_by(|range| { - if tab_offset < range.start() { - std::cmp::Ordering::Greater - } else if range.contains(tab_offset) { - std::cmp::Ordering::Equal - } else { - std::cmp::Ordering::Less - } - }); - // If the tab character is within a multi-line string, abort. - if string_range_index.is_ok() { - None - } else { - Some(Diagnostic::new( + let tab_offset = line.start() + TextSize::try_from(tab_index).unwrap(); + if indexer.triple_quoted_string_range(tab_offset).is_none() { + return Some(Diagnostic::new( TabIndentation, TextRange::at(line.start(), indent.text_len()), - )) + )); } - } else { - None } + None } diff --git a/crates/ruff_python_ast/src/source_code/indexer.rs b/crates/ruff_python_ast/src/source_code/indexer.rs index ff5d56b07e..16c9ab68c9 100644 --- a/crates/ruff_python_ast/src/source_code/indexer.rs +++ b/crates/ruff_python_ast/src/source_code/indexer.rs @@ -1,11 +1,12 @@ //! Struct used to index source code, to enable efficient lookup of tokens that //! are omitted from the AST (e.g., commented lines). -use crate::source_code::Locator; use ruff_text_size::{TextRange, TextSize}; use rustpython_parser::lexer::LexResult; use rustpython_parser::{StringKind, Tok}; +use crate::source_code::Locator; + pub struct Indexer { /// Stores the ranges of comments sorted by [`TextRange::start`] in increasing order. No two ranges are overlapping. comment_ranges: Vec, @@ -105,18 +106,28 @@ impl Indexer { &self.continuation_lines } - /// Return a slice of all ranges that include a triple-quoted string. The ranges are sorted by - /// [`TextRange::start`] in increasing order. No two ranges are overlapping. - pub fn triple_quoted_string_ranges(&self) -> &[TextRange] { - &self.triple_quoted_string_ranges - } - /// Returns `true` if the given offset is part of a continuation line. pub fn is_continuation(&self, offset: TextSize, locator: &Locator) -> bool { let line_start = locator.line_start(offset); self.continuation_lines.binary_search(&line_start).is_ok() } + /// Return the [`TextRange`] of the triple-quoted-string containing a given offset. + pub fn triple_quoted_string_range(&self, offset: TextSize) -> Option { + let Ok(string_range_index) = self.triple_quoted_string_ranges.binary_search_by(|range| { + if offset < range.start() { + std::cmp::Ordering::Greater + } else if range.contains(offset) { + std::cmp::Ordering::Equal + } else { + std::cmp::Ordering::Less + } + }) else { + return None; + }; + Some(self.triple_quoted_string_ranges[string_range_index]) + } + /// Return the [`TextRange`] of the f-string containing a given offset. pub fn f_string_range(&self, offset: TextSize) -> Option { let Ok(string_range_index) = self.f_string_ranges.binary_search_by(|range| { @@ -228,7 +239,7 @@ import os let contents = r#""this is a single-quoted string""#; let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); let indexer = Indexer::from_tokens(lxr.as_slice(), &Locator::new(contents)); - assert_eq!(indexer.triple_quoted_string_ranges(), []); + assert_eq!(indexer.triple_quoted_string_ranges, []); let contents = r#" """ @@ -238,7 +249,7 @@ import os let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); let indexer = Indexer::from_tokens(lxr.as_slice(), &Locator::new(contents)); assert_eq!( - indexer.triple_quoted_string_ranges(), + indexer.triple_quoted_string_ranges, [TextRange::new(TextSize::from(13), TextSize::from(71))] ); @@ -250,7 +261,7 @@ import os let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); let indexer = Indexer::from_tokens(lxr.as_slice(), &Locator::new(contents)); assert_eq!( - indexer.triple_quoted_string_ranges(), + indexer.triple_quoted_string_ranges, [TextRange::new(TextSize::from(13), TextSize::from(107))] ); @@ -267,7 +278,7 @@ import os let lxr: Vec = lexer::lex(contents, Mode::Module).collect(); let indexer = Indexer::from_tokens(lxr.as_slice(), &Locator::new(contents)); assert_eq!( - indexer.triple_quoted_string_ranges(), + indexer.triple_quoted_string_ranges, &[ TextRange::new(TextSize::from(13), TextSize::from(85)), TextRange::new(TextSize::from(98), TextSize::from(161))