History suggestions, frecency, cd by keyword, fish completions; bjoterm 0.2.0
The editor suggests history lines whose command exists, cd lines by the frecency of their directory, paths ignoring case, and the visited directory that cd KEYWORD... goes to. Tab completes arguments with fish's completions when fish is on PATH, and paths by the start of each part (~/P/B/B). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5012a5d993
commit
61d5a96eda
10 changed files with 751 additions and 110 deletions
|
|
@ -10,6 +10,9 @@
|
|||
(import (std simpletest))
|
||||
(import (bjoterm line))
|
||||
(import (bjosh editor))
|
||||
(import (bjosh shell))
|
||||
(import (bjosh dirs))
|
||||
(import (bjosh builtins))
|
||||
|
||||
(import/extern
|
||||
(temp-dir (: System.IO.Path.GetTempPath (-> string)))
|
||||
|
|
@ -47,29 +50,78 @@
|
|||
(expect "not hidden files" (file-candidates "") ["a\\ b " "abc.txt " "abd/"])
|
||||
(expect "hidden files for a dot" (file-candidates ".h") [".hidden "])
|
||||
(expect "in a directory" (file-candidates "abd/i") ["abd/inner "])
|
||||
(expect "the second word completes files" (record-ref (complete "cat ab") candidates) ["abc.txt " "abd/"])
|
||||
(expect "the first word completes commands"
|
||||
(any? (fun (c) (= c "jobs ")) (record-ref (complete "jo") candidates)) #t)
|
||||
(expect "a first word with a / completes files" (record-ref (complete "./ab") candidates)
|
||||
["./abc.txt " "./abd/"])
|
||||
(set-environment-variable! "XDG_STATE_HOME" (path-combine dir "state"))
|
||||
(def sh (make-shell None None ["bjosh"]))
|
||||
(defun (cands text) (record-ref (complete sh text) candidates))
|
||||
;; The ghost and the line of a suggestion.
|
||||
(defun (sg text history)
|
||||
(match (suggest sh text history)
|
||||
((Some s) (Some (Tuple (record-ref s ghost) (record-ref s line))))
|
||||
(None None)))
|
||||
(defun (g text history)
|
||||
(match (sg text history) ((Some t) (Some (fst t))) (None None)))
|
||||
|
||||
;; Completion.
|
||||
(expect "the second word completes files" (cands "cat ab") ["abc.txt " "abd/"])
|
||||
(expect "the first word completes commands" (any? (fun (c) (= c "jobs ")) (cands "jo")) #t)
|
||||
(expect "with their help"
|
||||
(let ((c (complete sh "prevd")))
|
||||
(Tuple (record-ref c candidates) (record-ref c descriptions)))
|
||||
(Tuple ["prevd "] ["go back in the directory history; also Alt-Left"]))
|
||||
(expect "a first word with a / completes files" (cands "./ab") ["./abc.txt " "./abd/"])
|
||||
(expect "a path ignoring case when nothing else matches" (cands "./ABC") ["./abc.txt "])
|
||||
(expect "each part as the start of a name" (cands "ls ./a/in") ["./abd/inner "])
|
||||
(expect "abbreviated" (abbreviated-candidates "a/i") ["abd/inner "])
|
||||
(expect "variables" (vec-contains? (cands "echo $HOM") "$HOME ") #t)
|
||||
(expect "the command at the end" (command-line-at "ls | git ch") "git ch")
|
||||
(expect "not inside quotes" (command-line-at "echo 'a|b' x") "echo 'a|b' x")
|
||||
(expect "after &&" (command-line-at "make && ./run -") "./run -")
|
||||
(when (some? (find-program "fish"))
|
||||
(expect "fish's completions, with descriptions"
|
||||
(let ((c (complete sh "systemctl sta")))
|
||||
(Tuple (vec-contains? (record-ref c candidates) "start ")
|
||||
(any? (fun (d) (not (string-empty? d))) (record-ref c descriptions))))
|
||||
(Tuple #t #t)))
|
||||
|
||||
;; Suggestions.
|
||||
(ignore (directory-create (path-combine dir "abd/deeper")))
|
||||
(ignore (directory-create (path-combine dir "other")))
|
||||
(expect "cd suggests the only directory" (suggest "cd ot" []) (Some "her/"))
|
||||
(expect "and not files" (suggest "cd abc" []) None)
|
||||
(expect "the common prefix of several" (suggest "cd abd/" []) (Some "deeper/"))
|
||||
(expect "a path argument suggests files, with no space" (suggest "cat abc" []) (Some ".txt"))
|
||||
(expect "several files, their common prefix" (suggest "cat a" []) None)
|
||||
(expect "in a directory" (suggest "cat abd/in" []) (Some "ner"))
|
||||
(expect "not the command" (suggest "ab" []) None)
|
||||
(expect "not an empty argument" (suggest "cat " []) None)
|
||||
(expect "not a second line" (suggest "echo 'a\ncat abc" []) None)
|
||||
(ignore (directory-create (path-combine dir "Mixed")))
|
||||
(expect "cd suggests the only directory" (g "cd ot" []) (Some "her/"))
|
||||
(expect "and not files" (g "cd abc" []) None)
|
||||
(expect "the common prefix of several" (g "cd abd/" []) (Some "deeper/"))
|
||||
(expect "a directory ignoring case replaces the word" (sg "cd mix" []) (Some (Tuple "ed/" "cd Mixed/")))
|
||||
(expect "a path argument suggests files, with no space" (g "cat abc" []) (Some ".txt"))
|
||||
(expect "several files, their common prefix" (g "cat a" []) None)
|
||||
(expect "in a directory" (g "cat abd/in" []) (Some "ner"))
|
||||
(expect "not the command" (g "ab" []) None)
|
||||
(expect "not an empty argument" (g "cat " []) None)
|
||||
(expect "not a second line" (g "echo 'a\ncat abc" []) None)
|
||||
(expect "cd from the history, newest first"
|
||||
(suggest "cd a" ["cd abd" "cd abd/deeper" "ls"]) (Some "bd/deeper"))
|
||||
(g "cd a" ["cd abd" "cd abd/deeper" "ls"]) (Some "bd/deeper"))
|
||||
(expect "not a directory that is gone"
|
||||
(suggest "cd o" ["cd other" "cd old-gone"]) (Some "ther"))
|
||||
(expect "only cd lines" (suggest "cd o" ["cdx other"]) (Some "ther/"))
|
||||
(g "cd o" ["cd other" "cd old-gone"]) (Some "ther"))
|
||||
(expect "only cd lines" (g "cd o" ["cdx other"]) (Some "ther/"))
|
||||
(expect "any line from the history" (g "ec" ["echo hi"]) (Some "ho hi"))
|
||||
(expect "the newest" (g "ec" ["echo hi" "echo ho"]) (Some "ho ho"))
|
||||
(expect "not one whose command is gone" (g "nosuch" ["nosuch-command-x a"]) None)
|
||||
(expect "ignoring case, it replaces the line" (sg "EC" ["echo hi"]) (Some (Tuple "ho hi" "echo hi")))
|
||||
(defun (visit! path)
|
||||
(set-current-directory! path)
|
||||
(dir-arrived! sh)
|
||||
(set-current-directory! dir))
|
||||
(visit! (path-combine dir "other"))
|
||||
(visit! (path-combine dir "other"))
|
||||
(visit! (path-combine dir "abd/deeper"))
|
||||
(expect "cd from the history by frecency" (g "cd " ["cd other" "cd abd/deeper"]) (Some "other"))
|
||||
(set-current-directory! (path-combine dir "other"))
|
||||
(def deeper (path-combine dir "abd/deeper"))
|
||||
(expect "a visited directory the words match"
|
||||
(sg "cd deep" []) (Some (Tuple (str " \u{2192} " deeper) (str "cd " deeper))))
|
||||
(expect "with several words" (g "cd abd dee" []) (Some (str " \u{2192} " deeper)))
|
||||
(expect "not the directory you are in" (g "cd oth" []) None)
|
||||
(expect "Tab after cd completes them too" (cands "cd deep") [(str deeper "/")])
|
||||
(set-current-directory! dir)
|
||||
(set-current-directory! here)
|
||||
(ignore (directory-delete-tree dir))
|
||||
0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue