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.
This commit is contained in:
Charlie Marsh 2023-08-27 15:03:08 -04:00 committed by GitHub
parent fc89976c24
commit 059757a8c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 104 additions and 115 deletions

View file

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

View file

@ -1,4 +1,4 @@
use ruff_text_size::TextSize;
use ruff_text_size::{Ranged, TextSize};
use ruff_diagnostics::Edit;

View file

@ -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>,

View file

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

View file

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

View file

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

View file

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

View file

@ -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<NoqaCode>,
}
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<NoqaDirectiveLine<'a>>,

View file

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

View file

@ -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(())
}

View file

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

View file

@ -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())

View file

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

View file

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

View file

@ -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.

View file

@ -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<Item = Edit>) -> Self {
let mut edits: Vec<Edit> = 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<Item = Edit>) -> Self {
let mut edits: Vec<Edit> = 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<Item = Edit>) -> Self {
let mut edits: Vec<Edit> = std::iter::once(edit).chain(rest).collect();
edits.sort_by_key(Edit::start);
edits.sort_by_key(Ranged::start);
Self {
edits,
applicability: Applicability::Manual,

View file

@ -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)]

View file

@ -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 {

View file

@ -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();

View file

@ -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)]

View file

@ -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" }

View file

@ -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#"