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 strings tools.completion
8 ui.commands 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 unicode.data 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 second 1array ;
72 M: listener-completion row-summary drop first ;
74 M: char-completion row-columns
75 drop first [ name-map at 1string ] keep 2array ;
77 M: definition-completion prototype-row
78 drop \ + definition-icon <image-name> "" 2array ;
80 M: definition-completion row-columns
81 drop first2 [ definition-icon <image-name> ] dip 2array ;
83 M: word-completion row-color
84 [ first vocabulary>> ] [ manifest>> ] bi* {
85 { [ dup not ] [ text-color ] }
86 { [ 2dup search-vocab-names>> in? ] [ text-color ] }
87 { [ over ".private" tail? ] [ COLOR: dark-red ] }
91 M: vocab-word-completion row-color 2drop COLOR: black ;
93 M: vocab-completion row-color
95 name>> ".private" tail? COLOR: dark-red text-color ?
96 ] [ drop COLOR: dark-gray ] if ;
98 M: color-completion row-color
99 drop second lookup-color ;
101 : up-to-caret ( caret document -- string )
102 [ { 0 0 } ] 2dip doc-range ;
104 : completion-mode ( interactor -- symbol )
105 [ manifest>> ] [ editor-caret ] [ model>> ] tri up-to-caret " \r\n" split
107 { [ dup complete-vocab? ] [ 2drop vocab-completion ] }
108 { [ dup complete-char? ] [ 2drop char-completion ] }
109 { [ dup complete-color? ] [ 2drop color-completion ] }
110 { [ dup complete-pathname? ] [ 2drop path-completion ] }
111 { [ dup complete-vocab-words? ] [ nip harvest second <vocab-word-completion> ] }
112 [ drop <word-completion> ]
115 TUPLE: completion-popup < track interactor table completion-mode ;
117 : find-completion-popup ( gadget -- popup )
118 [ completion-popup? ] find-parent ;
120 : <completion-model> ( editor element quot -- model )
121 [ <element-model> 1/3 seconds <delay> ] dip
122 '[ @ >alist 1000 short head ] <arrow> ;
124 M: completion-popup focusable-child* table>> ;
126 : completion-loc/doc/elt ( popup -- loc doc elt )
127 [ interactor>> [ editor-caret ] [ model>> ] bi ]
128 [ completion-mode>> completion-element ]
131 GENERIC#: accept-completion-hook 1 ( item popup -- )
133 : insert-completion ( item popup -- )
134 completion-loc/doc/elt set-elt-string ;
136 : accept-completion ( item table -- )
137 find-completion-popup
138 [ [ second ] dip insert-completion ]
139 [ [ first ] dip accept-completion-hook ]
142 : <completion-table> ( interactor completion-mode -- table )
143 [ completion-element ] [ completion-quot ] [ nip ] 2tri
144 [ <completion-model> ] dip <table>
145 monospace-font >>font
146 t >>selection-required?
151 dup '[ _ accept-completion ] >>action
152 [ hide-glass ] >>hook ;
154 : <completion-scroller> ( completion-popup -- scroller )
155 table>> <scroller> white-interior ;
157 : <completion-popup> ( interactor completion-mode -- popup )
158 [ vertical completion-popup new-track ] 2dip
159 [ [ >>interactor ] [ >>completion-mode ] bi* ] [ <completion-table> >>table ] 2bi
160 dup [ <completion-scroller> ] [ completion-mode>> completion-banner ] bi
161 completion-color <framed-labeled-gadget> 1 track-add ;
164 { T{ key-down f f "TAB" } [ table>> row-action ] }
165 { T{ key-down f f " " } [ table>> row-action ] }
168 : show-completion-popup ( interactor mode -- )
169 [ completion-element ] [ <completion-popup> ] 2bi
170 show-listener-popup ;
172 : code-completion-popup ( interactor -- )
173 dup completion-mode show-completion-popup ;
175 : history-completion-popup ( interactor -- )
176 history-completion show-completion-popup ;
178 : recall-previous ( interactor -- )
179 history>> history-recall-previous ;
181 : recall-next ( interactor -- )
182 history>> history-recall-next ;
184 : ?check-popup ( interactor -- interactor )
186 gadget-child dup completion-popup? [
187 completion-mode>> dup code-completion? [
188 over completion-mode =
189 [ dup popup>> hide-glass ] unless