Skip to content

Commit

Permalink
Indent suppressed ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Aug 14, 2023
1 parent 5622dac commit 12f9bbc
Show file tree
Hide file tree
Showing 19 changed files with 765 additions and 61 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ toml = { version = "0.7.2" }
tracing = "0.1.37"
tracing-indicatif = "0.3.4"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
unicode-width = "0.1.10"
wsl = { version = "0.1.0" }

# v1.0.1
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ strum_macros = { workspace = true }
thiserror = { version = "1.0.43" }
toml = { workspace = true }
typed-arena = { version = "2.0.2" }
unicode-width = { version = "0.1.10" }
unicode-width = { workspace = true }
unicode_names2 = { version = "0.6.0", git = "https://github.com/youknowone/unicode_names2.git", rev = "4ce16aa85cbcdd9cc830410f1a72ef9a235f2fde" }
wsl = { version = "0.1.0" }

Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ drop_bomb = { version = "0.1.5" }
rustc-hash = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
tracing = { version = "0.1.37", default-features = false, features = ["std"] }
unicode-width = { version = "0.1.10" }
static_assertions = "1.1.0"
tracing = { version = "0.1.37", default-features = false, features = ["std"] }
unicode-width = { workspace = true }

[dev-dependencies]
insta = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_formatter/src/printer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl<'a> Printer<'a> {
self.flush_line_suffixes(queue, stack, Some(element));
} else {
// Only print a newline if the current line isn't already empty
if self.state.line_width > 0 {
if self.state.line_width != 0 {
self.print_str("\n");
}

Expand Down
1 change: 1 addition & 0 deletions crates/ruff_python_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ rustc-hash = { workspace = true }
serde = { workspace = true, optional = true }
smallvec = { workspace = true }
thiserror = { workspace = true }
unicode-width = { workspace = true }

[dev-dependencies]
ruff_formatter = { path = "../ruff_formatter", features = ["serde"] }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[mixed_space_and_tab.py]
generated_code = true
ij_formatter_enabled = false
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
def test():
pass

# trailing comment that falls into the verbatim range
# under indent

def nested():
...

# trailing comment that falls into the verbatim range
# trailing outer comment
# fmt: on

a + b
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"indent_style": { "Space": 4 }
},
{
"indent_style": { "Space": 2 }
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"indent_style": { "Space": 4 }
},
{
"indent_style": { "Space": 1 }
},
{
"indent_style": "Tab"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def test():
# fmt: off
a_very_small_indent
(
not_fixed
)

if True:
pass
more
# fmt: on

formatted

def test():
a_small_indent
# fmt: off
# fix under-indented comments
(or_the_inner_expression +
expressions
)

if True:
pass
# fmt: on


# fmt: off
def test():
pass

# It is necessary to indent comments because the following fmt: on comment because it otherwise becomes a trailing comment
# of the `test` function if the "proper" indentation is larger than 2 spaces.
# fmt: on

disabled + formatting;

# fmt: on

formatted;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"indent_style": { "Space": 4 }
},
{
"indent_style": { "Space": 2 }
},
{
"indent_style": "Tab"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def test():
# fmt: off
a_very_small_indent
(
not_fixed
)

if True:
# Fun tab, space, tab, space. Followed by space, tab, tab, space
pass
more
else:
other
# fmt: on

39 changes: 12 additions & 27 deletions crates/ruff_python_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,31 +255,16 @@ if True:
#[ignore]
#[test]
fn quick_test() {
let src = r#"
with (
[
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"bbbbbbbbbb",
"cccccccccccccccccccccccccccccccccccccccccc",
dddddddddddddddddddddddddddddddd,
] as example1,
aaaaaaaaaaaaaaaaaaaaaaaaaa
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
+ cccccccccccccccccccccccccccc
+ ddddddddddddddddd as example2,
CtxManager2() as example2,
CtxManager2() as example2,
CtxManager2() as example2,
):
...
with [
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"bbbbbbbbbb",
"cccccccccccccccccccccccccccccccccccccccccc",
dddddddddddddddddddddddddddddddd,
] as example1, aaaaaaaaaaaaaaaaaaaaaaaaaa * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb * cccccccccccccccccccccccccccc + ddddddddddddddddd as example2, CtxManager222222222222222() as example2:
...
let src = r#"def test():
# fmt: off
a + b
# suppressed comments
a + b # formatted
"#;
// Tokenize once
Expand All @@ -304,9 +289,9 @@ with [
// Use `dbg_write!(f, []) instead of `write!(f, [])` in your formatting code to print some IR
// inside of a `Format` implementation
// use ruff_formatter::FormatContext;
// dbg!(formatted
// formatted
// .document()
// .display(formatted.context().source_code()));
// .display(formatted.context().source_code());
//
// dbg!(formatted
// .context()
Expand Down
Loading

0 comments on commit 12f9bbc

Please sign in to comment.