Skip to content

Commit

Permalink
Use enum; tweak docs
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Nov 28, 2023
1 parent 06110bf commit 61c0b88
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 29 deletions.
16 changes: 12 additions & 4 deletions crates/ruff_linter/src/rules/isort/order.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::rules::isort::sorting::ImportStyle;
use itertools::Itertools;

use super::settings::Settings;
Expand Down Expand Up @@ -61,22 +62,29 @@ pub(crate) fn order_imports<'a>(
alias.asname,
None,
None,
ImportStyle::Straight,
settings,
true,
),
ImportFrom((import_from, _, _, aliases)) => ModuleKey::from_module(
import_from.module,
None,
import_from.level,
aliases.first().map(|(alias, _)| (alias.name, alias.asname)),
ImportStyle::From,
settings,
false,
),
})
.collect()
} else {
let ordered_straight_imports = straight_imports.sorted_by_cached_key(|(alias, _)| {
ModuleKey::from_module(Some(alias.name), alias.asname, None, None, settings, true)
ModuleKey::from_module(
Some(alias.name),
alias.asname,
None,
None,
ImportStyle::Straight,
settings,
)
});
let ordered_from_imports =
from_imports.sorted_by_cached_key(|(import_from, _, _, aliases)| {
Expand All @@ -85,8 +93,8 @@ pub(crate) fn order_imports<'a>(
None,
import_from.level,
aliases.first().map(|(alias, _)| (alias.name, alias.asname)),
ImportStyle::From,
settings,
false,
)
});
if settings.from_first {
Expand Down
14 changes: 11 additions & 3 deletions crates/ruff_linter/src/rules/isort/sorting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,20 @@ impl<'a> From<String> for NatOrdStr<'a> {
}
}

#[derive(Debug, PartialOrd, Ord, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
pub(crate) enum Distance {
Nearest(u32),
Furthest(Reverse<u32>),
}

#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
pub(crate) enum ImportStyle {
// Ex) `import foo`
Straight,
// Ex) `from foo import bar`
From,
}

/// A comparable key to capture the desired sorting order for an imported module (e.g.,
/// `foo` in `from foo import bar`).
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq)]
Expand All @@ -90,8 +98,8 @@ impl<'a> ModuleKey<'a> {
asname: Option<&'a str>,
level: Option<u32>,
first_alias: Option<(&'a str, Option<&'a str>)>,
style: ImportStyle,
settings: &Settings,
straight_import: bool,
) -> Self {
let level = level.unwrap_or_default();

Expand All @@ -100,7 +108,7 @@ impl<'a> ModuleKey<'a> {
.unwrap_or_default(); // `false` < `true` so we get forced to top first

let maybe_length = (settings.length_sort
|| (settings.length_sort_straight && straight_import))
|| (settings.length_sort_straight && style == ImportStyle::Straight))
.then_some(name.map(str::width).unwrap_or_default() + level as usize);

let distance = match settings.relative_imports_order {
Expand Down
49 changes: 29 additions & 20 deletions crates/ruff_workspace/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2047,24 +2047,20 @@ pub struct IsortOptions {
)]
pub from_first: Option<bool>,

// Tables are required to go last.
/// A list of mappings from section names to modules.
/// By default custom sections are output last, but this can be overridden with `section-order`.
#[option(
default = "{}",
value_type = "dict[str, list[str]]",
scope = "sections",
example = r#"
# Group all Django imports into a separate section.
"django" = ["django"]
"#
)]
pub sections: Option<FxHashMap<ImportSection, Vec<String>>>,

/// Sort imports by their string length.
/// Sort imports by their string length, such that shorter imports appear
/// before longer imports. For example, by default, imports will be sorted
/// alphabetically, as in:
/// ```python
/// import collections
/// import os
/// ```
///
/// The string length is computed using the [`unicode_width`](https://crates.io/crates/unicode-width) crate,
/// i.e. it is the displayed width of the string according to [Unicode Standard Annex #11 rules](http://www.unicode.org/reports/tr11/).
/// Setting `length-sort = true` will instead sort such that shorter imports
/// appear before longer imports, as in:
/// ```python
/// import os
/// import collections
/// ```
#[option(
default = r#"false"#,
value_type = "bool",
Expand All @@ -2074,9 +2070,8 @@ pub struct IsortOptions {
)]
pub length_sort: Option<bool>,

/// Sort straight imports by their string length.
///
/// Same as `length-sort` but does not affect from imports.
/// Sort straight imports by their string length. Similar to `length-sort`,
/// but applies only to straight imports and doesn't affect `from` imports.
#[option(
default = r#"false"#,
value_type = "bool",
Expand All @@ -2085,6 +2080,20 @@ pub struct IsortOptions {
"#
)]
pub length_sort_straight: Option<bool>,

// Tables are required to go last.
/// A list of mappings from section names to modules.
/// By default custom sections are output last, but this can be overridden with `section-order`.
#[option(
default = "{}",
value_type = "dict[str, list[str]]",
scope = "sections",
example = r#"
# Group all Django imports into a separate section.
"django" = ["django"]
"#
)]
pub sections: Option<FxHashMap<ImportSection, Vec<String>>>,
}

impl IsortOptions {
Expand Down
4 changes: 2 additions & 2 deletions ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 61c0b88

Please sign in to comment.