Skip to content

Commit

Permalink
Expand docs; use an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Nov 21, 2023
1 parent 9401ca9 commit 338ea22
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
27 changes: 15 additions & 12 deletions crates/ruff_linter/src/rules/isort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,24 +182,28 @@ fn format_import_block(
pending_lines_before = false;
}

let mut lines_inserted = false;
let mut needs_lines_inserted = false;
#[derive(PartialEq)]
enum LineInsertion {
Necessary,
Inserted,
}

let mut line_insertion = None;
let mut is_first_statement = true;
let lines_between_types = settings.lines_between_types;
for import in imports {
match import {
Import((alias, comments)) => {
// Add a blank lines between direct and from imports
// Add a blank lines between direct and from imports.
if settings.from_first
&& lines_between_types > 0
&& needs_lines_inserted
&& !lines_inserted
&& line_insertion == Some(LineInsertion::Necessary)
{
for _ in 0..lines_between_types {
output.push_str(&stylist.line_ending());
}

lines_inserted = true;
line_insertion = Some(LineInsertion::Inserted);
}

output.push_str(&format::format_import(
Expand All @@ -210,22 +214,21 @@ fn format_import_block(
));

if !settings.from_first {
needs_lines_inserted = true;
line_insertion = Some(LineInsertion::Necessary);
}
}

ImportFrom((import_from, comments, trailing_comma, aliases)) => {
// Add a blank lines between direct and from imports
// Add a blank lines between direct and from imports.
if !settings.from_first
&& lines_between_types > 0
&& needs_lines_inserted
&& !lines_inserted
&& line_insertion == Some(LineInsertion::Necessary)
{
for _ in 0..lines_between_types {
output.push_str(&stylist.line_ending());
}

lines_inserted = true;
line_insertion = Some(LineInsertion::Inserted);
}

output.push_str(&format::format_import_from(
Expand All @@ -242,7 +245,7 @@ fn format_import_block(
));

if settings.from_first {
needs_lines_inserted = true;
line_insertion = Some(LineInsertion::Necessary);
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion crates/ruff_workspace/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2021,7 +2021,23 @@ pub struct IsortOptions {
)]
pub detect_same_package: Option<bool>,

/// Sort "from .. import .." imports before straight imports
/// Whether to place `import from` imports before straight imports when sorting.
///
/// For example, by default, imports will be sorted such that straight imports appear
/// before `import from` imports, as in:
/// ```python
/// import os
/// import sys
/// from typing import List
/// ```
///
/// Setting `from-first = true` will instead sort such that `import from` imports appear
/// before straight imports, as in:
/// ```python
/// from typing import List
/// import os
/// import sys
/// ```
#[option(
default = r#"false"#,
value_type = "bool",
Expand Down
2 changes: 1 addition & 1 deletion ruff.schema.json

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

0 comments on commit 338ea22

Please sign in to comment.