1 ! Copyright (C) 2003, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors classes.maybe combinators
4 combinators.short-circuit continuations hashtables io io.styles
5 kernel make math namespaces prettyprint.config sequences sets
6 splitting strings vocabs vocabs.parser words ;
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 adjoin ] if ;
26 GENERIC: vocabulary-name ( obj -- string )
28 M: word vocabulary-name
31 M: maybe vocabulary-name
32 class>> vocabulary>> ;
34 : record-vocab ( word -- )
42 : line-limit? ( -- ? )
43 line-limit get dup [ pprinter get line-count>> <= ] when ;
46 pprinter get indent>> [ CHAR: \s <string> write ] unless-zero ;
49 pprinter get 2dup last-newline>> = [
56 [ 1 + ] change-line-count drop
60 : text-fits? ( len -- ? )
64 [ pprinter get indent>> + ] dip <=
67 ! break only if position margin 2 / >
74 GENERIC: section-fits? ( section -- ? )
76 GENERIC: short-section ( section -- )
78 GENERIC: long-section ( section -- )
80 GENERIC: indent-section? ( section -- ? )
82 GENERIC: unindent-first-line? ( section -- ? )
84 GENERIC: newline-after? ( section -- ? )
86 GENERIC: short-section? ( section -- ? )
91 start-group? end-group?
94 : new-section ( length class -- section )
101 0 >>overhang ; inline
103 M: section section-fits?
104 [ end>> 1 - pprinter get last-newline>> - ]
105 [ overhang>> ] bi + text-fits? ;
107 M: section indent-section? drop f ;
109 M: section unindent-first-line? drop f ;
111 M: section newline-after? drop f ;
113 M: object short-section? section-fits? ;
115 : indent+ ( section n -- )
116 swap indent-section? [
117 pprinter get [ + ] change-indent drop
120 : <indent ( section -- ) tab-size get indent+ ;
122 : indent> ( section -- ) tab-size get neg indent+ ;
124 : <fresh-line ( section -- )
127 : fresh-line> ( section -- )
128 dup newline-after? [ end>> fresh-line ] [ drop ] if ;
130 : <long-section ( section -- )
131 dup unindent-first-line?
132 [ dup <fresh-line <indent ] [ dup <indent <fresh-line ] if ;
134 : long-section> ( section -- )
135 dup indent> fresh-line> ;
137 : pprint-section ( section -- )
139 dup style>> [ short-section ] with-style
142 [ dup style>> [ long-section ] with-style ]
148 TUPLE: line-break < section type ;
150 : <line-break> ( type -- section )
151 0 line-break new-section
154 M: line-break short-section drop ;
156 M: line-break long-section drop ;
159 TUPLE: block < section sections ;
161 : new-block ( class -- block )
163 V{ } clone >>sections ; inline
165 : <block> ( style -- block )
169 : pprinter-block ( -- block ) pprinter-stack get last ;
171 : add-section ( section -- )
172 pprinter-block sections>> push ;
174 : last-section ( -- section )
175 pprinter-block sections>>
176 [ line-break? not ] find-last nip ;
179 last-section t >>start-group? drop ;
182 last-section t >>end-group? drop ;
184 : advance ( section -- )
186 [ start>> pprinter get last-newline>> = not ]
190 : add-line-break ( type -- ) [ <line-break> add-section ] when* ;
192 M: block section-fits?
193 line-limit? [ drop t ] [ call-next-method ] if ;
195 : pprint-sections ( block advancer -- )
197 sections>> [ line-break? ] reject
198 unclip-slice pprint-section
200 [ [ pprint-section ] bi ] curry each ; inline
202 M: block short-section
203 [ advance ] pprint-sections ;
205 : do-break ( break -- )
208 [ end>> pprinter get last-newline>> - margin get 2/ > ] tri
209 or [ <fresh-line ] [ drop ] if ;
211 : empty-block? ( block -- ? ) sections>> empty? ;
213 : unless-empty-block ( block quot: ( block -- ) -- )
214 [ dup empty-block? [ drop ] ] dip if ; inline
216 : (<block) ( block -- ) pprinter-stack get push ;
218 : <block ( -- ) f <block> (<block) ;
220 : <object ( obj -- ) presented associate <block> (<block) ;
223 TUPLE: text-section < section string ;
225 : <text> ( string style -- text )
226 over length 1 + text-section new-section
230 M: text-section short-section string>> write ;
232 M: text-section long-section short-section ;
234 : styled-text ( string style -- ) <text> add-section ;
236 : text ( string -- ) f styled-text ;
239 TUPLE: inset < block narrow? ;
241 : <inset> ( narrow? -- block )
246 M: inset long-section
248 [ <fresh-line ] pprint-sections
253 M: inset indent-section? drop t ;
255 M: inset newline-after? drop t ;
257 : <inset ( narrow? -- ) <inset> (<block) ;
260 TUPLE: flow < block ;
262 : <flow> ( -- block )
265 M: flow short-section?
266 ! If we can make room for this entire block by inserting
267 ! a newline, do it; otherwise, don't bother, print it as
271 [ [ end>> 1 - ] [ start>> ] bi - text-fits? not ]
274 : <flow ( -- ) <flow> (<block) ;
276 ! Colon definition section
277 TUPLE: colon < block ;
279 : <colon> ( -- block )
282 M: colon long-section short-section ;
284 M: colon indent-section? drop t ;
286 M: colon unindent-first-line? drop t ;
288 : <colon ( -- ) <colon> (<block) ;
290 : save-end-position ( block -- )
291 position get >>end drop ;
294 pprinter-stack get pop [
295 [ save-end-position ] [ add-section ] bi
296 ] unless-empty-block ;
298 : do-pprint ( block -- )
299 <pprinter> pprinter [
309 ! Long section layout algorithm
310 : chop-break ( seq -- seq )
311 [ dup last line-break? ] [ but-last-slice ] while ;
316 : split-groups ( ? -- ) [ t , ] when ;
318 : split-before ( section -- )
320 [ start-group?>> prev get [ end-group?>> and ] when* ]
321 [ flow? prev get flow? not and ]
324 : split-after ( section -- )
325 [ end-group?>> ] [ f ] if* split-groups ;
327 : group-flow ( seq -- newseq )
330 2dup 1 - swap ?nth prev namespaces:set
331 2dup 1 + swap ?nth next namespaces:set
332 swap nth dup split-before dup , split-after
334 ] { } make { t } split harvest ;
336 : break-group? ( seq -- ? )
337 { [ first section-fits? ] [ last section-fits? not ] } 1&& ;
339 : ?break-group ( seq -- )
340 dup break-group? [ first <fresh-line ] [ drop ] if ;
342 M: block long-section
344 sections>> chop-break group-flow [
349 [ advance ] [ pprint-section ] bi
353 ] unless-empty-block ;
355 : pprinter-manifest ( -- manifest )
357 pprinter-use get members V{ } like >>search-vocabs
358 pprinter-in get >>current-vocab ;
360 : make-pprint ( obj quot manifest? -- block manifest/f )
362 0 position namespaces:set
363 HS{ } clone pprinter-use namespaces:set
364 V{ } clone recursion-check namespaces:set
365 V{ } clone pprinter-stack namespaces:set
367 [ over <object call pprinter-block ] dip
368 [ pprinter-manifest ] [ f ] if
369 ] with-scope ; inline
371 : with-pprint ( obj quot -- )
372 f make-pprint drop do-pprint ; inline