Skip to content

Commit

Permalink
Rename -size options to -width
Browse files Browse the repository at this point in the history
Signed-off-by: Micha Reiser <micha@reiser.io>
  • Loading branch information
MichaReiser committed Oct 18, 2023
1 parent f2ac348 commit 0fee9d2
Show file tree
Hide file tree
Showing 49 changed files with 325 additions and 222 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ exclude = [
"venv",
]

# Same as Black.
line-length = 88
# Same as Black's `line-length`.
line-width = 88

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
Expand Down
4 changes: 2 additions & 2 deletions crates/flake8_to_ruff/src/black.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! Extract Black configuration settings from a pyproject.toml.

use ruff_linter::line_width::LineLength;
use ruff_linter::line_width::LineWidth;
use ruff_linter::settings::types::PythonVersion;
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub(crate) struct Black {
#[serde(alias = "line-length", alias = "line_length")]
pub(crate) line_length: Option<LineLength>,
pub(crate) line_length: Option<LineWidth>,
#[serde(alias = "target-version", alias = "target_version")]
pub(crate) target_version: Option<Vec<PythonVersion>>,
}
14 changes: 7 additions & 7 deletions crates/flake8_to_ruff/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str::FromStr;

use itertools::Itertools;

use ruff_linter::line_width::LineLength;
use ruff_linter::line_width::LineWidth;
use ruff_linter::registry::Linter;
use ruff_linter::rule_selector::RuleSelector;
use ruff_linter::rules::flake8_pytest_style::types::{
Expand Down Expand Up @@ -117,8 +117,8 @@ pub(crate) fn convert(
"builtins" => {
options.builtins = Some(parser::parse_strings(value.as_ref()));
}
"max-line-length" | "max_line_length" => match LineLength::from_str(value) {
Ok(line_length) => options.line_length = Some(line_length),
"max-line-length" | "max_line_length" => match LineWidth::from_str(value) {
Ok(line_length) => options.line_width = Some(line_length),
Err(e) => {
warn_user!("Unable to parse '{key}' property: {e}");
}
Expand Down Expand Up @@ -401,7 +401,7 @@ pub(crate) 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_width = Some(*line_length);
}

if let Some(target_version) = &black.target_version {
Expand Down Expand Up @@ -462,7 +462,7 @@ mod tests {
use pep440_rs::VersionSpecifiers;

use pretty_assertions::assert_eq;
use ruff_linter::line_width::LineLength;
use ruff_linter::line_width::LineWidth;
use ruff_linter::registry::Linter;
use ruff_linter::rule_selector::RuleSelector;
use ruff_linter::rules::flake8_quotes;
Expand Down Expand Up @@ -523,7 +523,7 @@ mod tests {
Some(vec![]),
);
let expected = Pyproject::new(Options {
line_length: Some(LineLength::try_from(100).unwrap()),
line_width: Some(LineWidth::try_from(100).unwrap()),
lint: Some(LintOptions {
common: lint_default_options([]),
..LintOptions::default()
Expand All @@ -544,7 +544,7 @@ mod tests {
Some(vec![]),
);
let expected = Pyproject::new(Options {
line_length: Some(LineLength::try_from(100).unwrap()),
line_width: Some(LineWidth::try_from(100).unwrap()),
lint: Some(LintOptions {
common: lint_default_options([]),
..LintOptions::default()
Expand Down
24 changes: 13 additions & 11 deletions crates/ruff_cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clap::{command, Parser};
use regex::Regex;
use rustc_hash::FxHashMap;

use ruff_linter::line_width::LineLength;
use ruff_linter::line_width::LineWidth;
use ruff_linter::logging::LogLevel;
use ruff_linter::registry::Rule;
use ruff_linter::settings::types::{
Expand Down Expand Up @@ -259,9 +259,14 @@ pub struct CheckCommand {
force_exclude: bool,
#[clap(long, overrides_with("force_exclude"), hide = true)]
no_force_exclude: bool,
/// Set the line-length for length-associated rules and automatic formatting.
/// Set the line-width for width-associated rules.
#[arg(long, help_heading = "Rule configuration", hide = true)]
pub line_length: Option<LineLength>,
pub line_width: Option<LineWidth>,

/// Set the line-length for width-associated rules.
#[arg(long, help_heading = "Rule configuration", hide = true)]
pub line_length: Option<LineWidth>,

/// Regular expression matching the name of dummy variables.
#[arg(long, help_heading = "Rule configuration", hide = true)]
pub dummy_variable_rgx: Option<Regex>,
Expand Down Expand Up @@ -385,9 +390,6 @@ pub struct FormatCommand {
force_exclude: bool,
#[clap(long, overrides_with("force_exclude"), hide = true)]
no_force_exclude: bool,
/// Set the line-length.
#[arg(long, help_heading = "Rule configuration", hide = true)]
pub line_length: Option<LineLength>,
/// Ignore all configuration files.
#[arg(long, conflicts_with = "config", help_heading = "Miscellaneous")]
pub isolated: bool,
Expand Down Expand Up @@ -488,7 +490,7 @@ impl CheckCommand {
extend_unfixable: self.extend_unfixable,
fixable: self.fixable,
ignore: self.ignore,
line_length: self.line_length,
line_width: self.line_width.or(self.line_length),
per_file_ignores: self.per_file_ignores,
preview: resolve_bool_arg(self.preview, self.no_preview).map(PreviewMode::from),
respect_gitignore: resolve_bool_arg(
Expand Down Expand Up @@ -526,7 +528,7 @@ impl FormatCommand {
stdin_filename: self.stdin_filename,
},
CliOverrides {
line_length: self.line_length,
line_width: None,
respect_gitignore: resolve_bool_arg(
self.respect_gitignore,
self.no_respect_gitignore,
Expand Down Expand Up @@ -596,7 +598,7 @@ pub struct CliOverrides {
pub extend_unfixable: Option<Vec<RuleSelector>>,
pub fixable: Option<Vec<RuleSelector>>,
pub ignore: Option<Vec<RuleSelector>>,
pub line_length: Option<LineLength>,
pub line_width: Option<LineWidth>,
pub per_file_ignores: Option<Vec<PatternPrefixPair>>,
pub preview: Option<PreviewMode>,
pub respect_gitignore: Option<bool>,
Expand Down Expand Up @@ -663,8 +665,8 @@ impl ConfigurationTransformer for CliOverrides {
if let Some(force_exclude) = &self.force_exclude {
config.force_exclude = Some(*force_exclude);
}
if let Some(line_length) = &self.line_length {
config.line_length = Some(*line_length);
if let Some(line_width) = self.line_width {
config.line_width = Some(line_width);
}
if let Some(preview) = &self.preview {
config.preview = Some(*preview);
Expand Down
4 changes: 4 additions & 0 deletions crates/ruff_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ fn format(args: FormatCommand, log_level: LogLevel) -> Result<ExitStatus> {
}

pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {
if args.line_length.is_some() {
warn_user_once!("The option `--line-length` has been renamed to `--line-width` to emphasize that the limit is the display width of a line and not the number of characters. Use the option `--line-width` instead.");
}

let (cli, overrides) = args.partition();

// Construct the "default" settings. These are used when no `pyproject.toml`
Expand Down
13 changes: 13 additions & 0 deletions crates/ruff_cli/tests/.format.rs.pending-snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{"run_id":"1697613922-116275000","line":77,"new":{"module_name":"format","snapshot_name":"format_options","metadata":{"source":"crates/ruff_cli/tests/format.rs","assertion_line":64,"info":{"program":"ruff","args":["format","--config","/var/folders/pp/d0k78q8950s62486c1rt6zjw0000gn/T/.tmpYncQIu/ruff.toml","-"],"stdin":"\ndef foo(arg1, arg2,):\n print(\"Shouldn't change quotes. It exceeds the line width with the tab size 8\")\n\n\nif condition:\n\n print(\"Should change quotes\")\n\n"}},"snapshot":"success: true\nexit_code: 0\n----- stdout -----\ndef foo(arg1, arg2):\n\tprint(\n\t\t\"Shouldn't change quotes. It exceeds the line width with the tab size 8\"\n\t)\n\n\nif condition:\n\tprint('Should change quotes')\n\n----- stderr -----\nwarning: `ruff format` is not yet stable, and subject to change in future versions.\nwarning: The option `line-length` has been renamed to `line-width` to emphasize that the limit is the display width of a line and not the number of characters. Use `line-width` instead.\n"},"old":{"module_name":"format","metadata":{},"snapshot":"success: true\nexit_code: 0\n----- stdout -----\ndef foo(arg1, arg2):\n\tprint(\n\t\t\"Shouldn't change quotes. It exceeds the line width with the tab size 8\"\n\t)\n\n\nif condition:\n\tprint('Should change quotes')\n\n----- stderr -----\nwarning: `ruff format` is not yet stable, and subject to change in future versions."}}
{"run_id":"1697614864-891604000","line":221,"new":null,"old":null}
{"run_id":"1697614864-891604000","line":64,"new":null,"old":null}
{"run_id":"1697614864-891604000","line":179,"new":null,"old":null}
{"run_id":"1697614864-891604000","line":269,"new":null,"old":null}
{"run_id":"1697614864-891604000","line":15,"new":null,"old":null}
{"run_id":"1697614864-891604000","line":144,"new":null,"old":null}
{"run_id":"1697615157-557926000","line":15,"new":null,"old":null}
{"run_id":"1697615157-557926000","line":144,"new":null,"old":null}
{"run_id":"1697615157-557926000","line":179,"new":null,"old":null}
{"run_id":"1697615157-557926000","line":269,"new":null,"old":null}
{"run_id":"1697615157-557926000","line":64,"new":null,"old":null}
{"run_id":"1697615157-557926000","line":221,"new":null,"old":null}
2 changes: 1 addition & 1 deletion crates/ruff_cli/tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn format_options() -> Result<()> {
&ruff_toml,
r#"
tab-size = 8
line-length = 84
line-width = 84
[format]
indent-style = "tab"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[tool.ruff]
line-length = 88
line-width = 88
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.ruff]
line-length = 88
line-width = 88

[tool.ruff.isort]
lines-after-imports = 3
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/resources/test/fixtures/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.ruff]
line-length = 88
line-width = 88
extend-exclude = [
"excluded_file.py",
"migrations",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extend-exclude = '''
'''

[tool.ruff]
line-length = 120
line-width = 120
target-version = "py37"

[tool.mypy]
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_linter/src/checkers/physical_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ mod tests {
use ruff_python_parser::Mode;
use ruff_source_file::Locator;

use crate::line_width::LineLength;
use crate::line_width::LineWidth;
use crate::registry::Rule;
use crate::settings::LinterSettings;

Expand All @@ -107,19 +107,19 @@ mod tests {
let indexer = Indexer::from_tokens(&tokens, &locator);
let stylist = Stylist::from_tokens(&tokens, &locator);

let check_with_max_line_length = |line_length: LineLength| {
let check_with_max_line_length = |line_width: LineWidth| {
check_physical_lines(
&locator,
&stylist,
&indexer,
&[],
&LinterSettings {
line_length,
line_width,
..LinterSettings::for_rule(Rule::LineTooLong)
},
)
};
let line_length = LineLength::try_from(8).unwrap();
let line_length = LineWidth::try_from(8).unwrap();
assert_eq!(check_with_max_line_length(line_length), vec![]);
assert_eq!(check_with_max_line_length(line_length), vec![]);
}
Expand Down
18 changes: 9 additions & 9 deletions crates/ruff_linter/src/fix/edits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ruff_source_file::{Locator, NewlineWithTrailingNewline, UniversalNewlines};
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};

use crate::fix::codemods;
use crate::line_width::{LineLength, LineWidthBuilder, TabSize};
use crate::line_width::{LineWidth, LineWidthBuilder, TabSize};

/// Return the `Fix` to use when deleting a `Stmt`.
///
Expand Down Expand Up @@ -292,10 +292,10 @@ pub(crate) fn fits(
fix: &str,
node: AnyNodeRef,
locator: &Locator,
line_length: LineLength,
line_width: LineWidth,
tab_size: TabSize,
) -> bool {
all_lines_fit(fix, node, locator, line_length.value() as usize, tab_size)
all_lines_fit(fix, node, locator, line_width.value() as usize, tab_size)
}

/// Returns `true` if the fix fits within the maximum configured line length, or produces lines that
Expand All @@ -304,19 +304,19 @@ pub(crate) fn fits_or_shrinks(
fix: &str,
node: AnyNodeRef,
locator: &Locator,
line_length: LineLength,
line_width: LineWidth,
tab_size: TabSize,
) -> bool {
// Use the larger of the line length limit, or the longest line in the existing AST node.
let line_length = std::iter::once(line_length.value() as usize)
let line_length = std::iter::once(line_width.value() as usize)
.chain(
locator
.slice(locator.lines_range(node.range()))
.universal_newlines()
.map(|line| LineWidthBuilder::new(tab_size).add_str(&line).get()),
)
.max()
.unwrap_or(line_length.value() as usize);
.unwrap_or(line_width.value() as usize);

all_lines_fit(fix, node, locator, line_length, tab_size)
}
Expand All @@ -326,7 +326,7 @@ fn all_lines_fit(
fix: &str,
node: AnyNodeRef,
locator: &Locator,
line_length: usize,
line_width: usize,
tab_size: TabSize,
) -> bool {
let prefix = locator.slice(TextRange::new(
Expand All @@ -343,7 +343,7 @@ fn all_lines_fit(
// {} -> offset = 0
// """.format(0, 1) -> offset = 0
// ```
let measured_length = if idx == 0 {
let measured_width = if idx == 0 {
LineWidthBuilder::new(tab_size)
.add_str(prefix)
.add_str(&line)
Expand All @@ -352,7 +352,7 @@ fn all_lines_fit(
LineWidthBuilder::new(tab_size).add_str(&line).get()
};

measured_length <= line_length
measured_width <= line_width
})
}

Expand Down

0 comments on commit 0fee9d2

Please sign in to comment.