Fix zsh auto-completion

The bash and zsh scripts expect different data from Racket: zsh expects
a list of collect roots and other parent directories of collections,
while bash expects a list of collection names
This commit is contained in:
Jonathan Schuster 2014-04-06 22:51:06 -04:00 committed by Vincent St-Amour
parent 7b172ac6d7
commit f175490639
3 changed files with 29 additions and 10 deletions

View File

@ -1,15 +1,18 @@
#lang racket/base
(require racket/string setup/dirs setup/link)
(provide get-top-level-collection-paths)
(require racket/string racket/list racket/path setup/dirs setup/link)
(define (add-directory-collections c s)
(if (directory-exists? c)
(for/fold ([s s]) ([p (in-list (directory-list c))]
#:when (directory-exists? (build-path c p))
#:when (regexp-match? #rx#"^[a-zA-Z_%+-]*$" p))
(hash-set s (path-element->string p) #t))
(hash-set s (build-path c (path-element->string p)) #t))
s))
(define (get-all-top-level-collections)
(define (get-top-level-collection-paths)
(hash-keys
(for/fold ([s (hash)]) ([l (in-list
(current-library-collection-links))])
@ -19,12 +22,23 @@
(current-library-collection-paths))])
(add-directory-collections c s))]
[(path? l)
(let ([s (for*/fold ([s s]) ([c (in-list (links #:file l #:root? #f))])
(hash-set s c #t))])
(let ([s (for*/fold ([s s]) ([c (in-list (links #:file l #:root? #f #:with-path? #t))])
(hash-set s (cdr c) #t))])
(for*/fold ([s s]) ([c (in-list (links #:file l #:root? #t))])
(add-directory-collections c s)))]
[else (error 'get-all-top-level-collections
[else (error 'get-top-level-collection-paths
"unexpected value in `current-library-collection-links': ~e"
l)]))))
(for-each displayln (get-all-top-level-collections))
(module+ main
(define var "RACKET_COMPLETION_CLIENT")
(define shell (getenv var))
(cond [(equal? shell "bash")
(for ([p (get-top-level-collection-paths)])
(define-values [base name dir?] (split-path p))
(displayln name))]
[(equal? shell "zsh")
(for-each displayln
(remove-duplicates
(map path-only (get-top-level-collection-paths))))]
[else (error 'list-collects "unknown shell (missing ~a envvar)" var)]))

View File

@ -9,6 +9,9 @@
# will need to make sure that you have bash completions enabled, usually
# with "source /etc/bash_completion".
# Identify bash as the completion client
export RACKET_COMPLETION_CLIENT=bash
# This completes only *.{rkt,ss,scm,scrbl} files unless there are none,
# in which case it completes other things.
_racket_filedir() {
@ -116,7 +119,7 @@ _complete_collects() {
local cur="$1"
if [[ "${#_racket_collects[@]}" -eq 0 ]]; then
_racket_collects=(
$( $_racket_cmd -e '(require shell-completion/list-collects)' )
$( $_racket_cmd -e '(require (submod shell-completion/list-collects main))' )
)
fi
local wordlist=""

View File

@ -36,6 +36,9 @@
# the function since it's loaded on first use.)
#
# Identify zsh as the completion client
export RACKET_COMPLETION_CLIENT=zsh
###############################################################################
_racket_file_expander() {
@ -60,8 +63,7 @@ _racket_call() {
_racket_read_libfile_or_collect() {
local -a dirs
dirs=(
"${(f)$(_racket_call directories '
(for-each displayln (current-library-collection-paths))')}"
"${(f)$(_racket_call directories '(require (submod shell-completion/list-collects main))')}"
)
local -a ignored
ignored=('*.dep' '*.zo' 'compiled' '*/compiled')