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 | """ |
| 12 | import Tkinter |
| 13 | from configHandler import idleConf |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 14 | from sets import Set |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 15 | import re |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 16 | from sys import maxint as INFINITY |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 17 | |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 18 | BLOCKOPENERS = Set(["class", "def", "elif", "else", "except", "finally", "for", |
| 19 | "if", "try", "while"]) |
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>>')])] |
| 28 | |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 29 | context_depth = idleConf.GetOption("extensions", "CodeContext", |
| 30 | "numlines", type="int", default=3) |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 31 | bgcolor = idleConf.GetOption("extensions", "CodeContext", |
| 32 | "bgcolor", type="str", default="LightGray") |
| 33 | fgcolor = idleConf.GetOption("extensions", "CodeContext", |
| 34 | "fgcolor", type="str", default="Black") |
| 35 | def __init__(self, editwin): |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 36 | self.editwin = editwin |
| 37 | self.text = editwin.text |
| 38 | self.textfont = self.text["font"] |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 39 | self.label = None |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 40 | # self.info is a list of (line number, indent level, line text, block |
| 41 | # keyword) tuples providing the block structure associated with |
| 42 | # self.topvisible (the linenumber of the line displayed at the top of |
| 43 | # the edit window). self.info[0] is initialized as a 'dummy' line which |
| 44 | # starts the toplevel 'block' of the module. |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 45 | self.info = [(0, -1, "", False)] |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 46 | self.topvisible = 1 |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 47 | visible = idleConf.GetOption("extensions", "CodeContext", |
| 48 | "visible", type="bool", default=False) |
| 49 | if visible: |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 50 | self.toggle_code_context_event() |
| 51 | self.editwin.setvar('<<toggle-code-context>>', True) |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 52 | # Start two update cycles, one for context lines, one for font changes. |
| 53 | self.text.after(UPDATEINTERVAL, self.timer_event) |
| 54 | self.text.after(FONTUPDATEINTERVAL, self.font_timer_event) |
| 55 | |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 56 | def toggle_code_context_event(self, event=None): |
| 57 | if not self.label: |
Kurt B. Kaiser | 389482c | 2005-10-03 20:08:25 +0000 | [diff] [blame] | 58 | self.pad_frame = Tkinter.Frame(self.editwin.top, |
| 59 | bg=self.bgcolor, border=2, |
| 60 | relief="sunken") |
| 61 | self.label = Tkinter.Label(self.pad_frame, |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 62 | text="\n" * (self.context_depth - 1), |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 63 | anchor="w", justify="left", |
| 64 | font=self.textfont, |
| 65 | bg=self.bgcolor, fg=self.fgcolor, |
Kurt B. Kaiser | 389482c | 2005-10-03 20:08:25 +0000 | [diff] [blame] | 66 | border=0, |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 67 | width=1, # Don't request more than we get |
| 68 | ) |
Kurt B. Kaiser | 389482c | 2005-10-03 20:08:25 +0000 | [diff] [blame] | 69 | self.label.pack(side="top", fill="x", expand=True, |
| 70 | padx=4, pady=0) |
| 71 | self.pad_frame.pack(side="top", fill="x", expand=False, |
| 72 | padx=0, pady=0, |
| 73 | after=self.editwin.status_bar) |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 74 | else: |
| 75 | self.label.destroy() |
Kurt B. Kaiser | 389482c | 2005-10-03 20:08:25 +0000 | [diff] [blame] | 76 | self.pad_frame.destroy() |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 77 | self.label = None |
Kurt B. Kaiser | 4d5bc60 | 2004-06-06 01:29:22 +0000 | [diff] [blame] | 78 | idleConf.SetOption("extensions", "CodeContext", "visible", |
| 79 | str(self.label is not None)) |
| 80 | idleConf.SaveUserCfgFiles() |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 81 | |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 82 | def get_line_info(self, linenum): |
| 83 | """Get the line indent value, text, and any block start keyword |
| 84 | |
| 85 | If the line does not start a block, the keyword value is False. |
| 86 | The indentation of empty lines (or comment lines) is INFINITY. |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 87 | |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 88 | """ |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 89 | text = self.text.get("%d.0" % linenum, "%d.end" % linenum) |
| 90 | spaces, firstword = getspacesfirstword(text) |
| 91 | opener = firstword in BLOCKOPENERS and firstword |
| 92 | if len(text) == len(spaces) or text[len(spaces)] == '#': |
| 93 | indent = INFINITY |
| 94 | else: |
| 95 | indent = len(spaces) |
| 96 | return indent, text, opener |
| 97 | |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 98 | def get_context(self, new_topvisible, stopline=1, stopindent=0): |
| 99 | """Get context lines, starting at new_topvisible and working backwards. |
| 100 | |
| 101 | Stop when stopline or stopindent is reached. Return a tuple of context |
| 102 | data and the indent level at the top of the region inspected. |
| 103 | |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 104 | """ |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 105 | assert stopline > 0 |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 106 | lines = [] |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 107 | # The indentation level we are currently in: |
| 108 | lastindent = INFINITY |
| 109 | # For a line to be interesting, it must begin with a block opening |
| 110 | # keyword, and have less indentation than lastindent. |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 111 | for linenum in xrange(new_topvisible, stopline-1, -1): |
| 112 | indent, text, opener = self.get_line_info(linenum) |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 113 | if indent < lastindent: |
| 114 | lastindent = indent |
Kurt B. Kaiser | e3636e0 | 2004-04-26 22:26:04 +0000 | [diff] [blame] | 115 | if opener in ("else", "elif"): |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 116 | # We also show the if statement |
| 117 | lastindent += 1 |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 118 | if opener and linenum < new_topvisible and indent >= stopindent: |
| 119 | lines.append((linenum, indent, text, opener)) |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 120 | if lastindent <= stopindent: |
| 121 | break |
| 122 | lines.reverse() |
| 123 | return lines, lastindent |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 124 | |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 125 | def update_code_context(self): |
| 126 | """Update context information and lines visible in the context pane. |
| 127 | |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 128 | """ |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 129 | new_topvisible = int(self.text.index("@0,0").split('.')[0]) |
| 130 | if self.topvisible == new_topvisible: # haven't scrolled |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 131 | return |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 132 | if self.topvisible < new_topvisible: # scroll down |
| 133 | lines, lastindent = self.get_context(new_topvisible, |
| 134 | self.topvisible) |
| 135 | # retain only context info applicable to the region |
| 136 | # between topvisible and new_topvisible: |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 137 | while self.info[-1][1] >= lastindent: |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 138 | del self.info[-1] |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 139 | elif self.topvisible > new_topvisible: # scroll up |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 140 | stopindent = self.info[-1][1] + 1 |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 141 | # retain only context info associated |
| 142 | # with lines above new_topvisible: |
| 143 | while self.info[-1][0] >= new_topvisible: |
Kurt B. Kaiser | 7491022 | 2005-10-02 23:36:46 +0000 | [diff] [blame] | 144 | stopindent = self.info[-1][1] |
| 145 | del self.info[-1] |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 146 | lines, lastindent = self.get_context(new_topvisible, |
| 147 | self.info[-1][0]+1, |
| 148 | stopindent) |
| 149 | self.info.extend(lines) |
| 150 | self.topvisible = new_topvisible |
| 151 | |
| 152 | # empty lines in context pane: |
| 153 | context_strings = [""] * max(0, self.context_depth - len(self.info)) |
| 154 | # followed by the context hint lines: |
| 155 | context_strings += [x[2] for x in self.info[-self.context_depth:]] |
| 156 | self.label["text"] = '\n'.join(context_strings) |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 157 | |
| 158 | def timer_event(self): |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 159 | if self.label: |
Kurt B. Kaiser | 0a13579 | 2005-10-03 19:26:03 +0000 | [diff] [blame] | 160 | self.update_code_context() |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 161 | self.text.after(UPDATEINTERVAL, self.timer_event) |
| 162 | |
| 163 | def font_timer_event(self): |
| 164 | newtextfont = self.text["font"] |
Kurt B. Kaiser | d00587a | 2004-04-24 03:08:13 +0000 | [diff] [blame] | 165 | if self.label and newtextfont != self.textfont: |
Kurt B. Kaiser | 54d1a3b | 2004-04-21 20:06:26 +0000 | [diff] [blame] | 166 | self.textfont = newtextfont |
| 167 | self.label["font"] = self.textfont |
| 168 | self.text.after(FONTUPDATEINTERVAL, self.font_timer_event) |