Skip to content

Commit

Permalink
Separate LineWidth and LineLength implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanPlasse committed May 22, 2023
1 parent a92d6fb commit 451d494
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 168 deletions.
9 changes: 5 additions & 4 deletions crates/ruff/src/message/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::fs::relativize_path;
use crate::message::diff::Diff;
use crate::message::{Emitter, EmitterContext, Message};
use crate::registry::AsRule;
use crate::settings::line_width::{TabSize, Width};
use crate::settings::line_width::{LineWidth, TabSize};

bitflags! {
#[derive(Default)]
Expand Down Expand Up @@ -243,15 +243,16 @@ fn replace_whitespace(source: &str, annotation_range: TextRange) -> SourceCode {
let mut result = String::new();
let mut last_end = 0;
let mut range = annotation_range;
let mut line_width = Width::new(TAB_SIZE);
let mut line_width = LineWidth::new(TAB_SIZE);

for (index, c) in source.char_indices() {
let old_width = line_width.width();
let old_width = line_width.get();
line_width = line_width.add_char(c);

if matches!(c, '\t') {
// SAFETY: The difference is a value in the range [1..TAB_SIZE] which is guaranteed to be less than `u32`.
#[allow(clippy::cast_possible_truncation)]
let tab_width = (line_width.width() - old_width) as u32;
let tab_width = (line_width.get() - old_width) as u32;

if index < usize::from(annotation_range.start()) {
range += TextSize::new(tab_width - 1);
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use ruff_python_semantic::model::SemanticModel;
use crate::checkers::ast::Checker;
use crate::registry::AsRule;
use crate::rules::flake8_simplify::rules::fix_if;
use crate::settings::line_width::Width;
use crate::settings::line_width::LineWidth;

fn compare_expr(expr1: &ComparableExpr, expr2: &ComparableExpr) -> bool {
expr1.eq(expr2)
Expand Down Expand Up @@ -289,7 +289,7 @@ pub(crate) fn nested_if_statements(
.unwrap_or_default()
.universal_newlines()
.all(|line| {
Width::new(checker.settings.tab_size).add_str(&line)
LineWidth::new(checker.settings.tab_size).add_str(&line)
<= checker.settings.line_length
})
{
Expand Down Expand Up @@ -511,7 +511,7 @@ pub(crate) fn use_ternary_operator(checker: &mut Checker, stmt: &Stmt, parent: O

// Don't flag if the resulting expression would exceed the maximum line length.
let line_start = checker.locator.line_start(stmt.start());
if Width::new(checker.settings.tab_size)
if LineWidth::new(checker.settings.tab_size)
.add_str(&checker.locator.contents()[TextRange::new(line_start, stmt.start())])
.add_str(&contents)
> checker.settings.line_length
Expand Down Expand Up @@ -867,7 +867,7 @@ pub(crate) fn use_dict_get_with_default(

// Don't flag if the resulting expression would exceed the maximum line length.
let line_start = checker.locator.line_start(stmt.start());
if Width::new(checker.settings.tab_size)
if LineWidth::new(checker.settings.tab_size)
.add_str(&checker.locator.contents()[TextRange::new(line_start, stmt.start())])
.add_str(&contents)
> checker.settings.line_length
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/flake8_simplify/rules/ast_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ruff_python_ast::newlines::StrExt;

use crate::checkers::ast::Checker;
use crate::registry::AsRule;
use crate::settings::line_width::Width;
use crate::settings::line_width::LineWidth;

use super::fix_with;

Expand Down Expand Up @@ -112,7 +112,7 @@ pub(crate) fn multiple_with_statements(
.unwrap_or_default()
.universal_newlines()
.all(|line| {
Width::new(checker.settings.tab_size).add_str(&line)
LineWidth::new(checker.settings.tab_size).add_str(&line)
<= checker.settings.line_length
})
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ruff_python_ast::source_code::Generator;

use crate::checkers::ast::Checker;
use crate::registry::{AsRule, Rule};
use crate::settings::line_width::Width;
use crate::settings::line_width::LineWidth;

#[violation]
pub struct ReimplementedBuiltin {
Expand Down Expand Up @@ -224,7 +224,7 @@ pub(crate) fn convert_for_loop_to_any_all(

// Don't flag if the resulting expression would exceed the maximum line length.
let line_start = checker.locator.line_start(stmt.start());
if Width::new(checker.settings.tab_size)
if LineWidth::new(checker.settings.tab_size)
.add_str(&checker.locator.contents()[TextRange::new(line_start, stmt.start())])
.add_str(&contents)
> checker.settings.line_length
Expand Down Expand Up @@ -317,7 +317,7 @@ pub(crate) fn convert_for_loop_to_any_all(

// Don't flag if the resulting expression would exceed the maximum line length.
let line_start = checker.locator.line_start(stmt.start());
if Width::new(checker.settings.tab_size)
if LineWidth::new(checker.settings.tab_size)
.add_str(&checker.locator.contents()[TextRange::new(line_start, stmt.start())])
.add_str(&contents)
> checker.settings.line_length
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff/src/rules/isort/format.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ruff_python_ast::source_code::Stylist;

use crate::settings::line_width::{LineLength, Width};
use crate::settings::line_width::{LineLength, LineWidth};

use super::types::{AliasData, CommentSet, ImportFromData, Importable};

Expand Down Expand Up @@ -46,7 +46,7 @@ pub(crate) fn format_import_from(
comments: &CommentSet,
aliases: &[(AliasData, CommentSet)],
line_length: LineLength,
indentation_width: Width,
indentation_width: LineWidth,
stylist: &Stylist,
force_wrap_aliases: bool,
is_first: bool,
Expand Down Expand Up @@ -103,8 +103,8 @@ fn format_single_line(
aliases: &[(AliasData, CommentSet)],
is_first: bool,
stylist: &Stylist,
indentation_width: Width,
) -> (String, Width) {
indentation_width: LineWidth,
) -> (String, LineWidth) {
let mut output = String::with_capacity(CAPACITY);
let mut line_width = indentation_width;

Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/rules/isort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use types::{AliasData, EitherImport, TrailingComma};

use crate::rules::isort::categorize::KnownModules;
use crate::rules::isort::types::ImportBlock;
use crate::settings::line_width::{LineLength, Width};
use crate::settings::line_width::{LineLength, LineWidth};
use crate::settings::types::PythonVersion;

mod annotate;
Expand Down Expand Up @@ -67,7 +67,7 @@ pub(crate) fn format_imports(
comments: Vec<Comment>,
locator: &Locator,
line_length: LineLength,
indentation_width: Width,
indentation_width: LineWidth,
stylist: &Stylist,
src: &[PathBuf],
package: Option<&Path>,
Expand Down Expand Up @@ -166,7 +166,7 @@ pub(crate) fn format_imports(
fn format_import_block(
block: ImportBlock,
line_length: LineLength,
indentation_width: Width,
indentation_width: LineWidth,
stylist: &Stylist,
src: &[PathBuf],
package: Option<&Path>,
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/isort/rules/organize_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ruff_python_ast::source_code::{Indexer, Locator, Stylist};
use ruff_python_ast::whitespace::leading_space;

use crate::registry::AsRule;
use crate::settings::line_width::Width;
use crate::settings::line_width::LineWidth;
use crate::settings::Settings;

use super::super::block::Block;
Expand Down Expand Up @@ -118,7 +118,7 @@ pub(crate) fn organize_imports(
comments,
locator,
settings.line_length,
Width::new(settings.tab_size).add_str(indentation),
LineWidth::new(settings.tab_size).add_str(indentation),
stylist,
&settings.src,
package,
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff/src/rules/pycodestyle/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use unicode_width::UnicodeWidthStr;
use ruff_python_ast::newlines::Line;
use ruff_python_ast::source_code::Generator;

use crate::settings::line_width::{LineLength, TabSize, Width};
use crate::settings::line_width::{LineLength, LineWidth, TabSize};

pub(crate) fn is_ambiguous_name(name: &str) -> bool {
name == "l" || name == "I" || name == "O"
Expand Down Expand Up @@ -34,7 +34,7 @@ pub(super) fn is_overlong(
tab_size: TabSize,
) -> Option<Overlong> {
let mut start_offset = line.start();
let mut width = Width::new(tab_size);
let mut width = LineWidth::new(tab_size);

for c in line.chars() {
if width < limit {
Expand Down Expand Up @@ -66,14 +66,14 @@ pub(super) fn is_overlong(
// begins before the limit.
let last_chunk = chunks.last().unwrap_or(second_chunk);
if last_chunk.contains("://") {
if width.width() - last_chunk.width() <= limit.width() {
if width.get() - last_chunk.width() <= limit.get() {
return None;
}
}

Some(Overlong {
range: TextRange::new(start_offset, line.end()),
width: width.width(),
width: width.get(),
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub(crate) fn doc_line_too_long(line: &Line, settings: &Settings) -> Option<Diag
)
.map(|overlong| {
Diagnostic::new(
DocLineTooLong(overlong.width(), limit.width()),
DocLineTooLong(overlong.width(), limit.get()),
overlong.range(),
)
})
Expand Down
7 changes: 1 addition & 6 deletions crates/ruff/src/rules/pycodestyle/rules/line_too_long.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,5 @@ pub(crate) fn line_too_long(line: &Line, settings: &Settings) -> Option<Diagnost
&settings.task_tags,
settings.tab_size,
)
.map(|overlong| {
Diagnostic::new(
LineTooLong(overlong.width(), limit.width()),
overlong.range(),
)
})
.map(|overlong| Diagnostic::new(LineTooLong(overlong.width(), limit.get()), overlong.range()))
}

0 comments on commit 451d494

Please sign in to comment.