
    AHj:                    N   d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
mZmZmZ d dlmZmZ e
rd dlmZmZ d dlmZ d d	lmZ ed
   Z	  e ee            Z	 e G d d             Z ed      dd       ZddZ G d de      Z G d de      Zee e f   Z!	  G d de      Z"y)    )annotations)ABCabstractmethod)	dataclass)	lru_cache)TYPE_CHECKING
NamedTupleTupleoverload)Literalget_args)NodeQuery)cell_len)Size)

c                  (    e Zd ZU dZded<   	 ded<   y)
EditResultz5Contains information about an edit that has occurred.Locationend_locationstrreplaced_textN)__name__
__module____qualname____doc____annotations__     R/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/document/_document.pyr   r      s    ?:%r!   r   i   )maxsizec                $    | j                  d      S )zEncode the input text as utf-8 bytes.

    The returned encoded bytes may be retrieved from a cache.

    Args:
        text: The text to encode.

    Returns:
        The utf-8 bytes representing the input string.
    zutf-8)encodetexts    r"   _utf8_encoder(       s     ;;wr!   c                "    d| v ryd| v ryd| v ryy)zReturn the newline type used in this document.

    Args:
        text: The text to inspect.

    Returns:
        The Newline used in the file.
    r   r   r   r    r&   s    r"   _detect_newline_styler*   /   s%     ~		r!   c                  ^   e Zd ZdZedd       Zeedd              Zeedd              Zeedd              Z	edd       Z
edd       Zedd       Z	 	 d	 	 	 	 	 	 	 dd
ZddZeedd              Zeedd              Zeedd              Zeredd       Zedd       Zed d       Zy	)!DocumentBasezzDescribes the minimum functionality a Document implementation must
    provide in order to be used by the TextArea widget.c                     y)aC  Replace the text at the given range.

        Args:
            start: A tuple (row, column) where the edit starts.
            end: A tuple (row, column) where the edit ends.
            text: The text to insert between start and end.

        Returns:
            The new end location after the edit is complete.
        Nr    )selfstartendr'   s       r"   replace_rangezDocumentBase.replace_rangeF       r!   c                     y)z'The text from the document as a string.Nr    r.   s    r"   r'   zDocumentBase.textS   r2   r!   c                     y)z/Return the line separator used in the document.Nr    r4   s    r"   newlinezDocumentBase.newlineX   r2   r!   c                     y)zGet the lines of the document as a list of strings.

        The strings should *not* include newline characters. The newline
        character used for the document can be retrieved via the newline
        property.
        Nr    r4   s    r"   lineszDocumentBase.lines]   r2   r!   c                     y)aE  Returns the line with the given index from the document.

        This is used in rendering lines, and will be called by the
        TextArea for each line that is rendered.

        Args:
            index: The index of the line in the document.

        Returns:
            The str instance representing the line.
        Nr    )r.   indexs     r"   get_linezDocumentBase.get_lineg   r2   r!   c                     y)a  Get the text that falls between the start and end locations.

        Args:
            start: The start location of the selection.
            end: The end location of the selection.

        Returns:
            The text between start (inclusive) and end (exclusive).
        Nr    r.   r/   r0   s      r"   get_text_rangezDocumentBase.get_text_rangeu   r2   r!   c                     y)a<  Get the size of the document.

        The height is generally the number of lines, and the width
        is generally the maximum cell length of all the lines.

        Args:
            indent_width: The width to use for tab characters.

        Returns:
            The Size of the document bounding box.
        Nr    )r.   indent_widths     r"   get_sizezDocumentBase.get_size   r2   r!   Nc                    i S )a  Query the tree-sitter syntax tree.

        The default implementation always returns an empty list.

        To support querying in a subclass, this must be implemented.

        Args:
            query: The tree-sitter Query to perform.
            start_point: The (row, column byte) to start the query at.
            end_point: The (row, column byte) to end the query at.

        Returns:
            A dict mapping captured node names to lists of Nodes with that name.
        r    )r.   querystart_point	end_points       r"   query_syntax_treezDocumentBase.query_syntax_tree   s	    ( 	r!   c                     y Nr    )r.   rC   s     r"   prepare_queryzDocumentBase.prepare_query   s    r!   c                     y),Returns the number of lines in the document.Nr    r4   s    r"   
line_countzDocumentBase.line_count   r2   r!   c                     y)9Returns the location of the start of the document (0, 0).r   r   r    r4   s    r"   r/   zDocumentBase.start   s     r!   c                     y)0Returns the location of the end of the document.Nr    r4   s    r"   r0   zDocumentBase.end   r2   r!   c                     y rH   r    r.   
line_indexs     r"   __getitem__zDocumentBase.__getitem__   s    7:r!   c                     y rH   r    rS   s     r"   rU   zDocumentBase.__getitem__   s    ?Br!   c                     y)Return the content of a line as a string, excluding newline characters.

        Args:
            line_index: The index or slice of the line(s) to retrieve.

        Returns:
            The line or list of lines requested.
        Nr    rS   s     r"   rU   zDocumentBase.__getitem__   r2   r!   r/   r   r0   r   r'   r   returnr   rZ   r   rZ   NewlinerZ   	list[str]r:   intrZ   r   r/   r   r0   r   rZ   r   )r@   ra   rZ   r   )NN)rC   z'Query'rD   tuple[int, int] | NonerE   rc   rZ   zdict[str, list['Node']])rC   r   rZ   z'Query | None'rZ   ra   rZ   r   rT   ra   rZ   r   rT   slicerZ   r_   rT   zint | slicerZ   zstr | list[str])r   r   r   r   r   r1   propertyr'   r6   r8   r;   r>   rA   rF   rI   rL   r/   r0   r   r   rU   r    r!   r"   r,   r,   B   sj   ; 
 
 6  6 >  >      	 	    /3,0	 , *	
 
!, ;  ;    ?  ? 	: 
:	B 
B r!   r,   c                       e Zd ZdZddZedd       Zedd       Zedd       ZddZ	ddZ
ddZedd	       Zed fd
       Zedd       ZddZddZddZedd       Zedd       Zd dZ xZS )!Documentz-A document which can be opened in a TextArea.c                    t        |      | _        	 |j                  d      | _        	 |j	                  t        t                    s|s| j                  j                  d       y y )NF)keepends )r*   _newline
splitlines_linesendswithtupleVALID_NEWLINESappend)r.   r'   s     r"   __init__zDocument.__init__   sT    !6t!<3!%%!@	 ==~./tKKr" 8<r!   c                    | j                   S )a  Get the document as a list of strings, where each string represents a line.

        Newline characters are not included in at the end of the strings.

        The newline character used in this document can be found via the `Document.newline` property.
        rr   r4   s    r"   r8   zDocument.lines   s     {{r!   c                L    | j                   j                  | j                        S )zGet the text from the document.)rp   joinrr   r4   s    r"   r'   zDocument.text   s     }}!!$++..r!   c                    | j                   S )z<Get the Newline used in this document (e.g. '
', '
'. etc.))rp   r4   s    r"   r6   zDocument.newline   s     }}r!   c                    | j                   }|D cg c]  }t        |j                  |             }}t        |d      }t	        |      }t        ||      S c c}w )zThe Size of the document, taking into account the tab rendering width.

        Args:
            tab_width: The width to use for tab indents.

        Returns:
            The size (width, height) of the document.
        r   )default)rr   r   
expandtabsmaxlenr   )r.   	tab_widthr8   linecell_lengthsmax_cell_lengthheights          r"   rA   zDocument.get_size   sV     INO!;<OOlA6UOV,, Ps   !Ac                   t        ||f      \  }}|\  }}|\  }}	|j                         }
|j                  t        t                    r|
j                  d       | j                  }| j                  ||      }|t        |      k\  rd}n||   |	d }|t        |      k\  rd}n||   d| }|
r%||
d   z   |
d<   t        |
d         }|
d   |z   |
d<   nt        |      }||z   g}
|
|||dz    |t        |
      z   dz
  }||f}t        ||      S )a  Replace text at the given range.

        This is the only method by which a document may be updated.

        Args:
            start: A tuple (row, column) where the edit starts.
            end: A tuple (row, column) where the edit ends.
            text: The text to insert between start and end.

        Returns:
            The EditResult containing information about the completed
                replace operation.
        ro   Nr      )
sortedrq   rs   rt   ru   rv   rr   r>   r   r   )r.   r/   r0   r'   topbottomtop_row
top_column
bottom_rowbottom_columninsert_linesr8   r   after_selectionbefore_selectiondestination_columndestination_rowr   s                     r"   r1   zDocument.replace_range   sC    eS\*V!$*!
M(==~./#++C8U# O#J/?Oc%j !$W~kz:.a@LO!$\"%5!6+B//AL!$%5!6,>?L*6g
Q'!C$559');<,66r!   c                d   ||k(  ryt        ||f      \  }}|\  }}|\  }}| j                  }	||k(  r|	|   }
|
|| }|S |	|   }|| j                  dz
  k  r|	|   nd}||d }t        |dz   |      D ]  }|| j                  |	|   z   z  } || j                  k  r|| j                  z  }||d| z  }|S )a"  Get the text that falls between the start and end locations.

        Returns the text between `start` and `end`, including the appropriate
        line separator character as specified by `Document._newline`. Note that
        `_newline` is set automatically to the first line separator character
        found in the document.

        Args:
            start: The start location of the selection.
            end: The end location of the selection.

        Returns:
            The text between start (inclusive) and end (exclusive).
        ro   r   N)r   rr   rL   rangerp   )r.   r/   r0   r   r   r   r   r   r   r8   r   selected_text
start_lineend_linerows                  r"   r>   zDocument.get_text_range/  s     C<eS\*V!$*!
Mj >D M:M  wJ,6$//A:M,MuZ(SUH&z{3MWq[*5 <s!;;< DOO+..=!99r!   c                ,    t        | j                        S )rK   )r   rr   r4   s    r"   rL   zDocument.line_countU  s     4;;r!   c                    t         |   S )rN   )superr/   )r.   	__class__s    r"   r/   zDocument.startZ  s     w}r!   c                T    | j                   d   }| j                  dz
  t        |      fS )rQ   r   r   )rr   rL   r   )r.   	last_lines     r"   r0   zDocument.end_  s)     KKO	!#S^44r!   c                    |\  }}|t        | j                        z  |z   }t        |      D ]  }|t        | j                  |            z  }! |S )zGiven a location, returns the index from the document's text.

        Args:
            location: The location in the document.

        Returns:
            The index in the document's text.
        )r   r6   r   r;   )r.   locationr   columnr:   rT   s         r"   get_index_from_locationz Document.get_index_from_locatione  sU     Vc$,,''&0* 	4JSz233E	4r!   c                R   d|d}|dk  s|t        | j                        kD  rt        |      d}t        | j                        }t	        | j
                        D ]@  }|t        | j                  |            z   |z   }||k  r	|||z
  fc S ||k(  r	|dz   dfc S |}B t        |      )aE  Given a codepoint index in the document's text, returns the corresponding location.

        Args:
            index: The index in the document's text.

        Returns:
            The corresponding location.

        Raises:
            ValueError: If the index is doesn't correspond to a location in the document.
        zIndex z3 does not correspond to a location in the document.r   r   )r   r'   
ValueErrorr6   r   rL   r;   )r.   r:   error_messagecolumn_indexnewline_lengthrT   next_column_indexs          r"   get_location_from_indexz Document.get_location_from_indext  s     UIPQ 	 19DII.]++T\\*0 	-Js4==#<==N  (("EL$899++"Q**,L	- ''r!   c                    | |   }|S )zReturns the line with the given index from the document.

        Args:
            index: The index of the line in the document.

        Returns:
            The string representing the line.
        r    )r.   r:   line_strings      r"   r;   zDocument.get_line  s     5kr!   c                     y rH   r    rS   s     r"   rU   zDocument.__getitem__  s    36r!   c                     y rH   r    rS   s     r"   rU   zDocument.__getitem__  s    ;>r!   c                     | j                   |   S )rX   ry   rS   s     r"   rU   zDocument.__getitem__  s     {{:&&r!   )r'   r   rZ   Noner^   r[   r\   )r   ra   rZ   r   rY   rb   rd   re   )r   r   rZ   ra   )r:   ra   rZ   r   r`   rf   rg   ri   )r   r   r   r   rw   rj   r8   r'   r6   rA   r1   r>   rL   r/   r0   r   r   r;   r   rU   __classcell__)r   s   @r"   rl   rl      s    7	#   / /  -07d$L       5 5
(@
 6 6> >	'r!   rl   c                  ^    e Zd ZU dZdZded<   	 dZded<   	 ed
d       Ze	dd       Z
ddZy	)	Selectiona  A range of characters within a document from a start point to the end point.
    The location of the cursor is always considered to be the `end` point of the selection.
    The selection is inclusive of the minimum point and exclusive of the maximum point.
    rO   r   r/   r0   c                     | ||      S )zCreate a Selection with the same start and end point - a "cursor".

        Args:
            location: The location to create the zero-width Selection.
        r    )clsr   s     r"   cursorzSelection.cursor  s     8X&&r!   c                    | \  }}||k(  S )zBReturn True if the selection has 0 width, i.e. it's just a cursor.r    r=   s      r"   is_emptyzSelection.is_empty  s     
s|r!   c                n    t        | j                  d   | j                  d   f      \  }}||k\  xr ||k  S )z0Check if the given line is within the selection.r   )r   r/   r0   )r.   yr   r   s       r"   contains_linezSelection.contains_line  s8    djjmTXXa[9:VCx'AK'r!   N)r   r   rZ   z'Selection')rZ   bool)r   ra   rZ   r   )r   r   r   r   r/   r   r0   classmethodr   rj   r   r   r    r!   r"   r   r     sV    
 E8 C
 ' '  
(r!   r   N)r'   r   rZ   bytes)r'   r   rZ   r]   )#
__future__r   abcr   r   dataclassesr   	functoolsr   typingr   r	   r
   r   typing_extensionsr   r   tree_sitterr   r   textual._cellsr   textual.geometryr   r]   setru   r   r(   r*   r,   rl   ra   r   r   r    r!   r"   <module>r      s    " # !  = = /' # !
$
% 2Xg&' . & & & 4   &G3 GTc'| c'L c? I#(
 #(r!   