Skip to content

Commit

Permalink
Use char index rather than position for indent slice (#11645)
Browse files Browse the repository at this point in the history
## Summary

A beginner's mistake :)

Closes #11641.
  • Loading branch information
charliermarsh authored May 31, 2024
1 parent 8a25531 commit f9a6450
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion crates/ruff_python_codegen/src/stylist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ fn detect_indention(tokens: &[LexResult], locator: &Locator) -> Indentation {
}
TokenKind::NonLogicalNewline => {
let line = locator.line(range.end());
let indent_index = line.chars().position(|c| !c.is_whitespace());
let indent_index = line.char_indices().find_map(|(i, c)| {
if c.is_whitespace() {
None
} else {
Some(i)
}
});
if let Some(indent_index) = indent_index {
if indent_index > 0 {
let whitespace = &line[..indent_index];
Expand Down Expand Up @@ -223,6 +229,20 @@ x = (
&Indentation(" ".to_string())
);

let contents = r"
x = (
 1,
 2,
 3,
)
";
let locator = Locator::new(contents);
let tokens: Vec<_> = lex(contents, Mode::Module).collect();
assert_eq!(
Stylist::from_tokens(&tokens, &locator).indentation(),
&Indentation(" ".to_string())
);

// formfeed indent, see `detect_indention` comment.
let contents = r"
class FormFeedIndent:
Expand Down

0 comments on commit f9a6450

Please sign in to comment.