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

Fix Indexer fails to identify continuation preceded by newline #10351 #10354

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 28 additions & 7 deletions crates/ruff_python_index/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl Indexer {
let mut continuation_lines = Vec::new();
// Token, end
let mut prev_end = TextSize::default();
let mut prev_token: Option<&Tok> = None;
let mut line_start = TextSize::default();

for (tok, range) in tokens.iter().flatten() {
Expand All @@ -51,11 +50,7 @@ impl Indexer {
if text == "\r" && trivia.as_bytes().get(index + 1) == Some(&b'\n') {
continue;
}

// Newlines after a newline never form a continuation.
if !matches!(prev_token, Some(Tok::Newline | Tok::NonLogicalNewline)) {
continuation_lines.push(line_start);
}
continuation_lines.push(line_start);

// SAFETY: Safe because of the len assertion at the top of the function.
#[allow(clippy::cast_possible_truncation)]
Expand All @@ -80,7 +75,6 @@ impl Indexer {
_ => {}
}

prev_token = Some(tok);
prev_end = range.end();
}

Expand Down Expand Up @@ -361,6 +355,33 @@ f'foo { 'str1' \
TextSize::new(63),
]
);

let contents = r"
x = (
1
\
\
\

\
+ 2)
"
.trim();
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
let indexer = Indexer::from_tokens(lxr.as_slice(), &Locator::new(contents));
assert_eq!(
indexer.continuation_line_starts(),
[
// row 3
TextSize::new(12),
// row 4
TextSize::new(18),
// row 5
TextSize::new(24),
// row 7
TextSize::new(31),
]
);
}

#[test]
Expand Down