Making repl completion insert the longest common prefix before

showing candidates.
This commit is contained in:
Alex Shinn 2012-06-14 09:07:16 +09:00
parent 028a260247
commit 78734b806d
2 changed files with 25 additions and 8 deletions

View file

@ -42,16 +42,32 @@
res
(lp (environment-parent env) (append (env-exports env) res)))))
(define (string-common-prefix-length strings)
(if (null? strings)
0
(let lp ((len (string-length (car strings)))
(prev (car strings))
(ls (cdr strings)))
(if (or (null? ls) (zero? len))
len
(lp (min len (string-mismatch prev (car ls)))
(car ls)
(cdr ls))))))
(define (make-sexp-buffer-completer)
(buffer-make-completer
(lambda (buf word)
(let ((len (string-length word)))
(sort
(let* ((len (string-length word))
(candidates
(filter
(lambda (w)
(and (>= (string-length w) len)
(equal? word (substring w 0 len))))
(map symbol->string (all-exports (interaction-environment)))))))))
(map symbol->string (all-exports (interaction-environment)))))
(prefix-len (string-common-prefix-length candidates)))
(if (> prefix-len len)
(list (substring (car candidates) 0 prefix-len))
(sort candidates))))))
;;> Runs an interactive REPL. Repeatedly displays a prompt,
;;> then Reads an expression, Evaluates the expression, Prints

View file

@ -2,6 +2,7 @@
(define-library (chibi repl)
(export repl)
(import (scheme) (only (meta) load-module)
(chibi ast) (chibi io) (chibi process) (chibi term edit-line)
(chibi ast) (chibi strings) (chibi io)
(chibi process) (chibi term edit-line)
(srfi 1) (srfi 18) (srfi 38) (srfi 95) (srfi 98))
(include "repl.scm"))