
    CjA                        S SK Jr  S SKJr  S SKJrJrJrJr  S SK	J
r
Jr  \(       a  S SKJr   " S S\5      r\" SS5      r " S	 S
\5      r " S S\5      r " S S\5      rg)    )annotations)
attrgetter)TYPE_CHECKINGIterableIterator
NamedTuple)OffsetShapeWidgetc                  Z    \ rS rSr% SrS\S'    S\S'    \SS j5       rSS jrSS jr	S	r
g
)	Selection   zA selected range of lines.Offset | Nonestartendc                $    [        X/S S9nU " U6 $ )z|Create selection from 2 offsets.

Args:
    offset1: First offset.
    offset2: Second offset.

Returns:
    New Selection.
c                2    U R                   U R                  4$ )Nyx)offsets    c/home/rurouni/.local/share/pipx/venvs/strix-agent/lib/python3.13/site-packages/textual/selection.py<lambda>(Selection.from_offsets.<locals>.<lambda>   s    &((FHHAU    key)sorted)clsoffset1offset2offsetss       r   from_offsetsSelection.from_offsets   s     '+2UWG}r   c                ^   UR                  5       nU(       d  gU R                  c  SnSnOU R                  R                  u  p4U R                  c  [	        U5      n[	        US   5      nOU R                  R                  u  pV[        [	        U5      U5      nX5:X  a  X#   XF $ / nX#US-    n[	        U5      S:  a>  UGtpnUR                  XS 5        UR                  U
5        UR                  USU 5        O UR                  X#   XF 5        SR                  U5      $ ! [         a     Nf = f)ziExtract selection from text.

Args:
    text: Raw text pulled from widget.

Returns:
    Extracted text.
 Nr         
)

splitlinesr   	transposer   lenminappendextend
IndexErrorjoin)selftextlinesstart_line_indexstart_offsetend_line
end_offset	selectionselected_lines
first_line	mid_lines	last_lines               r   extractSelection.extract"   s4    !:: L-1ZZ-A-A*885zHU2YJ#'88#5#5 Hs5z8,'*<CC!	(Q,?~!#0>-JIZ67Y'Y{
34  !8!QR yy##  s   9D 
D,+D,c                ,   U u  p#Uc  Uc  gUb  Ub  XR                   :  d  XR                   :  a  gXR                   :X  a2  UR                   UR                   :X  a  UR                  UR                  4$ XR                   :X  a  SUR                  4$ XR                   :X  a  UR                  S4$ gUc1  Ub.  XR                   :X  a  SUR                  4$ XR                   :  a  ggUc1  Ub.  XR                   :X  a  UR                  S4$ XR                   :  a  ggg)zGet the selected span in a given line.

Args:
    y: Offset of the line.

Returns:
    A tuple of x start and end offset, or None for no selection.
N)r   r(   r   r(   r   )r4   r   r   r   s       r   get_spanSelection.get_spanL   s     
=S[77{a%%iGG|355 0ww~%EEz#%%xGG|ww{"=S_EEz#%%x55y;5,GG|ww{"77{r    N)r!   r	   r"   r	   returnr   )r5   strrF   rG   )r   intrF   ztuple[int, int] | None)__name__
__module____qualname____firstlineno____doc____annotations__classmethodr$   r@   rC   __static_attributes__rE   r   r   r   r      s4    $%	# ($T*r   r   Nc                  v    \ rS rSr% SrS\S'    S\S'    S\S'    S\S'    S	\S
'    S\S'    \SS j5       rSrg)SelectStart|   z Describes the start of a select.r   	containerr	   container_pointer_deltacontainer_initial_offsetcontainer_initial_scroll_offsetWidget | Nonecontent_widgetr   content_offsetc                    U R                   R                  R                  U R                  -   U R                   R                  U R
                  -
  -   $ )z-The pointer start offset adjusted for scroll.)rT   regionr   rU   scroll_offsetrW   r4   s    r   pointer_start_offset SelectStart.pointer_start_offset   sH     NN!!((**+~~++d.R.RRT	
r   rE   N)rF   r	   )	rI   rJ   rK   rL   rM   rN   propertyr_   rP   rE   r   r   rR   rR   |   sM    *)##E$$.%++5!!8!!N
 
r   rR   c                  <    \ rS rSr% SrS\S'    S\S'    S\S'   S	rg
)	SelectEnd   zThe end of a select.r   rT   rX   rY   r   rZ   rE   N)rI   rJ   rK   rL   rM   rN   rP   rE   r   r   rc   rc      s     1!!8!!=r   rc   c                      \ rS rSr% SrS\S'    S\S'    SrS\S	'    SS
 jr\SS j5       r	\SS j5       r
\SS j5       r\SS j5       r\SS j5       rSS jrSS jrSS jrSrg)SelectState   z3An object which describes the current select state.r	   screen_offsetrR   r   NzSelectEnd | Noner   c                    U R                   R                  R                  (       d  gU R                  b&  U R                  R                  R                  (       d  gg)z-Are the widgets involved attached to the DOM?FT)r   rT   is_attachedr   r^   s    r   is_attached_to_domSelectState.is_attached_to_dom   s<     zz##//88(:(:(F(Fr   c                2   U R                   c   eU R                  R                  SL=(       aj    U R                  R                  U R                   R                  L =(       a7    U R                  R                  SL=(       a    U R                   R                  SL$ )z/Does the start and end fall on the same widget?N)r   r   rY   rZ   r^   s    r   is_single_content_widget$SelectState.is_single_content_widget   s|     xx###JJ%%T1 4

))TXX-D-DD4

))54 ''t3		
r   c                    U R                   c   S5       eU R                  R                  nU R                   R                  nUc   eUc   eUR                  UR                  :  a  X!p!X4$ )z'Get the content offset in select order.8Unavailable until there is an end point to the selection)r   r   rZ   r-   r4   r8   r:   s      r   content_offsetsSelectState.content_offsets   sx     HH 	FE	F zz00XX,,
'''%%%,"8"88'1*''r   c                "   SSK Jn  SSKJn  U R                  R
                  b  U R                  R
                  OU R                  R                  /nU R                  bR  UR                  U R                  R
                  b  U R                  R
                  OU R                  R                  5        [        U5      S:X  a;  Uu  pE[        XA5      (       a  U$ [        XQ5      (       a  U$  UR                  " XE5      $ US   $ ! [         a    Us $ f = f)z/A widget that contains both ends of the select.r   )Screenr   r*   )textual.screenrv   textual.widgetr   r   rY   rT   r   r0   r.   
isinstanceget_common_ancestor
ValueError)r4   rv   r   widgetswidget1widget2s         r   select_containerSelectState.select_container   s     	*)
 ::,,8 

))ZZ))	
 88NN88**6 ''XX'' w<1&G'**'**11'CC 1:  s   (C? ?DDc                    [         R                  " U R                  R                  U R                  R
                  U R                  5      nU$ )z1A shape which overlays the area of selected text.)r
   selection_boundsr   r\   r   r_   rh   )r4   r   s     r   r   SelectState.selection_bounds   sB     !11!!((JJ++

  r   c                    U R                   R                  nU R                  nUR                  UR                  :  a  X!p!X4$ )z5Offsets used in selection bounds, in selection order.)r   r_   rh   r-   rr   s      r   ordered_offsetsSelectState.ordered_offsets   s@     zz66''
!!J$8$88'1*''r   c                .    [        XR                  U5      $ )zUpdate the state with the selction end.

Args:
    pointer_offset: Current mosue position.
    select_end: Selection end.

Returns:
    SelectState: New select state.

)rf   r   )r4   pointer_offset
select_ends      r   
update_endSelectState.update_end  s     >::zBBr   c                   U R                   c   S5       eU R                  R                  nU R                  R                  nU R                  R                  nU R                   R                  nU R                   R                  nU R
                  nUR                  UR                  :  a  XRpRUUpcUb  Ub  [        US 5      X'   Ub  Ub  [        S U5      X'   g g g )Nrq   )r   r   rY   rZ   r_   rh   r-   r   )r4   
selectionsstart_widgetstart_content_offsetr8   
end_widgetend_content_offsetr:   s           r   _apply_content_selections%SelectState._apply_content_selections  s    HH 	FE	F zz00#zz88zz66XX,,
!XX44''
,"8"88'1*"$ #5
 #(<(H'01Et'LJ$!&8&D%.t5G%HJ" 'E!r   c           
     l  ^^^^^^	^
^^^ U R                   c   S5       eU R                  m
U R                  nU R                  u  p#UR                  mUR                  mU R
                  R                  R                  U R                  R                  ::  a-  U R
                  R                  mU R                   R                  mO,U R                   R                  mU R
                  R                  m[        S5      m/ m	SU4S jjmS S S.           SU	U4S jjjmS	UUUUUU	U
UU4	S jjmT" U5        T	$ )
Nrq   _selection_orderc           	   3  &  >#    [        [        U R                  TS95      /nU(       ag  [        US   S5      nUc  UR	                  5         M+  Uv   UR                  nU(       a"  UR                  [        [        UTS95      5        U(       a  Mf  gg7f)z:Walk descendants of `root` depth-first in selection order.r   r(   N)iterr   displayed_and_visible_childrennextpopr0   )rootstackwidgetchildrenget_selection_orders       r   walk_in_select_order@SelectState._walk_selected_widgets.<locals>.walk_in_select_orderC  s      ;;/-E eBi.>IIK!@@LL" ($7 %s   B
BB)from_yto_yc               L  > USL =(       a    USL nT	" U 5       H  nUR                   (       d  UR                  (       d  M'  UR                  R                  nU(       d  Ub	  XaL a  SnOMP  Uc   eXs:  a  SnOM_  Uc
  Ub  Xt:  a    gTR	                  U5        Uc  M  XbL d  M    g   g)a  Collect selectable descendants between two content widgets.

Walks `container` in selection order, including selectable
non-container descendants.

When the start or end pointer lands on a gap (no content widget),
`from_y` / `to_y` fall back to a vertical bound on the widget's
`content_region.y` so the selection grows continuously as the
pointer moves, rather than snapping to the whole container.

Args:
    container: Top level widget, that is parent of `from_widget` and `to_widget.
    from_widget: First widget in sslection order or first in selection order.
    to_widget: Last widget in selection order or `None` for end of container.

    from_y: Start `y` of selection, or `None` for top.
    to_y: End `y` of selection, or `None` for end.
NT)is_containerallow_selectcontent_regionr   r0   )
rT   from_widget	to_widgetr   r   started
descendantwidget_yselectedr   s
           r   collect_range9SelectState._walk_selected_widgets.<locals>.collect_range^  s    4 "T)<fnG29=
***2I2I%4466".%4&*G$  &111#-&*G$$)9ho
+(Z-D+ >r   c                  >	 [        U R                  TS9 GHP  nUR                  (       a  UR                  nU(       d  M*  TR	                  U5      (       d  MB  UR
                  =(       a%    UR                  S:  =(       d    UR                  S:  nU(       a|  UR                  nUR                  nTU:  nT	U:  nU(       a  U(       a  T" USS5        M  U(       a  Tb  T" UST5        O	T" USST	S9  M  U(       a  T
b  T" UT
S5        O	T" USSTS9  M  T" U5        GM  UR                  (       d  GM  TR	                  UR                  5      (       d  GM?  TR                  U5        GMS     g)zeWalk children of `parent`, deciding inclusion per child.

Args:
    root: Initial node to walk from.
r   r   N)r   )r   )r   r   r   r\   overlapsis_scrollablemax_scroll_ymax_scroll_xr   bottomr   r   r0   )r   childchild_regionhas_hidden_content	child_topchild_bottomextends_aboveextends_belowr   end_yfirst_content_widgetr   last_content_widgetr   r   start_yvisits           r   r   1SelectState._walk_selected_widgets.<locals>.visit  sT     33' %%#(<<L' +44\BB ).)<)< ***Q.H%2D2Dq2H ' *$0NN	'3':':(/)(;(-(=(] *%t<$(  3> -eT;N O -eT4e L$(  4? -e5I4 P -eT4 P$ %L))).>.G.G,,/ / !.ir   )r   r   rF   zIterable[Widget])rT   r   r   rX   r   rX   r   
int | Noner   r   rF   None)r   r   rF   r   )r   r   r   r   r   r   r_   r-   rh   rY   r   )r4   r   ordered_startordered_endr   r   r   r   r   r   r   r   r   r   s       @@@@@@@@@@r   _walk_selected_widgets"SelectState._walk_selected_widgets)  s?   HH 	FE	F   0000 &*%9%9"//
 ::**448J8J8T8TT#'::#<#< "&(("9"9#'88#:#: "&**";";();<!#	@ "&#0	0	&0	 %0	
 0	 0	 0	 0	d:	/ :	/x 	r   rE   )rF   bool)rF   ztuple[Offset, Offset])rF   r   )rF   r
   )r   r	   r   rc   rF   rf   )r   zdict[Widget, Selection])rF   zlist[Widget])rI   rJ   rK   rL   rM   rN   r   rk   ra   rn   rs   r   r   r   r   r   r   rP   rE   r   r   rf   rf      s    =6% C	 # 
 
 ( (  @     ( (CI0dr   rf   )
__future__r   operatorr   typingr   r   r   r   textual.geometryr	   r
   rx   r   r   
SELECT_ALLrR   rc   rf   rE   r   r   <module>r      s]    "  @ @ *%j
 jZ tT"

* 
4>
 >l* lr   