Skip to content

Commit

Permalink
Introduce LineWidth
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanPlasse committed May 20, 2023
1 parent 9e23c64 commit cda81f3
Show file tree
Hide file tree
Showing 23 changed files with 252 additions and 171 deletions.
8 changes: 5 additions & 3 deletions crates/ruff/src/checkers/physical_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ mod tests {
use ruff_python_ast::source_code::{Indexer, Locator, Stylist};

use crate::registry::Rule;
use crate::settings::options::LineWidth;
use crate::settings::Settings;

use super::check_physical_lines;
Expand All @@ -196,7 +197,7 @@ mod tests {
let indexer = Indexer::from_tokens(&tokens, &locator);
let stylist = Stylist::from_tokens(&tokens, &locator);

let check_with_max_line_length = |line_length: usize| {
let check_with_max_line_length = |line_length: LineWidth| {
check_physical_lines(
Path::new("foo.py"),
&locator,
Expand All @@ -209,7 +210,8 @@ mod tests {
},
)
};
assert_eq!(check_with_max_line_length(8), vec![]);
assert_eq!(check_with_max_line_length(8), vec![]);
let line_length = LineWidth::from_line_length(8);
assert_eq!(check_with_max_line_length(line_length), vec![]);
assert_eq!(check_with_max_line_length(line_length), vec![]);
}
}
12 changes: 6 additions & 6 deletions crates/ruff/src/flake8_to_ruff/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::rules::{
flake8_annotations, flake8_bugbear, flake8_builtins, flake8_errmsg, flake8_pytest_style,
flake8_quotes, flake8_tidy_imports, mccabe, pep8_naming, pydocstyle,
};
use crate::settings::options::Options;
use crate::settings::options::{Options, LineWidth};
use crate::settings::pyproject::Pyproject;
use crate::settings::types::PythonVersion;
use crate::warn_user;
Expand Down Expand Up @@ -119,7 +119,7 @@ pub fn convert(
options.builtins = Some(parser::parse_strings(value.as_ref()));
}
"max-line-length" | "max_line_length" => match value.parse::<usize>() {
Ok(line_length) => options.line_length = Some(line_length),
Ok(line_length) => options.line_length = Some(LineWidth::from_line_length(line_length)),
Err(e) => {
warn_user!("Unable to parse '{key}' property: {e}");
}
Expand Down Expand Up @@ -402,7 +402,7 @@ pub fn convert(
// Extract any settings from the existing `pyproject.toml`.
if let Some(black) = &external_config.black {
if let Some(line_length) = &black.line_length {
options.line_length = Some(*line_length);
options.line_length = Some(LineWidth::from_line_length(*line_length));
}

if let Some(target_version) = &black.target_version {
Expand Down Expand Up @@ -465,7 +465,7 @@ mod tests {
use crate::rule_selector::RuleSelector;
use crate::rules::pydocstyle::settings::Convention;
use crate::rules::{flake8_quotes, pydocstyle};
use crate::settings::options::Options;
use crate::settings::options::{Options, LineWidth};
use crate::settings::pyproject::Pyproject;
use crate::settings::types::PythonVersion;

Expand Down Expand Up @@ -508,7 +508,7 @@ mod tests {
Some(vec![]),
)?;
let expected = Pyproject::new(Options {
line_length: Some(100),
line_length: Some(LineWidth::from_line_length(100)),
..default_options([])
});
assert_eq!(actual, expected);
Expand All @@ -527,7 +527,7 @@ mod tests {
Some(vec![]),
)?;
let expected = Pyproject::new(Options {
line_length: Some(100),
line_length: Some(LineWidth::from_line_length(100)),
..default_options([])
});
assert_eq!(actual, expected);
Expand Down
24 changes: 12 additions & 12 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::context::Context;
use crate::checkers::ast::Checker;
use crate::registry::AsRule;
use crate::rules::flake8_simplify::rules::fix_if;
use crate::rules::pycodestyle::helpers::WidthWithTabs;
use crate::settings::options::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| {
line.width_with_tabs(checker.settings.tab_size, None)
LineWidth::new(checker.settings.tab_size).add_str(&line)
<= checker.settings.line_length
})
{
Expand Down Expand Up @@ -510,11 +510,11 @@ 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());
let tab_size = checker.settings.tab_size;
let mut width = checker.locator.contents()[TextRange::new(line_start, stmt.start())]
.width_with_tabs(tab_size, None);
width = contents.width_with_tabs(tab_size, Some(width));
if width > checker.settings.line_length {
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
{
return;
}

Expand Down Expand Up @@ -866,11 +866,11 @@ 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());
let tab_size = checker.settings.tab_size;
let mut width = checker.locator.contents()[TextRange::new(line_start, stmt.start())]
.width_with_tabs(tab_size, None);
width = contents.width_with_tabs(tab_size, Some(width));
if width > checker.settings.line_length {
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
{
return;
}

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::rules::pycodestyle::helpers::WidthWithTabs;
use crate::settings::options::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| {
line.width_with_tabs(checker.settings.tab_size, None)
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 @@ -10,7 +10,7 @@ use ruff_python_ast::source_code::Generator;

use crate::checkers::ast::Checker;
use crate::registry::{AsRule, Rule};
use crate::rules::pycodestyle::helpers::WidthWithTabs;
use crate::settings::options::LineWidth;

#[violation]
pub struct ReimplementedBuiltin {
Expand Down Expand Up @@ -225,12 +225,11 @@ 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());
let tab_size = checker.settings.tab_size;
let mut width = checker.locator.contents()
[TextRange::new(line_start, stmt.start())]
.width_with_tabs(tab_size, None);
width = contents.width_with_tabs(tab_size, Some(width));
if width > checker.settings.line_length {
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
{
return;
}

Expand Down Expand Up @@ -317,12 +316,11 @@ 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());
let tab_size = checker.settings.tab_size;
let mut width = checker.locator.contents()
[TextRange::new(line_start, stmt.start())]
.width_with_tabs(tab_size, None);
width = contents.width_with_tabs(tab_size, Some(width));
if width > checker.settings.line_length {
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
{
return;
}

Expand Down
28 changes: 11 additions & 17 deletions crates/ruff/src/rules/isort/format.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use ruff_python_ast::source_code::Stylist;
use unicode_width::UnicodeWidthStr;

use crate::rules::pycodestyle::helpers::WidthWithTabs;
use crate::settings::options::TabSize;
use crate::settings::options::LineWidth;

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

Expand Down Expand Up @@ -47,9 +45,8 @@ pub(crate) fn format_import_from(
import_from: &ImportFromData,
comments: &CommentSet,
aliases: &[(AliasData, CommentSet)],
line_length: usize,
indentation_width: usize,
tab_size: TabSize,
line_length: LineWidth,
indentation_width: LineWidth,
stylist: &Stylist,
force_wrap_aliases: bool,
is_first: bool,
Expand All @@ -66,7 +63,6 @@ pub(crate) fn format_import_from(
aliases,
is_first,
stylist,
tab_size,
indentation_width,
);
return single_line;
Expand All @@ -88,7 +84,6 @@ pub(crate) fn format_import_from(
aliases,
is_first,
stylist,
tab_size,
indentation_width,
);
if import_width <= line_length || aliases.iter().any(|(alias, _)| alias.name == "*") {
Expand All @@ -108,9 +103,8 @@ fn format_single_line(
aliases: &[(AliasData, CommentSet)],
is_first: bool,
stylist: &Stylist,
tab_size: TabSize,
indentation_width: usize,
) -> (String, usize) {
indentation_width: LineWidth,
) -> (String, LineWidth) {
let mut output = String::with_capacity(CAPACITY);
let mut line_width = indentation_width;

Expand All @@ -126,36 +120,36 @@ fn format_single_line(
output.push_str("from ");
output.push_str(&module_name);
output.push_str(" import ");
line_width += 5 + module_name.width() + 8;
line_width = line_width.add_width(5).add_str(&module_name).add_width(8);

for (index, (AliasData { name, asname }, comments)) in aliases.iter().enumerate() {
if let Some(asname) = asname {
output.push_str(name);
output.push_str(" as ");
output.push_str(asname);
line_width += name.width() + 4 + asname.width();
line_width = line_width.add_str(name).add_width(4).add_str(asname);
} else {
output.push_str(name);
line_width += name.width();
line_width = line_width.add_str(name);
}
if index < aliases.len() - 1 {
output.push_str(", ");
line_width += 2;
line_width = line_width.add_width(2);
}

for comment in &comments.inline {
output.push(' ');
output.push(' ');
output.push_str(comment);
line_width = comment.width_with_tabs(tab_size, Some(line_width + 2));
line_width = line_width.add_width(2).add_str(comment);
}
}

for comment in &comments.inline {
output.push(' ');
output.push(' ');
output.push_str(comment);
line_width = comment.width_with_tabs(tab_size, Some(line_width + 2));
line_width = line_width.add_width(2).add_str(comment);
}

output.push_str(&stylist.line_ending());
Expand Down
14 changes: 5 additions & 9 deletions crates/ruff/src/rules/isort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use types::{AliasData, EitherImport, TrailingComma};

use crate::rules::isort::categorize::KnownModules;
use crate::rules::isort::types::ImportBlock;
use crate::settings::options::TabSize;
use crate::settings::options::LineWidth;
use crate::settings::types::PythonVersion;

mod annotate;
Expand Down Expand Up @@ -65,9 +65,8 @@ pub fn format_imports(
block: &Block,
comments: Vec<Comment>,
locator: &Locator,
line_length: usize,
indentation_width: usize,
tab_size: TabSize,
line_length: LineWidth,
indentation_width: LineWidth,
stylist: &Stylist,
src: &[PathBuf],
package: Option<&Path>,
Expand Down Expand Up @@ -110,7 +109,6 @@ pub fn format_imports(
block,
line_length,
indentation_width,
tab_size,
stylist,
src,
package,
Expand Down Expand Up @@ -166,9 +164,8 @@ pub fn format_imports(
#[allow(clippy::too_many_arguments, clippy::fn_params_excessive_bools)]
fn format_import_block(
block: ImportBlock,
line_length: usize,
indentation_width: usize,
tab_size: TabSize,
line_length: LineWidth,
indentation_width: LineWidth,
stylist: &Stylist,
src: &[PathBuf],
package: Option<&Path>,
Expand Down Expand Up @@ -271,7 +268,6 @@ fn format_import_block(
&aliases,
line_length,
indentation_width,
tab_size,
stylist,
force_wrap_aliases,
is_first_statement,
Expand Down
5 changes: 2 additions & 3 deletions crates/ruff/src/rules/isort/rules/organize_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use ruff_python_ast::source_code::{Indexer, Locator, Stylist};
use ruff_python_ast::whitespace::leading_space;

use crate::registry::AsRule;
use crate::rules::pycodestyle::helpers::WidthWithTabs;
use crate::settings::Settings;
use crate::settings::options::LineWidth;

use super::super::track::Block;
use super::super::{comments, format_imports};
Expand Down Expand Up @@ -118,8 +118,7 @@ pub(crate) fn organize_imports(
comments,
locator,
settings.line_length,
indentation.width_with_tabs(settings.tab_size, None),
settings.tab_size,
LineWidth::new(settings.tab_size).add_str(indentation),
stylist,
&settings.src,
package,
Expand Down

0 comments on commit cda81f3

Please sign in to comment.