Call syntax::with_globals before using a parser

This commit is contained in:
Seiichi Uchida 2018-03-15 18:55:52 +09:00
parent a353294fe4
commit eda626cfc9
2 changed files with 25 additions and 12 deletions

View file

@ -657,6 +657,14 @@ pub fn format_code_block(code_snippet: &str, config: &Config) -> Option<String>
}
pub fn format_input<T: Write>(
input: Input,
config: &Config,
out: Option<&mut T>,
) -> Result<(Summary, FileMap, FormatReport), (io::Error, Summary)> {
syntax::with_globals(|| format_input_inner(input, config, out))
}
fn format_input_inner<T: Write>(
input: Input,
config: &Config,
mut out: Option<&mut T>,

View file

@ -962,22 +962,27 @@ fn format_lazy_static(context: &RewriteContext, shape: Shape, ts: &TokenStream)
#[cfg(test)]
mod test {
use super::*;
use syntax;
use syntax::parse::{parse_stream_from_source_str, ParseSess};
use syntax::codemap::{FileName, FilePathMapping};
fn format_macro_args_str(s: &str) -> String {
let input = parse_stream_from_source_str(
FileName::Custom("stdin".to_owned()),
s.to_owned(),
&ParseSess::new(FilePathMapping::empty()),
None,
);
let shape = Shape {
width: 100,
indent: Indent::empty(),
offset: 0,
};
format_macro_args(input.into(), shape).unwrap()
let mut result = String::new();
syntax::with_globals(|| {
let input = parse_stream_from_source_str(
FileName::Custom("stdin".to_owned()),
s.to_owned(),
&ParseSess::new(FilePathMapping::empty()),
None,
);
let shape = Shape {
width: 100,
indent: Indent::empty(),
offset: 0,
};
result = format_macro_args(input.into(), shape).unwrap();
});
result
}
#[test]