1 ! Copyright (C) 2003, 2009 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 accessors sets vocabs.parser combinators vocabs ;
7 IN: prettyprint.sections
11 SYMBOL: recursion-check
12 SYMBOL: pprinter-stack
14 ! We record vocabs of all words
18 TUPLE: pprinter last-newline line-count indent ;
20 : <pprinter> ( -- pprinter ) 0 1 0 pprinter boa ;
22 : (record-vocab) ( vocab -- )
23 dup pprinter-in get dup [ vocab-name ] when =
24 [ drop ] [ pprinter-use get conjoin ] if ;
26 : record-vocab ( word -- )
34 : line-limit? ( -- ? )
35 line-limit get dup [ pprinter get line-count>> <= ] when ;
37 : do-indent ( -- ) pprinter get indent>> CHAR: \s <string> write ;
40 dup pprinter get last-newline>> = [
43 pprinter get (>>last-newline)
45 "..." write pprinter get return
47 pprinter get [ 1+ ] change-line-count drop
51 : text-fits? ( len -- ? )
53 [ 2drop t ] [ [ pprinter get indent>> + ] dip <= ] if ;
55 ! break only if position margin 2 / >
62 GENERIC: section-fits? ( section -- ? )
64 GENERIC: short-section ( section -- )
66 GENERIC: long-section ( section -- )
68 GENERIC: indent-section? ( section -- ? )
70 GENERIC: unindent-first-line? ( section -- ? )
72 GENERIC: newline-after? ( section -- ? )
74 GENERIC: short-section? ( section -- ? )
79 start-group? end-group?
82 : new-section ( length class -- section )
85 swap position [ + ] change
89 M: section section-fits? ( section -- ? )
90 [ end>> pprinter get last-newline>> - ]
94 M: section indent-section? drop f ;
96 M: section unindent-first-line? drop f ;
98 M: section newline-after? drop f ;
100 M: object short-section? section-fits? ;
102 : indent+ ( section n -- )
103 swap indent-section? [
104 pprinter get [ + ] change-indent drop
107 : <indent ( section -- ) tab-size get indent+ ;
109 : indent> ( section -- ) tab-size get neg indent+ ;
111 : <fresh-line ( section -- )
114 : fresh-line> ( section -- )
115 dup newline-after? [ end>> fresh-line ] [ drop ] if ;
117 : <long-section ( section -- )
118 dup unindent-first-line?
119 [ dup <fresh-line <indent ] [ dup <indent <fresh-line ] if ;
121 : long-section> ( section -- )
122 dup indent> fresh-line> ;
124 : pprint-section ( section -- )
126 dup style>> [ short-section ] with-style
129 [ dup style>> [ long-section ] with-style ]
135 TUPLE: line-break < section type ;
137 : <line-break> ( type -- section )
138 0 \ line-break new-section
141 M: line-break short-section drop ;
143 M: line-break long-section drop ;
146 TUPLE: block < section sections ;
148 : new-block ( style class -- block )
150 V{ } clone >>sections
151 swap >>style ; inline
153 : <block> ( style -- block )
156 : pprinter-block ( -- block ) pprinter-stack get last ;
158 : add-section ( section -- )
159 pprinter-block sections>> push ;
161 : last-section ( -- section )
162 pprinter-block sections>>
163 [ line-break? not ] find-last nip ;
166 last-section t >>start-group? drop ;
169 last-section t >>end-group? drop ;
171 : advance ( section -- )
172 [ start>> pprinter get last-newline>> = not ]
173 [ short-section? ] bi
176 : line-break ( type -- ) [ <line-break> add-section ] when* ;
178 M: block section-fits? ( section -- ? )
179 line-limit? [ drop t ] [ call-next-method ] if ;
181 : pprint-sections ( block advancer -- )
183 sections>> [ line-break? not ] filter
184 unclip-slice pprint-section
186 [ [ pprint-section ] bi ] curry each ; inline
188 M: block short-section ( block -- )
189 [ advance ] pprint-sections ;
191 : do-break ( break -- )
194 [ end>> pprinter get last-newline>> - margin get 2/ > ] tri
195 or [ <fresh-line ] [ drop ] if ;
197 : empty-block? ( block -- ? ) sections>> empty? ;
199 : if-nonempty ( block quot -- )
200 [ dup empty-block? [ drop ] ] dip if ; inline
202 : (<block) ( block -- ) pprinter-stack get push ;
204 : <block ( -- ) f <block> (<block) ;
206 : <object ( obj -- ) presented associate <block> (<block) ;
209 TUPLE: text < section string ;
211 : <text> ( string style -- text )
212 over length 1+ \ text new-section
216 M: text short-section string>> write ;
218 M: text long-section short-section ;
220 : styled-text ( string style -- ) <text> add-section ;
222 : text ( string -- ) H{ } styled-text ;
225 TUPLE: inset < block narrow? ;
227 : <inset> ( narrow? -- block )
232 M: inset long-section
234 [ <fresh-line ] pprint-sections
239 M: inset indent-section? drop t ;
241 M: inset newline-after? drop t ;
243 : <inset ( narrow? -- ) <inset> (<block) ;
246 TUPLE: flow < block ;
248 : <flow> ( -- block )
249 H{ } flow new-block ;
251 M: flow short-section? ( section -- ? )
252 #! If we can make room for this entire block by inserting
253 #! a newline, do it; otherwise, don't bother, print it as
256 [ [ end>> ] [ start>> ] bi - text-fits? not ] bi
259 : <flow ( -- ) <flow> (<block) ;
261 ! Colon definition section
262 TUPLE: colon < block ;
264 : <colon> ( -- block )
265 H{ } colon new-block ;
267 M: colon long-section short-section ;
269 M: colon indent-section? drop t ;
271 M: colon unindent-first-line? drop t ;
273 : <colon ( -- ) <colon> (<block) ;
275 : save-end-position ( block -- )
276 position get >>end drop ;
279 pprinter-stack get pop
280 [ [ save-end-position ] [ add-section ] bi ] if-nonempty ;
282 : do-pprint ( block -- )
283 <pprinter> pprinter [
293 ! Long section layout algorithm
294 : chop-break ( seq -- seq )
295 dup last line-break? [ but-last-slice chop-break ] when ;
300 : split-groups ( ? -- ) [ t , ] when ;
302 : split-before ( section -- )
303 [ start-group?>> prev get [ end-group?>> ] [ t ] if* and ]
304 [ flow? prev get flow? not and ]
307 : split-after ( section -- )
308 [ end-group?>> ] [ f ] if* split-groups ;
310 : group-flow ( seq -- newseq )
313 2dup 1- swap ?nth prev set
314 2dup 1+ swap ?nth next set
315 swap nth dup split-before dup , split-after
317 ] { } make { t } split harvest ;
319 : break-group? ( seq -- ? )
320 [ first section-fits? ] [ last section-fits? not ] bi and ;
322 : ?break-group ( seq -- )
323 dup break-group? [ first <fresh-line ] [ drop ] if ;
325 M: block long-section ( block -- )
327 sections>> chop-break group-flow [
332 [ advance ] [ pprint-section ] bi
338 : pprinter-manifest ( -- manifest )
340 [ [ pprinter-use get keys >vector ] dip (>>search-vocabs) ]
341 [ [ pprinter-in get ] dip (>>current-vocab) ]
345 : make-pprint ( obj quot -- block manifest )
348 H{ } clone pprinter-use set
349 V{ } clone recursion-check set
350 V{ } clone pprinter-stack set
355 ] with-scope ; inline
357 : with-pprint ( obj quot -- )
358 make-pprint drop do-pprint ; inline