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