annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/idlelib/parenmatch.py @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 """ParenMatch -- for parenthesis matching.
jpayne@69 2
jpayne@69 3 When you hit a right paren, the cursor should move briefly to the left
jpayne@69 4 paren. Paren here is used generically; the matching applies to
jpayne@69 5 parentheses, square brackets, and curly braces.
jpayne@69 6 """
jpayne@69 7 from idlelib.hyperparser import HyperParser
jpayne@69 8 from idlelib.config import idleConf
jpayne@69 9
jpayne@69 10 _openers = {')':'(',']':'[','}':'{'}
jpayne@69 11 CHECK_DELAY = 100 # milliseconds
jpayne@69 12
jpayne@69 13 class ParenMatch:
jpayne@69 14 """Highlight matching openers and closers, (), [], and {}.
jpayne@69 15
jpayne@69 16 There are three supported styles of paren matching. When a right
jpayne@69 17 paren (opener) is typed:
jpayne@69 18
jpayne@69 19 opener -- highlight the matching left paren (closer);
jpayne@69 20 parens -- highlight the left and right parens (opener and closer);
jpayne@69 21 expression -- highlight the entire expression from opener to closer.
jpayne@69 22 (For back compatibility, 'default' is a synonym for 'opener').
jpayne@69 23
jpayne@69 24 Flash-delay is the maximum milliseconds the highlighting remains.
jpayne@69 25 Any cursor movement (key press or click) before that removes the
jpayne@69 26 highlight. If flash-delay is 0, there is no maximum.
jpayne@69 27
jpayne@69 28 TODO:
jpayne@69 29 - Augment bell() with mismatch warning in status window.
jpayne@69 30 - Highlight when cursor is moved to the right of a closer.
jpayne@69 31 This might be too expensive to check.
jpayne@69 32 """
jpayne@69 33
jpayne@69 34 RESTORE_VIRTUAL_EVENT_NAME = "<<parenmatch-check-restore>>"
jpayne@69 35 # We want the restore event be called before the usual return and
jpayne@69 36 # backspace events.
jpayne@69 37 RESTORE_SEQUENCES = ("<KeyPress>", "<ButtonPress>",
jpayne@69 38 "<Key-Return>", "<Key-BackSpace>")
jpayne@69 39
jpayne@69 40 def __init__(self, editwin):
jpayne@69 41 self.editwin = editwin
jpayne@69 42 self.text = editwin.text
jpayne@69 43 # Bind the check-restore event to the function restore_event,
jpayne@69 44 # so that we can then use activate_restore (which calls event_add)
jpayne@69 45 # and deactivate_restore (which calls event_delete).
jpayne@69 46 editwin.text.bind(self.RESTORE_VIRTUAL_EVENT_NAME,
jpayne@69 47 self.restore_event)
jpayne@69 48 self.counter = 0
jpayne@69 49 self.is_restore_active = 0
jpayne@69 50
jpayne@69 51 @classmethod
jpayne@69 52 def reload(cls):
jpayne@69 53 cls.STYLE = idleConf.GetOption(
jpayne@69 54 'extensions','ParenMatch','style', default='opener')
jpayne@69 55 cls.FLASH_DELAY = idleConf.GetOption(
jpayne@69 56 'extensions','ParenMatch','flash-delay', type='int',default=500)
jpayne@69 57 cls.BELL = idleConf.GetOption(
jpayne@69 58 'extensions','ParenMatch','bell', type='bool', default=1)
jpayne@69 59 cls.HILITE_CONFIG = idleConf.GetHighlight(idleConf.CurrentTheme(),
jpayne@69 60 'hilite')
jpayne@69 61
jpayne@69 62 def activate_restore(self):
jpayne@69 63 "Activate mechanism to restore text from highlighting."
jpayne@69 64 if not self.is_restore_active:
jpayne@69 65 for seq in self.RESTORE_SEQUENCES:
jpayne@69 66 self.text.event_add(self.RESTORE_VIRTUAL_EVENT_NAME, seq)
jpayne@69 67 self.is_restore_active = True
jpayne@69 68
jpayne@69 69 def deactivate_restore(self):
jpayne@69 70 "Remove restore event bindings."
jpayne@69 71 if self.is_restore_active:
jpayne@69 72 for seq in self.RESTORE_SEQUENCES:
jpayne@69 73 self.text.event_delete(self.RESTORE_VIRTUAL_EVENT_NAME, seq)
jpayne@69 74 self.is_restore_active = False
jpayne@69 75
jpayne@69 76 def flash_paren_event(self, event):
jpayne@69 77 "Handle editor 'show surrounding parens' event (menu or shortcut)."
jpayne@69 78 indices = (HyperParser(self.editwin, "insert")
jpayne@69 79 .get_surrounding_brackets())
jpayne@69 80 self.finish_paren_event(indices)
jpayne@69 81 return "break"
jpayne@69 82
jpayne@69 83 def paren_closed_event(self, event):
jpayne@69 84 "Handle user input of closer."
jpayne@69 85 # If user bound non-closer to <<paren-closed>>, quit.
jpayne@69 86 closer = self.text.get("insert-1c")
jpayne@69 87 if closer not in _openers:
jpayne@69 88 return
jpayne@69 89 hp = HyperParser(self.editwin, "insert-1c")
jpayne@69 90 if not hp.is_in_code():
jpayne@69 91 return
jpayne@69 92 indices = hp.get_surrounding_brackets(_openers[closer], True)
jpayne@69 93 self.finish_paren_event(indices)
jpayne@69 94 return # Allow calltips to see ')'
jpayne@69 95
jpayne@69 96 def finish_paren_event(self, indices):
jpayne@69 97 if indices is None and self.BELL:
jpayne@69 98 self.text.bell()
jpayne@69 99 return
jpayne@69 100 self.activate_restore()
jpayne@69 101 # self.create_tag(indices)
jpayne@69 102 self.tagfuncs.get(self.STYLE, self.create_tag_expression)(self, indices)
jpayne@69 103 # self.set_timeout()
jpayne@69 104 (self.set_timeout_last if self.FLASH_DELAY else
jpayne@69 105 self.set_timeout_none)()
jpayne@69 106
jpayne@69 107 def restore_event(self, event=None):
jpayne@69 108 "Remove effect of doing match."
jpayne@69 109 self.text.tag_delete("paren")
jpayne@69 110 self.deactivate_restore()
jpayne@69 111 self.counter += 1 # disable the last timer, if there is one.
jpayne@69 112
jpayne@69 113 def handle_restore_timer(self, timer_count):
jpayne@69 114 if timer_count == self.counter:
jpayne@69 115 self.restore_event()
jpayne@69 116
jpayne@69 117 # any one of the create_tag_XXX methods can be used depending on
jpayne@69 118 # the style
jpayne@69 119
jpayne@69 120 def create_tag_opener(self, indices):
jpayne@69 121 """Highlight the single paren that matches"""
jpayne@69 122 self.text.tag_add("paren", indices[0])
jpayne@69 123 self.text.tag_config("paren", self.HILITE_CONFIG)
jpayne@69 124
jpayne@69 125 def create_tag_parens(self, indices):
jpayne@69 126 """Highlight the left and right parens"""
jpayne@69 127 if self.text.get(indices[1]) in (')', ']', '}'):
jpayne@69 128 rightindex = indices[1]+"+1c"
jpayne@69 129 else:
jpayne@69 130 rightindex = indices[1]
jpayne@69 131 self.text.tag_add("paren", indices[0], indices[0]+"+1c", rightindex+"-1c", rightindex)
jpayne@69 132 self.text.tag_config("paren", self.HILITE_CONFIG)
jpayne@69 133
jpayne@69 134 def create_tag_expression(self, indices):
jpayne@69 135 """Highlight the entire expression"""
jpayne@69 136 if self.text.get(indices[1]) in (')', ']', '}'):
jpayne@69 137 rightindex = indices[1]+"+1c"
jpayne@69 138 else:
jpayne@69 139 rightindex = indices[1]
jpayne@69 140 self.text.tag_add("paren", indices[0], rightindex)
jpayne@69 141 self.text.tag_config("paren", self.HILITE_CONFIG)
jpayne@69 142
jpayne@69 143 tagfuncs = {
jpayne@69 144 'opener': create_tag_opener,
jpayne@69 145 'default': create_tag_opener,
jpayne@69 146 'parens': create_tag_parens,
jpayne@69 147 'expression': create_tag_expression,
jpayne@69 148 }
jpayne@69 149
jpayne@69 150 # any one of the set_timeout_XXX methods can be used depending on
jpayne@69 151 # the style
jpayne@69 152
jpayne@69 153 def set_timeout_none(self):
jpayne@69 154 """Highlight will remain until user input turns it off
jpayne@69 155 or the insert has moved"""
jpayne@69 156 # After CHECK_DELAY, call a function which disables the "paren" tag
jpayne@69 157 # if the event is for the most recent timer and the insert has changed,
jpayne@69 158 # or schedules another call for itself.
jpayne@69 159 self.counter += 1
jpayne@69 160 def callme(callme, self=self, c=self.counter,
jpayne@69 161 index=self.text.index("insert")):
jpayne@69 162 if index != self.text.index("insert"):
jpayne@69 163 self.handle_restore_timer(c)
jpayne@69 164 else:
jpayne@69 165 self.editwin.text_frame.after(CHECK_DELAY, callme, callme)
jpayne@69 166 self.editwin.text_frame.after(CHECK_DELAY, callme, callme)
jpayne@69 167
jpayne@69 168 def set_timeout_last(self):
jpayne@69 169 """The last highlight created will be removed after FLASH_DELAY millisecs"""
jpayne@69 170 # associate a counter with an event; only disable the "paren"
jpayne@69 171 # tag if the event is for the most recent timer.
jpayne@69 172 self.counter += 1
jpayne@69 173 self.editwin.text_frame.after(
jpayne@69 174 self.FLASH_DELAY,
jpayne@69 175 lambda self=self, c=self.counter: self.handle_restore_timer(c))
jpayne@69 176
jpayne@69 177
jpayne@69 178 ParenMatch.reload()
jpayne@69 179
jpayne@69 180
jpayne@69 181 if __name__ == '__main__':
jpayne@69 182 from unittest import main
jpayne@69 183 main('idlelib.idle_test.test_parenmatch', verbosity=2)