|
11 | 11 | use syntax::ast;
|
12 | 12 | use syntax::codemap::{self, CodeMap, Span, BytePos};
|
13 | 13 | use syntax::visit;
|
14 |
| -use syntax::parse::token; |
15 |
| -use syntax::attr; |
| 14 | +use syntax::parse::{token, parser}; |
16 | 15 | use std::path::PathBuf;
|
17 | 16 |
|
18 | 17 | use utils;
|
19 | 18 | use config::Config;
|
20 |
| -use comment::FindUncommented; |
21 | 19 |
|
22 | 20 | use changes::ChangeSet;
|
23 | 21 | use rewrite::{Rewrite, RewriteContext};
|
@@ -363,68 +361,48 @@ impl<'a> FmtVisitor<'a> {
|
363 | 361 |
|
364 | 362 | fn format_mod(&mut self, m: &ast::Mod, s: Span, ident: ast::Ident, attrs: &[ast::Attribute]) {
|
365 | 363 | debug!("FmtVisitor::format_mod: ident: {:?}, span: {:?}", ident, s);
|
| 364 | + |
366 | 365 | // Decide whether this is an inline mod or an external mod.
|
367 |
| - // There isn't any difference between inline and external mod in AST, |
368 |
| - // so we use the trick of searching for an opening brace. |
369 |
| - // We can't use the inner span of the mod since it is weird when it |
370 |
| - // is empty (no items). |
371 |
| - // FIXME Use the inner span once rust-lang/rust#26755 is fixed. |
372 |
| - let open_brace = self.codemap.span_to_snippet(s).unwrap().find_uncommented("{"); |
373 |
| - match open_brace { |
374 |
| - None => { |
375 |
| - debug!("FmtVisitor::format_mod: external mod"); |
376 |
| - let file_path = self.module_file(ident, attrs, s); |
377 |
| - let filename = file_path.to_str().unwrap(); |
378 |
| - if self.changes.is_changed(filename) { |
379 |
| - // The file has already been reformatted, do nothing |
380 |
| - } else { |
381 |
| - self.format_separate_mod(m, filename); |
382 |
| - } |
383 |
| - // TODO Should rewrite properly `mod X;` |
384 |
| - } |
385 |
| - Some(open_brace) => { |
386 |
| - debug!("FmtVisitor::format_mod: internal mod"); |
387 |
| - debug!("... open_brace: {}, str: {:?}", |
388 |
| - open_brace, |
389 |
| - self.codemap.span_to_snippet(s)); |
390 |
| - // Format everything until opening brace |
391 |
| - // TODO Shoud rewrite properly |
392 |
| - self.format_missing(s.lo + BytePos(open_brace as u32)); |
393 |
| - self.block_indent += self.config.tab_spaces; |
394 |
| - visit::walk_mod(self, m); |
395 |
| - debug!("... last_pos after: {:?}", self.last_pos); |
396 |
| - self.block_indent -= self.config.tab_spaces; |
| 366 | + let local_file_name = self.codemap.span_to_filename(s); |
| 367 | + let is_internal = local_file_name == self.codemap.span_to_filename(m.inner); |
| 368 | + |
| 369 | + // TODO Should rewrite properly `mod X;` |
| 370 | + |
| 371 | + if is_internal { |
| 372 | + debug!("FmtVisitor::format_mod: internal mod"); |
| 373 | + self.block_indent += self.config.tab_spaces; |
| 374 | + visit::walk_mod(self, m); |
| 375 | + debug!("... last_pos after: {:?}", self.last_pos); |
| 376 | + self.block_indent -= self.config.tab_spaces; |
| 377 | + } else { |
| 378 | + debug!("FmtVisitor::format_mod: external mod"); |
| 379 | + let file_path = self.module_file(ident, attrs, local_file_name); |
| 380 | + let filename = file_path.to_str().unwrap(); |
| 381 | + if self.changes.is_changed(filename) { |
| 382 | + // The file has already been reformatted, do nothing |
| 383 | + } else { |
| 384 | + self.format_separate_mod(m, filename); |
397 | 385 | }
|
398 | 386 | }
|
399 |
| - self.format_missing(s.hi); |
| 387 | + |
400 | 388 | debug!("FmtVisitor::format_mod: exit");
|
401 | 389 | }
|
402 | 390 |
|
403 | 391 | /// Find the file corresponding to an external mod
|
404 |
| - /// Same algorithm as syntax::parse::eval_src_mod |
405 |
| - fn module_file(&self, id: ast::Ident, outer_attrs: &[ast::Attribute], id_sp: Span) -> PathBuf { |
406 |
| - // FIXME use libsyntax once rust-lang/rust#26750 is merged |
407 |
| - let mut prefix = PathBuf::from(&self.codemap.span_to_filename(id_sp)); |
408 |
| - prefix.pop(); |
409 |
| - let mod_string = token::get_ident(id); |
410 |
| - match attr::first_attr_value_str_by_name(outer_attrs, "path") { |
411 |
| - Some(d) => prefix.join(&*d), |
412 |
| - None => { |
413 |
| - let default_path_str = format!("{}.rs", mod_string); |
414 |
| - let secondary_path_str = format!("{}/mod.rs", mod_string); |
415 |
| - let default_path = prefix.join(&default_path_str); |
416 |
| - let secondary_path = prefix.join(&secondary_path_str); |
417 |
| - let default_exists = self.codemap.file_exists(&default_path); |
418 |
| - let secondary_exists = self.codemap.file_exists(&secondary_path); |
419 |
| - if default_exists { |
420 |
| - default_path |
421 |
| - } else if secondary_exists { |
422 |
| - secondary_path |
423 |
| - } else { |
424 |
| - // Should never appens since rustc parsed everything sucessfully |
425 |
| - panic!("Didn't found module {}", mod_string); |
426 |
| - } |
427 |
| - } |
| 392 | + fn module_file(&self, id: ast::Ident, attrs: &[ast::Attribute], filename: String) -> PathBuf { |
| 393 | + let dir_path = { |
| 394 | + let mut path = PathBuf::from(&filename); |
| 395 | + path.pop(); |
| 396 | + path |
| 397 | + }; |
| 398 | + |
| 399 | + if let Some(path) = parser::Parser::submod_path_from_attr(attrs, &dir_path) { |
| 400 | + return path; |
| 401 | + } |
| 402 | + |
| 403 | + match parser::Parser::default_submod_path(id, &dir_path, &self.codemap).result { |
| 404 | + Ok(parser::ModulePathSuccess { path, .. }) => path, |
| 405 | + _ => panic!("Couldn't find module {}", token::get_ident(id)) |
428 | 406 | }
|
429 | 407 | }
|
430 | 408 |
|
|
0 commit comments