Skip to content

Commit f9a6450

Browse files
Use char index rather than position for indent slice (#11645)
## Summary A beginner's mistake :) Closes #11641.
1 parent 8a25531 commit f9a6450

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

crates/ruff_python_codegen/src/stylist.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ fn detect_indention(tokens: &[LexResult], locator: &Locator) -> Indentation {
106106
}
107107
TokenKind::NonLogicalNewline => {
108108
let line = locator.line(range.end());
109-
let indent_index = line.chars().position(|c| !c.is_whitespace());
109+
let indent_index = line.char_indices().find_map(|(i, c)| {
110+
if c.is_whitespace() {
111+
None
112+
} else {
113+
Some(i)
114+
}
115+
});
110116
if let Some(indent_index) = indent_index {
111117
if indent_index > 0 {
112118
let whitespace = &line[..indent_index];
@@ -223,6 +229,20 @@ x = (
223229
&Indentation(" ".to_string())
224230
);
225231

232+
let contents = r"
233+
x = (
234+
 1,
235+
 2,
236+
 3,
237+
)
238+
";
239+
let locator = Locator::new(contents);
240+
let tokens: Vec<_> = lex(contents, Mode::Module).collect();
241+
assert_eq!(
242+
Stylist::from_tokens(&tokens, &locator).indentation(),
243+
&Indentation(" ".to_string())
244+
);
245+
226246
// formfeed indent, see `detect_indention` comment.
227247
let contents = r"
228248
class FormFeedIndent:

0 commit comments

Comments
 (0)