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