Skip to content

Commit 0132bcd

Browse files
committed
Avoid shebang causing line number mismatch
See #11 (comment)
1 parent 6776198 commit 0132bcd

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/manifest.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,18 @@ pub fn split_input(
3434
let (part_mani, source, template, sub_prelude) = match input {
3535
Input::File(_, _, content) => {
3636
assert_eq!(prelude_items.len(), 0);
37-
let content = strip_shebang(content);
37+
let (content, shebang_used) = strip_shebang(content);
3838
let (manifest, source) =
3939
find_embedded_manifest(content).unwrap_or((Manifest::Toml(""), content));
4040

4141
let source = if contains_main_method(source) {
42-
source.to_string()
42+
if shebang_used {
43+
format!("//\n{}", source)
44+
} else {
45+
source.to_string()
46+
}
4347
} else {
44-
format!("fn main() -> Result<(), Box<dyn std::error::Error+Sync+Send>> {{\n {{\n {} }}\n Ok(())\n}}", source)
48+
format!("fn main() -> Result<(), Box<dyn std::error::Error+Sync+Send>> {{ {{\n{} }}\n Ok(())\n}}", source)
4549
};
4650
(manifest, source, consts::FILE_TEMPLATE, false)
4751
}
@@ -169,7 +173,8 @@ name = "n"
169173
version = "0.1.0""#,
170174
STRIP_SECTION
171175
),
172-
r#"fn main() {}"#
176+
r#"//
177+
fn main() {}"#
173178
)
174179
);
175180

@@ -395,11 +400,11 @@ fn main() {}
395400
/**
396401
Returns a slice of the input string with the leading shebang, if there is one, omitted.
397402
*/
398-
fn strip_shebang(s: &str) -> &str {
403+
fn strip_shebang(s: &str) -> (&str, bool) {
399404
let re_shebang: Regex = Regex::new(r"^#![^\[].*?(\r\n|\n)").unwrap();
400405
match re_shebang.find(s) {
401-
Some(m) => &s[m.end()..],
402-
None => s,
406+
Some(m) => (&s[m.end()..], true),
407+
None => (s, false),
403408
}
404409
}
405410

0 commit comments

Comments
 (0)