1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs calendar colors colors.constants
4 combinators combinators.short-circuit definitions.icons
5 documents documents.elements fonts fry generic help.vocabs
6 kernel math math.vectors models.arrow models.delay parser
7 present sequences sets splitting tools.completion ui.commands
8 ui.gadgets ui.gadgets.editors ui.gadgets.glass
9 ui.gadgets.labeled ui.gadgets.scrollers ui.gadgets.tables
10 ui.gadgets.tracks ui.gadgets.worlds ui.gadgets.wrappers
11 ui.gestures ui.images ui.operations ui.pens.solid ui.theme
12 ui.theme.images ui.tools.common ui.tools.listener.history
13 ui.tools.listener.popups vocabs words ;
14 IN: ui.tools.listener.completion
16 ! We don't directly depend on the listener tool but we use a few slots
20 : history-list ( interactor -- alist )
22 [ dup string>> H{ { CHAR: \n CHAR: \s } } substitute ] { } map>assoc
25 : history-completions ( short interactor -- seq )
26 history-list over empty? [ nip ] [ members completions ] if ;
28 TUPLE: word-completion manifest ;
29 C: <word-completion> word-completion
31 TUPLE: vocab-word-completion vocab-name ;
32 C: <vocab-word-completion> vocab-word-completion
34 SINGLETONS: vocab-completion color-completion char-completion
35 path-completion history-completion ;
36 UNION: definition-completion word-completion
37 vocab-word-completion vocab-completion ;
38 UNION: code-completion definition-completion
39 color-completion char-completion path-completion ;
40 UNION: listener-completion code-completion history-completion ;
42 GENERIC: completion-quot ( interactor completion-mode -- quot )
44 : (completion-quot) ( interactor completion-mode quot -- quot' )
45 2nip '[ [ { } ] _ if-empty ] ; inline
47 M: word-completion completion-quot [ words-matching ] (completion-quot) ;
48 M: vocab-word-completion completion-quot nip vocab-name>> '[ _ vocab-words-matching ] ;
49 M: vocab-completion completion-quot [ vocabs-matching ] (completion-quot) ;
50 M: color-completion completion-quot [ colors-matching ] (completion-quot) ;
51 M: char-completion completion-quot [ chars-matching ] (completion-quot) ;
52 M: path-completion completion-quot [ paths-matching ] (completion-quot) ;
53 M: history-completion completion-quot drop '[ _ history-completions ] ;
55 GENERIC: completion-element ( completion-mode -- element )
57 M: object completion-element drop word-start-elt ;
58 M: history-completion completion-element drop one-line-elt ;
60 GENERIC: completion-banner ( completion-mode -- string )
62 M: word-completion completion-banner drop "Words" ;
63 M: vocab-word-completion completion-banner drop "Words" ;
64 M: vocab-completion completion-banner drop "Vocabularies" ;
65 M: color-completion completion-banner drop "Colors" ;
66 M: char-completion completion-banner drop "Unicode code point names" ;
67 M: path-completion completion-banner drop "Paths" ;
68 M: history-completion completion-banner drop "Input history" ;
70 ! Completion modes also implement the row renderer protocol
71 M: listener-completion row-columns drop present 1array ;
73 M: definition-completion prototype-row
74 drop \ + definition-icon <image-name> "" 2array ;
76 M: definition-completion row-columns
78 [ definition-icon <image-name> ]
82 M: word-completion row-color
83 [ vocabulary>> ] [ manifest>> ] bi* {
84 { [ dup not ] [ COLOR: black ] }
85 { [ 2dup search-vocab-names>> in? ] [ COLOR: black ] }
86 { [ over ".private" tail? ] [ COLOR: dark-red ] }
90 M: vocab-word-completion row-color 2drop COLOR: black ;
92 M: vocab-completion row-color
94 name>> ".private" tail? COLOR: dark-red COLOR: black ?
95 ] [ drop COLOR: dark-gray ] if ;
97 M: color-completion row-color
100 : up-to-caret ( caret document -- string )
101 [ { 0 0 } ] 2dip doc-range ;
103 : completion-mode ( interactor -- symbol )
104 [ manifest>> ] [ editor-caret ] [ model>> ] tri up-to-caret " \r\n" split
106 { [ dup complete-vocab? ] [ 2drop vocab-completion ] }
107 { [ dup complete-char? ] [ 2drop char-completion ] }
108 { [ dup complete-color? ] [ 2drop color-completion ] }
109 { [ dup complete-pathname? ] [ 2drop path-completion ] }
110 { [ dup complete-vocab-words? ] [ nip harvest second <vocab-word-completion> ] }
111 [ drop <word-completion> ]
114 TUPLE: completion-popup < track interactor table completion-mode ;
116 : find-completion-popup ( gadget -- popup )
117 [ completion-popup? ] find-parent ;
119 : <completion-model> ( editor element quot -- model )
120 [ <element-model> 1/3 seconds <delay> ] dip
121 '[ @ keys 1000 short head ] <arrow> ;
123 M: completion-popup focusable-child* table>> ;
125 : completion-loc/doc/elt ( popup -- loc doc elt )
126 [ interactor>> [ editor-caret ] [ model>> ] bi ]
127 [ completion-mode>> completion-element ]
130 GENERIC: completion-string ( object -- string )
132 M: object completion-string present ;
134 : method-completion-string ( word -- string )
135 "method-generic" word-prop present ;
137 M: method completion-string method-completion-string ;
139 GENERIC#: accept-completion-hook 1 ( item popup -- )
141 : insert-completion ( item popup -- )
142 [ completion-string ] [ completion-loc/doc/elt ] bi* set-elt-string ;
144 : accept-completion ( item table -- )
145 find-completion-popup
146 [ insert-completion ]
147 [ accept-completion-hook ]
150 : <completion-table> ( interactor completion-mode -- table )
151 [ completion-element ] [ completion-quot ] [ nip ] 2tri
152 [ <completion-model> ] dip <table>
153 monospace-font >>font
154 t >>selection-required?
159 dup '[ _ accept-completion ] >>action
160 [ hide-glass ] >>hook ;
162 : <completion-scroller> ( completion-popup -- scroller )
163 table>> <scroller> white-interior ;
165 : <completion-popup> ( interactor completion-mode -- popup )
166 [ vertical completion-popup new-track ] 2dip
167 [ [ >>interactor ] [ >>completion-mode ] bi* ] [ <completion-table> >>table ] 2bi
168 dup [ <completion-scroller> ] [ completion-mode>> completion-banner ] bi
169 completion-color <framed-labeled-gadget> 1 track-add ;
172 { T{ key-down f f "TAB" } [ table>> row-action ] }
173 { T{ key-down f f " " } [ table>> row-action ] }
176 : show-completion-popup ( interactor mode -- )
177 [ completion-element ] [ <completion-popup> ] 2bi
178 show-listener-popup ;
180 : code-completion-popup ( interactor -- )
181 dup completion-mode show-completion-popup ;
183 : history-completion-popup ( interactor -- )
184 history-completion show-completion-popup ;
186 : recall-previous ( interactor -- )
187 history>> history-recall-previous ;
189 : recall-next ( interactor -- )
190 history>> history-recall-next ;
192 : ?check-popup ( interactor -- interactor )
194 gadget-child dup completion-popup? [
195 completion-mode>> dup code-completion? [
196 over completion-mode =
197 [ dup popup>> hide-glass ] unless