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

Improve excepthandler_name_range #1368

Merged
merged 2 commits into from Dec 25, 2022
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
34 changes: 24 additions & 10 deletions src/ast/helpers.rs
Expand Up @@ -402,16 +402,30 @@ pub fn excepthandler_name_range(
handler: &Excepthandler,
locator: &SourceCodeLocator,
) -> Option<Range> {
let contents = locator.slice_source_code_range(&Range::from_located(handler));
let range = lexer::make_tokenizer(&contents)
.flatten()
.tuple_windows()
.find(|(tok, next_tok)| matches!(tok.1, Tok::As) && matches!(next_tok.1, Tok::Name { .. }))
.map(|((..), (start, _, end))| Range {
location: to_absolute(start, handler.location),
end_location: to_absolute(end, handler.location),
});
range
let ExcepthandlerKind::ExceptHandler {
name, type_, body, ..
} = &handler.node;
match (name, type_) {
(Some(_), Some(type_)) => {
let type_end_location = type_.end_location.unwrap();
let contents = locator.slice_source_code_range(&Range {
location: type_end_location,
end_location: body[0].location,
});
let range = lexer::make_tokenizer(&contents)
.flatten()
.tuple_windows()
.find(|(tok, next_tok)| {
matches!(tok.1, Tok::As) && matches!(next_tok.1, Tok::Name { .. })
})
.map(|((..), (start, _, end))| Range {
location: to_absolute(start, type_end_location),
end_location: to_absolute(end, type_end_location),
});
range
}
_ => None,
}
}

/// Return `true` if a `Stmt` appears to be part of a multi-statement line, with
Expand Down