<s> """<STR_LIT>""" <EOL> from pyqode . qt import QtGui <EOL> from pyqode . core . api import Mode , DelayJobRunner , TextHelper , TextDecoration <EOL> from pyqode . core . backend import NotRunning <EOL> from pyqode . core . backend . workers import findall <EOL> class OccurrencesHighlighterMode ( Mode ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def delay ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . timer . delay <EOL> @ delay . setter <EOL> def delay ( self , value ) : <EOL> self . timer . delay = value <EOL> if self . editor : <EOL> for clone in self . editor . clones : <EOL> try : <EOL> clone . modes . get ( self . __class__ ) . delay = value <EOL> except KeyError : <EOL> pass <EOL> @ property <EOL> def background ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _background <EOL> @ background . setter <EOL> def background ( self , value ) : <EOL> self . _background = value <EOL> if self . editor : <EOL> for clone in self . editor . clones : <EOL> try : <EOL> clone . modes . get ( self . __class__ ) . background = value <EOL> except KeyError : <EOL> pass <EOL> @ property <EOL> def foreground ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _foreground <EOL> @ foreground . setter <EOL> def foreground ( self , value ) : <EOL> self . _foreground = value <EOL> if self . editor : <EOL> for clone in self . editor . clones : <EOL> try : <EOL> clone . modes . get ( self . __class__ ) . foreground = value <EOL> except KeyError : <EOL> pass <EOL> @ property <EOL> def underlined ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _underlined <EOL> @ underlined . setter <EOL> def underlined ( self , value ) : <EOL> self . _underlined = value <EOL> if self . editor : <EOL> for clone in self . editor . clones : <EOL> try : <EOL> clone . modes . get ( self . __class__ ) . underlined = value <EOL> except KeyError : <EOL> pass <EOL> def __init__ ( self ) : <EOL> super ( OccurrencesHighlighterMode , self ) . __init__ ( ) <EOL> self . _decorations = [ ] <EOL> self . timer = DelayJobRunner ( delay = <NUM_LIT:1000> ) <EOL> self . _sub = None <EOL> self . _background = QtGui . QColor ( '<STR_LIT>' ) <EOL> self . _foreground = None <EOL> self . _underlined = False <EOL> def on_state_changed ( self , state ) : <EOL> if state : <EOL> self . editor . cursorPositionChanged . connect ( self . _request_highlight ) <EOL> else : <EOL> self . editor . cursorPositionChanged . disconnect ( <EOL> self . _request_highlight ) <EOL> self . timer . cancel_requests ( ) <EOL> def _clear_decos ( self ) : <EOL> for d in self . _decorations : <EOL> self . editor . decorations . remove ( d ) <EOL> self . _decorations [ : ] = [ ] <EOL> def _request_highlight ( self ) : <EOL> if self . editor is not None : <EOL> sub = TextHelper ( self . editor ) . word_under_cursor ( <EOL> select_whole_word = True ) . selectedText ( ) <EOL> if sub != self . _sub : <EOL> self . _clear_decos ( ) <EOL> if len ( sub ) > <NUM_LIT:1> : <EOL> self . timer . request_job ( self . _send_request ) <EOL> def _send_request ( self ) : <EOL> if self . editor is None : <EOL> return <EOL> cursor = self . editor . textCursor ( ) <EOL> self . _sub = TextHelper ( self . editor ) . word_under_cursor ( <EOL> select_whole_word = True ) . selectedText ( ) <EOL> if not cursor . hasSelection ( ) or cursor . selectedText ( ) == self . _sub : <EOL> request_data = { <EOL> '<STR_LIT:string>' : self . editor . toPlainText ( ) , <EOL> '<STR_LIT>' : self . _sub , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True <EOL> } <EOL> try : <EOL> self . editor . backend . send_request ( findall , request_data , <EOL> self . _on_results_available ) <EOL> except NotRunning : <EOL> self . _request_highlight ( ) <EOL> def _on_results_available ( self , results ) : <EOL> if len ( results ) > <NUM_LIT> : <EOL> results = results [ : <NUM_LIT> ] <EOL> current = self . editor . textCursor ( ) . position ( ) <EOL> if len ( results ) > <NUM_LIT:1> : <EOL> for start , end in results : <EOL> if start <= current <= end : <EOL> continue <EOL> deco = TextDecoration ( self . editor . textCursor ( ) , <EOL> start_pos = start , end_pos = end ) <EOL> if self . underlined : <EOL> deco . set_as_underlined ( self . _background ) <EOL> else : <EOL> deco . set_background ( QtGui . QBrush ( self . _background ) ) <EOL> if self . _foreground is not None : <EOL> deco . set_foreground ( self . _foreground ) <EOL> deco . draw_order = <NUM_LIT:3> <EOL> self . editor . decorations . append ( deco ) <EOL> self . _decorations . append ( deco ) <EOL> def clone_settings ( self , original ) : <EOL> self . delay = original . delay <EOL> self . background = original . background <EOL> self . foreground = original . foreground <EOL> self . underlined = original . underlined </s>
