MCPcopy Create free account
hub / github.com/bytecodealliance/wit-bindgen / push_str

Method push_str

crates/core/src/source.rs:51–95  ·  view source on GitHub ↗
(&mut self, src: &str)

Source from the content-addressed store, hash-verified

49 }
50
51 pub fn push_str(&mut self, src: &str) {
52 let lines = src.lines().collect::<Vec<_>>();
53 for (i, line) in lines.iter().enumerate() {
54 if !self.continuing_line {
55 if !line.is_empty() {
56 for _ in 0..self.indent {
57 self.s.push_str(" ");
58 }
59 }
60 self.continuing_line = true;
61 }
62
63 let trimmed = line.trim();
64 if trimmed.starts_with("//") {
65 self.in_line_comment = true;
66 }
67
68 if !self.in_line_comment {
69 if trimmed.starts_with('}') && self.s.ends_with(" ") {
70 self.s.pop();
71 self.s.pop();
72 }
73 }
74 self.s.push_str(if lines.len() == 1 {
75 line
76 } else {
77 line.trim_start()
78 });
79 if !self.in_line_comment {
80 if trimmed.ends_with('{') {
81 self.indent += 1;
82 }
83 if trimmed.starts_with('}') {
84 // Note that a `saturating_sub` is used here to prevent a panic
85 // here in the case of invalid code being generated in debug
86 // mode. It's typically easier to debug those issues through
87 // looking at the source code rather than getting a panic.
88 self.indent = self.indent.saturating_sub(1);
89 }
90 }
91 if i != lines.len() - 1 || src.ends_with('\n') {
92 self.newline();
93 }
94 }
95 }
96
97 pub fn indent(&mut self, amt: usize) {
98 self.indent += amt;

Callers 15

bump_versionFunction · 0.45
newMethod · 0.45
getMethod · 0.45
append_srcMethod · 0.45
write_strMethod · 0.45
simple_appendFunction · 0.45
newline_remapFunction · 0.45
if_elseFunction · 0.45
trim_wsFunction · 0.45
extendMethod · 0.45
export_interfaceMethod · 0.45
export_funcsMethod · 0.45

Calls 3

newlineMethod · 0.80
iterMethod · 0.45
is_emptyMethod · 0.45

Tested by 11

newMethod · 0.36
getMethod · 0.36
simple_appendFunction · 0.36
newline_remapFunction · 0.36
if_elseFunction · 0.36
trim_wsFunction · 0.36
run_runtime_testsMethod · 0.36
runtime_testMethod · 0.36
compose_wasm_with_wacMethod · 0.36
run_commandMethod · 0.36
render_errorMethod · 0.36