2009-01-09 18:58:13 -05:00
|
|
|
! Copyright (C) 2005, 2009 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2012-09-25 12:10:44 -04:00
|
|
|
USING: accessors arrays assocs colors.constants combinators
|
2012-10-22 21:51:38 -04:00
|
|
|
combinators.short-circuit fry io.directories io.files
|
|
|
|
io.files.info io.pathnames kernel locals make math math.order
|
2013-03-06 22:58:53 -05:00
|
|
|
sequences sequences.private sorting splitting typed
|
|
|
|
unicode.categories unicode.data vectors vocabs vocabs.hierarchy
|
|
|
|
;
|
2010-08-13 22:28:44 -04:00
|
|
|
|
2008-04-26 00:17:08 -04:00
|
|
|
IN: tools.completion
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2011-09-13 12:13:02 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: smart-index-from ( obj i seq -- n/f )
|
|
|
|
rot [ ch>lower ] [ ch>upper ] bi
|
2012-06-19 16:50:50 -04:00
|
|
|
'[ dup _ eq? [ drop t ] [ _ eq? ] if ] find-from drop ;
|
2011-09-13 12:13:02 -04:00
|
|
|
|
2012-06-19 16:50:50 -04:00
|
|
|
:: (fuzzy) ( accum i full ch -- accum i ? )
|
2011-09-13 12:13:02 -04:00
|
|
|
ch i full smart-index-from [
|
2012-06-19 16:50:50 -04:00
|
|
|
[ accum push ]
|
|
|
|
[ accum swap 1 + t ] bi
|
2007-09-20 18:09:08 -04:00
|
|
|
] [
|
2012-06-19 16:50:50 -04:00
|
|
|
f -1 f
|
2013-03-06 22:58:53 -05:00
|
|
|
] if* ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2012-09-25 12:45:44 -04:00
|
|
|
PRIVATE>
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: fuzzy ( full short -- indices )
|
2009-04-18 22:53:22 -04:00
|
|
|
dup [ length <vector> 0 ] curry 2dip
|
2012-06-19 16:50:50 -04:00
|
|
|
[ (fuzzy) ] with all? 2drop ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2012-09-25 12:45:44 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: (runs) ( runs n seq -- runs n )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
2dup number=
|
|
|
|
[ drop ] [ nip V{ } clone pick push ] if
|
2009-08-13 20:21:44 -04:00
|
|
|
1 +
|
2009-05-25 17:38:33 -04:00
|
|
|
] keep pick last push
|
2013-03-06 22:58:53 -05:00
|
|
|
] each ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2012-09-25 12:45:44 -04:00
|
|
|
PRIVATE>
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: runs ( seq -- newseq )
|
2012-06-19 16:50:50 -04:00
|
|
|
[ V{ } clone 1vector ] dip [ first ] keep (runs) drop ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2012-09-25 12:45:44 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: score-1 ( i full -- n )
|
|
|
|
{
|
|
|
|
{ [ over zero? ] [ 2drop 10 ] }
|
2009-08-13 20:21:44 -04:00
|
|
|
{ [ 2dup length 1 - number= ] [ 2drop 4 ] }
|
2013-03-06 22:58:53 -05:00
|
|
|
{ [ 2dup [ 1 - ] dip nth-unsafe Letter? not ] [ 2drop 10 ] }
|
|
|
|
{ [ 2dup [ 1 + ] dip nth-unsafe Letter? not ] [ 2drop 4 ] }
|
2008-04-11 13:57:43 -04:00
|
|
|
[ 2drop 1 ]
|
2013-03-06 22:58:53 -05:00
|
|
|
} cond ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2012-09-25 12:45:44 -04:00
|
|
|
PRIVATE>
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: score ( full fuzzy -- n )
|
|
|
|
dup [
|
2008-03-29 21:36:58 -04:00
|
|
|
[ [ length ] bi@ - 15 swap [-] 3 /f ] 2keep
|
2007-09-20 18:09:08 -04:00
|
|
|
runs [
|
|
|
|
[ 0 [ pick score-1 max ] reduce nip ] keep
|
|
|
|
length * +
|
2008-01-09 17:36:30 -05:00
|
|
|
] with each
|
2007-09-20 18:09:08 -04:00
|
|
|
] [
|
|
|
|
2drop 0
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
: rank-completions ( results -- newresults )
|
|
|
|
sort-keys <reversed>
|
2007-10-27 14:43:30 -04:00
|
|
|
[ 0 [ first max ] reduce 3 /f ] keep
|
2013-03-06 22:58:53 -05:00
|
|
|
[ first-unsafe < ] with filter
|
2010-05-17 23:20:46 -04:00
|
|
|
values ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: complete ( full short -- score )
|
|
|
|
[ dupd fuzzy score ] 2keep
|
2008-03-29 21:36:58 -04:00
|
|
|
[ <reversed> ] bi@
|
2007-09-20 18:09:08 -04:00
|
|
|
dupd fuzzy score max ;
|
|
|
|
|
|
|
|
: completion ( short candidate -- result )
|
2013-03-06 22:58:53 -05:00
|
|
|
[ second swap complete ] keep 2array ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2010-08-13 22:28:44 -04:00
|
|
|
: completion, ( short candidate -- )
|
2013-03-06 22:58:53 -05:00
|
|
|
completion dup first-unsafe 0 > [ , ] [ drop ] if ;
|
2010-08-13 22:28:44 -04:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: completions ( short candidates -- seq )
|
2010-08-13 22:28:44 -04:00
|
|
|
[ ] [
|
2011-09-13 12:13:02 -04:00
|
|
|
[ [ completion, ] with each ] { } make
|
2010-08-13 22:28:44 -04:00
|
|
|
rank-completions
|
|
|
|
] bi-curry if-empty ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-01-12 20:25:18 -05:00
|
|
|
: name-completions ( str seq -- seq' )
|
|
|
|
[ dup name>> ] { } map>assoc completions ;
|
|
|
|
|
2009-01-09 18:58:13 -05:00
|
|
|
: words-matching ( str -- seq )
|
2009-01-12 20:25:18 -05:00
|
|
|
all-words name-completions ;
|
2009-01-09 18:58:13 -05:00
|
|
|
|
|
|
|
: vocabs-matching ( str -- seq )
|
2011-10-23 20:05:22 -04:00
|
|
|
all-vocabs-recursive filter-vocabs name-completions ;
|
2009-02-02 16:58:09 -05:00
|
|
|
|
|
|
|
: chars-matching ( str -- seq )
|
2012-07-15 16:57:05 -04:00
|
|
|
name-map keys dup zip completions ;
|
2010-08-13 22:28:44 -04:00
|
|
|
|
2012-09-24 18:06:54 -04:00
|
|
|
: colors-matching ( str -- seq )
|
|
|
|
named-colors dup zip completions ;
|
2012-09-24 23:22:29 -04:00
|
|
|
|
2013-03-07 17:09:23 -05:00
|
|
|
: strings-matching ( str seq -- seq' )
|
|
|
|
dup zip completions keys ;
|
|
|
|
|
2012-10-22 21:51:38 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2012-10-22 22:17:52 -04:00
|
|
|
: directory-paths ( directory -- alist )
|
2012-10-22 21:51:38 -04:00
|
|
|
dup '[
|
|
|
|
[
|
2012-10-22 22:17:52 -04:00
|
|
|
[ dup _ prepend-path ]
|
2012-10-22 21:51:38 -04:00
|
|
|
[ file-info directory? [ path-separator append ] when ]
|
2012-10-22 22:17:52 -04:00
|
|
|
bi swap
|
|
|
|
] { } map>assoc
|
2012-10-22 21:51:38 -04:00
|
|
|
] with-directory-files ;
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: paths-matching ( str -- seq )
|
2012-10-23 12:49:44 -04:00
|
|
|
dup last-path-separator [ 1 + cut ] [ drop "" ] if swap
|
2012-10-22 21:51:38 -04:00
|
|
|
dup { [ exists? ] [ file-info directory? ] } 1&&
|
2012-10-22 22:17:52 -04:00
|
|
|
[ directory-paths completions ] [ 2drop { } ] if ;
|
2012-09-25 12:10:44 -04:00
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: (complete-single-vocab?) ( str -- ? )
|
|
|
|
{ "IN:" "USE:" "UNUSE:" "QUALIFIED:" "QUALIFIED-WITH:" }
|
|
|
|
member? ; inline
|
|
|
|
|
|
|
|
: complete-single-vocab? ( tokens -- ? )
|
|
|
|
dup last empty? [
|
|
|
|
harvest ?last (complete-single-vocab?)
|
|
|
|
] [
|
|
|
|
harvest dup length 1 >
|
|
|
|
[ 2 tail* ?first (complete-single-vocab?) ] [ drop f ] if
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
: chop-; ( seq -- seq' )
|
|
|
|
{ ";" } split1-last [ ] [ ] ?if ;
|
|
|
|
|
|
|
|
: complete-vocab-list? ( tokens -- ? )
|
|
|
|
chop-; 1 short head* "USING:" swap member? ;
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: complete-vocab? ( tokens -- ? )
|
|
|
|
{ [ complete-single-vocab? ] [ complete-vocab-list? ] } 1|| ;
|
|
|
|
|
2012-10-22 21:29:53 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: complete-token? ( tokens token -- ? )
|
|
|
|
over last empty? [
|
|
|
|
[ harvest ?last ] [ = ] bi*
|
|
|
|
] [
|
|
|
|
swap harvest dup length 1 >
|
|
|
|
[ 2 tail* ?first = ] [ 2drop f ] if
|
2013-03-06 22:58:53 -05:00
|
|
|
] if ; inline
|
2012-10-22 21:29:53 -04:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
2013-03-20 19:51:09 -04:00
|
|
|
: complete-char? ( tokens -- ? ) "CHAR:" complete-token? ;
|
2012-09-25 12:10:44 -04:00
|
|
|
|
2013-03-20 19:51:09 -04:00
|
|
|
: complete-color? ( tokens -- ? ) "COLOR:" complete-token? ;
|
2012-10-22 21:51:38 -04:00
|
|
|
|
2013-03-20 19:18:30 -04:00
|
|
|
: complete-pathname? ( tokens -- ? ) "P\"" complete-token? ;
|