Skip to content

Commit

Permalink
First-pass implementation of Display for ruff_workspace::Settings
Browse files Browse the repository at this point in the history
- A new utility macro, `display_settings`, has been created to reduce boilerplate for displaying settings fields
- `Display` has been implemented on many custom types used in `ruff_workspace::Settings`
- `--show-settings` now uses `Display` instead of `Debug` on `ruff_workspace::Settings`

Work left to be done:
- A lot of formatting for Vec<_> and HashSet<_> types has been stubbed out, using `Debug` as a fallback. There should be a way to add generic formatting support for these types as a modifier in `display_settings`.
- Several complex types were also stubbed out and need proper `Display` implementations rather than falling back on `Debug`
- An open question needs to be answered: should this output be valid TOML? How important is that?
  • Loading branch information
snowsignal committed Jan 11, 2024
1 parent 79f4abb commit 651c1aa
Show file tree
Hide file tree
Showing 35 changed files with 869 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crates/ruff_cli/src/commands/show_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) fn show_settings(
if let Some(settings_path) = pyproject_config.path.as_ref() {
writeln!(writer, "Settings path: {settings_path:?}")?;
}
writeln!(writer, "{settings:#?}")?;
writeln!(writer, "{settings}")?;

Ok(())
}
13 changes: 13 additions & 0 deletions crates/ruff_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod source_code;
use crate::formatter::Formatter;
use crate::group_id::UniqueGroupIdBuilder;
use crate::prelude::TagKind;
use std::fmt;
use std::fmt::{Debug, Display};
use std::marker::PhantomData;
use std::num::{NonZeroU16, NonZeroU8, TryFromIntError};
Expand Down Expand Up @@ -113,6 +114,12 @@ impl Default for IndentWidth {
}
}

impl fmt::Display for IndentWidth {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
std::write!(f, "{}", self.0)
}
}

impl TryFrom<u8> for IndentWidth {
type Error = TryFromIntError;

Expand Down Expand Up @@ -146,6 +153,12 @@ impl Default for LineWidth {
}
}

impl Display for LineWidth {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
std::write!(f, "{}", self.0)
}
}

impl TryFrom<u16> for LineWidth {
type Error = TryFromIntError;

Expand Down
13 changes: 13 additions & 0 deletions crates/ruff_linter/src/line_width.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::error::Error;
use std::fmt;
use std::hash::Hasher;
use std::num::{NonZeroU16, NonZeroU8, ParseIntError};
use std::str::FromStr;
Expand Down Expand Up @@ -39,6 +40,12 @@ impl Default for LineLength {
}
}

impl fmt::Display for LineLength {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl CacheKey for LineLength {
fn cache_key(&self, state: &mut CacheKeyHasher) {
state.write_u16(self.0.get());
Expand Down Expand Up @@ -248,6 +255,12 @@ impl Default for IndentWidth {
}
}

impl fmt::Display for IndentWidth {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl From<NonZeroU8> for IndentWidth {
fn from(tab_size: NonZeroU8) -> Self {
Self(tab_size)
Expand Down
19 changes: 18 additions & 1 deletion crates/ruff_linter/src/registry/rule_set.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::registry::Rule;
use itertools::Itertools;
use ruff_macros::CacheKey;
use std::fmt::{Debug, Formatter};
use std::fmt::{Debug, Display, Formatter};
use std::iter::FusedIterator;

const RULESET_SIZE: usize = 13;
Expand Down Expand Up @@ -269,6 +270,22 @@ impl Debug for RuleSet {
}
}

impl Display for RuleSet {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.is_empty() {
write!(f, "[]")
} else {
write!(
f,
"[\n\t{}\n]",
self.iter()
.map(|rule| format!("\"{rule:?}\""))
.join(",\n\t")
)
}
}
}

impl FromIterator<Rule> for RuleSet {
fn from_iter<T: IntoIterator<Item = Rule>>(iter: T) -> Self {
let mut set = RuleSet::empty();
Expand Down
19 changes: 19 additions & 0 deletions crates/ruff_linter/src/rules/flake8_annotations/settings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Settings for the `flake-annotations` plugin.
use crate::display_settings;
use ruff_macros::CacheKey;
use std::fmt::{Display, Formatter};

#[derive(Debug, Default, CacheKey)]
#[allow(clippy::struct_excessive_bools)]
Expand All @@ -11,3 +13,20 @@ pub struct Settings {
pub allow_star_arg_any: bool,
pub ignore_fully_untyped: bool,
}

impl Display for Settings {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
display_settings! {
formatter = f,
namespace = "linter.flake8_annotations.",
fields = [
self.mypy_init_return,
self.suppress_dummy_args,
self.suppress_none_returning,
self.allow_star_arg_any,
self.ignore_fully_untyped
]
}
Ok(())
}
}
16 changes: 16 additions & 0 deletions crates/ruff_linter/src/rules/flake8_bandit/settings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Settings for the `flake8-bandit` plugin.
use crate::display_settings;
use ruff_macros::CacheKey;
use std::fmt::{Display, Formatter};

pub fn default_tmp_dirs() -> Vec<String> {
["/tmp", "/var/tmp", "/dev/shm"]
Expand All @@ -22,3 +24,17 @@ impl Default for Settings {
}
}
}

impl Display for Settings {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
display_settings! {
formatter = f,
namespace = "linter.flake8_bandit.",
fields = [
self.hardcoded_tmp_directory | debug,
self.check_typed_exception
]
}
Ok(())
}
}
15 changes: 15 additions & 0 deletions crates/ruff_linter/src/rules/flake8_bugbear/settings.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
//! Settings for the `flake8-bugbear` plugin.
use crate::display_settings;
use ruff_macros::CacheKey;
use std::fmt::{Display, Formatter};

#[derive(Debug, Default, CacheKey)]
pub struct Settings {
pub extend_immutable_calls: Vec<String>,
}

impl Display for Settings {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
display_settings! {
formatter = f,
namespace = "linter.flake8_bugbear.",
fields = [
self.extend_immutable_calls | debug
]
}
Ok(())
}
}
15 changes: 15 additions & 0 deletions crates/ruff_linter/src/rules/flake8_builtins/settings.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
//! Settings for the `flake8-builtins` plugin.
use crate::display_settings;
use ruff_macros::CacheKey;
use std::fmt::{Display, Formatter};

#[derive(Debug, Default, CacheKey)]
pub struct Settings {
pub builtins_ignorelist: Vec<String>,
}

impl Display for Settings {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
display_settings! {
formatter = f,
namespace = "linter.flake8_builtins.",
fields = [
self.builtins_ignorelist | debug
]
}
Ok(())
}
}
15 changes: 15 additions & 0 deletions crates/ruff_linter/src/rules/flake8_comprehensions/settings.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
//! Settings for the `flake8-comprehensions` plugin.
use crate::display_settings;
use ruff_macros::CacheKey;
use std::fmt::{Display, Formatter};

#[derive(Debug, Default, CacheKey)]
pub struct Settings {
pub allow_dict_calls_with_keyword_arguments: bool,
}

impl Display for Settings {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
display_settings! {
formatter = f,
namespace = "linter.flake8_comprehensions.",
fields = [
self.allow_dict_calls_with_keyword_arguments
]
}
Ok(())
}
}
18 changes: 18 additions & 0 deletions crates/ruff_linter/src/rules/flake8_copyright/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
use once_cell::sync::Lazy;
use regex::Regex;
use std::fmt::{Display, Formatter};

use crate::display_settings;
use ruff_macros::CacheKey;

#[derive(Debug, CacheKey)]
Expand All @@ -24,3 +26,19 @@ impl Default for Settings {
}
}
}

impl Display for Settings {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
display_settings! {
formatter = f,
namespace = "linter.flake8_copyright",
fields = [
self.notice_rgx,
// TODO(jane): remove debug
self.author | debug,
self.min_file_size,
]
}
Ok(())
}
}
15 changes: 15 additions & 0 deletions crates/ruff_linter/src/rules/flake8_errmsg/settings.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
//! Settings for the `flake8-errmsg` plugin.
use crate::display_settings;
use ruff_macros::CacheKey;
use std::fmt::{Display, Formatter};

#[derive(Debug, Default, CacheKey)]
pub struct Settings {
pub max_string_length: usize,
}

impl Display for Settings {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
display_settings! {
formatter = f,
namespace = "linter.flake8_errmsg.",
fields = [
self.max_string_length
]
}
Ok(())
}
}
15 changes: 15 additions & 0 deletions crates/ruff_linter/src/rules/flake8_gettext/settings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::display_settings;
use ruff_macros::CacheKey;
use std::fmt::{Display, Formatter};

#[derive(Debug, CacheKey)]
pub struct Settings {
Expand All @@ -20,3 +22,16 @@ impl Default for Settings {
}
}
}

impl Display for Settings {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
display_settings! {
formatter = f,
namespace = "linter.flake8_gettext.",
fields = [
self.functions_names | debug
]
}
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Settings for the `flake8-implicit-str-concat` plugin.
use crate::display_settings;
use ruff_macros::CacheKey;
use std::fmt::{Display, Formatter};

#[derive(Debug, CacheKey)]
pub struct Settings {
Expand All @@ -14,3 +16,16 @@ impl Default for Settings {
}
}
}

impl Display for Settings {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
display_settings! {
formatter = f,
namespace = "linter.flake8_implicit_str_concat.",
fields = [
self.allow_multiline
]
}
Ok(())
}
}
17 changes: 17 additions & 0 deletions crates/ruff_linter/src/rules/flake8_import_conventions/settings.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Settings for import conventions.
use rustc_hash::{FxHashMap, FxHashSet};
use std::fmt::{Display, Formatter};

use crate::display_settings;
use ruff_macros::CacheKey;

const CONVENTIONAL_ALIASES: &[(&str, &str)] = &[
Expand Down Expand Up @@ -44,3 +46,18 @@ impl Default for Settings {
}
}
}

impl Display for Settings {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
display_settings! {
formatter = f,
namespace = "linter.flake8_import_conventions",
fields = [
self.aliases | debug,
self.banned_aliases | debug,
self.banned_from | debug,
]
}
Ok(())
}
}
Loading

0 comments on commit 651c1aa

Please sign in to comment.