Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 1 | """codecontext - Extension to display the block context above the edit window |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 2 | |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 3 | Once code has scrolled off the top of a window, it can be difficult to |
| 4 | determine which block you are in. This extension implements a pane at the top |
| 5 | of each IDLE edit window which provides block structure hints. These hints are |
| 6 | the lines which contain the block opening keywords, e.g. 'if', for the |
| 7 | enclosing block. The number of hint lines is determined by the numlines |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 8 | variable in the codecontext section of config-extensions.def. Lines which do |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 9 | not open blocks are not shown in the context hints pane. |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 10 | |
| 11 | """ |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 12 | import re |
Kurt B. Kaiser | 3536a5c | 2007-12-12 17:22:06 +0000 | [diff] [blame] | 13 | from sys import maxsize as INFINITY |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 14 | |
| 15 | import tkinter |
| 16 | from tkinter.constants import TOP, LEFT, X, W, SUNKEN |
| 17 | |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 18 | from idlelib.config import idleConf |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 19 | |
Terry Jan Reedy | 049882e | 2014-12-11 05:33:36 -0500 | [diff] [blame] | 20 | BLOCKOPENERS = {"class", "def", "elif", "else", "except", "finally", "for", |
| 21 | "if", "try", "while", "with"} |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 22 | UPDATEINTERVAL = 100 # millisec |
| 23 | FONTUPDATEINTERVAL = 1000 # millisec |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 24 | |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 25 | getspacesfirstword =\ |
| 26 | lambda s, c=re.compile(r"^(\s*)(\w*)"): c.match(s).groups() |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 27 | |
| 28 | class CodeContext: |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 29 | menudefs = [('options', [('!Code Conte_xt', '<<toggle-code-context>>')])] |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 30 | context_depth = idleConf.GetOption("extensions", "CodeContext", |
| 31 | "numlines", type="int", default=3) |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 32 | bgcolor = idleConf.GetOption("extensions", "CodeContext", |
| 33 | "bgcolor", type="str", default="LightGray") |
| 34 | fgcolor = idleConf.GetOption("extensions", "CodeContext", |
| 35 | "fgcolor", type="str", default="Black") |
| 36 | def __init__(self, editwin): |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 37 | self.editwin = editwin |
| 38 | self.text = editwin.text |
| 39 | self.textfont = self.text["font"] |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 40 | self.label = None |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 41 | # self.info is a list of (line number, indent level, line text, block |
| 42 | # keyword) tuples providing the block structure associated with |
| 43 | # self.topvisible (the linenumber of the line displayed at the top of |
| 44 | # the edit window). self.info[0] is initialized as a 'dummy' line which |
| 45 | # starts the toplevel 'block' of the module. |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 46 | self.info = [(0, -1, "", False)] |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 47 | self.topvisible = 1 |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 48 | visible = idleConf.GetOption("extensions", "CodeContext", |
| 49 | "visible", type="bool", default=False) |
| 50 | if visible: |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 51 | self.toggle_code_context_event() |
| 52 | self.editwin.setvar('<<toggle-code-context>>', True) |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 53 | # Start two update cycles, one for context lines, one for font changes. |
| 54 | self.text.after(UPDATEINTERVAL, self.timer_event) |
| 55 | self.text.after(FONTUPDATEINTERVAL, self.font_timer_event) |
| 56 | |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 57 | def toggle_code_context_event(self, event=None): |
| 58 | if not self.label: |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 59 | # Calculate the border width and horizontal padding required to |
| 60 | # align the context with the text in the main Text widget. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 61 | # |
Serhiy Storchaka | 645058d | 2015-05-06 14:00:04 +0300 | [diff] [blame] | 62 | # All values are passed through getint(), since some |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 63 | # values may be pixel objects, which can't simply be added to ints. |
| 64 | widgets = self.editwin.text, self.editwin.text_frame |
| 65 | # Calculate the required vertical padding |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 66 | padx = 0 |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 67 | for widget in widgets: |
Serhiy Storchaka | 645058d | 2015-05-06 14:00:04 +0300 | [diff] [blame] | 68 | padx += widget.tk.getint(widget.pack_info()['padx']) |
| 69 | padx += widget.tk.getint(widget.cget('padx')) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 70 | # Calculate the required border width |
| 71 | border = 0 |
| 72 | for widget in widgets: |
Serhiy Storchaka | 645058d | 2015-05-06 14:00:04 +0300 | [diff] [blame] | 73 | border += widget.tk.getint(widget.cget('border')) |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 74 | self.label = tkinter.Label(self.editwin.top, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 75 | text="\n" * (self.context_depth - 1), |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 76 | anchor=W, justify=LEFT, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 77 | font=self.textfont, |
| 78 | bg=self.bgcolor, fg=self.fgcolor, |
| 79 | width=1, #don't request more than we get |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 80 | padx=padx, border=border, |
| 81 | relief=SUNKEN) |
| 82 | # Pack the label widget before and above the text_frame widget, |
| 83 | # thus ensuring that it will appear directly above text_frame |
| 84 | self.label.pack(side=TOP, fill=X, expand=False, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 85 | before=self.editwin.text_frame) |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 86 | else: |
| 87 | self.label.destroy() |
| 88 | self.label = None |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 89 | idleConf.SetOption("extensions", "CodeContext", "visible", |
| 90 | str(self.label is not None)) |
| 91 | idleConf.SaveUserCfgFiles() |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 92 | |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 93 | def get_line_info(self, linenum): |
| 94 | """Get the line indent value, text, and any block start keyword |
| 95 | |
| 96 | If the line does not start a block, the keyword value is False. |
| 97 | The indentation of empty lines (or comment lines) is INFINITY. |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 98 | |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 99 | """ |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 100 | text = self.text.get("%d.0" % linenum, "%d.end" % linenum) |
| 101 | spaces, firstword = getspacesfirstword(text) |
| 102 | opener = firstword in BLOCKOPENERS and firstword |
| 103 | if len(text) == len(spaces) or text[len(spaces)] == '#': |
| 104 | indent = INFINITY |
| 105 | else: |
| 106 | indent = len(spaces) |
| 107 | return indent, text, opener |
| 108 | |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 109 | def get_context(self, new_topvisible, stopline=1, stopindent=0): |
| 110 | """Get context lines, starting at new_topvisible and working backwards. |
| 111 | |
| 112 | Stop when stopline or stopindent is reached. Return a tuple of context |
| 113 | data and the indent level at the top of the region inspected. |
| 114 | |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 115 | """ |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 116 | assert stopline > 0 |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 117 | lines = [] |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 118 | # The indentation level we are currently in: |
| 119 | lastindent = INFINITY |
| 120 | # For a line to be interesting, it must begin with a block opening |
| 121 | # keyword, and have less indentation than lastindent. |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 122 | for linenum in range(new_topvisible, stopline-1, -1): |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 123 | indent, text, opener = self.get_line_info(linenum) |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 124 | if indent < lastindent: |
| 125 | lastindent = indent |
Kurt B. Kaiser | e3636e0 | 2004-04-26 22:26:04 +0000 | [diff] [blame] | 126 | if opener in ("else", "elif"): |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 127 | # We also show the if statement |
| 128 | lastindent += 1 |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 129 | if opener and linenum < new_topvisible and indent >= stopindent: |
| 130 | lines.append((linenum, indent, text, opener)) |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 131 | if lastindent <= stopindent: |
| 132 | break |
| 133 | lines.reverse() |
| 134 | return lines, lastindent |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 135 | |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 136 | def update_code_context(self): |
| 137 | """Update context information and lines visible in the context pane. |
| 138 | |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 139 | """ |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 140 | new_topvisible = int(self.text.index("@0,0").split('.')[0]) |
| 141 | if self.topvisible == new_topvisible: # haven't scrolled |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 142 | return |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 143 | if self.topvisible < new_topvisible: # scroll down |
| 144 | lines, lastindent = self.get_context(new_topvisible, |
| 145 | self.topvisible) |
| 146 | # retain only context info applicable to the region |
| 147 | # between topvisible and new_topvisible: |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 148 | while self.info[-1][1] >= lastindent: |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 149 | del self.info[-1] |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 150 | elif self.topvisible > new_topvisible: # scroll up |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 151 | stopindent = self.info[-1][1] + 1 |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 152 | # retain only context info associated |
| 153 | # with lines above new_topvisible: |
| 154 | while self.info[-1][0] >= new_topvisible: |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 155 | stopindent = self.info[-1][1] |
| 156 | del self.info[-1] |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 157 | lines, lastindent = self.get_context(new_topvisible, |
| 158 | self.info[-1][0]+1, |
| 159 | stopindent) |
| 160 | self.info.extend(lines) |
| 161 | self.topvisible = new_topvisible |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 162 | # empty lines in context pane: |
| 163 | context_strings = [""] * max(0, self.context_depth - len(self.info)) |
| 164 | # followed by the context hint lines: |
| 165 | context_strings += [x[2] for x in self.info[-self.context_depth:]] |
| 166 | self.label["text"] = '\n'.join(context_strings) |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 167 | |
| 168 | def timer_event(self): |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 169 | if self.label: |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 170 | self.update_code_context() |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 171 | self.text.after(UPDATEINTERVAL, self.timer_event) |
| 172 | |
| 173 | def font_timer_event(self): |
| 174 | newtextfont = self.text["font"] |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 175 | if self.label and newtextfont != self.textfont: |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 176 | self.textfont = newtextfont |
| 177 | self.label["font"] = self.textfont |
| 178 | self.text.after(FONTUPDATEINTERVAL, self.font_timer_event) |