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 20, 2023
1 parent 256b98a commit 596a8ad
Show file tree
Hide file tree
Showing 49 changed files with 390 additions and 219 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
28 changes: 17 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,15 @@ 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)]
#[deprecated(note = "Deprecated in favor of `--line-width`")]
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 @@ -389,9 +395,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 @@ -465,6 +468,9 @@ impl CheckCommand {
/// Partition the CLI into command-line arguments and configuration
/// overrides.
pub fn partition(self) -> (CheckArguments, CliOverrides) {
#[allow(deprecated)]
let line_width = self.line_width.or(self.line_length);

(
CheckArguments {
add_noqa: self.add_noqa,
Expand Down Expand Up @@ -494,7 +500,7 @@ impl CheckCommand {
extend_unfixable: self.extend_unfixable,
fixable: self.fixable,
ignore: self.ignore,
line_length: self.line_length,
line_width,
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 @@ -533,7 +539,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 @@ -605,7 +611,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 @@ -672,8 +678,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
5 changes: 5 additions & 0 deletions crates/ruff_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ fn format(args: FormatCommand, log_level: LogLevel) -> Result<ExitStatus> {
}

pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {
#[allow(deprecated)]
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
2 changes: 1 addition & 1 deletion crates/ruff_cli/tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn format_options() -> Result<()> {
&ruff_toml,
r#"
tab-size = 8
line-length = 84
line-width = 84
[format]
indent-style = "tab"
Expand Down
76 changes: 76 additions & 0 deletions crates/ruff_cli/tests/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,79 @@ if __name__ == "__main__":
"###);
Ok(())
}

#[test]
fn deprecated_length_options() -> Result<()> {
let tempdir = TempDir::new()?;
let ruff_toml = tempdir.path().join("ruff.toml");
fs::write(
&ruff_toml,
r#"
line-length = 100
select = ["E501", "W505"]
[pycodestyle]
max-doc-length = 80
"#,
)?;

assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.args(STDIN_BASE_OPTIONS)
.arg("--config")
.arg(&ruff_toml)
.args(["--stdin-filename", "test.py"])
.arg("-")
.pass_stdin(r#"
_ = "---------------------------------------------------------------------------亜亜亜亜亜亜亜亜亜亜亜亜亜亜"
class Bar:
"""
This is a long sentence that ends with a shortened URL and, therefore, could easily be broken across multiple lines ([source](https://ruff.rs))
"""
"#), @r###"
success: false
exit_code: 1
----- stdout -----
test.py:2:91: E501 Line too long (109 > 100 width)
test.py:6:81: W505 Doc line too long (147 > 80 width)
test.py:6:101: E501 Line too long (147 > 100 width)
Found 3 errors.
----- stderr -----
warning: 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.
warning: The option `pycodestyle.max-doc-length` has been renamed to `pycodestyle.max-doc-width` to emphasize that the limit is the display width of a line and not the number of characters. Use `pycodestyle.max-doc-width` instead.
"###);
Ok(())
}

#[test]
fn deprecated_line_length_cli_option() -> Result<()> {
let tempdir = TempDir::new()?;
let ruff_toml = tempdir.path().join("ruff.toml");
fs::write(
&ruff_toml,
r#"
select = ["E501"]
"#,
)?;

assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.args(STDIN_BASE_OPTIONS)
.arg("--config")
.arg(&ruff_toml)
.args(["--stdin-filename", "test.py", "--line-length", "100"])
.arg("-")
.pass_stdin(r#"
_ = "---------------------------------------------------------------------------亜亜亜亜亜亜亜亜亜亜亜亜亜亜"
"#), @r###"
success: false
exit_code: 1
----- stdout -----
test.py:2:91: E501 Line too long (109 > 100 width)
Found 1 error.
----- stderr -----
warning: 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.
"###);
Ok(())
}
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 596a8ad

Please sign in to comment.