1 ! Copyright (C) 2003, 2010 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors classes 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: section long-section short-section ;
115 M: object short-section? section-fits? ;
117 : indent+ ( section n -- )
118 swap indent-section? [
119 pprinter get [ + ] change-indent drop
122 : <indent ( section -- ) tab-size get indent+ ;
124 : indent> ( section -- ) tab-size get neg indent+ ;
126 : <fresh-line ( section -- )
129 : fresh-line> ( section -- )
130 dup newline-after? [ end>> fresh-line ] [ drop ] if ;
132 : <long-section ( section -- )
133 dup unindent-first-line?
134 [ dup <fresh-line <indent ] [ dup <indent <fresh-line ] if ;
136 : long-section> ( section -- )
137 dup indent> fresh-line> ;
139 : pprint-section ( section -- )
141 dup style>> [ short-section ] with-style
144 [ dup style>> [ long-section ] with-style ]
150 TUPLE: line-break < section type ;
152 : <line-break> ( type -- section )
153 0 line-break new-section
156 M: line-break short-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 : styled-text ( string style -- ) <text> add-section ;
234 : text ( string -- ) f styled-text ;
237 TUPLE: inset < block narrow? ;
239 : <inset> ( narrow? -- block )
244 M: inset long-section
246 [ <fresh-line ] pprint-sections
251 M: inset indent-section? drop t ;
253 M: inset newline-after? drop t ;
255 : <inset ( narrow? -- ) <inset> (<block) ;
258 TUPLE: flow < block ;
260 : <flow> ( -- block )
263 M: flow short-section?
264 ! If we can make room for this entire block by inserting
265 ! a newline, do it; otherwise, don't bother, print it as
269 [ [ end>> 1 - ] [ start>> ] bi - text-fits? not ]
272 : <flow ( -- ) <flow> (<block) ;
274 ! Colon definition section
275 TUPLE: colon < block ;
277 : <colon> ( -- block )
280 M: colon indent-section? drop t ;
282 M: colon unindent-first-line? drop t ;
284 : <colon ( -- ) <colon> (<block) ;
286 : save-end-position ( block -- )
287 position get >>end drop ;
290 pprinter-stack get pop [
291 [ save-end-position ] [ add-section ] bi
292 ] unless-empty-block ;
294 : do-pprint ( block -- )
295 <pprinter> pprinter [
305 ! Long section layout algorithm
306 : chop-break ( seq -- seq )
307 [ dup last line-break? ] [ but-last-slice ] while ;
312 : split-groups ( ? -- ) [ t , ] when ;
314 : split-before ( section -- )
316 [ start-group?>> prev get [ end-group?>> and ] when* ]
317 [ flow? prev get flow? not and ]
320 : split-after ( section -- )
321 [ end-group?>> ] [ f ] if* split-groups ;
323 : group-flow ( seq -- newseq )
326 2dup 1 - swap ?nth prev namespaces:set
327 2dup 1 + swap ?nth next namespaces:set
328 swap nth dup split-before dup , split-after
330 ] { } make { t } split harvest ;
332 : break-group? ( seq -- ? )
333 { [ first section-fits? ] [ last section-fits? not ] } 1&& ;
335 : ?break-group ( seq -- )
336 dup break-group? [ first <fresh-line ] [ drop ] if ;
338 M: block long-section
340 sections>> chop-break group-flow [
345 [ advance ] [ pprint-section ] bi
349 ] unless-empty-block ;
351 : pprinter-manifest ( -- manifest )
353 pprinter-use get members V{ } like >>search-vocabs
354 pprinter-in get >>current-vocab ;
356 : make-pprint ( obj quot manifest? -- block manifest/f )
358 0 position namespaces:set
359 HS{ } clone pprinter-use namespaces:set
360 V{ } clone recursion-check namespaces:set
361 V{ } clone pprinter-stack namespaces:set
363 [ over <object call pprinter-block ] dip
364 [ pprinter-manifest ] [ f ] if
365 ] with-scope ; inline
367 : error-in-pprint ( obj -- )
368 <flow class-of name>> "~pprint error: " "~" surround text block> ;
370 : with-pprint ( obj quot -- )
372 [ drop [ error-in-pprint ] f make-pprint ] recover
373 drop do-pprint ; inline