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