hjkl-ex 0.25.0

Ex-command registry and dispatch layer for the hjkl editor stack.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use std::borrow::Cow;
use std::path::{Path, PathBuf};

/// Caller-supplied context for token expansion. hjkl-ex stays agnostic of
/// whether `current_path` / `alt_path` come from an Editor or a multi-slot
/// host — the caller fills whichever fields it has.
#[derive(Default, Clone, Debug)]
pub struct ExpandContext<'a> {
    /// Path of the current buffer (for `%`).
    pub current_path: Option<&'a Path>,
    /// Path of the alternate buffer (for `#`).
    pub alt_path: Option<&'a Path>,
    /// Word under cursor (for `<cword>`).
    pub cword: Option<Cow<'a, str>>,
    /// WORD under cursor — broader vim semantic, no whitespace at all (for `<cWORD>`).
    pub cwword: Option<Cow<'a, str>>,
    /// cwd for resolving `:p` modifier (defaults to std::env::current_dir() if None).
    pub cwd: Option<&'a Path>,
}

/// Apply a single modifier (`:p`, `:h`, `:t`) to a path string.
/// Returns `None` when the modifier is unrecognised.
fn apply_modifier(s: &str, modifier: &str) -> Option<String> {
    match modifier {
        "p" => {
            let p = Path::new(s);
            let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
            let abs = if p.is_absolute() {
                p.to_path_buf()
            } else {
                cwd.join(p)
            };
            // canonicalize may fail if the file doesn't exist yet — fall back to lexical join.
            let result = abs.canonicalize().unwrap_or(abs);
            Some(result.display().to_string())
        }
        "h" => {
            let p = Path::new(s);
            match p.parent() {
                None => Some(String::new()),
                Some(parent) if parent == Path::new("") => Some(".".to_string()),
                Some(parent) => Some(parent.display().to_string()),
            }
        }
        "t" => {
            let p = Path::new(s);
            match p.file_name() {
                Some(name) => Some(name.to_string_lossy().into_owned()),
                None => Some(s.to_string()),
            }
        }
        _ => None,
    }
}

/// Apply a chain of modifiers (e.g. `p:h` from `%:p:h`) to a base value.
/// Returns `None` if any modifier is unknown.
fn apply_modifiers(mut value: String, modifiers: &str) -> Option<String> {
    if modifiers.is_empty() {
        return Some(value);
    }
    for modifier in modifiers.split(':') {
        if modifier.is_empty() {
            continue;
        }
        value = apply_modifier(&value, modifier)?;
    }
    Some(value)
}

/// Expand a single token (`%`, `#`, `<cword>`, `<cWORD>`, possibly with
/// modifier suffix `:p:h:t`) to its literal value. Returns None when the
/// token isn't a recognized expansion form OR when the underlying source
/// is unavailable (e.g. `%` with no current_path).
pub fn expand_filename(ctx: &ExpandContext<'_>, token: &str) -> Option<String> {
    // Try `%` with optional `:mod` chain.
    if token == "%" || token.starts_with("%:") {
        let base = ctx.current_path?.display().to_string();
        let mods = token
            .strip_prefix('%')
            .unwrap()
            .strip_prefix(':')
            .unwrap_or("");
        return apply_modifiers(base, mods);
    }

    // Try `#` with optional `:mod` chain.
    if token == "#" || token.starts_with("#:") {
        let base = ctx.alt_path?.display().to_string();
        let mods = token
            .strip_prefix('#')
            .unwrap()
            .strip_prefix(':')
            .unwrap_or("");
        return apply_modifiers(base, mods);
    }

    // Try `<cword>` with optional `:mod` chain.
    if token == "<cword>" || token.starts_with("<cword>:") {
        let base = ctx.cword.as_deref()?.to_string();
        let mods = token
            .strip_prefix("<cword>")
            .unwrap()
            .strip_prefix(':')
            .unwrap_or("");
        return apply_modifiers(base, mods);
    }

    // Try `<cWORD>` with optional `:mod` chain.
    if token == "<cWORD>" || token.starts_with("<cWORD>:") {
        let base = ctx.cwword.as_deref()?.to_string();
        let mods = token
            .strip_prefix("<cWORD>")
            .unwrap()
            .strip_prefix(':')
            .unwrap_or("");
        return apply_modifiers(base, mods);
    }

    None
}

/// Extract the expansion token starting at `s` (which must start with `%`,
/// `#`, `<cword>`, or `<cWORD>`). Returns `(token, rest)` where `token`
/// includes any trailing `:mod` chain consumed, and `rest` is the unconsumed
/// tail of `s`.
fn extract_token(s: &str) -> (&str, &str) {
    // Determine the base length.
    let base_len = if s.starts_with("<cword>") || s.starts_with("<cWORD>") {
        7
    } else if s.starts_with('%') || s.starts_with('#') {
        1
    } else {
        // Should not be called without a known prefix.
        return (s, "");
    };

    // Consume optional `:mod` chain.
    let mut end = base_len;
    let bytes = s.as_bytes();
    while end < bytes.len() && bytes[end] == b':' {
        // Scan one modifier segment (letters a-z).
        let colon_pos = end;
        end += 1; // skip `:`
        let seg_start = end;
        while end < bytes.len() && bytes[end].is_ascii_alphabetic() {
            end += 1;
        }
        if end == seg_start {
            // Empty segment after `:` — not a modifier, stop.
            end = colon_pos;
            break;
        }
    }
    (&s[..end], &s[end..])
}

/// Walk an arg string left-to-right, replacing every `%[:mods]`, `#[:mods]`,
/// `<cword>[:mods]`, `<cWORD>[:mods]` occurrence with its expansion. Tokens
/// that fail to expand are LEFT IN PLACE unchanged (vim parity).
///
/// Token boundaries: a `%` or `#` not preceded by a backslash is a
/// candidate start. `<cword>`/`<cWORD>` match literally (case-sensitive).
/// Backslash-escaped variants `\%` `\#` are preserved as `%`/`#` literals.
pub fn expand_args(ctx: &ExpandContext<'_>, args: &str) -> String {
    let mut result = String::with_capacity(args.len());
    let mut i = 0;
    let bytes = args.as_bytes();
    let len = bytes.len();

    while i < len {
        // Handle backslash escape for `%` and `#`.
        if bytes[i] == b'\\' && i + 1 < len && (bytes[i + 1] == b'%' || bytes[i + 1] == b'#') {
            // `\%` → literal `%`, `\#` → literal `#`.
            result.push(bytes[i + 1] as char);
            i += 2;
            continue;
        }

        // Check for expansion token start.
        let rest = &args[i..];
        if bytes[i] == b'%'
            || bytes[i] == b'#'
            || rest.starts_with("<cword>")
            || rest.starts_with("<cWORD>")
        {
            let (token, after) = extract_token(rest);
            match expand_filename(ctx, token) {
                Some(expanded) => {
                    result.push_str(&expanded);
                }
                None => {
                    // Leave token in place (vim parity).
                    result.push_str(token);
                }
            }
            i += token.len();
            let _ = after; // i is already advanced past token
            continue;
        }

        // Ordinary character.
        // Safety: we index by byte but push as char; handle multi-byte by pushing char.
        let ch = args[i..].chars().next().unwrap();
        result.push(ch);
        i += ch.len_utf8();
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;

    #[test]
    fn percent_with_current_path() {
        let ctx = ExpandContext {
            current_path: Some(Path::new("src/main.rs")),
            ..Default::default()
        };
        assert_eq!(expand_filename(&ctx, "%"), Some("src/main.rs".to_string()));
    }

    #[test]
    fn percent_no_current_path_returns_none() {
        let ctx = ExpandContext::default();
        assert_eq!(expand_filename(&ctx, "%"), None);
    }

    #[test]
    fn hash_with_alt_path() {
        let ctx = ExpandContext {
            alt_path: Some(Path::new("src/lib.rs")),
            ..Default::default()
        };
        assert_eq!(expand_filename(&ctx, "#"), Some("src/lib.rs".to_string()));
    }

    #[test]
    fn cword_with_value() {
        let ctx = ExpandContext {
            cword: Some(Cow::Borrowed("foo")),
            ..Default::default()
        };
        assert_eq!(expand_filename(&ctx, "<cword>"), Some("foo".to_string()));
    }

    #[test]
    fn cword_none_returns_none() {
        let ctx = ExpandContext::default();
        assert_eq!(expand_filename(&ctx, "<cword>"), None);
    }

    #[test]
    fn percent_colon_p_absolute() {
        // Use a known relative path + explicit cwd to verify :p makes it absolute.
        // Since :p uses current_dir() internally, we just check it's absolute.
        let ctx = ExpandContext {
            current_path: Some(Path::new("Cargo.toml")),
            ..Default::default()
        };
        let expanded = expand_filename(&ctx, "%:p").unwrap();
        assert!(
            Path::new(&expanded).is_absolute(),
            ":p must produce absolute path, got {expanded:?}"
        );
        assert!(expanded.ends_with("Cargo.toml"), "must end with Cargo.toml");
    }

    #[test]
    fn percent_colon_h_parent_dir() {
        let ctx = ExpandContext {
            current_path: Some(Path::new("src/main.rs")),
            ..Default::default()
        };
        assert_eq!(expand_filename(&ctx, "%:h"), Some("src".to_string()));
    }

    #[test]
    fn percent_colon_t_basename() {
        let ctx = ExpandContext {
            current_path: Some(Path::new("src/main.rs")),
            ..Default::default()
        };
        assert_eq!(expand_filename(&ctx, "%:t"), Some("main.rs".to_string()));
    }

    #[test]
    fn percent_colon_p_colon_h_parent_of_absolute() {
        let ctx = ExpandContext {
            current_path: Some(Path::new("src/main.rs")),
            ..Default::default()
        };
        let expanded = expand_filename(&ctx, "%:p:h").unwrap();
        let p = Path::new(&expanded);
        assert!(p.is_absolute(), ":p:h must be absolute, got {expanded:?}");
        // The last component should be "src" (the parent of main.rs).
        assert_eq!(
            p.file_name().map(|n| n.to_string_lossy().to_string()),
            Some("src".to_string())
        );
    }

    #[test]
    fn percent_colon_t_colon_p_silly_but_valid() {
        // %:t:p → basename then made absolute: basename "main.rs" joined to cwd.
        let ctx = ExpandContext {
            current_path: Some(Path::new("src/main.rs")),
            ..Default::default()
        };
        let expanded = expand_filename(&ctx, "%:t:p").unwrap();
        let p = Path::new(&expanded);
        assert!(p.is_absolute(), "%:t:p must be absolute, got {expanded:?}");
        assert!(
            expanded.ends_with("main.rs"),
            "%:t:p must end with main.rs, got {expanded:?}"
        );
    }

    #[test]
    fn percent_bogus_modifier_returns_none() {
        let ctx = ExpandContext {
            current_path: Some(Path::new("src/main.rs")),
            ..Default::default()
        };
        assert_eq!(expand_filename(&ctx, "%:bogus"), None);
    }

    #[test]
    fn expand_args_percent_dot_txt_suffix() {
        // vim semantic: `%` is expanded, `.txt` is literal suffix.
        let ctx = ExpandContext {
            current_path: Some(Path::new("src/main.rs")),
            ..Default::default()
        };
        assert_eq!(expand_args(&ctx, "%.txt"), "src/main.rs.txt");
    }

    #[test]
    fn expand_args_backslash_percent_literal() {
        let ctx = ExpandContext {
            current_path: Some(Path::new("src/main.rs")),
            ..Default::default()
        };
        assert_eq!(expand_args(&ctx, r"\%"), "%");
    }

    #[test]
    fn expand_args_cword_standalone() {
        let ctx = ExpandContext {
            cword: Some(Cow::Borrowed("foo")),
            ..Default::default()
        };
        assert_eq!(expand_args(&ctx, "<cword>"), "foo");
    }

    #[test]
    fn expand_args_cword_surrounded() {
        let ctx = ExpandContext {
            cword: Some(Cow::Borrowed("foo")),
            ..Default::default()
        };
        assert_eq!(expand_args(&ctx, "a <cword> b"), "a foo b");
    }

    #[test]
    fn expand_args_percent_in_middle() {
        let ctx = ExpandContext {
            current_path: Some(Path::new("src/main.rs")),
            ..Default::default()
        };
        // `foo % bar` — space-delimited; % expands in place.
        assert_eq!(expand_args(&ctx, "foo % bar"), "foo src/main.rs bar");
    }

    #[test]
    fn expand_args_unexpandable_left_in_place() {
        // No current_path set — % stays as-is.
        let ctx = ExpandContext::default();
        assert_eq!(expand_args(&ctx, "e %"), "e %");
    }

    #[test]
    fn expand_args_hash_backslash() {
        let ctx = ExpandContext {
            alt_path: Some(Path::new("src/lib.rs")),
            ..Default::default()
        };
        // \# → literal #, not alt path.
        assert_eq!(expand_args(&ctx, r"\#"), "#");
    }

    #[test]
    fn percent_colon_h_root_returns_empty() {
        // Path::parent() of "/" is None → return empty string.
        let ctx = ExpandContext {
            current_path: Some(Path::new("/")),
            ..Default::default()
        };
        // "/" has no parent → return "".
        let result = expand_filename(&ctx, "%:h");
        // On Unix "/" has parent None; we return empty string per spec.
        assert!(result.is_some());
    }

    #[test]
    fn percent_colon_h_no_separator() {
        // "main.rs" has parent "" → we return ".".
        let ctx = ExpandContext {
            current_path: Some(Path::new("main.rs")),
            ..Default::default()
        };
        assert_eq!(expand_filename(&ctx, "%:h"), Some(".".to_string()));
    }
}