
    l0je                        d Z ddlmZ ddlmZ  G d d          Z ed           G d	 d
e                      Zdej        _          ed           G d de                      Zdej        _          ed           G d de                      Z	de	j        _          ed           G d de                      Z
de
j        _          ed           G d de                      Zdej        _         dededefdZdededefdZdS )u  An improved Wadler–Lindig pretty printer.

This implementation additionally:

- handles new lines in the text to format.
- removes some dead code from the canonical implementation.

References:

(1) Wadler, P., 1998. A prettier printer.
    Journal of Functional Programming, pp.223-244.
(2) Lindig, C. 2000. Strictly Pretty.
    https://lindig.github.io/papers/strictly-pretty-2000.pdf

Inspired by JAX's use of the same references above, but re-implemented from scratch.
    )	dataclass   )
ansi_stripc                   2    e Zd ZdZddZdeddfdZdd
ZdS )AbstractDoczBase class for all document types.

    For more on the following shorthand methods, see
    [the methods example](./methods.ipynb).
    otherreturn	ConcatDocc                 "    t          | |          S )zH`doc1 + doc2` offers a convenient shorthand for `ConcatDoc(doc1, doc2)`.)r
   selfr   s     _/home/wildlama/miniconda3/envs/lam/lib/python3.11/site-packages/wadler_lindig/_wadler_lindig.py__add__zAbstractDoc.__add__   s    u%%%    indentNestDocc                 $    t          | |          S )z]`doc.nest(indent)` offers a convenient shorthand for
        `NestDoc(doc, indent)`.
        )r   )r   )r   r   s     r   nestzAbstractDoc.nest"   s     tF++++r   GroupDocc                      t          |           S )z@`doc.group()` offers a convenient shorthand for `GroupDoc(doc)`.)r   r   s    r   groupzAbstractDoc.group(   s    ~~r   N)r   r   r	   r
   )r	   r   )__name__
__module____qualname____doc__r   intr   r    r   r   r   r      sh         & & & &,3 ,9 , , , ,     r   r   T)frozenc                       e Zd ZU dZeed<   dS )TextDoczFRepresents an unbroken piece of text to display. May include newlines.textN)r   r   r   r   str__annotations__r   r   r   r!   r!   -   s         PP
IIIIIr   r!   z.**Arguments:**

- `text`: the string of text.
c                   $    e Zd ZU dZeed<   d ZdS )BreakDoczIf in vertical mode then this is a valid place to insert a newline. If in
    horizontal mode then `self.text` will be displayed instead.
    r"   c                 6    d| j         v rt          d          d S )N
z"Cannot have newlines in BreakDocs.)r"   
ValueErrorr   s    r   __post_init__zBreakDoc.__post_init__B   s&    49ABBB r   N)r   r   r   r   r#   r$   r*   r   r   r   r&   r&   :   s?           IIIC C C C Cr   r&   z**Arguments:**

- `text`: the string of text to display if a newline is not inserted.
    Common values are `" "` (for example between elements of a list) or `""` (for
    example between the final element of a list and a closing ']').
c                   J    e Zd ZU dZeedf         ed<   dddZdedd fd	ZdS )
r
   a\  Concatenate multiple documents together, to be displayed one after another.

    If for example these consist only of `TextDoc`s and other `ConcatDoc`s then there is
    no implied breaking between them, so the formatted text may exceed the maximum
    width. You may wish to separate pieces with `BreakDoc`s to indicate this, for
    example.
    .childrenN)r,   c                    t          |          dk    r||}n$t          |          dk    s|t          d          t                              | d|           d S )Nr   zxMust be called as either `ConcatDoc(children=(foo, bar, ...))` or as `ConcatDoc(foo, bar, ...)` or as `foo + bar + ...`.r,   )lenr)   object__setattr__)r   r,   argss      r   __init__zConcatDoc.__init__]   si    t99q==X-HHYY]]h.F   	4X66666r   r   r	   c                 (    t          g | j        |R  S )N)r
   r,   r   s     r   r   zConcatDoc.__add__g   s    /$-/////r   )	r   r   r   r   tupler   r$   r2   r   r   r   r   r
   r
   O   sx           K$%%%% (, 7 7 7 7 70[ 0[ 0 0 0 0 0 0r   r
   z**Arguments:**

Can be called as any of:

- `ConcatDoc(doc1, doc2, doc3, ...)`
- `ConcatDoc(children=(doc1, doc2, doc3, ...))`
- `doc1 + doc2 + doc3 + ...`
c                   (    e Zd ZU dZeed<   eed<   dS )r   zkIf in vertical mode, increase the indent after each newline by `indent` whilst
    displaying `child`.
    childr   N)r   r   r   r   r   r$   r   r   r   r   r   r   v   s1           KKKKKr   r   aH  **Arguments:**

- `child`: the child document to display.
- `indent`: how much to increase the indent.

Frequently `child` will be `ConcatDoc(BreakDoc(""), another_doc)`, so that the first
line of `another_doc` will be indented as much as its later lines. See also the
[The `(break-group).nest-break` example](./pattern.ipynb).
c                       e Zd ZU dZeed<   dS )r   a  Groups the parts of a child document to be laid out all horizontally together or
    all vertically together.

    This decision will persist everywhere outside any child `GroupDoc`s, within which
    their own local rule is used. For example using `[...]` to denote a grouping:
    ```
    [
        foo,
        bar,
        [baz, qux]
    ]
    ```
    then `foo`, `bar` and `[baz, qux]` are laid out vertically, but the sub-group
    `[baz, qux]` is judged to have enough space, and so is laid out horizontally.
    r6   N)r   r   r   r   r   r$   r   r   r   r   r      s*            r   r   z:**Arguments:**

- `child`: the child document to display.
docwidthr	   c           	         | g}t          |          dk    rS|dk    rL|                                xt          dxK\  } |t          t	          t           t          |                                                    d          z  }n xt          dx$\  } |t          t          |                    z  }n xt          dx'\  } |	                    t          |                     nf xt          dx\  }  |                    |           n? xt          dx\  } |                    |           n }J t          |                      t          |          dk    r|dk    L|dk     S )Nr   r   )default)r.   popr!   maxmapr   
splitlinesr&   r
   extendreversedr   appendr   r#   )r8   r9   todor"   r,   r6   xs          r   	_verticalrE      s   "eD
d))a--EQJJhhjjSj&6&6&A&A&C&CDDaPPPP $$Z--...  $$$$$$88HX..//// %""""""E"""" # %%E"""" !$c!ff$$u d))a--EQJJ 19r   c           	         g }d}d}d}t          |           g}t          |          dk    r|                                xt          dx\  } |}n xt          dx\  } |}n xt
          dxn\  }	 |                    |	                    ddd|z  z                        |t          t          |		                    dd          d                             z  }nD xt          dxZ\  }	 |r|                    dd|z  z              |}n|                    |	           |t          t          |	                    z  }n xt          dx'\  }
 |                    t          |
                     n xt          dx5\  }} |                    |           |                    |           ||z  }nj t           dx[\  }|r@t          |||z
            s-|                    d           |                    |           d	}n|                    |           n 	 J t          |          dk    d
                    |          S )uB  Pretty-formats a document using a Wadler–Lindig pretty-printer.

    **Arguments:**

    - `doc`: a document to pretty-format as a string.
    - `width`: a best-effort maximum width to allow. May be exceeded if there are
        unbroken pieces of text which are wider than this.

    **Returns:**

    A string, corresponding to the pretty-formatted document.

    !!! info

        We extend the canonical Wadler–Lindig implementation with the ability to handle
        multiline text. We also remove what seems to be some dead code from their
        implementation.
    r   Tr   Nr(    r   F )r   r.   r<   boolr   r!   rB   replacer   rsplitr&   r
   r@   rA   r   rE   join)r8   r9   outswidth_so_farverticalr   rC   	vertical2indent2r"   r,   r6   extra_indents                r   pformat_docrT      s   & DLHF -5SMM?D
d))a--hhjj ii$ !WW  DLLtcL6H/HIIJJJJt{{4/C/CB/G$H$H I II   $$ :KKsV| 3444#)LLKK%%% C
4(8(8$9$99LL   %$$$$$88HX..//// %------F###E""",& . % 	'IeU\5I$J$J 	' KK%%%KK&&&$HH KK&&&& ! uE d))a--F 774==r   N)r   dataclassesr   _ansir   r   r!   r2   r&   r
   r   r   r   rJ   rE   r#   rT   r   r   r   <module>rW      sV   " " ! ! ! ! !             , $    k      $	C 	C 	C 	C 	C{ 	C 	C 	C   $0 0 0 0 0 0 0 08	   $    k      $    {   (  ; s t    &=[ = = = = = = = =r   