mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:24:57 +00:00
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:
parent
fc89976c24
commit
059757a8c8
23 changed files with 104 additions and 115 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -2531,6 +2531,7 @@ dependencies = [
|
||||||
"ruff_python_index",
|
"ruff_python_index",
|
||||||
"ruff_python_parser",
|
"ruff_python_parser",
|
||||||
"ruff_source_file",
|
"ruff_source_file",
|
||||||
|
"ruff_text_size",
|
||||||
"serde",
|
"serde",
|
||||||
"serde-wasm-bindgen",
|
"serde-wasm-bindgen",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use std::collections::BTreeSet;
|
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 rustc_hash::{FxHashMap, FxHashSet};
|
||||||
|
|
||||||
use ruff_diagnostics::{Diagnostic, Edit, Fix, IsolationLevel};
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use ruff_text_size::TextSize;
|
use ruff_text_size::{Ranged, TextSize};
|
||||||
|
|
||||||
use ruff_diagnostics::{Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{Diagnostic, Edit, Fix};
|
||||||
use ruff_source_file::Locator;
|
use ruff_source_file::Locator;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use ruff_text_size::TextSize;
|
use ruff_text_size::{Ranged, TextSize};
|
||||||
|
|
||||||
use ruff_diagnostics::Edit;
|
use ruff_diagnostics::Edit;
|
||||||
|
|
||||||
|
|
|
@ -164,7 +164,7 @@ impl<'a> SectionContexts<'a> {
|
||||||
lines.peek(),
|
lines.peek(),
|
||||||
) {
|
) {
|
||||||
if let Some(mut last) = last.take() {
|
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);
|
contexts.push(last);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,7 +181,7 @@ impl<'a> SectionContexts<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(mut last) = last.take() {
|
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);
|
contexts.push(last);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -267,6 +267,12 @@ struct SectionContextData {
|
||||||
summary_full_end: TextSize,
|
summary_full_end: TextSize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Ranged for SectionContextData {
|
||||||
|
fn range(&self) -> TextRange {
|
||||||
|
self.range
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) struct SectionContext<'a> {
|
pub(crate) struct SectionContext<'a> {
|
||||||
data: &'a SectionContextData,
|
data: &'a SectionContextData,
|
||||||
docstring_body: DocstringBody<'a>,
|
docstring_body: DocstringBody<'a>,
|
||||||
|
|
|
@ -17,6 +17,7 @@ use ruff_python_codegen::Stylist;
|
||||||
use ruff_python_index::Indexer;
|
use ruff_python_index::Indexer;
|
||||||
|
|
||||||
use ruff_source_file::{Locator, SourceFileBuilder};
|
use ruff_source_file::{Locator, SourceFileBuilder};
|
||||||
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
use crate::autofix::{fix_file, FixResult};
|
use crate::autofix::{fix_file, FixResult};
|
||||||
use crate::checkers::ast::check_ast;
|
use crate::checkers::ast::check_ast;
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::num::NonZeroUsize;
|
||||||
|
|
||||||
use colored::{Color, ColoredString, Colorize, Styles};
|
use colored::{Color, ColoredString, Colorize, Styles};
|
||||||
|
|
||||||
use ruff_text_size::{TextRange, TextSize};
|
use ruff_text_size::{Ranged, TextRange, TextSize};
|
||||||
use similar::{ChangeTag, TextDiff};
|
use similar::{ChangeTag, TextDiff};
|
||||||
|
|
||||||
use ruff_diagnostics::{Applicability, Fix};
|
use ruff_diagnostics::{Applicability, Fix};
|
||||||
|
|
|
@ -6,6 +6,7 @@ use serde_json::{json, Value};
|
||||||
|
|
||||||
use ruff_diagnostics::Edit;
|
use ruff_diagnostics::Edit;
|
||||||
use ruff_source_file::SourceCode;
|
use ruff_source_file::SourceCode;
|
||||||
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
use crate::message::{Emitter, EmitterContext, Message};
|
use crate::message::{Emitter, EmitterContext, Message};
|
||||||
use crate::registry::AsRule;
|
use crate::registry::AsRule;
|
||||||
|
|
|
@ -15,7 +15,7 @@ pub use junit::JunitEmitter;
|
||||||
pub use pylint::PylintEmitter;
|
pub use pylint::PylintEmitter;
|
||||||
use ruff_diagnostics::{Diagnostic, DiagnosticKind, Fix};
|
use ruff_diagnostics::{Diagnostic, DiagnosticKind, Fix};
|
||||||
use ruff_source_file::{SourceFile, SourceLocation};
|
use ruff_source_file::{SourceFile, SourceLocation};
|
||||||
use ruff_text_size::{TextRange, TextSize};
|
use ruff_text_size::{Ranged, TextRange, TextSize};
|
||||||
pub use text::TextEmitter;
|
pub use text::TextEmitter;
|
||||||
|
|
||||||
use crate::jupyter::Notebook;
|
use crate::jupyter::Notebook;
|
||||||
|
@ -66,14 +66,6 @@ impl Message {
|
||||||
pub fn compute_end_location(&self) -> SourceLocation {
|
pub fn compute_end_location(&self) -> SourceLocation {
|
||||||
self.file.to_source_code().source_location(self.end())
|
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 {
|
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> {
|
struct MessageWithLocation<'a> {
|
||||||
message: &'a Message,
|
message: &'a Message,
|
||||||
start_location: SourceLocation,
|
start_location: SourceLocation,
|
||||||
|
@ -154,7 +152,7 @@ mod tests {
|
||||||
|
|
||||||
use ruff_diagnostics::{Diagnostic, DiagnosticKind, Edit, Fix};
|
use ruff_diagnostics::{Diagnostic, DiagnosticKind, Edit, Fix};
|
||||||
use ruff_source_file::SourceFileBuilder;
|
use ruff_source_file::SourceFileBuilder;
|
||||||
use ruff_text_size::{TextRange, TextSize};
|
use ruff_text_size::{Ranged, TextRange, TextSize};
|
||||||
|
|
||||||
use crate::message::{Emitter, EmitterContext, Message};
|
use crate::message::{Emitter, EmitterContext, Message};
|
||||||
|
|
||||||
|
|
|
@ -536,7 +536,7 @@ fn add_noqa_inner(
|
||||||
let rule = diagnostic.kind.rule();
|
let rule = diagnostic.kind.rule();
|
||||||
if !includes(rule, codes) {
|
if !includes(rule, codes) {
|
||||||
matches_by_line
|
matches_by_line
|
||||||
.entry(directive_line.range.start())
|
.entry(directive_line.start())
|
||||||
.or_insert_with(|| {
|
.or_insert_with(|| {
|
||||||
(RuleSet::default(), Some(&directive_line.directive))
|
(RuleSet::default(), Some(&directive_line.directive))
|
||||||
})
|
})
|
||||||
|
@ -644,6 +644,13 @@ pub(crate) struct NoqaDirectiveLine<'a> {
|
||||||
pub(crate) matches: Vec<NoqaCode>,
|
pub(crate) matches: Vec<NoqaCode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Ranged for NoqaDirectiveLine<'_> {
|
||||||
|
/// The range of the `noqa` directive.
|
||||||
|
fn range(&self) -> TextRange {
|
||||||
|
self.range
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub(crate) struct NoqaDirectives<'a> {
|
pub(crate) struct NoqaDirectives<'a> {
|
||||||
inner: Vec<NoqaDirectiveLine<'a>>,
|
inner: Vec<NoqaDirectiveLine<'a>>,
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
use itertools::Itertools;
|
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::{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_parser::lexer::{LexResult, Spanned};
|
||||||
|
use ruff_python_parser::Tok;
|
||||||
use ruff_source_file::Locator;
|
use ruff_source_file::Locator;
|
||||||
|
use ruff_text_size::{Ranged, TextRange};
|
||||||
|
|
||||||
use crate::registry::Rule;
|
use crate::registry::Rule;
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
|
|
|
@ -317,11 +317,11 @@ mod tests {
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use ruff_text_size::Ranged;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use test_case::test_case;
|
use test_case::test_case;
|
||||||
|
|
||||||
use crate::assert_messages;
|
use crate::assert_messages;
|
||||||
use crate::message::Message;
|
|
||||||
use crate::registry::Rule;
|
use crate::registry::Rule;
|
||||||
use crate::rules::isort::categorize::{ImportSection, KnownModules};
|
use crate::rules::isort::categorize::{ImportSection, KnownModules};
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
|
@ -660,7 +660,7 @@ mod tests {
|
||||||
..Settings::for_rule(Rule::UnsortedImports)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
diagnostics.sort_by_key(Message::start);
|
diagnostics.sort_by_key(Ranged::start);
|
||||||
assert_messages!(snapshot, diagnostics);
|
assert_messages!(snapshot, diagnostics);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -688,7 +688,7 @@ mod tests {
|
||||||
..Settings::for_rule(Rule::UnsortedImports)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
diagnostics.sort_by_key(Message::start);
|
diagnostics.sort_by_key(Ranged::start);
|
||||||
assert_messages!(snapshot, diagnostics);
|
assert_messages!(snapshot, diagnostics);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -718,7 +718,7 @@ mod tests {
|
||||||
..Settings::for_rule(Rule::UnsortedImports)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
diagnostics.sort_by_key(Message::start);
|
diagnostics.sort_by_key(Ranged::start);
|
||||||
assert_messages!(snapshot, diagnostics);
|
assert_messages!(snapshot, diagnostics);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -746,7 +746,7 @@ mod tests {
|
||||||
..Settings::for_rule(Rule::UnsortedImports)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
diagnostics.sort_by_key(Message::start);
|
diagnostics.sort_by_key(Ranged::start);
|
||||||
assert_messages!(snapshot, diagnostics);
|
assert_messages!(snapshot, diagnostics);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -766,7 +766,7 @@ mod tests {
|
||||||
..Settings::for_rule(Rule::UnsortedImports)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
diagnostics.sort_by_key(Message::start);
|
diagnostics.sort_by_key(Ranged::start);
|
||||||
assert_messages!(snapshot, diagnostics);
|
assert_messages!(snapshot, diagnostics);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -937,7 +937,7 @@ mod tests {
|
||||||
..Settings::for_rule(Rule::UnsortedImports)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
diagnostics.sort_by_key(Message::start);
|
diagnostics.sort_by_key(Ranged::start);
|
||||||
assert_messages!(snapshot, diagnostics);
|
assert_messages!(snapshot, diagnostics);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -962,7 +962,7 @@ mod tests {
|
||||||
..Settings::for_rule(Rule::UnsortedImports)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
diagnostics.sort_by_key(Message::start);
|
diagnostics.sort_by_key(Ranged::start);
|
||||||
assert_messages!(snapshot, diagnostics);
|
assert_messages!(snapshot, diagnostics);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -983,7 +983,7 @@ mod tests {
|
||||||
..Settings::for_rule(Rule::UnsortedImports)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
diagnostics.sort_by_key(Message::start);
|
diagnostics.sort_by_key(Ranged::start);
|
||||||
assert_messages!(snapshot, diagnostics);
|
assert_messages!(snapshot, diagnostics);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1002,7 +1002,7 @@ mod tests {
|
||||||
..Settings::for_rule(Rule::UnsortedImports)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
diagnostics.sort_by_key(Message::start);
|
diagnostics.sort_by_key(Ranged::start);
|
||||||
assert_messages!(snapshot, diagnostics);
|
assert_messages!(snapshot, diagnostics);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use memchr::memchr_iter;
|
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_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
|
|
@ -15,13 +15,13 @@ mod tests {
|
||||||
|
|
||||||
use test_case::test_case;
|
use test_case::test_case;
|
||||||
|
|
||||||
use ruff_diagnostics::Diagnostic;
|
|
||||||
use ruff_python_ast::PySourceType;
|
use ruff_python_ast::PySourceType;
|
||||||
use ruff_python_codegen::Stylist;
|
use ruff_python_codegen::Stylist;
|
||||||
use ruff_python_index::Indexer;
|
use ruff_python_index::Indexer;
|
||||||
use ruff_python_parser::AsMode;
|
use ruff_python_parser::AsMode;
|
||||||
use ruff_python_trivia::textwrap::dedent;
|
use ruff_python_trivia::textwrap::dedent;
|
||||||
use ruff_source_file::Locator;
|
use ruff_source_file::Locator;
|
||||||
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
use crate::linter::{check_path, LinterResult};
|
use crate::linter::{check_path, LinterResult};
|
||||||
use crate::registry::{AsRule, Linter, Rule};
|
use crate::registry::{AsRule, Linter, Rule};
|
||||||
|
@ -535,7 +535,7 @@ mod tests {
|
||||||
None,
|
None,
|
||||||
source_type,
|
source_type,
|
||||||
);
|
);
|
||||||
diagnostics.sort_by_key(Diagnostic::start);
|
diagnostics.sort_by_key(Ranged::start);
|
||||||
let actual = diagnostics
|
let actual = diagnostics
|
||||||
.iter()
|
.iter()
|
||||||
.map(|diagnostic| diagnostic.kind.rule())
|
.map(|diagnostic| diagnostic.kind.rule())
|
||||||
|
|
|
@ -17,6 +17,7 @@ use ruff_python_parser::lexer::LexResult;
|
||||||
use ruff_python_parser::AsMode;
|
use ruff_python_parser::AsMode;
|
||||||
use ruff_python_trivia::textwrap::dedent;
|
use ruff_python_trivia::textwrap::dedent;
|
||||||
use ruff_source_file::{Locator, SourceFileBuilder};
|
use ruff_source_file::{Locator, SourceFileBuilder};
|
||||||
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
use crate::autofix::{fix_file, FixResult};
|
use crate::autofix::{fix_file, FixResult};
|
||||||
use crate::directives;
|
use crate::directives;
|
||||||
|
|
|
@ -3,7 +3,7 @@ use log::error;
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use ruff_text_size::{TextRange, TextSize};
|
use ruff_text_size::{Ranged, TextRange, TextSize};
|
||||||
|
|
||||||
use crate::Fix;
|
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.
|
/// Set the location of the diagnostic's parent node.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn set_parent(&mut self, parent: TextSize) {
|
pub fn set_parent(&mut self, parent: TextSize) {
|
||||||
self.parent = Some(parent);
|
self.parent = Some(parent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Ranged for Diagnostic {
|
||||||
|
fn range(&self) -> TextRange {
|
||||||
|
self.range
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::cmp::Ordering;
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
use serde::{Deserialize, Serialize};
|
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
|
/// A text edit to be applied to a source file. Inserts, deletes, or replaces
|
||||||
/// content at a given location.
|
/// content at a given location.
|
||||||
|
@ -62,20 +62,6 @@ impl Edit {
|
||||||
self.content.as_deref()
|
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 {
|
fn kind(&self) -> EditOperationKind {
|
||||||
if self.content.is_none() {
|
if self.content.is_none() {
|
||||||
EditOperationKind::Deletion
|
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)]
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
||||||
enum EditOperationKind {
|
enum EditOperationKind {
|
||||||
/// Edit that inserts new content into the source document.
|
/// Edit that inserts new content into the source document.
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use ruff_text_size::TextSize;
|
use ruff_text_size::{Ranged, TextSize};
|
||||||
|
|
||||||
use crate::edit::Edit;
|
use crate::edit::Edit;
|
||||||
|
|
||||||
|
@ -88,7 +88,7 @@ impl Fix {
|
||||||
/// Create a new [`Fix`] with [automatic applicability](Applicability::Automatic) from multiple [`Edit`] elements.
|
/// 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 {
|
pub fn automatic_edits(edit: Edit, rest: impl IntoIterator<Item = Edit>) -> Self {
|
||||||
let mut edits: Vec<Edit> = std::iter::once(edit).chain(rest).collect();
|
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 {
|
Self {
|
||||||
edits,
|
edits,
|
||||||
applicability: Applicability::Automatic,
|
applicability: Applicability::Automatic,
|
||||||
|
@ -108,7 +108,7 @@ impl Fix {
|
||||||
/// Create a new [`Fix`] with [suggested applicability](Applicability::Suggested) from multiple [`Edit`] elements.
|
/// 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 {
|
pub fn suggested_edits(edit: Edit, rest: impl IntoIterator<Item = Edit>) -> Self {
|
||||||
let mut edits: Vec<Edit> = std::iter::once(edit).chain(rest).collect();
|
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 {
|
Self {
|
||||||
edits,
|
edits,
|
||||||
applicability: Applicability::Suggested,
|
applicability: Applicability::Suggested,
|
||||||
|
@ -128,7 +128,7 @@ impl Fix {
|
||||||
/// Create a new [`Fix`] with [manual applicability](Applicability::Manual) from multiple [`Edit`] elements.
|
/// 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 {
|
pub fn manual_edits(edit: Edit, rest: impl IntoIterator<Item = Edit>) -> Self {
|
||||||
let mut edits: Vec<Edit> = std::iter::once(edit).chain(rest).collect();
|
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 {
|
Self {
|
||||||
edits,
|
edits,
|
||||||
applicability: Applicability::Manual,
|
applicability: Applicability::Manual,
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
mod call_stack;
|
use std::num::NonZeroU8;
|
||||||
mod line_suffixes;
|
|
||||||
mod printer_options;
|
use drop_bomb::DebugDropBomb;
|
||||||
mod queue;
|
use unicode_width::UnicodeWidthChar;
|
||||||
mod stack;
|
|
||||||
|
pub use printer_options::*;
|
||||||
|
use ruff_text_size::{Ranged, TextLen, TextSize};
|
||||||
|
|
||||||
use crate::format_element::document::Document;
|
use crate::format_element::document::Document;
|
||||||
use crate::format_element::tag::{Condition, GroupMode};
|
use crate::format_element::tag::{Condition, GroupMode};
|
||||||
|
@ -21,11 +23,12 @@ use crate::{
|
||||||
ActualStart, FormatElement, GroupId, IndentStyle, InvalidDocumentError, PrintError,
|
ActualStart, FormatElement, GroupId, IndentStyle, InvalidDocumentError, PrintError,
|
||||||
PrintResult, Printed, SourceMarker, TextRange,
|
PrintResult, Printed, SourceMarker, TextRange,
|
||||||
};
|
};
|
||||||
use drop_bomb::DebugDropBomb;
|
|
||||||
pub use printer_options::*;
|
mod call_stack;
|
||||||
use ruff_text_size::{TextLen, TextSize};
|
mod line_suffixes;
|
||||||
use std::num::NonZeroU8;
|
mod printer_options;
|
||||||
use unicode_width::UnicodeWidthChar;
|
mod queue;
|
||||||
|
mod stack;
|
||||||
|
|
||||||
/// Prints the format elements into a string
|
/// Prints the format elements into a string
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use ruff_text_size::{TextRange, TextSize};
|
|
||||||
use std::fmt::{Debug, Formatter};
|
use std::fmt::{Debug, Formatter};
|
||||||
|
|
||||||
|
use ruff_text_size::{Ranged, TextRange};
|
||||||
|
|
||||||
/// The source code of a document that gets formatted
|
/// The source code of a document that gets formatted
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default)]
|
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default)]
|
||||||
pub struct SourceCode<'a> {
|
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?");
|
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]
|
&code.text[self.range]
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub const fn range(&self) -> TextRange {
|
impl Ranged for SourceCodeSlice {
|
||||||
|
fn range(&self) -> TextRange {
|
||||||
self.range
|
self.range
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn start(&self) -> TextSize {
|
|
||||||
self.range.start()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const fn end(&self) -> TextSize {
|
|
||||||
self.range.end()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debug for SourceCodeSlice {
|
impl Debug for SourceCodeSlice {
|
||||||
|
|
|
@ -434,7 +434,7 @@ fn handle_own_line_comment_around_body<'a>(
|
||||||
// ```
|
// ```
|
||||||
let maybe_token = SimpleTokenizer::new(
|
let maybe_token = SimpleTokenizer::new(
|
||||||
locator.contents(),
|
locator.contents(),
|
||||||
TextRange::new(preceding.end(), comment.slice().start()),
|
TextRange::new(preceding.end(), comment.start()),
|
||||||
)
|
)
|
||||||
.skip_trivia()
|
.skip_trivia()
|
||||||
.next();
|
.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
|
// 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.
|
// 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()
|
.unwrap_or_default()
|
||||||
.len();
|
.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
|
// 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).
|
// 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()
|
.unwrap_or_default()
|
||||||
.len();
|
.len();
|
||||||
|
|
||||||
|
@ -848,7 +848,7 @@ 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!(
|
||||||
SimpleTokenizer::up_to_without_back_comment(comment.slice().start(), locator.contents())
|
SimpleTokenizer::up_to_without_back_comment(comment.start(), locator.contents())
|
||||||
.skip_trivia()
|
.skip_trivia()
|
||||||
.next_back(),
|
.next_back(),
|
||||||
Some(SimpleToken {
|
Some(SimpleToken {
|
||||||
|
@ -881,7 +881,7 @@ fn handle_slice_comments<'a>(
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(node) = node {
|
if let Some(node) = node {
|
||||||
if comment.slice().start() < node.start() {
|
if comment.start() < node.start() {
|
||||||
CommentPlacement::leading(node.as_ref(), comment)
|
CommentPlacement::leading(node.as_ref(), comment)
|
||||||
} else {
|
} else {
|
||||||
// If a trailing comment is an end of line comment that's fine because we have a node
|
// 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(
|
let mut tokens = SimpleTokenizer::new(
|
||||||
locator.contents(),
|
locator.contents(),
|
||||||
TextRange::new(preceding_end, comment.slice().start()),
|
TextRange::new(preceding_end, comment.start()),
|
||||||
)
|
)
|
||||||
.skip_trivia()
|
.skip_trivia()
|
||||||
.skip_while(|token| token.kind == SimpleTokenKind::RParen);
|
.skip_while(|token| token.kind == SimpleTokenKind::RParen);
|
||||||
|
@ -1138,7 +1138,7 @@ fn handle_expr_if_comment<'a>(
|
||||||
locator.contents(),
|
locator.contents(),
|
||||||
);
|
);
|
||||||
// Between `if` and `test`
|
// 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);
|
return CommentPlacement::leading(test.as_ref(), comment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1148,9 +1148,7 @@ fn handle_expr_if_comment<'a>(
|
||||||
locator.contents(),
|
locator.contents(),
|
||||||
);
|
);
|
||||||
// Between `else` and `orelse`
|
// Between `else` and `orelse`
|
||||||
if else_token.range.start() < comment.slice().start()
|
if else_token.start() < comment.start() && comment.start() < orelse.start() {
|
||||||
&& comment.slice().start() < orelse.start()
|
|
||||||
{
|
|
||||||
return CommentPlacement::leading(orelse.as_ref(), comment);
|
return CommentPlacement::leading(orelse.as_ref(), comment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1585,7 +1583,7 @@ fn handle_comprehension_comment<'a>(
|
||||||
// in c
|
// in c
|
||||||
// ]
|
// ]
|
||||||
// ```
|
// ```
|
||||||
if comment.slice().start() < in_token.start() {
|
if comment.start() < in_token.start() {
|
||||||
// attach as dangling comments on the target
|
// attach as dangling comments on the target
|
||||||
// (to be rendered as leading on the "in")
|
// (to be rendered as leading on the "in")
|
||||||
return if is_own_line {
|
return if is_own_line {
|
||||||
|
@ -1604,7 +1602,7 @@ fn handle_comprehension_comment<'a>(
|
||||||
// c
|
// c
|
||||||
// ]
|
// ]
|
||||||
// ```
|
// ```
|
||||||
if comment.slice().start() < comprehension.iter.start() {
|
if comment.start() < comprehension.iter.start() {
|
||||||
return if is_own_line {
|
return if is_own_line {
|
||||||
CommentPlacement::Default(comment)
|
CommentPlacement::Default(comment)
|
||||||
} else {
|
} else {
|
||||||
|
@ -1639,12 +1637,10 @@ fn handle_comprehension_comment<'a>(
|
||||||
locator.contents(),
|
locator.contents(),
|
||||||
);
|
);
|
||||||
if is_own_line {
|
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);
|
return CommentPlacement::dangling(if_node, comment);
|
||||||
}
|
}
|
||||||
} else if if_token.start() < comment.slice().start()
|
} else if if_token.start() < comment.start() && comment.start() < if_node.start() {
|
||||||
&& comment.slice().start() < if_node.start()
|
|
||||||
{
|
|
||||||
return CommentPlacement::dangling(if_node, comment);
|
return CommentPlacement::dangling(if_node, comment);
|
||||||
}
|
}
|
||||||
last_end = if_node.end();
|
last_end = if_node.end();
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use memchr::{memchr2, memchr3, memrchr3_iter};
|
use memchr::{memchr2, memchr3, memrchr3_iter};
|
||||||
use unic_ucd_ident::{is_xid_continue, is_xid_start};
|
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};
|
use crate::{is_python_whitespace, Cursor};
|
||||||
|
|
||||||
|
@ -131,19 +131,12 @@ impl SimpleToken {
|
||||||
pub const fn kind(&self) -> SimpleTokenKind {
|
pub const fn kind(&self) -> SimpleTokenKind {
|
||||||
self.kind
|
self.kind
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(unused)]
|
impl Ranged for SimpleToken {
|
||||||
pub const fn range(&self) -> TextRange {
|
fn range(&self) -> TextRange {
|
||||||
self.range
|
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)]
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
||||||
|
|
|
@ -26,6 +26,7 @@ ruff_python_formatter = { path = "../ruff_python_formatter" }
|
||||||
ruff_python_index = { path = "../ruff_python_index" }
|
ruff_python_index = { path = "../ruff_python_index" }
|
||||||
ruff_python_parser = { path = "../ruff_python_parser" }
|
ruff_python_parser = { path = "../ruff_python_parser" }
|
||||||
ruff_source_file = { path = "../ruff_source_file" }
|
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_error_panic_hook = { version = "0.1.7", optional = true }
|
||||||
console_log = { version = "1.0.0" }
|
console_log = { version = "1.0.0" }
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use js_sys::Error;
|
use js_sys::Error;
|
||||||
|
|
||||||
use ruff_python_parser::lexer::LexResult;
|
|
||||||
use ruff_python_parser::{parse_tokens, Mode};
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
@ -26,8 +23,11 @@ use ruff_python_ast::PySourceType;
|
||||||
use ruff_python_codegen::Stylist;
|
use ruff_python_codegen::Stylist;
|
||||||
use ruff_python_formatter::{format_module, format_node, PyFormatOptions};
|
use ruff_python_formatter::{format_module, format_node, PyFormatOptions};
|
||||||
use ruff_python_index::{CommentRangesBuilder, Indexer};
|
use ruff_python_index::{CommentRangesBuilder, Indexer};
|
||||||
|
use ruff_python_parser::lexer::LexResult;
|
||||||
use ruff_python_parser::AsMode;
|
use ruff_python_parser::AsMode;
|
||||||
|
use ruff_python_parser::{parse_tokens, Mode};
|
||||||
use ruff_source_file::{Locator, SourceLocation};
|
use ruff_source_file::{Locator, SourceLocation};
|
||||||
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
#[wasm_bindgen(typescript_custom_section)]
|
#[wasm_bindgen(typescript_custom_section)]
|
||||||
const TYPES: &'static str = r#"
|
const TYPES: &'static str = r#"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue