mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Use char index rather than position for indent slice (#11645)
## Summary A beginner's mistake :) Closes https://github.com/astral-sh/ruff/issues/11641.
This commit is contained in:
parent
8a25531a71
commit
f9a64503c8
1 changed files with 21 additions and 1 deletions
|
@ -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];
|
||||
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue