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