]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/tools/listener/completion/completion.factor
f3a423e6d955c8f9182d5557806a32df8d108005
[factor.git] / basis / ui / tools / listener / completion / completion.factor
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
15
16 ! We don't directly depend on the listener tool but we use a few slots
17 SLOT: interactor
18 SLOT: history
19
20 : history-list ( interactor -- alist )
21     history>> elements>>
22     [ dup string>> H{ { CHAR: \n CHAR: \s } } substitute ] { } map>assoc
23     <reversed> ;
24
25 : history-completions ( short interactor -- seq )
26     history-list over empty? [ nip ] [ members completions ] if ;
27
28 TUPLE: word-completion manifest ;
29 C: <word-completion> word-completion
30
31 TUPLE: vocab-word-completion vocab-name ;
32 C: <vocab-word-completion> vocab-word-completion
33
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 ;
41
42 GENERIC: completion-quot ( interactor completion-mode -- quot )
43
44 : (completion-quot) ( interactor completion-mode quot -- quot' )
45     2nip '[ [ { } ] _ if-empty ] ; inline
46
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 ] ;
54
55 GENERIC: completion-element ( completion-mode -- element )
56
57 M: object completion-element drop word-start-elt ;
58 M: history-completion completion-element drop one-line-elt ;
59
60 GENERIC: completion-banner ( completion-mode -- string )
61
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" ;
69
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 ;
73
74 M: char-completion row-columns
75     drop first [ name-map at 1string ] keep 2array ;
76
77 M: definition-completion prototype-row
78     drop \ + definition-icon <image-name> "" 2array ;
79
80 M: definition-completion row-columns
81     drop first2 [ definition-icon <image-name> ] dip 2array ;
82
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 ] }
88         [ COLOR: dark-gray ]
89     } cond 2nip ;
90
91 M: vocab-word-completion row-color 2drop COLOR: black ;
92
93 M: vocab-completion row-color
94     drop dup vocab? [
95         name>> ".private" tail? COLOR: dark-red text-color ?
96     ] [ drop COLOR: dark-gray ] if ;
97
98 M: color-completion row-color
99     drop second lookup-color ;
100
101 : up-to-caret ( caret document -- string )
102     [ { 0 0 } ] 2dip doc-range ;
103
104 : completion-mode ( interactor -- symbol )
105     [ manifest>> ] [ editor-caret ] [ model>> ] tri up-to-caret " \r\n" split
106     {
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> ]
113     } cond ;
114
115 TUPLE: completion-popup < track interactor table completion-mode ;
116
117 : find-completion-popup ( gadget -- popup )
118     [ completion-popup? ] find-parent ;
119
120 : <completion-model> ( editor element quot -- model )
121     [ <element-model> 1/3 seconds <delay> ] dip
122     '[ @ >alist 1000 short head ] <arrow> ;
123
124 M: completion-popup focusable-child* table>> ;
125
126 : completion-loc/doc/elt ( popup -- loc doc elt )
127     [ interactor>> [ editor-caret ] [ model>> ] bi ]
128     [ completion-mode>> completion-element ]
129     bi ;
130
131 GENERIC#: accept-completion-hook 1 ( item popup -- )
132
133 : insert-completion ( item popup -- )
134     completion-loc/doc/elt set-elt-string ;
135
136 : accept-completion ( item table -- )
137     find-completion-popup
138     [ [ second ] dip insert-completion ]
139     [ [ first ] dip accept-completion-hook ]
140     2bi ;
141
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?
147         t >>single-click?
148         30 >>min-cols
149         10 >>min-rows
150         10 >>max-rows
151         dup '[ _ accept-completion ] >>action
152         [ hide-glass ] >>hook ;
153
154 : <completion-scroller> ( completion-popup -- scroller )
155     table>> <scroller> white-interior ;
156
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 ;
162
163 completion-popup H{
164     { T{ key-down f f "TAB" } [ table>> row-action ] }
165     { T{ key-down f f " " } [ table>> row-action ] }
166 } set-gestures
167
168 : show-completion-popup ( interactor mode -- )
169     [ completion-element ] [ <completion-popup> ] 2bi
170     show-listener-popup ;
171
172 : code-completion-popup ( interactor -- )
173     dup completion-mode show-completion-popup ;
174
175 : history-completion-popup ( interactor -- )
176     history-completion show-completion-popup ;
177
178 : recall-previous ( interactor -- )
179     history>> history-recall-previous ;
180
181 : recall-next ( interactor -- )
182     history>> history-recall-next ;
183
184 : ?check-popup ( interactor -- interactor )
185     dup popup>> [
186         gadget-child dup completion-popup? [
187             completion-mode>> dup code-completion? [
188                 over completion-mode =
189                 [ dup popup>> hide-glass ] unless
190             ] [ drop ] if
191         ] [ drop ] if
192     ] when* ;