Skip to content

Commit

Permalink
rustc_ast/comments: Modernize some enum reexports
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Aug 6, 2020
1 parent 46f48d3 commit 000c070
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
18 changes: 10 additions & 8 deletions src/librustc_ast/util/comments.rs
@@ -1,5 +1,3 @@
pub use CommentStyle::*;

use crate::ast::AttrStyle;
use crate::token::CommentKind;
use rustc_span::source_map::SourceMap;
Expand Down Expand Up @@ -198,7 +196,7 @@ pub fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comme

if let Some(shebang_len) = rustc_lexer::strip_shebang(text) {
comments.push(Comment {
style: Isolated,
style: CommentStyle::Isolated,
lines: vec![text[..shebang_len].to_string()],
pos: start_bpos,
});
Expand All @@ -214,7 +212,7 @@ pub fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comme
while let Some(next_newline) = &token_text[idx + 1..].find('\n') {
idx = idx + 1 + next_newline;
comments.push(Comment {
style: BlankLine,
style: CommentStyle::BlankLine,
lines: vec![],
pos: start_bpos + BytePos((pos + idx) as u32),
});
Expand All @@ -228,9 +226,9 @@ pub fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comme
_ => true,
};
let style = match (code_to_the_left, code_to_the_right) {
(_, true) => Mixed,
(false, false) => Isolated,
(true, false) => Trailing,
(_, true) => CommentStyle::Mixed,
(false, false) => CommentStyle::Isolated,
(true, false) => CommentStyle::Trailing,
};

// Count the number of chars since the start of the line by rescanning.
Expand All @@ -246,7 +244,11 @@ pub fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comme
rustc_lexer::TokenKind::LineComment => {
if line_doc_comment_style(token_text).is_none() {
comments.push(Comment {
style: if code_to_the_left { Trailing } else { Isolated },
style: if code_to_the_left {
CommentStyle::Trailing
} else {
CommentStyle::Isolated
},
lines: vec![token_text.to_string()],
pos: start_bpos + BytePos(pos as u32),
})
Expand Down
25 changes: 13 additions & 12 deletions src/librustc_ast_pretty/pprust.rs
Expand Up @@ -10,8 +10,9 @@ use rustc_ast::attr;
use rustc_ast::ptr::P;
use rustc_ast::token::{self, BinOpToken, CommentKind, DelimToken, Nonterminal, Token, TokenKind};
use rustc_ast::tokenstream::{TokenStream, TokenTree};
use rustc_ast::util::classify;
use rustc_ast::util::comments::{gather_comments, Comment, CommentStyle};
use rustc_ast::util::parser::{self, AssocOp, Fixity};
use rustc_ast::util::{classify, comments};
use rustc_span::edition::Edition;
use rustc_span::source_map::{SourceMap, Spanned};
use rustc_span::symbol::{kw, sym, Ident, IdentPrinter, Symbol};
Expand Down Expand Up @@ -50,27 +51,27 @@ impl PpAnn for NoAnn {}

pub struct Comments<'a> {
sm: &'a SourceMap,
comments: Vec<comments::Comment>,
comments: Vec<Comment>,
current: usize,
}

impl<'a> Comments<'a> {
pub fn new(sm: &'a SourceMap, filename: FileName, input: String) -> Comments<'a> {
let comments = comments::gather_comments(sm, filename, input);
let comments = gather_comments(sm, filename, input);
Comments { sm, comments, current: 0 }
}

pub fn next(&self) -> Option<comments::Comment> {
pub fn next(&self) -> Option<Comment> {
self.comments.get(self.current).cloned()
}

pub fn trailing_comment(
&mut self,
span: rustc_span::Span,
next_pos: Option<BytePos>,
) -> Option<comments::Comment> {
) -> Option<Comment> {
if let Some(cmnt) = self.next() {
if cmnt.style != comments::Trailing {
if cmnt.style != CommentStyle::Trailing {
return None;
}
let span_line = self.sm.lookup_char_pos(span.hi());
Expand Down Expand Up @@ -462,9 +463,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
}
}

fn print_comment(&mut self, cmnt: &comments::Comment) {
fn print_comment(&mut self, cmnt: &Comment) {
match cmnt.style {
comments::Mixed => {
CommentStyle::Mixed => {
if !self.is_beginning_of_line() {
self.zerobreak();
}
Expand All @@ -483,7 +484,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
}
self.zerobreak()
}
comments::Isolated => {
CommentStyle::Isolated => {
self.hardbreak_if_not_bol();
for line in &cmnt.lines {
// Don't print empty lines because they will end up as trailing
Expand All @@ -494,7 +495,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.hardbreak();
}
}
comments::Trailing => {
CommentStyle::Trailing => {
if !self.is_beginning_of_line() {
self.word(" ");
}
Expand All @@ -512,7 +513,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.end();
}
}
comments::BlankLine => {
CommentStyle::BlankLine => {
// We need to do at least one, possibly two hardbreaks.
let twice = match self.last_token() {
pp::Token::String(s) => ";" == s,
Expand All @@ -531,7 +532,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
}
}

fn next_comment(&mut self) -> Option<comments::Comment> {
fn next_comment(&mut self) -> Option<Comment> {
self.comments().as_mut().and_then(|c| c.next())
}

Expand Down

0 comments on commit 000c070

Please sign in to comment.