]> gitweb.factorcode.org Git - factor.git/blob - basis/prettyprint/sections/sections.factor
Move make to its own vocabulary, remove fry _ feature
[factor.git] / basis / prettyprint / sections / sections.factor
1 ! Copyright (C) 2003, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays generic hashtables io kernel math assocs
4 namespaces make sequences strings io.styles vectors words
5 prettyprint.config splitting classes continuations
6 io.streams.nested accessors sets ;
7 IN: prettyprint.sections
8
9 ! State
10 SYMBOL: position
11 SYMBOL: recursion-check
12 SYMBOL: pprinter-stack
13
14 ! We record vocabs of all words
15 SYMBOL: pprinter-in
16 SYMBOL: pprinter-use
17
18 TUPLE: pprinter last-newline line-count indent ;
19
20 : <pprinter> ( -- pprinter ) 0 1 0 pprinter boa ;
21
22 : record-vocab ( word -- )
23     vocabulary>> [ pprinter-use get conjoin ] when* ;
24
25 ! Utility words
26 : line-limit? ( -- ? )
27     line-limit get dup [ pprinter get line-count>> <= ] when ;
28
29 : do-indent ( -- ) pprinter get indent>> CHAR: \s <string> write ;
30
31 : fresh-line ( n -- )
32     dup pprinter get last-newline>> = [
33         drop
34     ] [
35         pprinter get (>>last-newline)
36         line-limit? [
37             "..." write pprinter get return
38         ] when
39         pprinter get [ 1+ ] change-line-count drop
40         nl do-indent
41     ] if ;
42
43 : text-fits? ( len -- ? )
44     margin get dup zero?
45     [ 2drop t ] [ >r pprinter get indent>> + r> <= ] if ;
46
47 ! break only if position margin 2 / >
48 SYMBOL: soft
49
50 ! always breaks
51 SYMBOL: hard
52
53 ! Section protocol
54 GENERIC: section-fits? ( section -- ? )
55
56 GENERIC: short-section ( section -- )
57
58 GENERIC: long-section ( section -- )
59
60 GENERIC: indent-section? ( section -- ? )
61
62 GENERIC: unindent-first-line? ( section -- ? )
63
64 GENERIC: newline-after? ( section -- ? )
65
66 GENERIC: short-section? ( section -- ? )
67
68 ! Sections
69 TUPLE: section
70 start end
71 start-group? end-group?
72 style overhang ;
73
74 : new-section ( length class -- section )
75     new
76         position get >>start
77         swap position [ + ] change
78         position get >>end
79         0 >>overhang ; inline
80
81 M: section section-fits? ( section -- ? )
82     [ end>> pprinter get last-newline>> - ]
83     [ overhang>> ] bi
84     + text-fits? ;
85
86 M: section indent-section? drop f ;
87
88 M: section unindent-first-line? drop f ;
89
90 M: section newline-after? drop f ;
91
92 M: object short-section? section-fits? ;
93
94 : indent+ ( section n -- )
95     swap indent-section? [
96         pprinter get [ + ] change-indent drop
97     ] [ drop ] if ;
98
99 : <indent ( section -- ) tab-size get indent+ ;
100
101 : indent> ( section -- ) tab-size get neg indent+ ;
102
103 : <fresh-line ( section -- )
104     start>> fresh-line ;
105
106 : fresh-line> ( section -- )
107     dup newline-after? [ end>> fresh-line ] [ drop ] if ;
108
109 : <long-section ( section -- )
110     dup unindent-first-line?
111     [ dup <fresh-line <indent ] [ dup <indent <fresh-line ] if ;
112
113 : long-section> ( section -- )
114     dup indent> fresh-line> ;
115
116 : pprint-section ( section -- )
117     dup short-section? [
118         dup style>> [ short-section ] with-style
119     ] [
120         [ <long-section ]
121         [ dup style>> [ long-section ] with-style ]
122         [ long-section> ]
123         tri
124     ] if ;
125
126 ! Break section
127 TUPLE: line-break < section type ;
128
129 : <line-break> ( type -- section )
130     0 \ line-break new-section
131         swap >>type ;
132
133 M: line-break short-section drop ;
134
135 M: line-break long-section drop ;
136
137 ! Block sections
138 TUPLE: block < section sections ;
139
140 : new-block ( style class -- block )
141     0 swap new-section
142         V{ } clone >>sections
143         swap >>style ; inline
144
145 : <block> ( style -- block )
146     block new-block ;
147
148 : pprinter-block ( -- block ) pprinter-stack get peek ;
149
150 : add-section ( section -- )
151     pprinter-block sections>> push ;
152
153 : last-section ( -- section )
154     pprinter-block sections>>
155     [ line-break? not ] find-last nip ;
156
157 : start-group ( -- )
158     last-section t >>start-group? drop ;
159
160 : end-group ( -- )
161     last-section t >>end-group? drop ;
162
163 : advance ( section -- )
164     [ start>> pprinter get last-newline>> = not ]
165     [ short-section? ] bi
166     and [ bl ] when ;
167
168 : line-break ( type -- ) [ <line-break> add-section ] when* ;
169
170 M: block section-fits? ( section -- ? )
171     line-limit? [ drop t ] [ call-next-method ] if ;
172
173 : pprint-sections ( block advancer -- )
174     [
175         sections>> [ line-break? not ] filter
176         unclip-slice pprint-section
177     ] dip
178     [ [ pprint-section ] bi ] curry each ; inline
179
180 M: block short-section ( block -- )
181     [ advance ] pprint-sections ;
182
183 : do-break ( break -- )
184     [ ]
185     [ type>> hard eq? ]
186     [ end>> pprinter get last-newline>> - margin get 2/ > ] tri
187     or [ <fresh-line ] [ drop ] if ;
188
189 : empty-block? ( block -- ? ) sections>> empty? ;
190
191 : if-nonempty ( block quot -- )
192     >r dup empty-block? [ drop ] r> if ; inline
193
194 : (<block) ( block -- ) pprinter-stack get push ;
195
196 : <block ( -- ) f <block> (<block) ;
197
198 : <object ( obj -- ) presented associate <block> (<block) ;
199
200 ! Text section
201 TUPLE: text < section string ;
202
203 : <text> ( string style -- text )
204     over length 1+ \ text new-section
205         swap >>style
206         swap >>string ;
207
208 M: text short-section string>> write ;
209
210 M: text long-section short-section ;
211
212 : styled-text ( string style -- ) <text> add-section ;
213
214 : text ( string -- ) H{ } styled-text ;
215
216 ! Inset section
217 TUPLE: inset < block narrow? ;
218
219 : <inset> ( narrow? -- block )
220     H{ } inset new-block
221         2 >>overhang
222         swap >>narrow? ;
223
224 M: inset long-section
225     dup narrow?>> [
226         [ <fresh-line ] pprint-sections
227     ] [
228         call-next-method
229     ] if ;
230
231 M: inset indent-section? drop t ;
232
233 M: inset newline-after? drop t ;
234
235 : <inset ( narrow? -- ) <inset> (<block) ;
236
237 ! Flow section
238 TUPLE: flow < block ;
239
240 : <flow> ( -- block )
241     H{ } flow new-block ;
242
243 M: flow short-section? ( section -- ? )
244     #! If we can make room for this entire block by inserting
245     #! a newline, do it; otherwise, don't bother, print it as
246     #! a short section
247     [ section-fits? ]
248     [ [ end>> ] [ start>> ] bi - text-fits? not ] bi
249     or ;
250
251 : <flow ( -- ) <flow> (<block) ;
252
253 ! Colon definition section
254 TUPLE: colon < block ;
255
256 : <colon> ( -- block )
257     H{ } colon new-block ;
258
259 M: colon long-section short-section ;
260
261 M: colon indent-section? drop t ;
262
263 M: colon unindent-first-line? drop t ;
264
265 : <colon ( -- ) <colon> (<block) ;
266
267 : save-end-position ( block -- )
268     position get >>end drop ;
269
270 : block> ( -- )
271     pprinter-stack get pop
272     [ [ save-end-position ] [ add-section ] bi ] if-nonempty ;
273
274 : do-pprint ( block -- )
275     <pprinter> pprinter [
276         [
277             dup style>> [
278                 [
279                     short-section
280                 ] curry with-return
281             ] with-nesting
282         ] if-nonempty
283     ] with-variable ;
284
285 ! Long section layout algorithm
286 : chop-break ( seq -- seq )
287     dup peek line-break? [ but-last-slice chop-break ] when ;
288
289 SYMBOL: prev
290 SYMBOL: next
291
292 : split-groups ( ? -- ) [ t , ] when ;
293
294 : split-before ( section -- )
295     [ start-group?>> prev get [ end-group?>> ] [ t ] if* and ]
296     [ flow? prev get flow? not and ]
297     bi or split-groups ;
298
299 : split-after ( section -- )
300     [ end-group?>> ] [ f ] if* split-groups ;
301
302 : group-flow ( seq -- newseq )
303     [
304         dup length [
305             2dup 1- swap ?nth prev set
306             2dup 1+ swap ?nth next set
307             swap nth dup split-before dup , split-after
308         ] with each
309     ] { } make { t } split harvest ;
310
311 : break-group? ( seq -- ? )
312     [ first section-fits? ] [ peek section-fits? not ] bi and ;
313
314 : ?break-group ( seq -- )
315     dup break-group? [ first <fresh-line ] [ drop ] if ;
316
317 M: block long-section ( block -- )
318     [
319         sections>> chop-break group-flow [
320             dup ?break-group [
321                 dup line-break? [
322                     do-break
323                 ] [
324                     [ advance ] [ pprint-section ] bi
325                 ] if
326             ] each
327         ] each
328     ] if-nonempty ;