| 1 | //! Editor snippets, directive autocomplete, and insert helpers.
|
| 2 |
|
| 3 | pub const DIRECTIVES: &[&str] = &["feed", "cut", "image", "barcode", "qr", "date", "time"];
|
| 4 |
|
| 5 | pub fn directive_completions(prefix: &str) -> Vec<&'static str> {
|
| 6 | let p = prefix.to_ascii_lowercase();
|
| 7 | DIRECTIVES
|
| 8 | .iter()
|
| 9 | .copied()
|
| 10 | .filter(|d| d.starts_with(&p))
|
| 11 | .collect()
|
| 12 | }
|
| 13 |
|
| 14 | /// Transparent continuation shown after the typed `@` prefix (e.g. `ba` → `rcode`).
|
| 15 | pub fn directive_ghost_suffix(prefix: &str) -> Option<String> {
|
| 16 | let matches = directive_completions(prefix);
|
| 17 | let full = match matches.len() {
|
| 18 | 0 => return None,
|
| 19 | 1 => matches[0],
|
| 20 | _ => longest_common_prefix(&matches)?,
|
| 21 | };
|
| 22 | if full.len() > prefix.len() {
|
| 23 | Some(full[prefix.len()..].to_string())
|
| 24 | } else {
|
| 25 | None
|
| 26 | }
|
| 27 | }
|
| 28 |
|
| 29 | /// Full directive name to insert when the user presses Tab.
|
| 30 | pub fn complete_directive(prefix: &str) -> Option<&'static str> {
|
| 31 | let matches = directive_completions(prefix);
|
| 32 | match matches.len() {
|
| 33 | 0 => None,
|
| 34 | 1 => Some(matches[0]),
|
| 35 | _ => {
|
| 36 | let common = longest_common_prefix(&matches)?;
|
| 37 | if common.len() > prefix.len() {
|
| 38 | Some(common)
|
| 39 | } else {
|
| 40 | Some(matches[0])
|
| 41 | }
|
| 42 | }
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | fn longest_common_prefix(matches: &[&'static str]) -> Option<&'static str> {
|
| 47 | if matches.is_empty() {
|
| 48 | return None;
|
| 49 | }
|
| 50 | let mut common = matches[0];
|
| 51 | for candidate in &matches[1..] {
|
| 52 | let mut len = 0;
|
| 53 | while len < common.len()
|
| 54 | && len < candidate.len()
|
| 55 | && common.as_bytes()[len] == candidate.as_bytes()[len]
|
| 56 | {
|
| 57 | len += 1;
|
| 58 | }
|
| 59 | common = &common[..len];
|
| 60 | if common.is_empty() {
|
| 61 | return None;
|
| 62 | }
|
| 63 | }
|
| 64 | Some(common)
|
| 65 | }
|
| 66 |
|
| 67 | /// Replace the partial directive after `@` with `completed`; returns new cursor index.
|
| 68 | pub fn apply_directive_completion(
|
| 69 | body: &mut String,
|
| 70 | at_index: usize,
|
| 71 | cursor: usize,
|
| 72 | completed: &str,
|
| 73 | ) -> usize {
|
| 74 | let replace_start = at_index + 1;
|
| 75 | let cursor = cursor.min(body.len());
|
| 76 | let replace_start = replace_start.min(cursor);
|
| 77 | body.replace_range(replace_start..cursor, completed);
|
| 78 | at_index + 1 + completed.len()
|
| 79 | }
|
| 80 |
|
| 81 | pub struct InsertOption {
|
| 82 | pub label: String,
|
| 83 | pub snippet: String,
|
| 84 | }
|
| 85 |
|
| 86 | pub fn image_insert_options() -> Vec<InsertOption> {
|
| 87 | vec![InsertOption {
|
| 88 | label: "Image from file".into(),
|
| 89 | snippet: "@image logo.png\n".into(),
|
| 90 | }]
|
| 91 | }
|
| 92 |
|
| 93 | pub fn barcode_insert_options() -> Vec<InsertOption> {
|
| 94 | let mut out = Vec::new();
|
| 95 | for &sym in crate::dsl::BarcodeSymbology::ALL {
|
| 96 | if sym.supports_native() {
|
| 97 | out.push(InsertOption {
|
| 98 | label: format!("Native {}", sym.as_str()),
|
| 99 | snippet: format!("@barcode {} ${{Data}}\n", sym.as_str()),
|
| 100 | });
|
| 101 | }
|
| 102 | }
|
| 103 | for &sym in crate::dsl::BarcodeSymbology::ALL {
|
| 104 | out.push(InsertOption {
|
| 105 | label: format!("Raster {}", sym.as_str()),
|
| 106 | snippet: format!("@barcode RASTER {} ${{Data}}\n", sym.as_str()),
|
| 107 | });
|
| 108 | }
|
| 109 | out
|
| 110 | }
|
| 111 |
|
| 112 | pub fn qr_insert_options() -> Vec<InsertOption> {
|
| 113 | vec![
|
| 114 | InsertOption {
|
| 115 | label: "Native QR (ESC/POS)".into(),
|
| 116 | snippet: "@qr ${Data}\n".into(),
|
| 117 | },
|
| 118 | InsertOption {
|
| 119 | label: "Raster QR (auto version)".into(),
|
| 120 | snippet: "@qr RASTER ${Data} scale=4 ecc=M\n".into(),
|
| 121 | },
|
| 122 | InsertOption {
|
| 123 | label: "Raster QR (version 5)".into(),
|
| 124 | snippet: "@qr RASTER ${Data} version=5 scale=4 ecc=M\n".into(),
|
| 125 | },
|
| 126 | ]
|
| 127 | }
|
| 128 |
|
| 129 | pub fn date_time_insert_options() -> Vec<InsertOption> {
|
| 130 | vec![
|
| 131 | InsertOption {
|
| 132 | label: "@date (regional)".into(),
|
| 133 | snippet: "@date\n".into(),
|
| 134 | },
|
| 135 | InsertOption {
|
| 136 | label: "@date DD-YYYY-MM".into(),
|
| 137 | snippet: "@date DD-YYYY-MM\n".into(),
|
| 138 | },
|
| 139 | InsertOption {
|
| 140 | label: "@date MMMM DD".into(),
|
| 141 | snippet: "@date MMMM DD\n".into(),
|
| 142 | },
|
| 143 | InsertOption {
|
| 144 | label: "@time (24h)".into(),
|
| 145 | snippet: "@time\n".into(),
|
| 146 | },
|
| 147 | InsertOption {
|
| 148 | label: "@time 12h".into(),
|
| 149 | snippet: "@time 12h\n".into(),
|
| 150 | },
|
| 151 | InsertOption {
|
| 152 | label: "@time 24h".into(),
|
| 153 | snippet: "@time 24h\n".into(),
|
| 154 | },
|
| 155 | ]
|
| 156 | }
|
| 157 |
|
| 158 | pub fn parameter_insert_options(source: &str) -> Vec<InsertOption> {
|
| 159 | use std::collections::BTreeSet;
|
| 160 |
|
| 161 | let mut names = BTreeSet::new();
|
| 162 | if let Ok(doc) = crate::dsl::parse(source) {
|
| 163 | for name in crate::params::collect_parameter_names(&doc) {
|
| 164 | names.insert(name);
|
| 165 | }
|
| 166 | }
|
| 167 | for preset in [
|
| 168 | "DocumentNumber",
|
| 169 | "Data",
|
| 170 | "Name",
|
| 171 | "StoreName",
|
| 172 | "Total",
|
| 173 | "OrderId",
|
| 174 | "Date",
|
| 175 | "Time",
|
| 176 | ] {
|
| 177 | names.insert(preset.to_string());
|
| 178 | }
|
| 179 |
|
| 180 | names
|
| 181 | .into_iter()
|
| 182 | .map(|name| InsertOption {
|
| 183 | label: format!("${{{name}}}"),
|
| 184 | snippet: format!("${{{name}}}"),
|
| 185 | })
|
| 186 | .collect()
|
| 187 | }
|
| 188 |
|
| 189 | /// If the cursor is after `@` on the current line, return (start_index, typed_prefix).
|
| 190 | pub fn at_directive_prefix(body: &str, cursor: usize) -> Option<(usize, String)> {
|
| 191 | let cursor = cursor.min(body.len());
|
| 192 | let before = &body[..cursor];
|
| 193 | let line_start = before.rfind('\n').map(|i| i + 1).unwrap_or(0);
|
| 194 | let line = &before[line_start..];
|
| 195 | let at = line.find('@')?;
|
| 196 | if at > 0 && !line[..at].trim().is_empty() {
|
| 197 | return None;
|
| 198 | }
|
| 199 | let fragment = &line[at + 1..];
|
| 200 | if fragment.contains(' ') {
|
| 201 | return None;
|
| 202 | }
|
| 203 | Some((line_start + at, fragment.to_string()))
|
| 204 | }
|
| 205 |
|
| 206 | pub fn insert_at_cursor(body: &str, cursor: usize, text: &str) -> (String, usize) {
|
| 207 | let cursor = cursor.min(body.len());
|
| 208 | let mut out = String::with_capacity(body.len() + text.len());
|
| 209 | out.push_str(&body[..cursor]);
|
| 210 | out.push_str(text);
|
| 211 | out.push_str(&body[cursor..]);
|
| 212 | let new_cursor = cursor + text.len();
|
| 213 | (out, new_cursor)
|
| 214 | }
|
| 215 |
|
| 216 | #[cfg(test)]
|
| 217 | mod tests {
|
| 218 | use super::*;
|
| 219 |
|
| 220 | #[test]
|
| 221 | fn ghost_suffix_for_partial_directive() {
|
| 222 | assert_eq!(directive_ghost_suffix("ba"), Some("rcode".into()));
|
| 223 | }
|
| 224 |
|
| 225 | #[test]
|
| 226 | fn ghost_suffix_none_when_ambiguous() {
|
| 227 | assert!(directive_ghost_suffix("").is_none());
|
| 228 | }
|
| 229 |
|
| 230 | #[test]
|
| 231 | fn completes_directive_on_tab() {
|
| 232 | assert_eq!(complete_directive("ba"), Some("barcode"));
|
| 233 | }
|
| 234 |
|
| 235 | #[test]
|
| 236 | fn applies_directive_completion() {
|
| 237 | let mut body = "hello\n@ba".to_string();
|
| 238 | let end = body.len();
|
| 239 | let cursor = apply_directive_completion(&mut body, 6, end, "barcode");
|
| 240 | assert_eq!(body, "hello\n@barcode");
|
| 241 | assert_eq!(cursor, 6 + 1 + "barcode".len());
|
| 242 | }
|
| 243 |
|
| 244 | #[test]
|
| 245 | fn completes_directives() {
|
| 246 | let hits = directive_completions("ba");
|
| 247 | assert!(hits.contains(&"barcode"));
|
| 248 | }
|
| 249 |
|
| 250 | #[test]
|
| 251 | fn detects_at_prefix() {
|
| 252 | let body = "hello\n@bar";
|
| 253 | let (start, frag) = at_directive_prefix(body, body.len()).unwrap();
|
| 254 | assert_eq!(start, 6);
|
| 255 | assert_eq!(frag, "bar");
|
| 256 | }
|
| 257 |
|
| 258 | #[test]
|
| 259 | fn inserts_snippet() {
|
| 260 | let (text, cur) = insert_at_cursor("ab", 1, "X");
|
| 261 | assert_eq!(text, "aXb");
|
| 262 | assert_eq!(cur, 2);
|
| 263 | }
|
| 264 | }
|