Refactor methods onto Printer struct.
No (intentional) changes to behavior. This is intended to avoid the anti-pattern of having to import individual methods throughout code.
This commit is contained in:
parent
a1f180bde3
commit
bac4bb9613
File diff suppressed because it is too large
Load Diff
@ -21,7 +21,6 @@ use std::io;
|
||||
use std::mem;
|
||||
use std::usize;
|
||||
use syntax::ast;
|
||||
use syntax::print::pp;
|
||||
use syntax::print::pprust::PrintState;
|
||||
use util::nodemap::NodeMap;
|
||||
use hir;
|
||||
@ -157,7 +156,7 @@ impl<'a, 'tcx, O:DataFlowOperator> pprust::PpAnn for DataFlowContext<'a, 'tcx, O
|
||||
ps.synth_comment(
|
||||
format!("id {}: {}{}{}{}", id, entry_str,
|
||||
gens_str, action_kills_str, scope_kills_str))?;
|
||||
pp::space(&mut ps.s)?;
|
||||
ps.s.space()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ use rustc_mir::util::{write_mir_pretty, write_mir_graphviz};
|
||||
|
||||
use syntax::ast::{self, BlockCheckMode};
|
||||
use syntax::fold::{self, Folder};
|
||||
use syntax::print::{pp, pprust};
|
||||
use syntax::print::{pprust};
|
||||
use syntax::print::pprust::PrintState;
|
||||
use syntax::ptr::P;
|
||||
use syntax::util::small_vector::SmallVector;
|
||||
@ -357,24 +357,24 @@ impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> {
|
||||
pprust::NodeName(_) => Ok(()),
|
||||
|
||||
pprust::NodeItem(item) => {
|
||||
pp::space(&mut s.s)?;
|
||||
s.s.space()?;
|
||||
s.synth_comment(item.id.to_string())
|
||||
}
|
||||
pprust::NodeSubItem(id) => {
|
||||
pp::space(&mut s.s)?;
|
||||
s.s.space()?;
|
||||
s.synth_comment(id.to_string())
|
||||
}
|
||||
pprust::NodeBlock(blk) => {
|
||||
pp::space(&mut s.s)?;
|
||||
s.s.space()?;
|
||||
s.synth_comment(format!("block {}", blk.id))
|
||||
}
|
||||
pprust::NodeExpr(expr) => {
|
||||
pp::space(&mut s.s)?;
|
||||
s.s.space()?;
|
||||
s.synth_comment(expr.id.to_string())?;
|
||||
s.pclose()
|
||||
}
|
||||
pprust::NodePat(pat) => {
|
||||
pp::space(&mut s.s)?;
|
||||
s.s.space()?;
|
||||
s.synth_comment(format!("pat {}", pat.id))
|
||||
}
|
||||
}
|
||||
@ -414,24 +414,24 @@ impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
|
||||
match node {
|
||||
pprust_hir::NodeName(_) => Ok(()),
|
||||
pprust_hir::NodeItem(item) => {
|
||||
pp::space(&mut s.s)?;
|
||||
s.s.space()?;
|
||||
s.synth_comment(item.id.to_string())
|
||||
}
|
||||
pprust_hir::NodeSubItem(id) => {
|
||||
pp::space(&mut s.s)?;
|
||||
s.s.space()?;
|
||||
s.synth_comment(id.to_string())
|
||||
}
|
||||
pprust_hir::NodeBlock(blk) => {
|
||||
pp::space(&mut s.s)?;
|
||||
s.s.space()?;
|
||||
s.synth_comment(format!("block {}", blk.id))
|
||||
}
|
||||
pprust_hir::NodeExpr(expr) => {
|
||||
pp::space(&mut s.s)?;
|
||||
s.s.space()?;
|
||||
s.synth_comment(expr.id.to_string())?;
|
||||
s.pclose()
|
||||
}
|
||||
pprust_hir::NodePat(pat) => {
|
||||
pp::space(&mut s.s)?;
|
||||
s.s.space()?;
|
||||
s.synth_comment(format!("pat {}", pat.id))
|
||||
}
|
||||
}
|
||||
@ -456,13 +456,13 @@ impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
|
||||
fn post(&self, s: &mut pprust::State, node: pprust::AnnNode) -> io::Result<()> {
|
||||
match node {
|
||||
pprust::NodeIdent(&ast::Ident { name, ctxt }) => {
|
||||
pp::space(&mut s.s)?;
|
||||
s.s.space()?;
|
||||
// FIXME #16420: this doesn't display the connections
|
||||
// between syntax contexts
|
||||
s.synth_comment(format!("{}{:?}", name.as_u32(), ctxt))
|
||||
}
|
||||
pprust::NodeName(&name) => {
|
||||
pp::space(&mut s.s)?;
|
||||
s.s.space()?;
|
||||
s.synth_comment(name.as_u32().to_string())
|
||||
}
|
||||
_ => Ok(()),
|
||||
@ -514,10 +514,10 @@ impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
|
||||
fn post(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Result<()> {
|
||||
match node {
|
||||
pprust_hir::NodeExpr(expr) => {
|
||||
pp::space(&mut s.s)?;
|
||||
pp::word(&mut s.s, "as")?;
|
||||
pp::space(&mut s.s)?;
|
||||
pp::word(&mut s.s, &self.tables.get().expr_ty(expr).to_string())?;
|
||||
s.s.space()?;
|
||||
s.s.word("as")?;
|
||||
s.s.space()?;
|
||||
s.s.word(&self.tables.get().expr_ty(expr).to_string())?;
|
||||
s.pclose()
|
||||
}
|
||||
_ => Ok(()),
|
||||
@ -945,13 +945,13 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
|
||||
for node_id in uii.all_matching_node_ids(hir_map) {
|
||||
let node = hir_map.get(node_id);
|
||||
pp_state.print_node(node)?;
|
||||
pp::space(&mut pp_state.s)?;
|
||||
pp_state.s.space()?;
|
||||
let path = annotation.node_path(node_id)
|
||||
.expect("--unpretty missing node paths");
|
||||
pp_state.synth_comment(path)?;
|
||||
pp::hardbreak(&mut pp_state.s)?;
|
||||
pp_state.s.hardbreak()?;
|
||||
}
|
||||
pp::eof(&mut pp_state.s)
|
||||
pp_state.s.eof()
|
||||
})
|
||||
}
|
||||
_ => unreachable!(),
|
||||
|
@ -1480,10 +1480,9 @@ impl<'a> Parser<'a> {
|
||||
match ty.node {
|
||||
TyKind::Rptr(ref lifetime, ref mut_ty) => {
|
||||
let sum_with_parens = pprust::to_string(|s| {
|
||||
use print::pp::word;
|
||||
use print::pprust::PrintState;
|
||||
|
||||
word(&mut s.s, "&")?;
|
||||
s.s.word("&")?;
|
||||
s.print_opt_lifetime(lifetime)?;
|
||||
s.print_mutability(mut_ty.mutbl)?;
|
||||
s.popen()?;
|
||||
@ -2542,14 +2541,13 @@ impl<'a> Parser<'a> {
|
||||
};
|
||||
let sugg = pprust::to_string(|s| {
|
||||
use print::pprust::PrintState;
|
||||
use print::pp::word;
|
||||
s.popen()?;
|
||||
s.print_expr(&e)?;
|
||||
word(&mut s.s, ".")?;
|
||||
s.s.word( ".")?;
|
||||
s.print_usize(float.trunc() as usize)?;
|
||||
s.pclose()?;
|
||||
word(&mut s.s, ".")?;
|
||||
word(&mut s.s, fstr.splitn(2, ".").last().unwrap())
|
||||
s.s.word(".")?;
|
||||
s.s.word(fstr.splitn(2, ".").last().unwrap())
|
||||
});
|
||||
err.span_suggestion(
|
||||
lo.to(self.prev_span),
|
||||
|
@ -262,7 +262,7 @@ pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> {
|
||||
}
|
||||
|
||||
pub struct Printer<'a> {
|
||||
pub out: Box<io::Write+'a>,
|
||||
out: Box<io::Write+'a>,
|
||||
buf_len: usize,
|
||||
/// Width of lines we're constrained to
|
||||
margin: isize,
|
||||
@ -577,75 +577,75 @@ impl<'a> Printer<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convenience functions to talk to the printer.
|
||||
// Convenience functions to talk to the printer.
|
||||
|
||||
/// "raw box"
|
||||
pub fn rbox(p: &mut Printer, indent: usize, b: Breaks) -> io::Result<()> {
|
||||
p.pretty_print(Token::Begin(BeginToken {
|
||||
offset: indent as isize,
|
||||
breaks: b
|
||||
}))
|
||||
}
|
||||
/// "raw box"
|
||||
pub fn rbox(&mut self, indent: usize, b: Breaks) -> io::Result<()> {
|
||||
self.pretty_print(Token::Begin(BeginToken {
|
||||
offset: indent as isize,
|
||||
breaks: b
|
||||
}))
|
||||
}
|
||||
|
||||
/// Inconsistent breaking box
|
||||
pub fn ibox(p: &mut Printer, indent: usize) -> io::Result<()> {
|
||||
rbox(p, indent, Breaks::Inconsistent)
|
||||
}
|
||||
/// Inconsistent breaking box
|
||||
pub fn ibox(&mut self, indent: usize) -> io::Result<()> {
|
||||
self.rbox(indent, Breaks::Inconsistent)
|
||||
}
|
||||
|
||||
/// Consistent breaking box
|
||||
pub fn cbox(p: &mut Printer, indent: usize) -> io::Result<()> {
|
||||
rbox(p, indent, Breaks::Consistent)
|
||||
}
|
||||
/// Consistent breaking box
|
||||
pub fn cbox(&mut self, indent: usize) -> io::Result<()> {
|
||||
self.rbox(indent, Breaks::Consistent)
|
||||
}
|
||||
|
||||
pub fn break_offset(p: &mut Printer, n: usize, off: isize) -> io::Result<()> {
|
||||
p.pretty_print(Token::Break(BreakToken {
|
||||
offset: off,
|
||||
blank_space: n as isize
|
||||
}))
|
||||
}
|
||||
pub fn break_offset(&mut self, n: usize, off: isize) -> io::Result<()> {
|
||||
self.pretty_print(Token::Break(BreakToken {
|
||||
offset: off,
|
||||
blank_space: n as isize
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn end(p: &mut Printer) -> io::Result<()> {
|
||||
p.pretty_print(Token::End)
|
||||
}
|
||||
pub fn end(&mut self) -> io::Result<()> {
|
||||
self.pretty_print(Token::End)
|
||||
}
|
||||
|
||||
pub fn eof(p: &mut Printer) -> io::Result<()> {
|
||||
p.pretty_print(Token::Eof)
|
||||
}
|
||||
pub fn eof(&mut self) -> io::Result<()> {
|
||||
self.pretty_print(Token::Eof)
|
||||
}
|
||||
|
||||
pub fn word(p: &mut Printer, wrd: &str) -> io::Result<()> {
|
||||
p.pretty_print(Token::String(wrd.to_string(), wrd.len() as isize))
|
||||
}
|
||||
pub fn word(&mut self, wrd: &str) -> io::Result<()> {
|
||||
self.pretty_print(Token::String(wrd.to_string(), wrd.len() as isize))
|
||||
}
|
||||
|
||||
pub fn huge_word(p: &mut Printer, wrd: &str) -> io::Result<()> {
|
||||
p.pretty_print(Token::String(wrd.to_string(), SIZE_INFINITY))
|
||||
}
|
||||
pub fn huge_word(&mut self, wrd: &str) -> io::Result<()> {
|
||||
self.pretty_print(Token::String(wrd.to_string(), SIZE_INFINITY))
|
||||
}
|
||||
|
||||
pub fn zero_word(p: &mut Printer, wrd: &str) -> io::Result<()> {
|
||||
p.pretty_print(Token::String(wrd.to_string(), 0))
|
||||
}
|
||||
pub fn zero_word(&mut self, wrd: &str) -> io::Result<()> {
|
||||
self.pretty_print(Token::String(wrd.to_string(), 0))
|
||||
}
|
||||
|
||||
pub fn spaces(p: &mut Printer, n: usize) -> io::Result<()> {
|
||||
break_offset(p, n, 0)
|
||||
}
|
||||
fn spaces(&mut self, n: usize) -> io::Result<()> {
|
||||
self.break_offset(n, 0)
|
||||
}
|
||||
|
||||
pub fn zerobreak(p: &mut Printer) -> io::Result<()> {
|
||||
spaces(p, 0)
|
||||
}
|
||||
pub fn zerobreak(&mut self) -> io::Result<()> {
|
||||
self.spaces(0)
|
||||
}
|
||||
|
||||
pub fn space(p: &mut Printer) -> io::Result<()> {
|
||||
spaces(p, 1)
|
||||
}
|
||||
pub fn space(&mut self) -> io::Result<()> {
|
||||
self.spaces(1)
|
||||
}
|
||||
|
||||
pub fn hardbreak(p: &mut Printer) -> io::Result<()> {
|
||||
spaces(p, SIZE_INFINITY as usize)
|
||||
}
|
||||
pub fn hardbreak(&mut self) -> io::Result<()> {
|
||||
self.spaces(SIZE_INFINITY as usize)
|
||||
}
|
||||
|
||||
pub fn hardbreak_tok_offset(off: isize) -> Token {
|
||||
Token::Break(BreakToken {offset: off, blank_space: SIZE_INFINITY})
|
||||
}
|
||||
pub fn hardbreak_tok_offset(off: isize) -> Token {
|
||||
Token::Break(BreakToken {offset: off, blank_space: SIZE_INFINITY})
|
||||
}
|
||||
|
||||
pub fn hardbreak_tok() -> Token {
|
||||
hardbreak_tok_offset(0)
|
||||
pub fn hardbreak_tok() -> Token {
|
||||
Self::hardbreak_tok_offset(0)
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user