|
7 | 7 | enclosing block. The number of hint lines is determined by the maxlines
|
8 | 8 | variable in the codecontext section of config-extensions.def. Lines which do
|
9 | 9 | not open blocks are not shown in the context hints pane.
|
10 |
| -
|
11 | 10 | """
|
12 | 11 | import re
|
13 | 12 | from sys import maxsize as INFINITY
|
|
17 | 16 |
|
18 | 17 | from idlelib.config import idleConf
|
19 | 18 |
|
20 |
| -BLOCKOPENERS = {"class", "def", "elif", "else", "except", "finally", "for", |
21 |
| - "if", "try", "while", "with", "async"} |
| 19 | +BLOCKOPENERS = {'class', 'def', 'if', 'elif', 'else', 'while', 'for', |
| 20 | + 'try', 'except', 'finally', 'with', 'async'} |
22 | 21 |
|
23 | 22 |
|
24 | 23 | def get_spaces_firstword(codeline, c=re.compile(r"^(\s*)(\w*)")):
|
@@ -84,7 +83,7 @@ def __del__(self):
|
84 | 83 | if self.t1 is not None:
|
85 | 84 | try:
|
86 | 85 | self.text.after_cancel(self.t1)
|
87 |
| - except tkinter.TclError: |
| 86 | + except tkinter.TclError: # pragma: no cover |
88 | 87 | pass
|
89 | 88 | self.t1 = None
|
90 | 89 |
|
@@ -112,19 +111,19 @@ def toggle_code_context_event(self, event=None):
|
112 | 111 | padx += widget.tk.getint(info['padx'])
|
113 | 112 | padx += widget.tk.getint(widget.cget('padx'))
|
114 | 113 | border += widget.tk.getint(widget.cget('border'))
|
115 |
| - self.context = tkinter.Text( |
| 114 | + context = self.context = tkinter.Text( |
116 | 115 | self.editwin.text_frame,
|
117 | 116 | height=1,
|
118 | 117 | width=1, # Don't request more than we get.
|
119 | 118 | highlightthickness=0,
|
120 | 119 | padx=padx, border=border, relief=SUNKEN, state='disabled')
|
121 | 120 | self.update_font()
|
122 | 121 | self.update_highlight_colors()
|
123 |
| - self.context.bind('<ButtonRelease-1>', self.jumptoline) |
| 122 | + context.bind('<ButtonRelease-1>', self.jumptoline) |
124 | 123 | # Get the current context and initiate the recurring update event.
|
125 | 124 | self.timer_event()
|
126 | 125 | # Grid the context widget above the text widget.
|
127 |
| - self.context.grid(row=0, column=1, sticky=NSEW) |
| 126 | + context.grid(row=0, column=1, sticky=NSEW) |
128 | 127 |
|
129 | 128 | line_number_colors = idleConf.GetHighlight(idleConf.CurrentTheme(),
|
130 | 129 | 'linenumber')
|
@@ -215,18 +214,25 @@ def update_code_context(self):
|
215 | 214 | self.context['state'] = 'disabled'
|
216 | 215 |
|
217 | 216 | def jumptoline(self, event=None):
|
218 |
| - "Show clicked context line at top of editor." |
219 |
| - lines = len(self.info) |
220 |
| - if lines == 1: # No context lines are showing. |
221 |
| - newtop = 1 |
222 |
| - else: |
223 |
| - # Line number clicked. |
224 |
| - contextline = int(float(self.context.index('insert'))) |
225 |
| - # Lines not displayed due to maxlines. |
226 |
| - offset = max(1, lines - self.context_depth) - 1 |
227 |
| - newtop = self.info[offset + contextline][0] |
228 |
| - self.text.yview(f'{newtop}.0') |
229 |
| - self.update_code_context() |
| 217 | + """ Show clicked context line at top of editor. |
| 218 | +
|
| 219 | + If a selection was made, don't jump; allow copying. |
| 220 | + If no visible context, show the top line of the file. |
| 221 | + """ |
| 222 | + try: |
| 223 | + self.context.index("sel.first") |
| 224 | + except tkinter.TclError: |
| 225 | + lines = len(self.info) |
| 226 | + if lines == 1: # No context lines are showing. |
| 227 | + newtop = 1 |
| 228 | + else: |
| 229 | + # Line number clicked. |
| 230 | + contextline = int(float(self.context.index('insert'))) |
| 231 | + # Lines not displayed due to maxlines. |
| 232 | + offset = max(1, lines - self.context_depth) - 1 |
| 233 | + newtop = self.info[offset + contextline][0] |
| 234 | + self.text.yview(f'{newtop}.0') |
| 235 | + self.update_code_context() |
230 | 236 |
|
231 | 237 | def timer_event(self):
|
232 | 238 | "Event on editor text widget triggered every UPDATEINTERVAL ms."
|
|
0 commit comments