MCPcopy Create free account
hub / github.com/TheRedDeveloper/ply-engine / cleanup_empty_styles

Function cleanup_empty_styles

src/text_input.rs:2161–2313  ·  view source on GitHub ↗

Remove empty style tags (`{style|}`) from the raw string, EXCEPT those that contain the cursor. A cursor is "inside" an empty style tag if its visual position equals the visual position of that tag's content area. Returns the new raw string and the (possibly adjusted) visual cursor position.

(raw: &str, cursor_visual_pos: usize)

Source from the content-addressed store, hash-verified

2159 ///
2160 /// Returns the new raw string and the (possibly adjusted) visual cursor position.
2161 pub fn cleanup_empty_styles(raw: &str, cursor_visual_pos: usize) -> (String, usize) {
2162 let chars: Vec<char> = raw.chars().collect();
2163 let len = chars.len();
2164 let mut result = String::with_capacity(raw.len());
2165 let mut i = 0;
2166 let mut visual = 0usize;
2167 let mut escaped = false;
2168 let mut cursor_adj = cursor_visual_pos;
2169
2170 // We need to track style nesting to correctly identify empty tags
2171 while i < len {
2172 let c = chars[i];
2173
2174 if escaped {
2175 result.push(c);
2176 visual += 1;
2177 escaped = false;
2178 i += 1;
2179 continue;
2180 }
2181
2182 match c {
2183 '\\' => {
2184 escaped = true;
2185 result.push(c);
2186 i += 1;
2187 }
2188 '{' => {
2189 // Look ahead: find the matching `|`, then check if there's content
2190 // before the closing `}`. Pattern: `{...| <content> }`
2191 // Find the `|` that ends this style definition
2192 let mut j = i + 1;
2193 let mut style_escaped = false;
2194 let mut found_pipe = false;
2195 while j < len {
2196 if style_escaped {
2197 style_escaped = false;
2198 j += 1;
2199 continue;
2200 }
2201 if chars[j] == '\\' {
2202 style_escaped = true;
2203 j += 1;
2204 continue;
2205 }
2206 if chars[j] == '|' {
2207 found_pipe = true;
2208 j += 1; // j now points to first char after `|`
2209 break;
2210 }
2211 if chars[j] == '{' {
2212 // Nested `{` inside style def — not valid but push through
2213 j += 1;
2214 continue;
2215 }
2216 j += 1;
2217 }
2218

Calls

no outgoing calls