
    AHjs                        d Z 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 ddlmZ ddlmZ e G d	 d
e             Z G d de      Z G d de      Zy)zO

Contains the `Suggester` class, used by the [Input](/widgets/input) widget.

    )annotations)ABCabstractmethod)	dataclass)Iterable)LRUCache)DOMNode)Messagec                  (    e Zd ZU dZded<   	 ded<   y)SuggestionReadyz+Sent when a completion suggestion is ready.strvalue
suggestionN)__name__
__module____qualname____doc____annotations__     I/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/suggester.pyr   r      s    5J3O r   r   c                  H    e Zd ZU dZded<   	 dddddZddZedd	       Zy
)	SuggesterzDefines how widgets generate completion suggestions.

    To define a custom suggester, subclass `Suggester` and implement the async method
    `get_suggestion`.
    See [`SuggestFromList`][textual.suggester.SuggestFromList] for an example.
    z LRUCache[str, str | None] | NonecacheTF)	use_cachecase_sensitivec               :    |rt        d      nd| _        || _        y)a+  Create a suggester object.

        Args:
            use_cache: Whether to cache suggestion results.
            case_sensitive: Whether suggestions are case sensitive or not.
                If they are not, incoming values are casefolded before generating
                the suggestion.
        i   N)r   r   r   )selfr   r   s      r   __init__zSuggester.__init__'   s     (1Xd^d
,r   c                D  K   | j                   r|n|j                         }| j                  || j                  vr5| j                  |       d{   }| j                  || j                  |<   n| j                  |   }|y|j	                  t        ||             y7 Nw)a7  Used by widgets to get completion suggestions.

        Note:
            When implementing custom suggesters, this method does not need to be
            overridden.

        Args:
            requester: The message target that requested a suggestion.
            value: The current value to complete.
        N)r   casefoldr   get_suggestionpost_messager   )r   	requesterr   normalized_valuer   s        r   _get_suggestionzSuggester._get_suggestion3   s      %)$7$75U^^=M::!1!C#223CDDJzz%/9

+,$45JujAB Es   AB BAB c                   K   yw)a  Try to get a completion suggestion for the given input value.

        Custom suggesters should implement this method.

        Note:
            The value argument will be casefolded if `self.case_sensitive` is `False`.

        Note:
            If your implementation is not deterministic, you may need to disable caching.

        Args:
            value: The current value of the requester widget.

        Returns:
            A valid suggestion or `None`.
        Nr   )r   r   s     r   r"   zSuggester.get_suggestionK   s     $ 	s   N)r   boolr   r(   returnNone)r$   r	   r   r   r)   r*   r   r   r)   z
str | None)	r   r   r   r   r   r   r&   r   r"   r   r   r   r   r      s6     ,+$,0 
-C0  r   r   c                  <     e Zd ZdZdd	 	 	 	 	 d fdZddZ xZS )SuggestFromLista  Give completion suggestions based on a fixed list of options.

    Example:
        ```py
        countries = ["England", "Scotland", "Portugal", "Spain", "France"]

        class MyApp(App[None]):
            def compose(self) -> ComposeResult:
                yield Input(suggester=SuggestFromList(countries, case_sensitive=False))
        ```

        If the user types ++p++ inside the input widget, a completion suggestion
        for `"Portugal"` appears.
    Tr   c                   t         |   |       t        |      | _        | j                  r| j                  | _        y| j                  D cg c]  }|j                          c}| _        yc c}w )a  Creates a suggester based off of a given iterable of possibilities.

        Args:
            suggestions: Valid suggestions sorted by decreasing priority.
            case_sensitive: Whether suggestions are computed in a case sensitive manner
                or not. The values provided in the argument `suggestions` represent the
                canonical representation of the completions and they will be suggested
                with that same casing.
        r.   N)superr   list_suggestionsr   r!   _for_comparison)r   suggestionsr   r   	__class__s       r   r   zSuggestFromList.__init__p   sm     	7 - ""  	 ;?:K:KLJ*%%'L 	 Ms   A.c                   K   t        | j                        D ]'  \  }}|j                  |      s| j                  |   c S  yw)zGets a completion from the given possibilities.

        Args:
            value: The current value.

        Returns:
            A valid completion suggestion or `None`.
        N)	enumerater3   
startswithr2   )r   r   idxr   s       r   r"   zSuggestFromList.get_suggestion   sJ       ))=)=> 	.OC$$U+((--	. s
   -AA)r4   zIterable[str]r   r(   r)   r*   r+   )r   r   r   r   r   r"   __classcell__)r5   s   @r   r-   r-   `   s1      EI
(
=A
	
(r   r-   N)r   
__future__r   abcr   r   dataclassesr   typingr   textual.cacher   textual.domr	   textual.messager
   r   r   r-   r   r   r   <module>rB      sW    # # !  "  # !g ! !A AH0i 0r   