Skip to content

Commit

Permalink
Avoid shebang causing line number mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Apr 10, 2023
1 parent 6776198 commit 0132bcd
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ pub fn split_input(
let (part_mani, source, template, sub_prelude) = match input {
Input::File(_, _, content) => {
assert_eq!(prelude_items.len(), 0);
let content = strip_shebang(content);
let (content, shebang_used) = strip_shebang(content);
let (manifest, source) =
find_embedded_manifest(content).unwrap_or((Manifest::Toml(""), content));

let source = if contains_main_method(source) {
source.to_string()
if shebang_used {
format!("//\n{}", source)
} else {
source.to_string()
}
} else {
format!("fn main() -> Result<(), Box<dyn std::error::Error+Sync+Send>> {{\n {{\n {} }}\n Ok(())\n}}", source)
format!("fn main() -> Result<(), Box<dyn std::error::Error+Sync+Send>> {{ {{\n{} }}\n Ok(())\n}}", source)
};
(manifest, source, consts::FILE_TEMPLATE, false)
}
Expand Down Expand Up @@ -169,7 +173,8 @@ name = "n"
version = "0.1.0""#,
STRIP_SECTION
),
r#"fn main() {}"#
r#"//
fn main() {}"#
)
);

Expand Down Expand Up @@ -395,11 +400,11 @@ fn main() {}
/**
Returns a slice of the input string with the leading shebang, if there is one, omitted.
*/
fn strip_shebang(s: &str) -> &str {
fn strip_shebang(s: &str) -> (&str, bool) {
let re_shebang: Regex = Regex::new(r"^#![^\[].*?(\r\n|\n)").unwrap();
match re_shebang.find(s) {
Some(m) => &s[m.end()..],
None => s,
Some(m) => (&s[m.end()..], true),
None => (s, false),
}
}

Expand Down

0 comments on commit 0132bcd

Please sign in to comment.