)L i.FdZddlmZddlZddlmZmZddlmZm Z m Z m Z m Z m Z ddlmZddlmZmZddlmZdd lmZdd lmZd d lmZmZerdd lmZgdZGddeZGddeZ GddeZ!Gdde e e"dfe"fZ#e#Z$GddeZ%y)z Adaptor classes for using Pygments lexers within prompt_toolkit. This includes syntax synchronization code, so that we don't have to start lexing at the beginning of a document, when displaying a very large text. ) annotationsN)ABCMetaabstractmethod) TYPE_CHECKINGCallableDict GeneratorIterableTuple)Document) FilterOrBool to_filter)StyleAndTextTuples) split_linespygments_token_to_classname)Lexer SimpleLexer)r) PygmentsLexer SyntaxSync SyncFromStart RegexSyncc.eZdZdZe ddZy)ra Syntax synchronizer. This is a tool that finds a start position for the lexer. This is especially important when editing big documents; we don't want to start the highlighting by running the lexer from the beginning of the file. That is very slow when editing. cy)a/ Return the position from where we can start lexing as a (row, column) tuple. :param document: `Document` instance that contains all the lines. :param lineno: The line that we want to highlight. (We need to return this line, or an earlier position.) Nselfdocumentlinenos d/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/prompt_toolkit/lexers/pygments.pyget_sync_start_positionz"SyntaxSync.get_sync_start_position)sNrr r intreturnztuple[int, int])__name__ __module__ __qualname____doc__rr"rr#r!rr!s3     *-      r#r) metaclassc$eZdZdZ ddZy)rzB Always start the syntax highlighting from the beginning. cy)Nrrrrs r!r"z%SyncFromStart.get_sync_start_position<sr#Nr$)r'r(r)r*r"rr#r!rr7s$ *- r#rcFeZdZdZdZdZddZ d dZed dZ y) rzQ Synchronize by starting at a line that matches the given regex pattern. idc8tj||_y)N)recompile_compiled_pattern)rpatterns r!__init__zRegexSync.__init__Os!#G!4r#c|j}|j}t|td||jz dD],}|j ||}|s||j fcS||jkry|dfS)zH Scan backwards, and find a possible position to start. r.r)r4linesrangemax MAX_BACKWARDSmatchstartFROM_START_IF_NO_SYNC_POS_FOUND)rrr r5r9ir=s r!r"z!RegexSync.get_sync_start_positionRs ((vs2v0B0B'BCRH (AMM%(+E%++-'' ( D88 819 r#cXddddd}|j|jd}||S)zV Create a :class:`.RegexSync` instance for this Pygments lexer class. z^\s*(class|def)\s+z <[/a-zA-Z]z \bfunction\b)PythonzPython 3HTML JavaScript^)getname)cls lexer_clspatternsps r!from_pygments_lexer_clsz!RegexSync.from_pygments_lexer_clsjs6,-!)   LL -1v r#N)r5strr&Noner$)rIPygmentsLexerClsr&r) r'r(r)r*r<r?r6r" classmethodrLrr#r!rrBsL M'*#5 *- 0r#rceZdZdZddZy) _TokenCachez Cache that converts Pygments tokens into `prompt_toolkit` style objects. ``Token.A.B.C`` will be converted into: ``class:pygments,pygments.A,pygments.A.B,pygments.A.B.C`` c,dt|z}|||<|S)Nzclass:r)rkeyresults r! __missing__z_TokenCache.__missing__s 7<<S  r#N)rTztuple[str, ...]r&rM)r'r(r)r*rVrr#r!rRrR~s r#rR.c\eZdZdZdZdZ d d dZe d d dZd dZ y) ra Lexer that calls a pygments lexer. Example:: from pygments.lexers.html import HtmlLexer lexer = PygmentsLexer(HtmlLexer) Note: Don't forget to also load a Pygments compatible style. E.g.:: from prompt_toolkit.styles.from_pygments import style_from_pygments_cls from pygments.styles import get_style_by_name style = style_from_pygments_cls(get_style_by_name('monokai')) :param pygments_lexer_cls: A `Lexer` from Pygments. :param sync_from_start: Start lexing at the start of the document. This will always give the best results, but it will be slow for bigger documents. (When the last part of the document is display, then the whole document will be lexed by Pygments on every key stroke.) It is recommended to disable this for inputs that are expected to be more than 1,000 lines. :param syntax_sync: `SyntaxSync` object. 2r0Nc||_t||_|ddd|_|xstj ||_y)NF)stripnlstripallensurenl)pygments_lexer_clsrsync_from_startpygments_lexerrrL syntax_sync)rr]r^r`s r!r6zPygmentsLexer.__init__sN #5(91EE  ' )*K*K + r#c~ddlm}ddlm} ||}||j|S#|$r t cYSwxYw)z3 Create a `Lexer` from a filename. r)get_lexer_for_filename) ClassNotFound)r^)pygments.lexersrb pygments.utilrc __class__r)rHfilenamer^rbrcr_s r! from_filenamezPygmentsLexer.from_filenamesH ;/ R3H=N~//Q Q !=  !s *<<c ttttfddf}ii dfd d fd d d fd d  fd d  fd }|S) z Create a lexer function that takes a line number and returns the list of (style_str, text) tuples as the Pygments lexer returns for that line. NcPjr tSjS)z8The Syntax synchronization object that we currently use.)r^rr`)rsr!get_syntax_syncz3PygmentsLexer.lex_document..get_syntax_syncs###%$&'''r#cnjD]!\}}||ks ||z jks|cSy)z@Return a generator close to line 'i', or None if none was found.N)itemsREUSE_GENERATOR_MAX_DISTANCE)r@ generatorr line_generatorsrs r!find_closest_generatorz:PygmentsLexer.lex_document..find_closest_generatorsC%4%:%:%< %! 6A:!f*t/P/P"P$$ %r#c3~Kdfd }ttt|Ed{y7w)z Create a generator that yields the lexed lines. Each iteration it yields a (line_number, [(style_str, text), ...]) tuple. c3Kdjjdd}jj|D]\}}}t||fyw)N )joinr9r_get_tokens_unprocessed _token_cache)text_tvcolumnrr start_linenos r!get_text_fragmentszUPygmentsLexer.lex_document..create_line_generator..get_text_fragmentss`yy !>?H $22II$O-GAq!'q/1,,-sAAN)r&zIterable[tuple[str, str]]) enumeraterlist)r}r|r~rrs`` r!create_line_generatorz9PygmentsLexer.lex_document..create_line_generators1 - -!T2D2F-G!H,W W Ws 0=;=c|}|r|Std| jz }|dk(rd}d}nj|\}}|}|r|S||}|rt||dz }||<|S)z_ Find an already started generator that is close, or create a new one. rr)r;MIN_LINES_BACKWARDSr"next) r@rorowr|rrrqrkrprs r! get_generatorz1PygmentsLexer.lex_document..get_generator s /q1I  Aq43334AAv-/GGRST V/q1I  1#v> Yq),OI & r#c |S#t$r;|}|D]*\}}||<||k(s||<|dzvr|dz=|ccYSYgSwxYw)z*Return the tokens for a given line number.r)KeyError)r@ronumlinecacherrps r!get_linez,PygmentsLexer.lex_document..get_line0s *Qx *)!, "+ *IC!%E#Jax56 27e+ %cAg$Sz) *I% *s#A A A  A )r&r)r@r%r&zLineGenerator | None)r)r}r%r|r%r& LineGenerator)r@r%r&r)r@r%r&r)r r r%r) rrrrrrrqrrkrps `` @@@@@@r! lex_documentzPygmentsLexer.lex_documentsV "%-?(?"@$"LM 0257 (  X(% % N 0r#)TN)r]ztype[PygmentsLexerCls]r^r r`zSyntaxSync | Noner&rN)T)rgrMr^r r&r)rr r&z#Callable[[int], StyleAndTextTuples]) r'r(r)r*rrnr6rPrhrrr#r!rrs: $' )-)-  2 & '    &<@RR-9R RR"or#r)&r* __future__rr2abcrrtypingrrrr r r prompt_toolkit.documentr prompt_toolkit.filtersr r"prompt_toolkit.formatted_text.baser#prompt_toolkit.formatted_text.utilsrprompt_toolkit.styles.pygmentsrbaserrpygments.lexerrO__all__rrrrMrRrwrrr#r!rs# 'LL,:A;F$8  7 ,J9 9x $uS#X+, } yEyr#