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