]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/tools/listener/completion/completion.factor
Updating code in basis and extra for stricter vocab search path semantics
[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 documents documents.elements fry kernel words sets splitting math
5 math.vectors models.delay models.arrow combinators.short-circuit
6 parser present sequences tools.completion help.vocabs generic fonts
7 definitions.icons ui.images ui.commands ui.operations ui.gadgets
8 ui.gadgets.editors ui.gadgets.glass ui.gadgets.scrollers
9 ui.gadgets.tables ui.gadgets.tracks ui.gadgets.labeled
10 ui.gadgets.worlds ui.gadgets.wrappers ui.gestures ui.pens.solid
11 ui.tools.listener.history combinators vocabs ui.tools.listener.popups ;
12 IN: ui.tools.listener.completion
13
14 ! We don't directly depend on the listener tool but we use a few slots
15 SLOT: interactor
16 SLOT: history
17
18 : history-list ( interactor -- alist )
19     history>> elements>>
20     [ dup string>> { { CHAR: \n CHAR: \s } } substitute ] { } map>assoc
21     <reversed> ;
22
23 TUPLE: word-completion manifest ;
24 C: <word-completion> word-completion
25
26 SINGLETONS: vocab-completion char-completion history-completion ;
27 UNION: definition-completion word-completion vocab-completion ;
28 UNION: listener-completion definition-completion char-completion history-completion ;
29
30 GENERIC: completion-quot ( interactor completion-mode -- quot )
31
32 : (completion-quot) ( interactor completion-mode quot -- quot' )
33     2nip '[ [ { } ] _ if-empty ] ; inline
34
35 M: word-completion completion-quot [ words-matching ] (completion-quot) ;
36 M: vocab-completion completion-quot [ vocabs-matching ] (completion-quot) ;
37 M: char-completion completion-quot [ chars-matching ] (completion-quot) ;
38 M: history-completion completion-quot drop '[ drop _ history-list ] ;
39
40 GENERIC: completion-element ( completion-mode -- element )
41
42 M: object completion-element drop word-start-elt ;
43 M: history-completion completion-element drop one-line-elt ;
44
45 GENERIC: completion-banner ( completion-mode -- string )
46
47 M: word-completion completion-banner drop "Words" ;
48 M: vocab-completion completion-banner drop "Vocabularies" ;
49 M: char-completion completion-banner drop "Unicode code point names" ;
50 M: history-completion completion-banner drop "Input history" ;
51
52 ! Completion modes also implement the row renderer protocol
53 M: listener-completion row-columns drop present 1array ;
54
55 M: definition-completion prototype-row
56     drop \ + definition-icon <image-name> "" 2array ;
57
58 M: definition-completion row-columns
59     drop
60     [ definition-icon <image-name> ]
61     [ present ] bi
62     2array ;
63
64 M: word-completion row-color
65     [ vocabulary>> ] [ manifest>> ] bi* {
66         { [ 2dup search-vocabs>> memq? ] [ COLOR: black ] }
67         { [ over ".private" tail? ] [ COLOR: dark-red ] }
68         [ COLOR: dark-gray ]
69     } cond 2nip ;
70
71 M: vocab-completion row-color
72     drop vocab? COLOR: black COLOR: dark-gray ? ;
73
74 : complete-IN:/USE:? ( tokens -- ? )
75     1 short head* 2 short tail* { "IN:" "USE:" } intersects? ;
76
77 : chop-; ( seq -- seq' )
78     { ";" } split1-last [ ] [ ] ?if ;
79
80 : complete-USING:? ( tokens -- ? )
81     chop-; 1 short head* { "USING:" } intersects? ;
82
83 : complete-CHAR:? ( tokens -- ? )
84     2 short tail* "CHAR:" swap member? ;
85
86 : up-to-caret ( caret document -- string )
87     [ { 0 0 } ] 2dip doc-range ;
88
89 : completion-mode ( interactor -- symbol )
90     [ manifest>> ] [ editor-caret ] [ model>> ] tri up-to-caret " \r\n" split
91     {
92         { [ dup { [ complete-IN:/USE:? ] [ complete-USING:? ] } 1|| ] [ 2drop vocab-completion ] }
93         { [ dup complete-CHAR:? ] [ 2drop char-completion ] }
94         [ drop <word-completion> ]
95     } cond ;
96
97 TUPLE: completion-popup < track interactor table completion-mode ;
98
99 : find-completion-popup ( gadget -- popup )
100     [ completion-popup? ] find-parent ;
101
102 : <completion-model> ( editor element quot -- model )
103     [ <element-model> 1/3 seconds <delay> ] dip
104     '[ @ keys 1000 short head ] <arrow> ;
105
106 M: completion-popup focusable-child* table>> ;
107
108 : completion-loc/doc/elt ( popup -- loc doc elt )
109     [ interactor>> [ editor-caret ] [ model>> ] bi ]
110     [ completion-mode>> completion-element ]
111     bi ;
112
113 GENERIC: completion-string ( object -- string )
114
115 M: object completion-string present ;
116
117 : method-completion-string ( word -- string )
118     "method-generic" word-prop present ;
119
120 M: method-body completion-string method-completion-string ;
121
122 GENERIC# accept-completion-hook 1 ( item popup -- )
123
124 : insert-completion ( item popup -- )
125     [ completion-string ] [ completion-loc/doc/elt ] bi* set-elt-string ;
126
127 : accept-completion ( item table -- )
128     find-completion-popup
129     [ insert-completion ]
130     [ accept-completion-hook ]
131     [ nip hide-glass ]
132     2tri ;
133
134 : <completion-table> ( interactor completion-mode -- table )
135     [ completion-element ] [ completion-quot ] [ nip ] 2tri
136     [ <completion-model> ] dip <table>
137         monospace-font >>font
138         t >>selection-required?
139         t >>single-click?
140         30 >>min-cols
141         10 >>min-rows
142         10 >>max-rows
143         dup '[ _ accept-completion ] >>action ;
144
145 : <completion-scroller> ( completion-popup -- scroller )
146     table>> <scroller> COLOR: white <solid> >>interior ;
147
148 : <completion-popup> ( interactor completion-mode -- popup )
149     [ vertical completion-popup new-track ] 2dip
150     [ [ >>interactor ] [ >>completion-mode ] bi* ] [ <completion-table> >>table ] 2bi
151     dup [ <completion-scroller> ] [ completion-mode>> completion-banner ] bi
152     <labeled-gadget> 1 track-add ;
153
154 completion-popup H{
155     { T{ key-down f f "TAB" } [ table>> row-action ] }
156     { T{ key-down f f " " } [ table>> row-action ] }
157 } set-gestures
158
159 : show-completion-popup ( interactor mode -- )
160     [ completion-element ] [ <completion-popup> ] 2bi
161     show-listener-popup ;
162
163 : code-completion-popup ( interactor -- )
164     dup completion-mode show-completion-popup ;
165
166 : history-completion-popup ( interactor -- )
167     history-completion show-completion-popup ;
168
169 : recall-previous ( interactor -- )
170     history>> history-recall-previous ;
171
172 : recall-next ( interactor -- )
173     history>> history-recall-next ;
174
175 : completion-gesture ( gesture completion -- value/f operation/f )
176     table>> selected-row
177     [ [ nip ] [ gesture>operation ] 2bi ] [ drop f ] if ;
178
179 M: completion-popup handle-gesture ( gesture completion -- ? )
180     2dup completion-gesture dup [
181         [ nip hide-glass ] [ invoke-command ] 2bi* f
182     ] [ 2drop call-next-method ] if ;