Skip to content

Commit

Permalink
Introduce TabSize
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanPlasse committed May 20, 2023
1 parent a71c537 commit 9e23c64
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 15 deletions.
5 changes: 3 additions & 2 deletions crates/ruff/src/rules/isort/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use ruff_python_ast::source_code::Stylist;
use unicode_width::UnicodeWidthStr;

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

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

Expand Down Expand Up @@ -48,7 +49,7 @@ pub(crate) fn format_import_from(
aliases: &[(AliasData, CommentSet)],
line_length: usize,
indentation_width: usize,
tab_size: u8,
tab_size: TabSize,
stylist: &Stylist,
force_wrap_aliases: bool,
is_first: bool,
Expand Down Expand Up @@ -107,7 +108,7 @@ fn format_single_line(
aliases: &[(AliasData, CommentSet)],
is_first: bool,
stylist: &Stylist,
tab_size: u8,
tab_size: TabSize,
indentation_width: usize,
) -> (String, usize) {
let mut output = String::with_capacity(CAPACITY);
Expand Down
5 changes: 3 additions & 2 deletions crates/ruff/src/rules/isort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +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::types::PythonVersion;

mod annotate;
Expand Down Expand Up @@ -66,7 +67,7 @@ pub fn format_imports(
locator: &Locator,
line_length: usize,
indentation_width: usize,
tab_size: u8,
tab_size: TabSize,
stylist: &Stylist,
src: &[PathBuf],
package: Option<&Path>,
Expand Down Expand Up @@ -167,7 +168,7 @@ fn format_import_block(
block: ImportBlock,
line_length: usize,
indentation_width: usize,
tab_size: u8,
tab_size: TabSize,
stylist: &Stylist,
src: &[PathBuf],
package: Option<&Path>,
Expand Down
12 changes: 7 additions & 5 deletions crates/ruff/src/rules/pycodestyle/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
use ruff_python_ast::newlines::Line;
use ruff_python_ast::source_code::Generator;

use crate::settings::options::TabSize;

pub(crate) fn is_ambiguous_name(name: &str) -> bool {
name == "l" || name == "I" || name == "O"
}
Expand All @@ -29,9 +31,9 @@ pub(super) fn is_overlong(
limit: usize,
ignore_overlong_task_comments: bool,
task_tags: &[String],
tab_size: u8,
tab_size: TabSize,
) -> Option<Overlong> {
let tab_size = tab_size as usize;
let tab_size: usize = tab_size.into();
let mut start_offset = line.start();
let mut width = 0;

Expand Down Expand Up @@ -96,12 +98,12 @@ impl Overlong {
}

pub(crate) trait WidthWithTabs {
fn width_with_tabs(&self, tab_size: u8, current_width: Option<usize>) -> usize;
fn width_with_tabs(&self, tab_size: TabSize, current_width: Option<usize>) -> usize;
}

impl WidthWithTabs for str {
fn width_with_tabs(&self, tab_size: u8, current_width: Option<usize>) -> usize {
let tab_size = tab_size as usize;
fn width_with_tabs(&self, tab_size: TabSize, current_width: Option<usize>) -> usize {
let tab_size: usize = tab_size.into();
let current_width = current_width.unwrap_or(0);
self.chars().fold(current_width, |width, c| {
width
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/pycodestyle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ mod tests {
let diagnostics = test_path(
Path::new("pycodestyle/E501_2.py"),
&settings::Settings {
tab_size,
tab_size: tab_size.into(),
..settings::Settings::for_rule(Rule::LineTooLong)
},
)?;
Expand Down
4 changes: 3 additions & 1 deletion crates/ruff/src/settings/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use crate::settings::types::{
FilePattern, PerFileIgnore, PythonVersion, SerializationFormat, Version,
};

use super::options::TabSize;

#[derive(Debug, Default)]
pub struct RuleSelection {
pub select: Option<Vec<RuleSelector>>,
Expand Down Expand Up @@ -57,7 +59,7 @@ pub struct Configuration {
pub ignore_init_module_imports: Option<bool>,
pub include: Option<Vec<FilePattern>>,
pub line_length: Option<usize>,
pub tab_size: Option<u8>,
pub tab_size: Option<TabSize>,
pub namespace_packages: Option<Vec<PathBuf>>,
pub required_version: Option<Version>,
pub respect_gitignore: Option<bool>,
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff/src/settings/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::rules::{
};
use crate::settings::types::FilePatternSet;

use super::options::TabSize;
use super::types::{FilePattern, PythonVersion};
use super::Settings;

Expand All @@ -28,7 +29,7 @@ pub const TARGET_VERSION: PythonVersion = PythonVersion::Py310;

pub const LINE_LENGTH: usize = 88;

pub const TAB_SIZE: u8 = 4;
pub const TAB_SIZE: TabSize = TabSize(4);

pub const TASK_TAGS: &[&str] = &["TODO", "FIXME", "XXX"];

Expand Down
3 changes: 2 additions & 1 deletion crates/ruff/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::settings::configuration::Configuration;
use crate::settings::types::{FilePatternSet, PerFileIgnore, PythonVersion, SerializationFormat};
use crate::warn_user_once_by_id;

use self::options::TabSize;
use self::rule_table::RuleTable;

pub mod configuration;
Expand Down Expand Up @@ -99,7 +100,7 @@ pub struct Settings {
pub external: FxHashSet<String>,
pub ignore_init_module_imports: bool,
pub line_length: usize,
pub tab_size: u8,
pub tab_size: TabSize,
pub namespace_packages: Vec<PathBuf>,
pub src: Vec<PathBuf>,
pub task_tags: Vec<String>,
Expand Down
21 changes: 19 additions & 2 deletions crates/ruff/src/settings/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};

use ruff_macros::ConfigurationOptions;
use ruff_macros::{CacheKey, ConfigurationOptions};

use crate::rule_selector::RuleSelector;
use crate::rules::{
Expand All @@ -14,6 +14,23 @@ use crate::rules::{
};
use crate::settings::types::{PythonVersion, SerializationFormat, Version};

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, CacheKey)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]

pub struct TabSize(pub u8);

impl From<u8> for TabSize {
fn from(n: u8) -> Self {
Self(n)
}
}

impl From<TabSize> for usize {
fn from(tab_size: TabSize) -> Self {
tab_size.0 as usize
}
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
Expand Down Expand Up @@ -318,7 +335,7 @@ pub struct Options {
"#
)]
/// The tabulation size to calculate line length.
pub tab_size: Option<u8>,
pub tab_size: Option<TabSize>,
#[option(
default = "None",
value_type = "str",
Expand Down

0 comments on commit 9e23c64

Please sign in to comment.