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

Function delete_visual_range

src/text_input.rs:2093–2154  ·  view source on GitHub ↗

Delete visible characters in the visual range `[visual_start, visual_end)`. Preserves all style tag structure (`{name|`, `}`) and only removes the content characters that fall within the visual range.

(raw: &str, visual_start: usize, visual_end: usize)

Source from the content-addressed store, hash-verified

2091 /// Preserves all style tag structure (`{name|`, `}`) and only removes the content
2092 /// characters that fall within the visual range.
2093 pub fn delete_visual_range(raw: &str, visual_start: usize, visual_end: usize) -> String {
2094 if visual_start >= visual_end {
2095 return raw.to_string();
2096 }
2097
2098 let chars: Vec<char> = raw.chars().collect();
2099 let len = chars.len();
2100 let mut result = String::with_capacity(raw.len());
2101 let mut visual = 0usize;
2102 let mut i = 0;
2103 let mut in_style_def = false;
2104
2105 while i < len {
2106 let c = chars[i];
2107
2108 match c {
2109 '\\' if !in_style_def && i + 1 < len => {
2110 // Escaped pair `\X` counts as one visible char
2111 let in_range = visual >= visual_start && visual < visual_end;
2112 if !in_range {
2113 result.push(c);
2114 result.push(chars[i + 1]);
2115 }
2116 visual += 1;
2117 i += 2;
2118 }
2119 '{' if !in_style_def => {
2120 in_style_def = true;
2121 result.push(c); // Always keep tag structure
2122 i += 1;
2123 }
2124 '|' if in_style_def => {
2125 in_style_def = false;
2126 result.push(c);
2127 // Check for empty content
2128 if i + 1 < len && chars[i + 1] == '}' {
2129 visual += 1; // Empty content has a visual position but is structural
2130 }
2131 i += 1;
2132 }
2133 '}' if !in_style_def => {
2134 result.push(c); // Always keep `}`
2135 visual += 1; // `}` has a visual position but is structural
2136 i += 1;
2137 }
2138 _ if in_style_def => {
2139 result.push(c); // Tag name chars — always keep
2140 i += 1;
2141 }
2142 _ => {
2143 let in_range = visual >= visual_start && visual < visual_end;
2144 if !in_range {
2145 result.push(c);
2146 }
2147 visual += 1;
2148 i += 1;
2149 }
2150 }

Calls

no outgoing calls