BlueGit

timfa posprint

Files

posprint / src / editor_assist.rs

editor_assist.rs

editor_assist.rs 3.3 KiB Download Raw
1//! Editor snippets, directive autocomplete, and insert helpers.
2
3pub const DIRECTIVES: &[&str] = &["feed", "cut", "image", "barcode", "qr"];
4
5pub 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
14pub struct InsertOption {
15 pub label: String,
16 pub snippet: String,
17}
18
19pub fn image_insert_options() -> Vec<InsertOption> {
20 vec![InsertOption {
21 label: "Image from file".into(),
22 snippet: "@image logo.png\n".into(),
23 }]
24}
25
26pub fn barcode_insert_options() -> Vec<InsertOption> {
27 let mut out = Vec::new();
28 for &sym in crate::dsl::BarcodeSymbology::ALL {
29 if sym.supports_native() {
30 out.push(InsertOption {
31 label: format!("Native {}", sym.as_str()),
32 snippet: format!("@barcode {} ${{Data}}\n", sym.as_str()),
33 });
34 }
35 }
36 for &sym in crate::dsl::BarcodeSymbology::ALL {
37 out.push(InsertOption {
38 label: format!("Raster {}", sym.as_str()),
39 snippet: format!("@barcode RASTER {} ${{Data}}\n", sym.as_str()),
40 });
41 }
42 out
43}
44
45pub fn qr_insert_options() -> Vec<InsertOption> {
46 vec![
47 InsertOption {
48 label: "Native QR (ESC/POS)".into(),
49 snippet: "@qr ${Data}\n".into(),
50 },
51 InsertOption {
52 label: "Raster QR (auto version)".into(),
53 snippet: "@qr RASTER ${Data} scale=4 ecc=M\n".into(),
54 },
55 InsertOption {
56 label: "Raster QR (version 5)".into(),
57 snippet: "@qr RASTER ${Data} version=5 scale=4 ecc=M\n".into(),
58 },
59 ]
60}
61
62/// If the cursor is after `@` on the current line, return (start_index, typed_prefix).
63pub fn at_directive_prefix(body: &str, cursor: usize) -> Option<(usize, String)> {
64 let cursor = cursor.min(body.len());
65 let before = &body[..cursor];
66 let line_start = before.rfind('\n').map(|i| i + 1).unwrap_or(0);
67 let line = &before[line_start..];
68 let at = line.find('@')?;
69 if at > 0 && !line[..at].trim().is_empty() {
70 return None;
71 }
72 let fragment = &line[at + 1..];
73 if fragment.contains(' ') {
74 return None;
75 }
76 Some((line_start + at, fragment.to_string()))
77}
78
79pub fn insert_at_cursor(body: &str, cursor: usize, text: &str) -> (String, usize) {
80 let cursor = cursor.min(body.len());
81 let mut out = String::with_capacity(body.len() + text.len());
82 out.push_str(&body[..cursor]);
83 out.push_str(text);
84 out.push_str(&body[cursor..]);
85 let new_cursor = cursor + text.len();
86 (out, new_cursor)
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92
93 #[test]
94 fn completes_directives() {
95 let hits = directive_completions("ba");
96 assert!(hits.contains(&"barcode"));
97 }
98
99 #[test]
100 fn detects_at_prefix() {
101 let body = "hello\n@bar";
102 let (start, frag) = at_directive_prefix(body, body.len()).unwrap();
103 assert_eq!(start, 6);
104 assert_eq!(frag, "bar");
105 }
106
107 #[test]
108 fn inserts_snippet() {
109 let (text, cur) = insert_at_cursor("ab", 1, "X");
110 assert_eq!(text, "aXb");
111 assert_eq!(cur, 2);
112 }
113}