Skip to content

Commit 530335d

Browse files
committed
Documentation
1 parent 34aea38 commit 530335d

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

docs/api/pprint.rst

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
basilisp.pprint
22
===============
33

4+
.. lpy:currentns:: basilisp.pprint
5+
46
.. toctree::
57
:maxdepth: 2
68
:caption: Contents:
79

10+
.. _pretty_printing:
11+
812
Pretty Printing
913
---------------
1014

@@ -15,8 +19,7 @@ Pretty printing built-in data structures is as easy as a call to :lpy:fn:`pprint
1519
(require '[basilisp.pprint :as pprint])
1620
(pprint/pprint (range 30))
1721
18-
The output can be configured using a number of different control variables, which
19-
are expressed as dynamic Vars.
22+
The output can be configured using a number of different control variables, which are expressed as dynamic Vars.
2023

2124
- :lpy:var:`*print-base*`
2225
- :lpy:var:`*print-miser-width*`
@@ -27,11 +30,9 @@ are expressed as dynamic Vars.
2730
- :lpy:var:`*print-sort-keys*`
2831
- :lpy:var:`*print-suppress-namespaces*`
2932

30-
You can pretty print the last result from the REPL using the :lpy:fn:`pp` convenience
31-
macro.
33+
You can pretty print the last result from the REPL using the :lpy:fn:`pp` convenience macro.
3234

33-
As an alternative, the :lpy:fn:`write` API enables a more ergonomic API for
34-
configuring the printer using keyword arguments rather than dynamic Vars.
35+
As an alternative, the :lpy:fn:`write` API enables a more ergonomic API for configuring the printer using keyword arguments rather than dynamic Vars.
3536

3637
.. code-block::
3738
@@ -40,10 +41,54 @@ configuring the printer using keyword arguments rather than dynamic Vars.
4041
;; ...
4142
;; write-out #'basilisp.pprint/write-out}
4243
44+
.. _custom_pretty_print_dispatch_function:
45+
4346
Custom Pretty Print Dispatch Function
4447
-------------------------------------
4548

46-
TBD
49+
The default dispatch function is :lpy:fn:`simple-dispatch` which can print most builtin Basilisp types.
50+
Using the builtin macros and utilities, it is possible to create a custom dispatch function.
51+
52+
.. _pretty_printing_concepts:
53+
54+
Pretty Printing Concepts
55+
^^^^^^^^^^^^^^^^^^^^^^^^
56+
57+
The pretty printing algorithm used in ``basilisp.pprint`` is based on the XP algorithm defined in Richard Water's 1989 paper "XP: A Common Lisp Pretty Printing System" as adapted in Clojure's ``pprint`` by Tom Faulhaber.
58+
There are three basic concepts in the XP algorithm which are necessary in order to create a custom dispatch function.
59+
60+
- *Logical blocks* are groups of output that should be treated as a single unit by the pretty printer.
61+
Logical blocks can nest, so one logical block may contain 0 or more other logical blocks.
62+
For example, a vector may contain a map; the vector would be a logical block and the map would also be a logical block.
63+
``simple-dispatch`` even treats key/value pairs in associative type outputs as a logical block, so they are printed on the same line whenever possible.
64+
65+
A dispatch function can emit a logical block using the :lpy:fn:`pprint-logical-block` macro.
66+
67+
- *Conditional newlines* can be emitted anywhere a newline may need inserted into the output stream.
68+
Newlines can be one of 3 different types which hints to the pretty printer when a newline should be emitted.
69+
70+
Dispatch functions can emit newlines in any supported style using the :lpy:fn:`pprint-newline` function.
71+
72+
- ``:linear`` style newlines should be emitted whenever the enclosing logical block does not fit on a single line.
73+
Note that if any linear newline is emitted in a block, every linear newline will be emitted in that block.
74+
75+
- ``:mandatory`` style newlines are emitted in all cases.
76+
77+
- ``:miser`` style newlines are emitted only when the output will occur in the "miser" region as defined by :lpy:var:`*print-miser-width*`.
78+
This allows additional newlines to be emitted as the output nests closer to the right margin.
79+
80+
- *Indentation* commands indicate how indentation of subsequent lines in a logical block should be defined.
81+
Indentation may be defined relative to either the starting column of the current logical block or to the current column of the output.
82+
83+
Dispatch functions can control indentation using the :lpy:fn:`pprint-indent` function.
84+
85+
Pretty printing is most useful for viewing large, nested structures in a more human-friendly way.
86+
To that end, dispatch functions wishing to print any collection may want to use the :lpy:fn:`print-length-loop` macro to loop over the output, respecting the :lpy:var:`basilisp.core/*print-length*` setting.
87+
88+
Dispatch functions which may need to be called on nested elements should use :lpy:fn:`write-out` to ensure that :lpy:var:`basilisp.core/*print-level*` is respected.
89+
Scalar values can be printed with :lpy:fn:`basilisp.core/pr` or just written directly to :lpy:var:`*out*`.
90+
91+
.. _unimplemented_pprint_features:
4792

4893
Unimplemented Features
4994
----------------------
@@ -54,13 +99,17 @@ The following features from ``clojure.pprint`` are not currently implemented:
5499
- ``code-dispatch`` for printing code
55100
- ``cl-format``
56101

102+
.. _pprint_references:
103+
57104
References
58105
----------
59106

60107
- Tom Faulhaber et al.; ``clojure.pprint``
61108
- Oppen, Derek; \"Prettyprinting\"; October 1980
62109
- Waters, Richard; \"XP: A Common Lisp Pretty Printing System\"; March 1989
63110

111+
.. _pprint_api:
112+
64113
API
65114
---
66115

docs/interfaces.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ In day-to-day usage, you will not typically need to use these interfaces, but th
2222
.. automethod:: _lrepr
2323
.. automethod:: lrepr
2424

25-
.. lpy:currentns:: basilisp.core
25+
.. lpy:currentns:: basilisp.core
2626
2727
.. automodule:: basilisp.lang.interfaces
2828
:members:

src/basilisp/pprint.lpy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@
236236

237237
.. seealso::
238238

239-
:lpy:fn:`pprint-logical-block`, :lpy:fn:`print-length-loop`,
240-
:lpy:fn:`pprint-newline`, :lpy:fn:`pprint-indent`"
239+
:ref:`pretty_printing_concepts`, :lpy:fn:`pprint-logical-block`,
240+
:lpy:fn:`print-length-loop`, :lpy:fn:`pprint-newline`, :lpy:fn:`pprint-indent`"
241241
(start-block [this prefix per-line-prefix suffix])
242242
(end-block [this])
243243
(pp-indent [this relative-to offset])

0 commit comments

Comments
 (0)