Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove spaces from import statements #7859

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from foo\
.bar import baz

import tqdm . tqdm

# At the top-level, force one empty line after an import, but allow up to two empty
# lines.
import os
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@
( # comment
bar,
)

from tqdm . auto import tqdm
21 changes: 17 additions & 4 deletions crates/ruff_python_formatter/src/other/identifier.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ruff_formatter::{FormatOwnedWithRule, FormatRefWithRule};
use ruff_python_ast::Identifier;
use ruff_python_trivia::is_python_whitespace;
use ruff_text_size::Ranged;

use crate::prelude::*;
Expand Down Expand Up @@ -31,6 +32,7 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for Identifier {
/// A formatter for a dot-delimited identifier, as seen in import statements:
/// ```python
/// import foo.bar
/// from tqdm . auto import tqdm
/// ```
///
/// Dot-delimited identifiers can contain newlines via continuations (backslashes) after the
Expand All @@ -54,14 +56,25 @@ impl<'a> DotDelimitedIdentifier<'a> {

impl Format<PyFormatContext<'_>> for DotDelimitedIdentifier<'_> {
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
// An import identifier can contain newlines by inserting continuations (backslashes) after
// An import identifier can contain whitespace around the dots:
// ```python
// import importlib . metadata
// ```
// It can also contain newlines by inserting continuations (backslashes) after
// a dot-delimited segment, as in:
// ```python
// import foo\
// .bar
// .bar
// ```
if memchr::memchr(b'\\', f.context().source()[self.0.range()].as_bytes()).is_some() {
text(self.0.as_str(), Some(self.0.start())).fmt(f)
if f.context().source()[self.0.range()]
.chars()
.any(|c| is_python_whitespace(c) || matches!(c, '\n' | '\r' | '\\'))
{
let no_whitespace: String = f.context().source()[self.0.range()]
.chars()
.filter(|c| !is_python_whitespace(*c) && !matches!(c, '\n' | '\r' | '\\'))
.collect();
text(&no_whitespace, Some(self.0.start())).fmt(f)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annoying that we pass &str in here only for it to be converted to String internally when formatting, but I don't see any alternative APIs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could change the text element to use Cow, there are more places where we had owned strings previously.

} else {
source_text_slice(self.0.range()).fmt(f)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import foo\
from foo\
.bar import baz

import tqdm . tqdm

# At the top-level, force one empty line after an import, but allow up to two empty
# lines.
import os
Expand Down Expand Up @@ -110,6 +112,8 @@ import foo.bar

from foo.bar import baz

import tqdm.tqdm

# At the top-level, force one empty line after an import, but allow up to two empty
# lines.
import os
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ from a import \
( # comment
bar,
)

from tqdm . auto import tqdm
```

## Output
Expand Down Expand Up @@ -115,6 +117,8 @@ from a import (
from a import ( # comment
bar,
)

from tqdm.auto import tqdm
```


Expand Down
Loading