<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> import sys <EOL> from pyqode . core . api import TextBlockHelper , folding , TextDecoration , DelayJobRunner <EOL> from pyqode . core . api . folding import FoldScope <EOL> from pyqode . core . api . panel import Panel <EOL> from pyqode . qt import QtCore , QtWidgets , QtGui , PYQT5_API <EOL> from pyqode . core . api . utils import TextHelper , drift_color , keep_tc_pos <EOL> def _logger ( ) : <EOL> """<STR_LIT>""" <EOL> return logging . getLogger ( __name__ ) <EOL> class FoldingPanel ( Panel ) : <EOL> """<STR_LIT>""" <EOL> trigger_state_changed = QtCore . Signal ( QtGui . QTextBlock , bool ) <EOL> collapse_all_triggered = QtCore . Signal ( ) <EOL> expand_all_triggered = QtCore . Signal ( ) <EOL> @ property <EOL> def native_look ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _native <EOL> @ native_look . setter <EOL> def native_look ( self , value ) : <EOL> self . _native = value <EOL> if self . editor : <EOL> for clone in self . editor . clones : <EOL> try : <EOL> clone . modes . get ( self . __class__ ) . native_look = value <EOL> except KeyError : <EOL> pass <EOL> @ property <EOL> def custom_indicators_icons ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _custom_indicators <EOL> @ custom_indicators_icons . setter <EOL> def custom_indicators_icons ( self , value ) : <EOL> if len ( value ) != <NUM_LIT:4> : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . _custom_indicators = value <EOL> if self . editor : <EOL> for clone in self . editor . clones : <EOL> try : <EOL> clone . modes . get ( <EOL> self . __class__ ) . custom_indicators_icons = value <EOL> except KeyError : <EOL> pass <EOL> @ property <EOL> def custom_fold_region_background ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _custom_color <EOL> @ custom_fold_region_background . setter <EOL> def custom_fold_region_background ( self , value ) : <EOL> self . _custom_color = value <EOL> if self . editor : <EOL> for clone in self . editor . clones : <EOL> try : <EOL> clone . modes . get ( <EOL> self . __class__ ) . custom_fold_region_background = value <EOL> except KeyError : <EOL> pass <EOL> @ property <EOL> def highlight_caret_scope ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _highlight_caret <EOL> @ highlight_caret_scope . setter <EOL> def highlight_caret_scope ( self , value ) : <EOL> if value != self . _highlight_caret : <EOL> self . _highlight_caret = value <EOL> if self . editor : <EOL> if value : <EOL> self . _block_nbr = - <NUM_LIT:1> <EOL> self . editor . cursorPositionChanged . connect ( <EOL> self . _highlight_caret_scope ) <EOL> else : <EOL> self . _block_nbr = - <NUM_LIT:1> <EOL> self . editor . cursorPositionChanged . disconnect ( <EOL> self . _highlight_caret_scope ) <EOL> for clone in self . editor . clones : <EOL> try : <EOL> clone . modes . get ( <EOL> self . __class__ ) . highlight_caret_scope = value <EOL> except KeyError : <EOL> pass <EOL> def __init__ ( self , highlight_caret_scope = False ) : <EOL> Panel . __init__ ( self ) <EOL> self . _native = True <EOL> self . _custom_indicators = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> self . _custom_color = QtGui . QColor ( '<STR_LIT>' ) <EOL> self . _block_nbr = - <NUM_LIT:1> <EOL> self . _highlight_caret = False <EOL> self . highlight_caret_scope = highlight_caret_scope <EOL> self . _indic_size = <NUM_LIT:16> <EOL> self . _scope_decos = [ ] <EOL> self . _block_decos = [ ] <EOL> self . setMouseTracking ( True ) <EOL> self . scrollable = True <EOL> self . _mouse_over_line = None <EOL> self . _current_scope = None <EOL> self . _prev_cursor = None <EOL> self . context_menu = None <EOL> self . action_collapse = None <EOL> self . action_expand = None <EOL> self . action_collapse_all = None <EOL> self . action_expand_all = None <EOL> self . _original_background = None <EOL> self . _highlight_runner = DelayJobRunner ( delay = <NUM_LIT> ) <EOL> def on_install ( self , editor ) : <EOL> """<STR_LIT>""" <EOL> super ( FoldingPanel , self ) . on_install ( editor ) <EOL> self . context_menu = QtWidgets . QMenu ( _ ( '<STR_LIT>' ) , self . editor ) <EOL> action = self . action_collapse = QtWidgets . QAction ( <EOL> _ ( '<STR_LIT>' ) , self . context_menu ) <EOL> action . setShortcut ( '<STR_LIT>' ) <EOL> action . triggered . connect ( self . _on_action_toggle ) <EOL> self . context_menu . addAction ( action ) <EOL> action = self . action_expand = QtWidgets . QAction ( _ ( '<STR_LIT>' ) , <EOL> self . context_menu ) <EOL> action . setShortcut ( '<STR_LIT>' ) <EOL> action . triggered . connect ( self . _on_action_toggle ) <EOL> self . context_menu . addAction ( action ) <EOL> self . context_menu . addSeparator ( ) <EOL> action = self . action_collapse_all = QtWidgets . QAction ( <EOL> _ ( '<STR_LIT>' ) , self . context_menu ) <EOL> action . setShortcut ( '<STR_LIT>' ) <EOL> action . triggered . connect ( self . _on_action_collapse_all_triggered ) <EOL> self . context_menu . addAction ( action ) <EOL> action = self . action_expand_all = QtWidgets . QAction ( <EOL> _ ( '<STR_LIT>' ) , self . context_menu ) <EOL> action . setShortcut ( '<STR_LIT>' ) <EOL> action . triggered . connect ( self . _on_action_expand_all_triggered ) <EOL> self . context_menu . addAction ( action ) <EOL> self . editor . add_menu ( self . context_menu ) <EOL> def sizeHint ( self ) : <EOL> """<STR_LIT>""" <EOL> fm = QtGui . QFontMetricsF ( self . editor . font ( ) ) <EOL> size_hint = QtCore . QSize ( fm . height ( ) , fm . height ( ) ) <EOL> if size_hint . width ( ) > <NUM_LIT:16> : <EOL> size_hint . setWidth ( <NUM_LIT:16> ) <EOL> return size_hint <EOL> def paintEvent ( self , event ) : <EOL> super ( FoldingPanel , self ) . paintEvent ( event ) <EOL> painter = QtGui . QPainter ( self ) <EOL> if self . _mouse_over_line is not None : <EOL> block = self . editor . document ( ) . findBlockByNumber ( <EOL> self . _mouse_over_line ) <EOL> try : <EOL> self . _draw_fold_region_background ( block , painter ) <EOL> except ValueError : <EOL> pass <EOL> for top_position , line_number , block in self . editor . visible_blocks : <EOL> if TextBlockHelper . is_fold_trigger ( block ) : <EOL> collapsed = TextBlockHelper . is_collapsed ( block ) <EOL> mouse_over = self . _mouse_over_line == line_number <EOL> self . _draw_fold_indicator ( <EOL> top_position , mouse_over , collapsed , painter ) <EOL> if collapsed : <EOL> for deco in self . _block_decos : <EOL> if deco . block == block : <EOL> break <EOL> else : <EOL> self . _add_fold_decoration ( block , FoldScope ( block ) ) <EOL> else : <EOL> for deco in self . _block_decos : <EOL> if deco . block == block : <EOL> self . _block_decos . remove ( deco ) <EOL> self . editor . decorations . remove ( deco ) <EOL> del deco <EOL> break <EOL> def _draw_fold_region_background ( self , block , painter ) : <EOL> """<STR_LIT>""" <EOL> r = folding . FoldScope ( block ) <EOL> th = TextHelper ( self . editor ) <EOL> start , end = r . get_range ( ignore_blank_lines = True ) <EOL> if start > <NUM_LIT:0> : <EOL> top = th . line_pos_from_number ( start ) <EOL> else : <EOL> top = <NUM_LIT:0> <EOL> bottom = th . line_pos_from_number ( end + <NUM_LIT:1> ) <EOL> h = bottom - top <EOL> if h == <NUM_LIT:0> : <EOL> h = self . sizeHint ( ) . height ( ) <EOL> w = self . sizeHint ( ) . width ( ) <EOL> self . _draw_rect ( QtCore . QRectF ( <NUM_LIT:0> , top , w , h ) , painter ) <EOL> def _draw_rect ( self , rect , painter ) : <EOL> """<STR_LIT>""" <EOL> c = self . _custom_color <EOL> if self . _native : <EOL> c = self . get_system_bck_color ( ) <EOL> grad = QtGui . QLinearGradient ( rect . topLeft ( ) , <EOL> rect . topRight ( ) ) <EOL> if sys . platform == '<STR_LIT>' : <EOL> grad . setColorAt ( <NUM_LIT:0> , c . lighter ( <NUM_LIT:100> ) ) <EOL> grad . setColorAt ( <NUM_LIT:1> , c . lighter ( <NUM_LIT> ) ) <EOL> outline = c . darker ( <NUM_LIT> ) <EOL> else : <EOL> grad . setColorAt ( <NUM_LIT:0> , c . lighter ( <NUM_LIT> ) ) <EOL> grad . setColorAt ( <NUM_LIT:1> , c . lighter ( <NUM_LIT> ) ) <EOL> outline = c . darker ( <NUM_LIT:100> ) <EOL> painter . fillRect ( rect , grad ) <EOL> painter . setPen ( QtGui . QPen ( outline ) ) <EOL> painter . drawLine ( rect . topLeft ( ) + <EOL> QtCore . QPointF ( <NUM_LIT:1> , <NUM_LIT:0> ) , <EOL> rect . topRight ( ) - <EOL> QtCore . QPointF ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> painter . drawLine ( rect . bottomLeft ( ) + <EOL> QtCore . QPointF ( <NUM_LIT:1> , <NUM_LIT:0> ) , <EOL> rect . bottomRight ( ) - <EOL> QtCore . QPointF ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> painter . drawLine ( rect . topRight ( ) + <EOL> QtCore . QPointF ( <NUM_LIT:0> , <NUM_LIT:1> ) , <EOL> rect . bottomRight ( ) - <EOL> QtCore . QPointF ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> painter . drawLine ( rect . topLeft ( ) + <EOL> QtCore . QPointF ( <NUM_LIT:0> , <NUM_LIT:1> ) , <EOL> rect . bottomLeft ( ) - <EOL> QtCore . QPointF ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> @ staticmethod <EOL> def get_system_bck_color ( ) : <EOL> """<STR_LIT>""" <EOL> def merged_colors ( colorA , colorB , factor ) : <EOL> maxFactor = <NUM_LIT:100> <EOL> colorA = QtGui . QColor ( colorA ) <EOL> colorB = QtGui . QColor ( colorB ) <EOL> tmp = colorA <EOL> tmp . setRed ( ( tmp . red ( ) * factor ) / maxFactor + <EOL> ( colorB . red ( ) * ( maxFactor - factor ) ) / maxFactor ) <EOL> tmp . setGreen ( ( tmp . green ( ) * factor ) / maxFactor + <EOL> ( colorB . green ( ) * ( maxFactor - factor ) ) / maxFactor ) <EOL> tmp . setBlue ( ( tmp . blue ( ) * factor ) / maxFactor + <EOL> ( colorB . blue ( ) * ( maxFactor - factor ) ) / maxFactor ) <EOL> return tmp <EOL> pal = QtWidgets . QApplication . instance ( ) . palette ( ) <EOL> b = pal . window ( ) . color ( ) <EOL> h = pal . highlight ( ) . color ( ) <EOL> return merged_colors ( b , h , <NUM_LIT:50> ) <EOL> def _draw_fold_indicator ( self , top , mouse_over , collapsed , painter ) : <EOL> """<STR_LIT>""" <EOL> rect = QtCore . QRect ( <NUM_LIT:0> , top , self . sizeHint ( ) . width ( ) , <EOL> self . sizeHint ( ) . height ( ) ) <EOL> if self . _native : <EOL> if os . environ [ '<STR_LIT>' ] . lower ( ) not in PYQT5_API : <EOL> opt = QtGui . QStyleOptionViewItemV2 ( ) <EOL> else : <EOL> opt = QtWidgets . QStyleOptionViewItem ( ) <EOL> opt . rect = rect <EOL> opt . state = ( QtWidgets . QStyle . State_Active | <EOL> QtWidgets . QStyle . State_Item | <EOL> QtWidgets . QStyle . State_Children ) <EOL> if not collapsed : <EOL> opt . state |= QtWidgets . QStyle . State_Open <EOL> if mouse_over : <EOL> opt . state |= ( QtWidgets . QStyle . State_MouseOver | <EOL> QtWidgets . QStyle . State_Enabled | <EOL> QtWidgets . QStyle . State_Selected ) <EOL> opt . palette . setBrush ( QtGui . QPalette . Window , <EOL> self . palette ( ) . highlight ( ) ) <EOL> opt . rect . translate ( - <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> self . style ( ) . drawPrimitive ( QtWidgets . QStyle . PE_IndicatorBranch , <EOL> opt , painter , self ) <EOL> else : <EOL> index = <NUM_LIT:0> <EOL> if not collapsed : <EOL> index = <NUM_LIT:2> <EOL> if mouse_over : <EOL> index += <NUM_LIT:1> <EOL> QtGui . QIcon ( self . _custom_indicators [ index ] ) . paint ( painter , rect ) <EOL> @ staticmethod <EOL> def find_parent_scope ( block ) : <EOL> """<STR_LIT>""" <EOL> original = block <EOL> if not TextBlockHelper . is_fold_trigger ( block ) : <EOL> while block . text ( ) . strip ( ) == '<STR_LIT>' and block . isValid ( ) : <EOL> block = block . next ( ) <EOL> ref_lvl = TextBlockHelper . get_fold_lvl ( block ) - <NUM_LIT:1> <EOL> block = original <EOL> while ( block . blockNumber ( ) and <EOL> ( not TextBlockHelper . is_fold_trigger ( block ) or <EOL> TextBlockHelper . get_fold_lvl ( block ) > ref_lvl ) ) : <EOL> block = block . previous ( ) <EOL> return block <EOL> def _clear_scope_decos ( self ) : <EOL> """<STR_LIT>""" <EOL> for deco in self . _scope_decos : <EOL> self . editor . decorations . remove ( deco ) <EOL> self . _scope_decos [ : ] = [ ] <EOL> def _get_scope_highlight_color ( self ) : <EOL> """<STR_LIT>""" <EOL> color = self . editor . background <EOL> if color . lightness ( ) < <NUM_LIT> : <EOL> color = drift_color ( color , <NUM_LIT> ) <EOL> else : <EOL> color = drift_color ( color , <NUM_LIT> ) <EOL> return color <EOL> def _add_scope_deco ( self , start , end , parent_start , parent_end , base_color , <EOL> factor ) : <EOL> """<STR_LIT>""" <EOL> color = drift_color ( base_color , factor = factor ) <EOL> if start > <NUM_LIT:0> : <EOL> d = TextDecoration ( self . editor . document ( ) , <EOL> start_line = parent_start , end_line = start ) <EOL> d . set_full_width ( True , clear = False ) <EOL> d . draw_order = <NUM_LIT:2> <EOL> d . set_background ( color ) <EOL> self . editor . decorations . append ( d ) <EOL> self . _scope_decos . append ( d ) <EOL> if end <= self . editor . document ( ) . blockCount ( ) : <EOL> d = TextDecoration ( self . editor . document ( ) , <EOL> start_line = end , end_line = parent_end + <NUM_LIT:1> ) <EOL> d . set_full_width ( True , clear = False ) <EOL> d . draw_order = <NUM_LIT:2> <EOL> d . set_background ( color ) <EOL> self . editor . decorations . append ( d ) <EOL> self . _scope_decos . append ( d ) <EOL> def _add_scope_decorations ( self , block , start , end ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> parent = FoldScope ( block ) . parent ( ) <EOL> except ValueError : <EOL> parent = None <EOL> if TextBlockHelper . is_fold_trigger ( block ) : <EOL> base_color = self . _get_scope_highlight_color ( ) <EOL> factor_step = <NUM_LIT:5> <EOL> if base_color . lightness ( ) < <NUM_LIT> : <EOL> factor_step = <NUM_LIT:10> <EOL> factor = <NUM_LIT> <EOL> else : <EOL> factor = <NUM_LIT:100> <EOL> while parent : <EOL> parent_start , parent_end = parent . get_range ( ) <EOL> self . _add_scope_deco ( <EOL> start , end + <NUM_LIT:1> , parent_start , parent_end , <EOL> base_color , factor ) <EOL> start = parent_start <EOL> end = parent_end <EOL> parent = parent . parent ( ) <EOL> factor += factor_step <EOL> parent_start = <NUM_LIT:0> <EOL> parent_end = self . editor . document ( ) . blockCount ( ) <EOL> self . _add_scope_deco ( <EOL> start , end + <NUM_LIT:1> , parent_start , parent_end , base_color , <EOL> factor + factor_step ) <EOL> else : <EOL> self . _clear_scope_decos ( ) <EOL> def _highlight_surrounding_scopes ( self , block ) : <EOL> """<STR_LIT>""" <EOL> scope = FoldScope ( block ) <EOL> if ( self . _current_scope is None or <EOL> self . _current_scope . get_range ( ) != scope . get_range ( ) ) : <EOL> self . _current_scope = scope <EOL> self . _clear_scope_decos ( ) <EOL> start , end = scope . get_range ( ) <EOL> if not TextBlockHelper . is_collapsed ( block ) : <EOL> self . _add_scope_decorations ( block , start , end ) <EOL> def mouseMoveEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> super ( FoldingPanel , self ) . mouseMoveEvent ( event ) <EOL> th = TextHelper ( self . editor ) <EOL> line = th . line_nbr_from_position ( event . pos ( ) . y ( ) ) <EOL> if line >= <NUM_LIT:0> : <EOL> block = FoldScope . find_parent_scope ( <EOL> self . editor . document ( ) . findBlockByNumber ( line ) ) <EOL> if TextBlockHelper . is_fold_trigger ( block ) : <EOL> if self . _mouse_over_line is None : <EOL> QtWidgets . QApplication . setOverrideCursor ( <EOL> QtGui . QCursor ( QtCore . Qt . PointingHandCursor ) ) <EOL> if self . _mouse_over_line != block . blockNumber ( ) and self . _mouse_over_line is not None : <EOL> self . _mouse_over_line = block . blockNumber ( ) <EOL> self . _highlight_surrounding_scopes ( block ) <EOL> else : <EOL> self . _mouse_over_line = block . blockNumber ( ) <EOL> self . _highlight_runner . request_job ( <EOL> self . _highlight_surrounding_scopes , block ) <EOL> self . _highight_block = block <EOL> else : <EOL> self . _highlight_runner . cancel_requests ( ) <EOL> self . _mouse_over_line = None <EOL> QtWidgets . QApplication . restoreOverrideCursor ( ) <EOL> self . repaint ( ) <EOL> def leaveEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> super ( FoldingPanel , self ) . leaveEvent ( event ) <EOL> QtWidgets . QApplication . restoreOverrideCursor ( ) <EOL> self . _highlight_runner . cancel_requests ( ) <EOL> if not self . highlight_caret_scope : <EOL> self . _clear_scope_decos ( ) <EOL> self . _mouse_over_line = None <EOL> self . _current_scope = None <EOL> else : <EOL> self . _block_nbr = - <NUM_LIT:1> <EOL> self . _highlight_caret_scope ( ) <EOL> self . editor . repaint ( ) <EOL> def _add_fold_decoration ( self , block , region ) : <EOL> """<STR_LIT>""" <EOL> deco = TextDecoration ( block ) <EOL> deco . signals . clicked . connect ( self . _on_fold_deco_clicked ) <EOL> deco . tooltip = region . text ( max_lines = <NUM_LIT> ) <EOL> deco . draw_order = <NUM_LIT:1> <EOL> deco . block = block <EOL> deco . select_line ( ) <EOL> deco . set_outline ( drift_color ( <EOL> self . _get_scope_highlight_color ( ) , <NUM_LIT> ) ) <EOL> deco . set_background ( self . _get_scope_highlight_color ( ) ) <EOL> deco . set_foreground ( QtGui . QColor ( '<STR_LIT>' ) ) <EOL> self . _block_decos . append ( deco ) <EOL> self . editor . decorations . append ( deco ) <EOL> def toggle_fold_trigger ( self , block ) : <EOL> """<STR_LIT>""" <EOL> if not TextBlockHelper . is_fold_trigger ( block ) : <EOL> return <EOL> region = FoldScope ( block ) <EOL> if region . collapsed : <EOL> region . unfold ( ) <EOL> if self . _mouse_over_line is not None : <EOL> self . _add_scope_decorations ( <EOL> region . _trigger , * region . get_range ( ) ) <EOL> else : <EOL> region . fold ( ) <EOL> self . _clear_scope_decos ( ) <EOL> self . _refresh_editor_and_scrollbars ( ) <EOL> self . trigger_state_changed . emit ( region . _trigger , region . collapsed ) <EOL> def mousePressEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if self . _mouse_over_line is not None : <EOL> block = self . editor . document ( ) . findBlockByNumber ( <EOL> self . _mouse_over_line ) <EOL> self . toggle_fold_trigger ( block ) <EOL> def _on_fold_deco_clicked ( self , deco ) : <EOL> """<STR_LIT>""" <EOL> self . toggle_fold_trigger ( deco . block ) <EOL> def on_state_changed ( self , state ) : <EOL> """<STR_LIT>""" <EOL> if state : <EOL> self . editor . key_pressed . connect ( self . _on_key_pressed ) <EOL> if self . _highlight_caret : <EOL> self . editor . cursorPositionChanged . connect ( <EOL> self . _highlight_caret_scope ) <EOL> self . _block_nbr = - <NUM_LIT:1> <EOL> self . editor . new_text_set . connect ( self . _clear_block_deco ) <EOL> else : <EOL> self . editor . key_pressed . disconnect ( self . _on_key_pressed ) <EOL> if self . _highlight_caret : <EOL> self . editor . cursorPositionChanged . disconnect ( <EOL> self . _highlight_caret_scope ) <EOL> self . _block_nbr = - <NUM_LIT:1> <EOL> self . editor . new_text_set . disconnect ( self . _clear_block_deco ) <EOL> def _on_key_pressed ( self , event ) : <EOL> """<STR_LIT>""" <EOL> delete_request = event . key ( ) in [ QtCore . Qt . Key_Backspace , <EOL> QtCore . Qt . Key_Delete ] <EOL> if event . text ( ) or delete_request : <EOL> cursor = self . editor . textCursor ( ) <EOL> if cursor . hasSelection ( ) : <EOL> positions_to_check = cursor . selectionStart ( ) , cursor . selectionEnd ( ) <EOL> else : <EOL> positions_to_check = ( cursor . position ( ) , ) <EOL> for pos in positions_to_check : <EOL> block = self . editor . document ( ) . findBlock ( pos ) <EOL> th = TextBlockHelper ( ) <EOL> if th . is_fold_trigger ( block ) and th . is_collapsed ( block ) : <EOL> self . toggle_fold_trigger ( block ) <EOL> if delete_request and cursor . hasSelection ( ) : <EOL> scope = FoldScope ( self . find_parent_scope ( block ) ) <EOL> tc = TextHelper ( self . editor ) . select_lines ( * scope . get_range ( ) ) <EOL> if tc . selectionStart ( ) > cursor . selectionStart ( ) : <EOL> start = cursor . selectionStart ( ) <EOL> else : <EOL> start = tc . selectionStart ( ) <EOL> if tc . selectionEnd ( ) < cursor . selectionEnd ( ) : <EOL> end = cursor . selectionEnd ( ) <EOL> else : <EOL> end = tc . selectionEnd ( ) <EOL> tc . setPosition ( start ) <EOL> tc . setPosition ( end , tc . KeepAnchor ) <EOL> self . editor . setTextCursor ( tc ) <EOL> @ staticmethod <EOL> def _show_previous_blank_lines ( block ) : <EOL> """<STR_LIT>""" <EOL> pblock = block . previous ( ) <EOL> while ( pblock . text ( ) . strip ( ) == '<STR_LIT>' and <EOL> pblock . blockNumber ( ) >= <NUM_LIT:0> ) : <EOL> pblock . setVisible ( True ) <EOL> pblock = pblock . previous ( ) <EOL> def refresh_decorations ( self , force = False ) : <EOL> """<STR_LIT>""" <EOL> cursor = self . editor . textCursor ( ) <EOL> if ( self . _prev_cursor is None or force or <EOL> self . _prev_cursor . blockNumber ( ) != cursor . blockNumber ( ) ) : <EOL> for deco in self . _block_decos : <EOL> self . editor . decorations . remove ( deco ) <EOL> for deco in self . _block_decos : <EOL> deco . set_outline ( drift_color ( <EOL> self . _get_scope_highlight_color ( ) , <NUM_LIT> ) ) <EOL> deco . set_background ( self . _get_scope_highlight_color ( ) ) <EOL> self . editor . decorations . append ( deco ) <EOL> self . _prev_cursor = cursor <EOL> def _refresh_editor_and_scrollbars ( self ) : <EOL> """<STR_LIT>""" <EOL> TextHelper ( self . editor ) . mark_whole_doc_dirty ( ) <EOL> self . editor . repaint ( ) <EOL> s = self . editor . size ( ) <EOL> s . setWidth ( s . width ( ) + <NUM_LIT:1> ) <EOL> self . editor . resizeEvent ( QtGui . QResizeEvent ( self . editor . size ( ) , s ) ) <EOL> def collapse_all ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _clear_block_deco ( ) <EOL> block = self . editor . document ( ) . firstBlock ( ) <EOL> last = self . editor . document ( ) . lastBlock ( ) <EOL> while block . isValid ( ) : <EOL> lvl = TextBlockHelper . get_fold_lvl ( block ) <EOL> trigger = TextBlockHelper . is_fold_trigger ( block ) <EOL> if trigger : <EOL> if lvl == <NUM_LIT:0> : <EOL> self . _show_previous_blank_lines ( block ) <EOL> TextBlockHelper . set_collapsed ( block , True ) <EOL> block . setVisible ( lvl == <NUM_LIT:0> ) <EOL> if block == last and block . text ( ) . strip ( ) == '<STR_LIT>' : <EOL> block . setVisible ( True ) <EOL> self . _show_previous_blank_lines ( block ) <EOL> block = block . next ( ) <EOL> self . _refresh_editor_and_scrollbars ( ) <EOL> tc = self . editor . textCursor ( ) <EOL> tc . movePosition ( tc . Start ) <EOL> self . editor . setTextCursor ( tc ) <EOL> self . collapse_all_triggered . emit ( ) <EOL> def _clear_block_deco ( self ) : <EOL> """<STR_LIT>""" <EOL> for deco in self . _block_decos : <EOL> self . editor . decorations . remove ( deco ) <EOL> self . _block_decos [ : ] = [ ] <EOL> def expand_all ( self ) : <EOL> """<STR_LIT>""" <EOL> block = self . editor . document ( ) . firstBlock ( ) <EOL> while block . isValid ( ) : <EOL> TextBlockHelper . set_collapsed ( block , False ) <EOL> block . setVisible ( True ) <EOL> block = block . next ( ) <EOL> self . _clear_block_deco ( ) <EOL> self . _refresh_editor_and_scrollbars ( ) <EOL> self . expand_all_triggered . emit ( ) <EOL> def _on_action_toggle ( self ) : <EOL> """<STR_LIT>""" <EOL> block = FoldScope . find_parent_scope ( self . editor . textCursor ( ) . block ( ) ) <EOL> self . toggle_fold_trigger ( block ) <EOL> def _on_action_collapse_all_triggered ( self ) : <EOL> """<STR_LIT>""" <EOL> self . collapse_all ( ) <EOL> def _on_action_expand_all_triggered ( self ) : <EOL> """<STR_LIT>""" <EOL> self . expand_all ( ) <EOL> def _highlight_caret_scope ( self ) : <EOL> """<STR_LIT>""" <EOL> cursor = self . editor . textCursor ( ) <EOL> block_nbr = cursor . blockNumber ( ) <EOL> if self . _block_nbr != block_nbr : <EOL> block = FoldScope . find_parent_scope ( <EOL> self . editor . textCursor ( ) . block ( ) ) <EOL> try : <EOL> s = FoldScope ( block ) <EOL> except ValueError : <EOL> self . _clear_scope_decos ( ) <EOL> else : <EOL> self . _mouse_over_line = block . blockNumber ( ) <EOL> if TextBlockHelper . is_fold_trigger ( block ) : <EOL> self . _highlight_surrounding_scopes ( block ) <EOL> self . _block_nbr = block_nbr <EOL> def clone_settings ( self , original ) : <EOL> self . native_look = original . native_look <EOL> self . custom_indicators_icons = original . custom_indicators_icons <EOL> self . highlight_caret_scope = original . highlight_caret_scope <EOL> self . custom_fold_region_background = original . custom_fold_region_background </s>
