Skip to content

Commit a80a6c7

Browse files
committed
Improve output code formatting
1 parent fe1991d commit a80a6c7

File tree

8 files changed

+51
-17
lines changed

8 files changed

+51
-17
lines changed

binding-generator/src/string_ext.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub trait StringExt {
2525
) -> bool;
2626
fn extend_join(&mut self, it: impl Iterator<Item = impl AsRef<str>>, sep: &str);
2727
fn extend_sep(&mut self, sep: &str, s: &str);
28-
fn push_indented_str(&mut self, indent: Indent, val: &str);
28+
fn push_indented_lines(&mut self, indent: Indent, val: &str);
2929
fn bump_counter(&mut self);
3030
fn cleanup_name(&mut self);
3131
}
@@ -211,13 +211,16 @@ impl StringExt for String {
211211
self.push_str(s);
212212
}
213213

214-
fn push_indented_str(&mut self, indent: Indent, val: &str) {
214+
fn push_indented_lines(&mut self, indent: Indent, val: &str) {
215215
let mut lines = val.lines_with_nl();
216216
if let Some(line) = lines.next() {
217217
self.push_str(line);
218218
}
219219
for line in lines {
220-
self.extend(iter::repeat(indent.symbol).take(indent.len));
220+
// there is more than just a newline in the buffer
221+
if line.len() > 1 {
222+
self.extend(iter::repeat(indent.symbol).take(indent.len));
223+
}
221224
self.push_str(line);
222225
}
223226
}
@@ -346,7 +349,7 @@ impl CompiledInterpolation<'_> {
346349
}
347350
Compiled::IntpLiteral(s) => out += s,
348351
Compiled::Var(name) => {
349-
out.push_indented_str(line_indent, params.get(name).map_or(INVALID_PARAM_NAME, |x| x.as_ref()))
352+
out.push_indented_lines(line_indent, params.get(name).map_or(INVALID_PARAM_NAME, |x| x.as_ref()))
350353
}
351354
Compiled::IntpLineEnd(s) => {
352355
out += s;

binding-generator/src/test.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,26 @@ test3 end
372372
.interpolate(&HashMap::from([("name1", ""), ("name2", "test2"), ("name3", "test3")]));
373373
assert_eq!(" test2 test3", res);
374374
}
375+
376+
// empty lines don't get indent
377+
{
378+
let tpl = "
379+
block {
380+
{{var}}
381+
}";
382+
let var = "\
383+
line1
384+
385+
line2";
386+
let expected = "\
387+
block {
388+
line1
389+
390+
line2
391+
}";
392+
let res = tpl.compile_interpolation().interpolate(&HashMap::from([("var", var)]));
393+
assert_eq!(expected, res);
394+
}
375395
}
376396

377397
#[test]

binding-generator/src/writer/rust_native/comment.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ fn add_comment_markers<'c>(comment: &'c str, marker: &str) -> Cow<'c, str> {
234234
String::with_capacity(comment.len() + (marker.len() + 1) * 128),
235235
|mut out_prefixed, line| {
236236
out_prefixed.push_str(marker);
237-
out_prefixed.push(' ');
237+
// there is more than just a newline in the buffer
238+
if line.len() > 1 {
239+
out_prefixed.push(' ');
240+
}
238241
out_prefixed + line
239242
},
240243
)

binding-generator/src/writer/rust_native/mod.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,12 @@ impl GeneratorVisitor<'_> for RustNativeBindingWriter<'_> {
165165
fn visit_class(&mut self, class: Class) {
166166
self.emit_debug_log(&class);
167167
if class.kind().is_trait() {
168-
self.prelude_traits.push(format!(
169-
"super::{}",
170-
class.rust_trait_name(NameStyle::decl(), Constness::Const)
171-
));
172168
self
173169
.prelude_traits
174-
.push(format!("super::{}", class.rust_trait_name(NameStyle::decl(), Constness::Mut)));
170+
.push(class.rust_trait_name(NameStyle::decl(), Constness::Const).into_owned());
171+
self
172+
.prelude_traits
173+
.push(class.rust_trait_name(NameStyle::decl(), Constness::Mut).into_owned());
175174
}
176175
let name = class.cpp_name(CppNameStyle::Reference).into_owned();
177176
self.rust_classes.push((name.clone(), class.gen_rust(self.opencv_version)));
@@ -216,7 +215,7 @@ impl GeneratorVisitor<'_> for RustNativeBindingWriter<'_> {
216215
write_generated_type(&self.types_dir, "cpp", prio, &safe_id, || typ.gen_cpp());
217216
}
218217

219-
fn goodbye(self) {
218+
fn goodbye(mut self) {
220219
static RUST: Lazy<CompiledInterpolation> = Lazy::new(|| include_str!("tpl/module/rust.tpl.rs").compile_interpolation());
221220

222221
static RUST_PRELUDE: Lazy<CompiledInterpolation> =
@@ -230,7 +229,13 @@ impl GeneratorVisitor<'_> for RustNativeBindingWriter<'_> {
230229
insert_lines(&mut rust, self.rust_typedefs.into_iter().collect());
231230
insert_lines(&mut rust, self.rust_funcs);
232231
insert_lines(&mut rust, self.rust_classes);
233-
let prelude = RUST_PRELUDE.interpolate(&HashMap::from([("traits", self.prelude_traits.join(", "))]));
232+
let pub_use_traits = if self.prelude_traits.is_empty() {
233+
"".to_string()
234+
} else {
235+
self.prelude_traits.sort_unstable();
236+
format!("pub use super::{{{}}};", self.prelude_traits.join(", "))
237+
};
238+
let prelude = RUST_PRELUDE.interpolate(&HashMap::from([("pub_use_traits", pub_use_traits)]));
234239
let comment = RenderComment::new(&self.comment, self.opencv_version);
235240
let comment = comment.render_with_comment_marker("//!");
236241
File::create(&self.rust_path)

binding-generator/src/writer/rust_native/tpl/class/boxed.tpl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{doc_comment}}
22
{{debug}}
33
pub struct {{rust_local}} {
4-
ptr: {{rust_extern_mut}}
4+
ptr: {{rust_extern_mut}},
55
}
66

77
opencv_type_boxed! { {{rust_local}} }
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
pub mod prelude {
2-
pub use { {{traits}} };
2+
{{pub_use_traits}}
33
}
4-
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{{comment}}
2-
use crate::{mod_prelude::*, {{static_modules}}};
2+
use crate::mod_prelude::*;
3+
use crate::{{{static_modules}}};
34
{{prelude}}
45

56
{{code}}

build/generator/collector.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,10 @@ fn is_type_externs_file(path: &Path, module: &str) -> bool {
240240
fn copy_indent(mut read: impl BufRead, write: &mut impl Write, indent: &str) -> Result<()> {
241241
let mut line = Vec::with_capacity(100);
242242
while read.read_until(b'\n', &mut line)? != 0 {
243-
write.write_all(indent.as_bytes())?;
243+
// there is more than just a newline in the buffer
244+
if line.len() > 1 {
245+
write.write_all(indent.as_bytes())?;
246+
}
244247
write.write_all(&line)?;
245248
line.clear();
246249
}

0 commit comments

Comments
 (0)