From 78734b806def231250166c956e4e526a3a97f142 Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Thu, 14 Jun 2012 09:07:16 +0900 Subject: [PATCH] Making repl completion insert the longest common prefix before showing candidates. --- lib/chibi/repl.scm | 30 +++++++++++++++++++++++------- lib/chibi/repl.sld | 3 ++- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/chibi/repl.scm b/lib/chibi/repl.scm index 0f31b101..814848aa 100644 --- a/lib/chibi/repl.scm +++ b/lib/chibi/repl.scm @@ -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 - (filter - (lambda (w) - (and (>= (string-length w) len) - (equal? word (substring w 0 len)))) - (map symbol->string (all-exports (interaction-environment))))))))) + (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))))) + (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 diff --git a/lib/chibi/repl.sld b/lib/chibi/repl.sld index 9e273af7..edd1353e 100644 --- a/lib/chibi/repl.sld +++ b/lib/chibi/repl.sld @@ -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"))