mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-05 05:04:24 +00:00
Use SnippetProvider in FmtVisitor and RewriteContext
This commit is contained in:
parent
98860ab890
commit
0928762562
@ -15,6 +15,7 @@ use syntax::parse::ParseSess;
|
||||
|
||||
use config::{Config, IndentStyle};
|
||||
use shape::Shape;
|
||||
use visitor::SnippetProvider;
|
||||
|
||||
pub trait Rewrite {
|
||||
/// Rewrite self into shape.
|
||||
@ -34,11 +35,12 @@ pub struct RewriteContext<'a> {
|
||||
pub is_if_else_block: bool,
|
||||
// When rewriting chain, veto going multi line except the last element
|
||||
pub force_one_line_chain: bool,
|
||||
pub snippet_provider: &'a SnippetProvider,
|
||||
}
|
||||
|
||||
impl<'a> RewriteContext<'a> {
|
||||
pub fn snippet(&self, span: Span) -> String {
|
||||
self.codemap.span_to_snippet(span).unwrap()
|
||||
pub fn snippet(&self, span: Span) -> &str {
|
||||
self.snippet_provider.span_to_snippet(span).unwrap()
|
||||
}
|
||||
|
||||
/// Return true if we should use block indent style for rewriting function call.
|
||||
|
@ -84,6 +84,7 @@ pub struct FmtVisitor<'a> {
|
||||
pub block_indent: Indent,
|
||||
pub config: &'a Config,
|
||||
pub is_if_else_block: bool,
|
||||
pub snippet_provier: SnippetProvider,
|
||||
}
|
||||
|
||||
impl<'a> FmtVisitor<'a> {
|
||||
@ -538,25 +539,18 @@ impl<'a> FmtVisitor<'a> {
|
||||
block_indent: Indent::empty(),
|
||||
config: config,
|
||||
is_if_else_block: false,
|
||||
snippet_provier: SnippetProvider::from_codemap(parse_session.codemap(), span),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn opt_snippet(&self, span: Span) -> Option<String> {
|
||||
self.codemap.span_to_snippet(span).ok()
|
||||
pub fn opt_snippet<'b: 'a>(&'a self, span: Span) -> Option<&'b str> {
|
||||
self.snippet_provier
|
||||
.span_to_snippet(span)
|
||||
.map(|s| unsafe { mem::transmute::<&'a str, &'b str>(s) })
|
||||
}
|
||||
|
||||
pub fn snippet(&self, span: Span) -> String {
|
||||
match self.codemap.span_to_snippet(span) {
|
||||
Ok(s) => s,
|
||||
Err(_) => {
|
||||
eprintln!(
|
||||
"Couldn't make snippet for span {:?}->{:?}",
|
||||
self.codemap.lookup_char_pos(span.lo()),
|
||||
self.codemap.lookup_char_pos(span.hi())
|
||||
);
|
||||
"".to_owned()
|
||||
}
|
||||
}
|
||||
pub fn snippet<'b: 'a>(&'a self, span: Span) -> &'b str {
|
||||
self.opt_snippet(span).unwrap()
|
||||
}
|
||||
|
||||
// Returns true if we should skip the following item.
|
||||
@ -753,6 +747,7 @@ impl<'a> FmtVisitor<'a> {
|
||||
use_block: false,
|
||||
is_if_else_block: false,
|
||||
force_one_line_chain: false,
|
||||
snippet_provider: &self.snippet_provier,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user