Use our own FileName struct rather than exporting libsyntax's

This commit is contained in:
Nick Cameron 2018-05-21 12:01:31 +12:00
parent 539d4d9665
commit 843c12601a
9 changed files with 53 additions and 31 deletions

View File

@ -215,7 +215,7 @@ fn execute(opts: &Options) -> Result<(WriteMode, Summary), failure::Error> {
config.set().file_lines(options.file_lines);
for f in config.file_lines().files() {
match *f {
FileName::Custom(ref f) if f == "stdin" => {}
FileName::Stdin => {}
_ => eprintln!("Warning: Extra file listed in file_lines option '{}'", f),
}
}
@ -500,7 +500,7 @@ impl GetOptsOptions {
FileName::Real(_) => {
eprintln!("Warning: Extra file listed in file_lines option '{}'", f)
}
_ => eprintln!("Warning: Not a file '{}'", f),
FileName::Stdin => eprintln!("Warning: Not a file '{}'", f),
}
}
}

View File

@ -11,13 +11,14 @@
//! This module contains types and functions to support formatting specific line ranges.
use std::collections::HashMap;
use std::path::PathBuf;
use std::rc::Rc;
use std::{cmp, iter, str};
use std::{cmp, fmt, iter, str};
use serde::de::{Deserialize, Deserializer};
use serde_json as json;
use syntax::codemap::{FileMap, FileName};
use syntax::codemap::{self, FileMap};
/// A range of lines in a file, inclusive of both ends.
pub struct LineRange {
@ -26,9 +27,34 @@ pub struct LineRange {
pub hi: usize,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum FileName {
Real(PathBuf),
Stdin,
}
impl From<codemap::FileName> for FileName {
fn from(name: codemap::FileName) -> FileName {
match name {
codemap::FileName::Real(p) => FileName::Real(p),
codemap::FileName::Custom(ref f) if f == "stdin" => FileName::Stdin,
_ => unreachable!(),
}
}
}
impl fmt::Display for FileName {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
FileName::Real(p) => write!(f, "{}", p.to_str().unwrap()),
FileName::Stdin => write!(f, "stdin"),
}
}
}
impl LineRange {
pub fn file_name(&self) -> &FileName {
&self.file.name
pub fn file_name(&self) -> FileName {
self.file.name.clone().into()
}
}
@ -169,12 +195,12 @@ impl FileLines {
/// Returns true if `range` is fully contained in `self`.
#[allow(dead_code)]
pub(crate) fn contains(&self, range: &LineRange) -> bool {
self.file_range_matches(range.file_name(), |r| r.contains(Range::from(range)))
self.file_range_matches(&range.file_name(), |r| r.contains(Range::from(range)))
}
/// Returns true if any lines in `range` are in `self`.
pub(crate) fn intersects(&self, range: &LineRange) -> bool {
self.file_range_matches(range.file_name(), |r| r.intersects(Range::from(range)))
self.file_range_matches(&range.file_name(), |r| r.intersects(Range::from(range)))
}
/// Returns true if `line` from `file_name` is in `self`.
@ -232,7 +258,7 @@ struct JsonSpan {
fn deserialize_filename<'de, D: Deserializer<'de>>(d: D) -> Result<FileName, D::Error> {
let s = String::deserialize(d)?;
if s == "stdin" {
Ok(FileName::Custom(s))
Ok(FileName::Stdin)
} else {
Ok(FileName::Real(s.into()))
}

View File

@ -17,7 +17,7 @@ use std::path::{Path, PathBuf};
use std::{env, fs};
use config::config_type::ConfigType;
pub use config::file_lines::FileLines;
pub use config::file_lines::{FileLines, FileName};
pub use config::lists::*;
pub use config::options::*;

View File

@ -8,11 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use syntax::codemap::FileName;
use config::config_type::ConfigType;
use config::lists::*;
use config::Config;
use config::{Config, FileName};
use std::collections::HashSet;
use std::path::{Path, PathBuf};

View File

@ -15,9 +15,8 @@ use std::io::{self, BufWriter, Read, Write};
use std::path::Path;
use checkstyle::output_checkstyle_file;
use config::{Config, NewlineStyle, Verbosity, WriteMode};
use config::{Config, FileName, NewlineStyle, Verbosity, WriteMode};
use rustfmt_diff::{make_diff, output_modified, print_diff, Mismatch};
use syntax::codemap::FileName;
#[cfg(test)]
use FileRecord;

View File

@ -46,7 +46,6 @@ use std::rc::Rc;
use std::time::Duration;
use syntax::ast;
pub use syntax::codemap::FileName;
use syntax::codemap::{CodeMap, FilePathMapping, Span};
use syntax::errors::emitter::{ColorConfig, EmitterWriter};
use syntax::errors::{DiagnosticBuilder, Handler};
@ -59,9 +58,10 @@ use shape::Indent;
use utils::use_colored_tty;
use visitor::{FmtVisitor, SnippetProvider};
pub use config::options::CliOptions;
pub use config::summary::Summary;
pub use config::{load_config, Color, Config, FileLines, Verbosity, WriteMode};
pub use config::{
load_config, CliOptions, Color, Config, FileLines, FileName, Verbosity, WriteMode,
};
#[macro_use]
mod utils;
@ -97,8 +97,6 @@ mod types;
mod vertical;
pub(crate) mod visitor;
const STDIN: &str = "<stdin>";
// A map of the files of a crate, with their new content
pub(crate) type FileMap = Vec<FileRecord>;
@ -397,7 +395,7 @@ fn should_emit_verbose<F>(path: &FileName, config: &Config, f: F)
where
F: Fn(),
{
if config.verbose() == Verbosity::Verbose && path.to_string() != STDIN {
if config.verbose() == Verbosity::Verbose && path != &FileName::Stdin {
f();
}
}
@ -626,7 +624,7 @@ fn parse_input<'sess>(
Input::File(file) => parse::new_parser_from_file(parse_session, &file),
Input::Text(text) => parse::new_parser_from_source_str(
parse_session,
FileName::Custom("stdin".to_owned()),
syntax::codemap::FileName::Custom("stdin".to_owned()),
text,
),
};
@ -797,7 +795,7 @@ fn format_input_inner<T: Write>(
let main_file = match input {
Input::File(ref file) => FileName::Real(file.clone()),
Input::Text(..) => FileName::Custom("stdin".to_owned()),
Input::Text(..) => FileName::Stdin,
};
let krate = match parse_input(input, &parse_session, config) {

View File

@ -10,11 +10,11 @@
use std::borrow::Cow;
use syntax::codemap::{BytePos, FileName, Pos, Span};
use syntax::codemap::{BytePos, Pos, Span};
use codemap::LineRangeUtils;
use comment::{rewrite_comment, CodeCharKind, CommentCodeSlices};
use config::WriteMode;
use config::{FileName, WriteMode};
use shape::{Indent, Shape};
use utils::{count_newlines, last_line_width, mk_sp};
use visitor::FmtVisitor;
@ -177,7 +177,7 @@ impl<'a> FmtVisitor<'a> {
// Annoyingly, the library functions for splitting by lines etc. are not
// quite right, so we must do it ourselves.
let char_pos = self.codemap.lookup_char_pos(span.lo());
let file_name = &char_pos.file.name;
let file_name = &char_pos.file.name.clone().into();
let mut status = SnippetStatus::new(char_pos.line);
let snippet = &*match self.config.write_mode() {

View File

@ -13,9 +13,10 @@ use std::io;
use std::path::{Path, PathBuf};
use syntax::ast;
use syntax::codemap::{self, FileName};
use syntax::codemap;
use syntax::parse::{parser, DirectoryOwnership};
use config::FileName;
use utils::contains_skip;
/// List all the files containing modules of a crate.
@ -28,12 +29,12 @@ pub fn list_files<'a>(
let root_filename = codemap.span_to_filename(krate.span);
{
let parent = match root_filename {
FileName::Real(ref path) => path.parent().unwrap(),
codemap::FileName::Real(ref path) => path.parent().unwrap(),
_ => Path::new(""),
};
list_submodules(&krate.module, parent, None, codemap, &mut result)?;
}
result.insert(root_filename, &krate.module);
result.insert(root_filename.into(), &krate.module);
Ok(result)
}

View File

@ -594,7 +594,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
pub fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -> bool {
for attr in attrs {
if attr.name() == DEPR_SKIP_ANNOTATION {
let file_name = self.codemap.span_to_filename(attr.span);
let file_name = self.codemap.span_to_filename(attr.span).into();
self.report.append(
file_name,
vec![FormattingError::from_span(
@ -607,7 +607,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
if attr.path.segments.len() == 1
|| attr.path.segments[1].ident.to_string() != "skip"
{
let file_name = self.codemap.span_to_filename(attr.span);
let file_name = self.codemap.span_to_filename(attr.span).into();
self.report.append(
file_name,
vec![FormattingError::from_span(