rustc_ast/comments: Modernize some enum reexports

This commit is contained in:
Vadim Petrochenkov 2020-07-22 11:42:36 +03:00
parent 46f48d31fe
commit 000c070b70
2 changed files with 23 additions and 20 deletions

View file

@ -1,5 +1,3 @@
pub use CommentStyle::*;
use crate::ast::AttrStyle;
use crate::token::CommentKind;
use rustc_span::source_map::SourceMap;
@ -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,
});
@ -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),
});
@ -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.
@ -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),
})

View file

@ -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};
@ -50,17 +51,17 @@ 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()
}
@ -68,9 +69,9 @@ impl<'a> Comments<'a> {
&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());
@ -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();
}
@ -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
@ -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(" ");
}
@ -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,
@ -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())
}