blob: 12212150c6dc8eb4a49e39d15fa810201be0f445 [file] [log] [blame]
wohlganger58fc71c2017-09-10 16:19:47 -05001"""ParenMatch -- for parenthesis matching.
David Scherer7aced172000-08-15 01:13:23 +00002
3When you hit a right paren, the cursor should move briefly to the left
4paren. Paren here is used generically; the matching applies to
5parentheses, square brackets, and curly braces.
David Scherer7aced172000-08-15 01:13:23 +00006"""
Terry Jan Reedy6fa5bdc2016-05-28 13:22:31 -04007from idlelib.hyperparser import HyperParser
8from idlelib.config import idleConf
David Scherer7aced172000-08-15 01:13:23 +00009
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010_openers = {')':'(',']':'[','}':'{'}
Martin Pantereb995702016-07-28 01:11:04 +000011CHECK_DELAY = 100 # milliseconds
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000012
David Scherer7aced172000-08-15 01:13:23 +000013class ParenMatch:
wohlgangerfae2c352017-06-27 21:36:23 -050014 """Highlight matching openers and closers, (), [], and {}.
David Scherer7aced172000-08-15 01:13:23 +000015
wohlgangerfae2c352017-06-27 21:36:23 -050016 There are three supported styles of paren matching. When a right
17 paren (opener) is typed:
David Scherer7aced172000-08-15 01:13:23 +000018
wohlgangerfae2c352017-06-27 21:36:23 -050019 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 Scherer7aced172000-08-15 01:13:23 +000023
wohlgangerfae2c352017-06-27 21:36:23 -050024 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 Scherer7aced172000-08-15 01:13:23 +000027
28 TODO:
wohlgangerfae2c352017-06-27 21:36:23 -050029 - 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 Scherer7aced172000-08-15 01:13:23 +000032 """
David Scherer7aced172000-08-15 01:13:23 +000033
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000034 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 Scherer7aced172000-08-15 01:13:23 +000040 def __init__(self, editwin):
41 self.editwin = editwin
42 self.text = editwin.text
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000043 # 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 Reedy3ff55a82016-08-10 23:44:54 -040048 self.bell = self.text.bell if self.BELL else lambda: None
David Scherer7aced172000-08-15 01:13:23 +000049 self.counter = 0
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000050 self.is_restore_active = 0
David Scherer7aced172000-08-15 01:13:23 +000051 self.set_style(self.STYLE)
52
wohlganger58fc71c2017-09-10 16:19:47 -050053 @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. Kaiserb1754452005-11-18 22:05:48 +000064 def activate_restore(self):
wohlgangerfae2c352017-06-27 21:36:23 -050065 "Activate mechanism to restore text from highlighting."
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000066 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):
wohlgangerfae2c352017-06-27 21:36:23 -050072 "Remove restore event bindings."
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000073 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 Scherer7aced172000-08-15 01:13:23 +000078 def set_style(self, style):
wohlgangerfae2c352017-06-27 21:36:23 -050079 "Set tag and timeout functions."
David Scherer7aced172000-08-15 01:13:23 +000080 self.STYLE = style
wohlgangerfae2c352017-06-27 21:36:23 -050081 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 Scherer7aced172000-08-15 01:13:23 +000088
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000089 def flash_paren_event(self, event):
wohlgangerfae2c352017-06-27 21:36:23 -050090 "Handle editor 'show surrounding parens' event (menu or shortcut)."
Terry Jan Reedy14fbe722014-06-17 16:35:20 -040091 indices = (HyperParser(self.editwin, "insert")
92 .get_surrounding_brackets())
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000093 if indices is None:
Terry Jan Reedy3ff55a82016-08-10 23:44:54 -040094 self.bell()
Serhiy Storchaka213ce122017-06-27 07:02:32 +030095 return "break"
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000096 self.activate_restore()
97 self.create_tag(indices)
wohlgangerfae2c352017-06-27 21:36:23 -050098 self.set_timeout()
Serhiy Storchaka213ce122017-06-27 07:02:32 +030099 return "break"
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000100
101 def paren_closed_event(self, event):
wohlgangerfae2c352017-06-27 21:36:23 -0500102 "Handle user input of closer."
103 # If user bound non-closer to <<paren-closed>>, quit.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000104 closer = self.text.get("insert-1c")
105 if closer not in _openers:
Terry Jan Reedy89225872017-08-07 13:37:10 -0400106 return
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000107 hp = HyperParser(self.editwin, "insert-1c")
108 if not hp.is_in_code():
Terry Jan Reedy89225872017-08-07 13:37:10 -0400109 return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000110 indices = hp.get_surrounding_brackets(_openers[closer], True)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000111 if indices is None:
Terry Jan Reedy3ff55a82016-08-10 23:44:54 -0400112 self.bell()
Terry Jan Reedy89225872017-08-07 13:37:10 -0400113 return
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000114 self.activate_restore()
115 self.create_tag(indices)
David Scherer7aced172000-08-15 01:13:23 +0000116 self.set_timeout()
Terry Jan Reedy89225872017-08-07 13:37:10 -0400117 return
David Scherer7aced172000-08-15 01:13:23 +0000118
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000119 def restore_event(self, event=None):
wohlgangerfae2c352017-06-27 21:36:23 -0500120 "Remove effect of doing match."
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000121 self.text.tag_delete("paren")
122 self.deactivate_restore()
123 self.counter += 1 # disable the last timer, if there is one.
David Scherer7aced172000-08-15 01:13:23 +0000124
125 def handle_restore_timer(self, timer_count):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000126 if timer_count == self.counter:
127 self.restore_event()
David Scherer7aced172000-08-15 01:13:23 +0000128
David Scherer7aced172000-08-15 01:13:23 +0000129 # any one of the create_tag_XXX methods can be used depending on
130 # the style
131
wohlgangerfae2c352017-06-27 21:36:23 -0500132 def create_tag_opener(self, indices):
David Scherer7aced172000-08-15 01:13:23 +0000133 """Highlight the single paren that matches"""
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000134 self.text.tag_add("paren", indices[0])
David Scherer7aced172000-08-15 01:13:23 +0000135 self.text.tag_config("paren", self.HILITE_CONFIG)
136
wohlgangerfae2c352017-06-27 21:36:23 -0500137 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. Kaiserb1754452005-11-18 22:05:48 +0000146 def create_tag_expression(self, indices):
David Scherer7aced172000-08-15 01:13:23 +0000147 """Highlight the entire expression"""
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000148 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 Scherer7aced172000-08-15 01:13:23 +0000153 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. Kaiserb1754452005-11-18 22:05:48 +0000159 """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 Scherer7aced172000-08-15 01:13:23 +0000172
173 def set_timeout_last(self):
wohlgangerfae2c352017-06-27 21:36:23 -0500174 """The last highlight created will be removed after FLASH_DELAY millisecs"""
David Scherer7aced172000-08-15 01:13:23 +0000175 # associate a counter with an event; only disable the "paren"
176 # tag if the event is for the most recent timer.
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000177 self.counter += 1
Terry Jan Reedy14fbe722014-06-17 16:35:20 -0400178 self.editwin.text_frame.after(
179 self.FLASH_DELAY,
180 lambda self=self, c=self.counter: self.handle_restore_timer(c))
181
182
wohlganger58fc71c2017-09-10 16:19:47 -0500183ParenMatch.reload()
184
185
Terry Jan Reedy14fbe722014-06-17 16:35:20 -0400186if __name__ == '__main__':
187 import unittest
188 unittest.main('idlelib.idle_test.test_parenmatch', verbosity=2)