Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate modules and files listing of reformatting
- Loading branch information
Showing
8 changed files
with
87 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use utils; | ||
|
||
use std::path::{Path, PathBuf}; | ||
use std::collections::HashMap; | ||
|
||
use syntax::ast; | ||
use syntax::codemap; | ||
use syntax::parse::{parser, token}; | ||
|
||
|
||
/// List all the files containing modules of a crate. | ||
/// If a file is used twice in a crate, it appears only once. | ||
pub fn list_modules<'a>(krate: &'a ast::Crate, | ||
codemap: &codemap::CodeMap) | ||
-> HashMap<PathBuf, &'a ast::Mod> { | ||
let mut result = HashMap::new(); | ||
let root_filename: PathBuf = codemap.span_to_filename(krate.span).into(); | ||
list_submodules(&krate.module, root_filename.parent().unwrap(), codemap, &mut result); | ||
result.insert(root_filename, &krate.module); | ||
result | ||
} | ||
|
||
/// Recursively list all external modules included in a module. | ||
fn list_submodules<'a>(module: &'a ast::Mod, | ||
search_dir: &Path, | ||
codemap: &codemap::CodeMap, | ||
result: &mut HashMap<PathBuf, &'a ast::Mod>) { | ||
debug!("list_submodules: search_dir: {:?}", search_dir); | ||
for item in module.items.iter() { | ||
if let ast::ItemMod(ref sub_mod) = item.node { | ||
if !utils::contains_skip(&item.attrs) { | ||
let is_internal = codemap.span_to_filename(item.span) == | ||
codemap.span_to_filename(sub_mod.inner); | ||
let dir_path = if is_internal { | ||
let dir: &str = &token::get_ident(item.ident); | ||
search_dir.join(dir) | ||
} else { | ||
let mod_path = module_file(item.ident, &item.attrs, search_dir, codemap); | ||
let dir_path = mod_path.parent().unwrap().to_owned(); | ||
result.insert(mod_path, sub_mod); | ||
dir_path | ||
}; | ||
list_submodules(sub_mod, &dir_path, codemap, result); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Find the file corresponding to an external mod | ||
fn module_file(id: ast::Ident, | ||
attrs: &[ast::Attribute], | ||
dir_path: &Path, | ||
codemap: &codemap::CodeMap) | ||
-> PathBuf { | ||
if let Some(path) = parser::Parser::submod_path_from_attr(attrs, &dir_path) { | ||
return path; | ||
} | ||
|
||
match parser::Parser::default_submod_path(id, &dir_path, codemap).result { | ||
Ok(parser::ModulePathSuccess { path, .. }) => path, | ||
Err(_) => panic!("Couldn't find module {}", token::get_ident(id)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ mod mod2b; | |
|
||
mod mymod1 { | ||
use mod2a::{Foo,Bar}; | ||
mod mod3a; | ||
} | ||
|
||
#[path="mod2c.rs"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Another mod | ||
fn a( ) { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ mod mod2b; | |
|
||
mod mymod1 { | ||
use mod2a::{Foo, Bar}; | ||
mod mod3a; | ||
} | ||
|
||
#[path="mod2c.rs"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Another mod | ||
fn a() { | ||
} |