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