Skip to content

Commit 843c126

Browse files
committed
Use our own FileName struct rather than exporting libsyntax's
1 parent 539d4d9 commit 843c126

File tree

9 files changed

+53
-31
lines changed

9 files changed

+53
-31
lines changed

src/bin/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ fn execute(opts: &Options) -> Result<(WriteMode, Summary), failure::Error> {
215215
config.set().file_lines(options.file_lines);
216216
for f in config.file_lines().files() {
217217
match *f {
218-
FileName::Custom(ref f) if f == "stdin" => {}
218+
FileName::Stdin => {}
219219
_ => eprintln!("Warning: Extra file listed in file_lines option '{}'", f),
220220
}
221221
}
@@ -500,7 +500,7 @@ impl GetOptsOptions {
500500
FileName::Real(_) => {
501501
eprintln!("Warning: Extra file listed in file_lines option '{}'", f)
502502
}
503-
_ => eprintln!("Warning: Not a file '{}'", f),
503+
FileName::Stdin => eprintln!("Warning: Not a file '{}'", f),
504504
}
505505
}
506506
}

src/config/file_lines.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
//! This module contains types and functions to support formatting specific line ranges.
1212
1313
use std::collections::HashMap;
14+
use std::path::PathBuf;
1415
use std::rc::Rc;
15-
use std::{cmp, iter, str};
16+
use std::{cmp, fmt, iter, str};
1617

1718
use serde::de::{Deserialize, Deserializer};
1819
use serde_json as json;
1920

20-
use syntax::codemap::{FileMap, FileName};
21+
use syntax::codemap::{self, FileMap};
2122

2223
/// A range of lines in a file, inclusive of both ends.
2324
pub struct LineRange {
@@ -26,9 +27,34 @@ pub struct LineRange {
2627
pub hi: usize,
2728
}
2829

30+
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
31+
pub enum FileName {
32+
Real(PathBuf),
33+
Stdin,
34+
}
35+
36+
impl From<codemap::FileName> for FileName {
37+
fn from(name: codemap::FileName) -> FileName {
38+
match name {
39+
codemap::FileName::Real(p) => FileName::Real(p),
40+
codemap::FileName::Custom(ref f) if f == "stdin" => FileName::Stdin,
41+
_ => unreachable!(),
42+
}
43+
}
44+
}
45+
46+
impl fmt::Display for FileName {
47+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48+
match self {
49+
FileName::Real(p) => write!(f, "{}", p.to_str().unwrap()),
50+
FileName::Stdin => write!(f, "stdin"),
51+
}
52+
}
53+
}
54+
2955
impl LineRange {
30-
pub fn file_name(&self) -> &FileName {
31-
&self.file.name
56+
pub fn file_name(&self) -> FileName {
57+
self.file.name.clone().into()
3258
}
3359
}
3460

@@ -169,12 +195,12 @@ impl FileLines {
169195
/// Returns true if `range` is fully contained in `self`.
170196
#[allow(dead_code)]
171197
pub(crate) fn contains(&self, range: &LineRange) -> bool {
172-
self.file_range_matches(range.file_name(), |r| r.contains(Range::from(range)))
198+
self.file_range_matches(&range.file_name(), |r| r.contains(Range::from(range)))
173199
}
174200

175201
/// Returns true if any lines in `range` are in `self`.
176202
pub(crate) fn intersects(&self, range: &LineRange) -> bool {
177-
self.file_range_matches(range.file_name(), |r| r.intersects(Range::from(range)))
203+
self.file_range_matches(&range.file_name(), |r| r.intersects(Range::from(range)))
178204
}
179205

180206
/// Returns true if `line` from `file_name` is in `self`.
@@ -232,7 +258,7 @@ struct JsonSpan {
232258
fn deserialize_filename<'de, D: Deserializer<'de>>(d: D) -> Result<FileName, D::Error> {
233259
let s = String::deserialize(d)?;
234260
if s == "stdin" {
235-
Ok(FileName::Custom(s))
261+
Ok(FileName::Stdin)
236262
} else {
237263
Ok(FileName::Real(s.into()))
238264
}

src/config/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::path::{Path, PathBuf};
1717
use std::{env, fs};
1818

1919
use config::config_type::ConfigType;
20-
pub use config::file_lines::FileLines;
20+
pub use config::file_lines::{FileLines, FileName};
2121
pub use config::lists::*;
2222
pub use config::options::*;
2323

src/config/options.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use syntax::codemap::FileName;
12-
1311
use config::config_type::ConfigType;
1412
use config::lists::*;
15-
use config::Config;
13+
use config::{Config, FileName};
1614

1715
use std::collections::HashSet;
1816
use std::path::{Path, PathBuf};

src/filemap.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ use std::io::{self, BufWriter, Read, Write};
1515
use std::path::Path;
1616

1717
use checkstyle::output_checkstyle_file;
18-
use config::{Config, NewlineStyle, Verbosity, WriteMode};
18+
use config::{Config, FileName, NewlineStyle, Verbosity, WriteMode};
1919
use rustfmt_diff::{make_diff, output_modified, print_diff, Mismatch};
20-
use syntax::codemap::FileName;
2120

2221
#[cfg(test)]
2322
use FileRecord;

src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ use std::rc::Rc;
4646
use std::time::Duration;
4747

4848
use syntax::ast;
49-
pub use syntax::codemap::FileName;
5049
use syntax::codemap::{CodeMap, FilePathMapping, Span};
5150
use syntax::errors::emitter::{ColorConfig, EmitterWriter};
5251
use syntax::errors::{DiagnosticBuilder, Handler};
@@ -59,9 +58,10 @@ use shape::Indent;
5958
use utils::use_colored_tty;
6059
use visitor::{FmtVisitor, SnippetProvider};
6160

62-
pub use config::options::CliOptions;
6361
pub use config::summary::Summary;
64-
pub use config::{load_config, Color, Config, FileLines, Verbosity, WriteMode};
62+
pub use config::{
63+
load_config, CliOptions, Color, Config, FileLines, FileName, Verbosity, WriteMode,
64+
};
6565

6666
#[macro_use]
6767
mod utils;
@@ -97,8 +97,6 @@ mod types;
9797
mod vertical;
9898
pub(crate) mod visitor;
9999

100-
const STDIN: &str = "<stdin>";
101-
102100
// A map of the files of a crate, with their new content
103101
pub(crate) type FileMap = Vec<FileRecord>;
104102

@@ -397,7 +395,7 @@ fn should_emit_verbose<F>(path: &FileName, config: &Config, f: F)
397395
where
398396
F: Fn(),
399397
{
400-
if config.verbose() == Verbosity::Verbose && path.to_string() != STDIN {
398+
if config.verbose() == Verbosity::Verbose && path != &FileName::Stdin {
401399
f();
402400
}
403401
}
@@ -626,7 +624,7 @@ fn parse_input<'sess>(
626624
Input::File(file) => parse::new_parser_from_file(parse_session, &file),
627625
Input::Text(text) => parse::new_parser_from_source_str(
628626
parse_session,
629-
FileName::Custom("stdin".to_owned()),
627+
syntax::codemap::FileName::Custom("stdin".to_owned()),
630628
text,
631629
),
632630
};
@@ -797,7 +795,7 @@ fn format_input_inner<T: Write>(
797795

798796
let main_file = match input {
799797
Input::File(ref file) => FileName::Real(file.clone()),
800-
Input::Text(..) => FileName::Custom("stdin".to_owned()),
798+
Input::Text(..) => FileName::Stdin,
801799
};
802800

803801
let krate = match parse_input(input, &parse_session, config) {

src/missed_spans.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
use std::borrow::Cow;
1212

13-
use syntax::codemap::{BytePos, FileName, Pos, Span};
13+
use syntax::codemap::{BytePos, Pos, Span};
1414

1515
use codemap::LineRangeUtils;
1616
use comment::{rewrite_comment, CodeCharKind, CommentCodeSlices};
17-
use config::WriteMode;
17+
use config::{FileName, WriteMode};
1818
use shape::{Indent, Shape};
1919
use utils::{count_newlines, last_line_width, mk_sp};
2020
use visitor::FmtVisitor;
@@ -177,7 +177,7 @@ impl<'a> FmtVisitor<'a> {
177177
// Annoyingly, the library functions for splitting by lines etc. are not
178178
// quite right, so we must do it ourselves.
179179
let char_pos = self.codemap.lookup_char_pos(span.lo());
180-
let file_name = &char_pos.file.name;
180+
let file_name = &char_pos.file.name.clone().into();
181181
let mut status = SnippetStatus::new(char_pos.line);
182182

183183
let snippet = &*match self.config.write_mode() {

src/modules.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ use std::io;
1313
use std::path::{Path, PathBuf};
1414

1515
use syntax::ast;
16-
use syntax::codemap::{self, FileName};
16+
use syntax::codemap;
1717
use syntax::parse::{parser, DirectoryOwnership};
1818

19+
use config::FileName;
1920
use utils::contains_skip;
2021

2122
/// List all the files containing modules of a crate.
@@ -28,12 +29,12 @@ pub fn list_files<'a>(
2829
let root_filename = codemap.span_to_filename(krate.span);
2930
{
3031
let parent = match root_filename {
31-
FileName::Real(ref path) => path.parent().unwrap(),
32+
codemap::FileName::Real(ref path) => path.parent().unwrap(),
3233
_ => Path::new(""),
3334
};
3435
list_submodules(&krate.module, parent, None, codemap, &mut result)?;
3536
}
36-
result.insert(root_filename, &krate.module);
37+
result.insert(root_filename.into(), &krate.module);
3738
Ok(result)
3839
}
3940

src/visitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
594594
pub fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -> bool {
595595
for attr in attrs {
596596
if attr.name() == DEPR_SKIP_ANNOTATION {
597-
let file_name = self.codemap.span_to_filename(attr.span);
597+
let file_name = self.codemap.span_to_filename(attr.span).into();
598598
self.report.append(
599599
file_name,
600600
vec![FormattingError::from_span(
@@ -607,7 +607,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
607607
if attr.path.segments.len() == 1
608608
|| attr.path.segments[1].ident.to_string() != "skip"
609609
{
610-
let file_name = self.codemap.span_to_filename(attr.span);
610+
let file_name = self.codemap.span_to_filename(attr.span).into();
611611
self.report.append(
612612
file_name,
613613
vec![FormattingError::from_span(

0 commit comments

Comments
 (0)