Update js formatting

This commit is contained in:
Robert Masen
2018-06-15 12:55:37 -05:00
parent 19d6cf1488
commit 2d7e7cd73e
8 changed files with 2461 additions and 1390 deletions

View File

@ -158,7 +158,7 @@ impl Bindgen {
let js_path = out_dir.join(stem).with_extension("js");
File::create(&js_path)
.and_then(|mut f| f.write_all(js.as_bytes()))
.and_then(|mut f| f.write_all(reset_indentation(&js).as_bytes()))
.with_context(|_| format!("failed to write `{}`", js_path.display()))?;
if self.typescript {
@ -207,7 +207,7 @@ impl Bindgen {
module.exports = wasmInstance.exports;
", path.file_name().unwrap().to_str().unwrap()));
shim
reset_indentation(&shim)
}
}
@ -380,3 +380,32 @@ impl fmt::Display for MyError {
self.0.fmt(f)
}
}
fn reset_indentation(s: &str) -> String {
indent_recurse(s.lines(), 0)
}
fn indent_recurse(mut lines: ::std::str::Lines, current_indent: usize) -> String {
if let Some(line) = lines.next() {
let mut trimmed = line.trim().to_owned();
let mut next_indent = current_indent;
let mut current_indent = current_indent;
if trimmed.ends_with('{') {
next_indent += 1;
}
if trimmed.starts_with('}') || trimmed.ends_with('}') {
if current_indent > 0 {
current_indent -= 1;
}
if next_indent > 0 {
next_indent -= 1;
}
}
if trimmed.starts_with('?') || trimmed.starts_with(':') {
current_indent += 1;
}
format!("\n{}{}{}", " ".repeat(current_indent), &trimmed, &indent_recurse(lines, next_indent))
} else {
String::new()
}
}