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