diff --git a/src/bundler.rs b/src/bundler.rs index 049f849..e112a23 100644 --- a/src/bundler.rs +++ b/src/bundler.rs @@ -19,7 +19,7 @@ const LIBRS_FILENAME: &str = "src/lib.rs"; lazy_static! { static ref COMMENT_RE: Regex = source_line_regex(r" ").unwrap(); static ref WARN_RE: Regex = source_line_regex(r" #!\[warn\(.*").unwrap(); - static ref USECRATE_RE: Regex = source_line_regex(r" use crate::(?P.*);$").unwrap(); + static ref USECRATE_RE: Regex = source_line_regex(r" use crate::(?P.*)$").unwrap(); static ref MINIFY_RE: Regex = Regex::new(r"^\s*(?P.*)\s*$").unwrap(); } @@ -219,19 +219,32 @@ impl<'a> Bundler<'a> { writeln!(self.bundle_file, "pub mod {} {{", mod_name)?; self.skip_use.insert(String::from(mod_import)); + let mut inner_submod = String::new(); while mod_reader.read_line(&mut line)? > 0 { line.truncate(line.trim_end().len()); if COMMENT_RE.is_match(&line) || WARN_RE.is_match(&line) { - } else if let Some(cap) = USECRATE_RE.captures(&line) { - let submodname = cap - .name("submod") - .ok_or_else(|| anyhow!("capture not found"))? - .as_str(); - write!(self.bundle_file, "use ")?; - for _ in 0..lvl { - write!(self.bundle_file, "super::")?; + } else if USECRATE_RE.is_match(&line) || !inner_submod.is_empty() { + if let Some(cap) = USECRATE_RE.captures(&line) { + write!(self.bundle_file, "use ")?; + for _ in 0..lvl { + write!(self.bundle_file, "super::")?; + } + let submodname = cap + .name("submod") + .ok_or_else(|| anyhow!("capture not found"))? + .as_str(); + if submodname.ends_with(';') { + writeln!(self.bundle_file, "{}", submodname)?; + } else { + inner_submod = submodname.to_string(); + } + } else { + inner_submod.push_str(&line); + if inner_submod.ends_with(';') { + writeln!(self.bundle_file, "{}", inner_submod)?; + inner_submod.clear(); + } } - writeln!(self.bundle_file, "{};", submodname)?; } else if let Some(cap) = mod_re.captures(&line) { let submodname = cap .name("m") diff --git a/tests/testdata/input/usecrate/src/lib.rs b/tests/testdata/input/usecrate/src/lib.rs index 8497174..e16e600 100644 --- a/tests/testdata/input/usecrate/src/lib.rs +++ b/tests/testdata/input/usecrate/src/lib.rs @@ -1,2 +1,4 @@ pub mod submod1; pub mod submod2; +pub mod submod3; +pub mod submod4; diff --git a/tests/testdata/input/usecrate/src/main.rs b/tests/testdata/input/usecrate/src/main.rs index 9c7ae52..b9f7ad2 100644 --- a/tests/testdata/input/usecrate/src/main.rs +++ b/tests/testdata/input/usecrate/src/main.rs @@ -1,6 +1,8 @@ extern crate usecrate; use usecrate::submod2::hello_world2; +use usecrate::submod4::hello_world4; fn main() { hello_world2(); + hello_world4(); } diff --git a/tests/testdata/input/usecrate/src/submod3.rs b/tests/testdata/input/usecrate/src/submod3.rs new file mode 100644 index 0000000..5457d96 --- /dev/null +++ b/tests/testdata/input/usecrate/src/submod3.rs @@ -0,0 +1,3 @@ +pub fn hello_world3__long_identifier_name_for_multiline() { + println!("Hello, world 3!"); +} diff --git a/tests/testdata/input/usecrate/src/submod4.rs b/tests/testdata/input/usecrate/src/submod4.rs new file mode 100644 index 0000000..8cabf4c --- /dev/null +++ b/tests/testdata/input/usecrate/src/submod4.rs @@ -0,0 +1,10 @@ +use crate::{ + submod1, + submod3::hello_world3__long_identifier_name_for_multiline, +}; + +pub fn hello_world4() { + submod1::hello_world1(); + hello_world3__long_identifier_name_for_multiline(); + println!("Hello, world 4!"); +} diff --git a/tests/testdata/output/usecrate.rs b/tests/testdata/output/usecrate.rs index 6cf0836..771426b 100644 --- a/tests/testdata/output/usecrate.rs +++ b/tests/testdata/output/usecrate.rs @@ -11,8 +11,23 @@ pub fn hello_world2() { println!("Hello, world 2!"); } } +pub mod submod3 { +pub fn hello_world3__long_identifier_name_for_multiline() { + println!("Hello, world 3!"); +} +} +pub mod submod4 { +use super::{ submod1, submod3::hello_world3__long_identifier_name_for_multiline,}; +pub fn hello_world4() { + submod1::hello_world1(); + hello_world3__long_identifier_name_for_multiline(); + println!("Hello, world 4!"); +} +} } use self::usecrate::submod2::hello_world2; +use self::usecrate::submod4::hello_world4; fn main() { hello_world2(); + hello_world4(); }