arf-console 0.4.3

A cross-platform R console written in Rust
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Shell mode completion.
//!
//! Provides path completion and optional executable name completion
//! for shell mode commands.

use super::meta::MetaCommandCompleter;
use super::path::PathCompletionOptions;
use super::string_context::path_to_suggestions;
use reedline::{Completer, Span, Suggestion};

/// Shell separators that start a new token.
const TOKEN_SEPARATORS: &[char] = &['|', ';', '&', '<', '>'];
/// Shell separators that start a new command segment.
const COMMAND_SEPARATORS: &[char] = &['|', ';'];

/// Find the byte position where the current token starts.
///
/// Walks forward through the line up to `pos`, tracking the position after
/// the last whitespace or shell separator character.
///
/// Known limitation: quote state is not tracked. Whitespace inside a quoted
/// segment (e.g. `"dir with spaces/"`) is still treated as a token boundary,
/// so Tab-completing again inside an already-inserted quoted path may produce
/// incorrect span positions. Quote-aware tokenization is deferred to a future
/// version.
fn current_token_start(line: &str, pos: usize) -> usize {
    let pos = pos.min(line.len());
    if !line.is_char_boundary(pos) {
        return 0;
    }
    let slice = &line[..pos];
    let mut last_sep_end = 0;
    for (byte_pos, ch) in slice.char_indices() {
        if ch.is_whitespace() || TOKEN_SEPARATORS.contains(&ch) {
            last_sep_end = byte_pos + ch.len_utf8();
        }
    }
    last_sep_end
}

/// Determine whether the cursor is in a command-name position.
///
/// Returns true if the current token is the first non-whitespace token in the
/// current command segment (i.e., after the last shell separator like `|`, `;`).
fn is_command_position(line: &str, pos: usize) -> bool {
    let pos = pos.min(line.len());
    if !line.is_char_boundary(pos) {
        return false;
    }
    let token_start = current_token_start(line, pos);
    let before_token = &line[..token_start];

    // Find the byte position after the last shell segment separator
    let last_sep_end = {
        let mut sep_end = 0;
        let mut prev: Option<char> = None;
        for (byte_pos, ch) in before_token.char_indices() {
            let is_separator =
                COMMAND_SEPARATORS.contains(&ch) || (ch == '&' && !matches!(prev, Some('<' | '>')));
            if is_separator {
                sep_end = byte_pos + ch.len_utf8();
            }
            prev = Some(ch);
        }
        sep_end
    };

    // If everything between the last separator and token start is whitespace,
    // the current token is the command name in the segment
    before_token[last_sep_end..]
        .chars()
        .all(|c| c.is_whitespace())
}

/// Collect all file names found in the directories listed in `path_str`.
///
/// Results are sorted and deduplicated. No executable-bit filtering is applied
/// (false positives are acceptable for completion purposes).
/// Separated from `collect_path_executables` for testability.
fn collect_executables_from_path_str(path_str: &str) -> Vec<String> {
    let mut names: std::collections::HashSet<String> = std::collections::HashSet::new();

    for dir in std::env::split_paths(path_str) {
        if let Ok(read_dir) = std::fs::read_dir(&dir) {
            for entry in read_dir.filter_map(|e| e.ok()) {
                if let Ok(file_type) = entry.file_type()
                    && (file_type.is_file() || file_type.is_symlink())
                {
                    names.insert(entry.file_name().to_string_lossy().into_owned());
                }
            }
        }
    }

    let mut result: Vec<String> = names.into_iter().collect();
    result.sort();
    result
}

fn collect_path_executables() -> Vec<String> {
    collect_executables_from_path_str(&std::env::var("PATH").unwrap_or_default())
}

/// Completer for shell mode.
///
/// Provides path completion for all arguments and optionally completes
/// executable names from PATH when the cursor is in command position.
pub struct ShellCompleter {
    meta_completer: MetaCommandCompleter,
    command_names: bool,
    command_cache: Option<Vec<String>>,
}

impl ShellCompleter {
    /// Create a new `ShellCompleter`.
    ///
    /// When `command_names` is true, Tab-completing the first token of a shell
    /// command also suggests executable names from PATH directories.
    pub fn new(command_names: bool) -> Self {
        Self {
            meta_completer: MetaCommandCompleter::with_exclusions(
                MetaCommandCompleter::shell_mode_exclusions(),
            ),
            command_names,
            command_cache: None,
        }
    }

    fn get_command_names(&mut self) -> &[String] {
        if self.command_cache.is_none() {
            self.command_cache = Some(collect_path_executables());
        }
        self.command_cache.as_ref().unwrap()
    }
}

impl Completer for ShellCompleter {
    fn complete(&mut self, line: &str, pos: usize) -> Vec<Suggestion> {
        let pos = pos.min(line.len());
        if !line.is_char_boundary(pos) {
            return vec![];
        }
        let trimmed = line.trim_start();

        // Delegate meta commands to MetaCommandCompleter
        if trimmed.starts_with(':') {
            return self.meta_completer.complete(line, pos);
        }

        // Find the current token start and extract the partial text
        let token_start = current_token_start(line, pos);
        let partial = &line[token_start..pos];

        // Get path completions for the current token
        let mut suggestions =
            path_to_suggestions(partial, pos, token_start, &PathCompletionOptions::default());

        // Wrap paths containing spaces in double quotes.
        // This intentionally optimizes for common paths and keeps behavior
        // simple. We do not fully escape shell metacharacters here; paths with
        // uncommon characters such as `$`, backticks, or embedded quotes may
        // still require manual editing before execution.
        for s in &mut suggestions {
            #[cfg(windows)]
            {
                s.value = s.value.replace('/', "\\");
            }
            if s.value.contains(' ') {
                #[cfg(windows)]
                if s.value.ends_with('\\') {
                    s.value.push('\\');
                }
                s.value = format!("\"{}\"", s.value);
                if let Some(match_indices) = &mut s.match_indices {
                    for index in match_indices.iter_mut() {
                        *index += 1;
                    }
                }
            }
        }

        // Add command name completions when in command position and enabled
        if self.command_names && is_command_position(line, pos) {
            let cmd_suggestions: Vec<Suggestion> = self
                .get_command_names()
                .iter()
                .filter(|name| partial.is_empty() || name.starts_with(partial))
                .map(|name| Suggestion {
                    value: name.clone(),
                    display_override: None,
                    description: Some("command".to_string()),
                    extra: None,
                    span: Span {
                        start: token_start,
                        end: pos,
                    },
                    append_whitespace: false,
                    style: None,
                    match_indices: None,
                })
                .collect();

            // Command names follow path suggestions
            suggestions.extend(cmd_suggestions);
        }

        suggestions
    }
}

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

    #[test]
    fn test_current_token_start_first_token() {
        assert_eq!(current_token_start("ls", 2), 0);
        assert_eq!(current_token_start("  ls", 4), 2);
    }

    #[test]
    fn test_current_token_start_argument() {
        // "ls /tmp/" - cursor at end, token starts after space
        assert_eq!(current_token_start("ls /tmp/", 8), 3);
    }

    #[test]
    fn test_current_token_start_after_pipe() {
        // "ls | cat" - cursor at 8, token starts at 5
        assert_eq!(current_token_start("ls | cat", 8), 5);
    }

    #[test]
    fn test_current_token_start_empty() {
        assert_eq!(current_token_start("", 0), 0);
    }

    #[test]
    fn test_current_token_start_non_char_boundary_is_safe() {
        assert_eq!(current_token_start("aé", 2), 0);
    }

    #[test]
    fn test_is_command_position_first_token() {
        assert!(is_command_position("ls", 2));
        assert!(is_command_position("  ls", 4));
        assert!(is_command_position("", 0));
    }

    #[test]
    fn test_is_command_position_argument() {
        assert!(!is_command_position("ls /tmp", 7));
        assert!(!is_command_position("cat foo bar", 11));
    }

    #[test]
    fn test_is_command_position_after_pipe() {
        assert!(is_command_position("ls | ", 5));
        assert!(is_command_position("ls | cat", 8));
    }

    #[test]
    fn test_is_command_position_after_semicolon() {
        assert!(is_command_position("echo hi; ", 9));
        assert!(is_command_position("echo hi; ls", 11));
    }

    #[test]
    fn test_is_command_position_after_redirection_is_false() {
        assert!(!is_command_position("echo hi > out", 13));
        assert!(!is_command_position("echo hi 2>&1", "echo hi 2>&1".len()));
        assert!(!is_command_position("echo hi <&0", "echo hi <&0".len()));
    }

    #[test]
    fn test_shell_completer_delegates_meta_commands() {
        let mut completer = ShellCompleter::new(false);
        // :cd is available in shell mode (not excluded)
        let suggestions = completer.complete(":c", 2);
        assert!(
            suggestions.iter().any(|s| s.value == "cd"),
            "should suggest :cd for :c"
        );
    }

    #[test]
    fn test_shell_completer_excludes_shell_mode_meta_commands() {
        let mut completer = ShellCompleter::new(false);
        let suggestions = completer.complete(":", 1);
        let values: Vec<&str> = suggestions.iter().map(|s| s.value.as_str()).collect();
        assert!(
            !values.contains(&"shell"),
            ":shell should be excluded in shell mode"
        );
        assert!(
            !values.contains(&"help"),
            ":help should be excluded in shell mode"
        );
    }

    #[test]
    fn test_shell_completer_no_command_names_when_disabled() {
        let mut completer = ShellCompleter::new(false);
        let suggestions = completer.complete("", 0);
        assert!(
            suggestions
                .iter()
                .all(|s| s.description.as_deref() != Some("command")),
            "command_names=false should not produce command suggestions"
        );
    }

    #[test]
    fn test_shell_completer_path_completion_for_argument() {
        // Path completion should work for arguments even when command_names=false
        let mut completer = ShellCompleter::new(false);
        // Completing after "cat " - any path suggestions should have span starting at 4
        let suggestions = completer.complete("cat /", 5);
        for s in &suggestions {
            assert_eq!(s.span.start, 4, "span should start at the token start (4)");
        }
    }

    #[test]
    fn test_collect_executables_from_path_str_finds_files() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("mybin"), b"").unwrap();
        std::fs::write(dir.path().join("othertool"), b"").unwrap();
        std::fs::create_dir(dir.path().join("not-a-command-dir")).unwrap();

        let path_str = dir.path().to_string_lossy();
        let names = collect_executables_from_path_str(&path_str);

        assert!(names.contains(&"mybin".to_string()), "should find mybin");
        assert!(
            names.contains(&"othertool".to_string()),
            "should find othertool"
        );
        assert!(
            !names.contains(&"not-a-command-dir".to_string()),
            "should not include directory names"
        );
    }

    #[test]
    fn test_collect_executables_from_path_str_deduplicates() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("shared"), b"").unwrap();

        // Same directory listed twice via std::env::join_paths
        let twice = std::env::join_paths([dir.path(), dir.path()]).unwrap();
        let path_str = twice.to_string_lossy();
        let names = collect_executables_from_path_str(&path_str);
        let count = names.iter().filter(|n| n.as_str() == "shared").count();
        assert_eq!(
            count, 1,
            "duplicates from repeated PATH entries should be removed"
        );
    }

    #[test]
    fn test_collect_executables_from_path_str_sorted() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("zz_last"), b"").unwrap();
        std::fs::write(dir.path().join("aa_first"), b"").unwrap();

        let path_str = dir.path().to_string_lossy();
        let names = collect_executables_from_path_str(&path_str);

        let aa = names.iter().position(|n| n == "aa_first").unwrap();
        let zz = names.iter().position(|n| n == "zz_last").unwrap();
        assert!(aa < zz, "results should be sorted alphabetically");
    }

    #[test]
    fn test_collect_executables_from_path_str_ignores_missing_dirs() {
        let names =
            collect_executables_from_path_str("/nonexistent_path_that_does_not_exist_12345");
        assert!(
            names.is_empty(),
            "missing directories should not cause a panic or yield results"
        );
    }

    #[test]
    fn test_shell_completer_command_names_prefix_filter() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("myapp"), b"").unwrap();
        std::fs::write(dir.path().join("myother"), b"").unwrap();
        std::fs::write(dir.path().join("unrelated"), b"").unwrap();

        let path_str = dir.path().to_string_lossy().into_owned();
        let mut completer = ShellCompleter::new(true);
        // Pre-populate cache with our controlled directory
        completer.command_cache = Some(collect_executables_from_path_str(&path_str));

        let suggestions = completer.complete("my", 2);
        let cmd_values: Vec<&str> = suggestions
            .iter()
            .filter(|s| s.description.as_deref() == Some("command"))
            .map(|s| s.value.as_str())
            .collect();

        assert!(cmd_values.contains(&"myapp"), "should suggest myapp");
        assert!(cmd_values.contains(&"myother"), "should suggest myother");
        assert!(
            !cmd_values.contains(&"unrelated"),
            "should not suggest unrelated"
        );
    }

    #[test]
    fn test_shell_completer_command_names_not_in_arg_position() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("mybin"), b"").unwrap();

        let path_str = dir.path().to_string_lossy().into_owned();
        let mut completer = ShellCompleter::new(true);
        completer.command_cache = Some(collect_executables_from_path_str(&path_str));

        // "ls my" — cursor is in argument position, not command position
        let suggestions = completer.complete("ls my", 5);
        let has_cmd = suggestions
            .iter()
            .any(|s| s.description.as_deref() == Some("command") && s.value == "mybin");
        assert!(
            !has_cmd,
            "command names should not appear in argument position"
        );
    }
}