Avoid emitting empty logical lines (#4452)

This commit is contained in:
Charlie Marsh 2023-05-16 12:33:33 -04:00 committed by GitHub
parent 4b05ca1198
commit 7e0d018b35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 5 deletions

View file

@ -49,3 +49,18 @@ if False:
#: #:
if False: # if False: #
print() print()
#:
if False:
print()
print()
#:
if False:
print()
if False:
print()
#:
if False:
print()

View file

@ -250,5 +250,20 @@ f()"#
"f()", "f()",
]; ];
assert_eq!(actual, expected); assert_eq!(actual, expected);
let contents = r#"
if False:
print()
"#
.trim();
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let actual: Vec<String> = LogicalLines::from_tokens(&lxr, &locator)
.into_iter()
.map(|line| line.text_trimmed().to_string())
.collect();
let expected = vec!["if False:", "print()", ""];
assert_eq!(actual, expected);
} }
} }

View file

@ -500,11 +500,16 @@ impl LogicalLinesBuilder {
fn finish_line(&mut self) { fn finish_line(&mut self) {
let end = self.tokens.len() as u32; let end = self.tokens.len() as u32;
if self.current_line.tokens_start < end { if self.current_line.tokens_start < end {
self.lines.push(Line { let is_empty = self.tokens[self.current_line.tokens_start as usize..end as usize]
flags: self.current_line.flags, .iter()
tokens_start: self.current_line.tokens_start, .all(|token| token.kind.is_newline());
tokens_end: end, if !is_empty {
}); self.lines.push(Line {
flags: self.current_line.flags,
tokens_start: self.current_line.tokens_start,
tokens_end: end,
});
}
self.current_line = CurrentLine { self.current_line = CurrentLine {
flags: TokenFlags::default(), flags: TokenFlags::default(),

View file

@ -167,6 +167,11 @@ pub enum TokenKind {
} }
impl TokenKind { impl TokenKind {
#[inline]
pub const fn is_newline(&self) -> bool {
matches!(self, TokenKind::Newline | TokenKind::NonLogicalNewline)
}
#[inline] #[inline]
pub const fn is_unary(&self) -> bool { pub const fn is_unary(&self) -> bool {
matches!(self, TokenKind::Plus | TokenKind::Minus | TokenKind::Star) matches!(self, TokenKind::Plus | TokenKind::Minus | TokenKind::Star)