PageRenderTime 27ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/idlelib/codecontext.py

https://github.com/albertz/CPython
Python | 244 lines | 214 code | 4 blank | 26 comment | 6 complexity | 69a08f2e1fb1e3a5b5af22a1ee74448d MD5 | raw file
  1. """codecontext - display the block context above the edit window
  2. Once code has scrolled off the top of a window, it can be difficult to
  3. determine which block you are in. This extension implements a pane at the top
  4. of each IDLE edit window which provides block structure hints. These hints are
  5. the lines which contain the block opening keywords, e.g. 'if', for the
  6. enclosing block. The number of hint lines is determined by the maxlines
  7. variable in the codecontext section of config-extensions.def. Lines which do
  8. not open blocks are not shown in the context hints pane.
  9. """
  10. import re
  11. from sys import maxsize as INFINITY
  12. import tkinter
  13. from tkinter.constants import TOP, X, SUNKEN
  14. from idlelib.config import idleConf
  15. BLOCKOPENERS = {"class", "def", "elif", "else", "except", "finally", "for",
  16. "if", "try", "while", "with", "async"}
  17. UPDATEINTERVAL = 100 # millisec
  18. CONFIGUPDATEINTERVAL = 1000 # millisec
  19. def get_spaces_firstword(codeline, c=re.compile(r"^(\s*)(\w*)")):
  20. "Extract the beginning whitespace and first word from codeline."
  21. return c.match(codeline).groups()
  22. def get_line_info(codeline):
  23. """Return tuple of (line indent value, codeline, block start keyword).
  24. The indentation of empty lines (or comment lines) is INFINITY.
  25. If the line does not start a block, the keyword value is False.
  26. """
  27. spaces, firstword = get_spaces_firstword(codeline)
  28. indent = len(spaces)
  29. if len(codeline) == indent or codeline[indent] == '#':
  30. indent = INFINITY
  31. opener = firstword in BLOCKOPENERS and firstword
  32. return indent, codeline, opener
  33. class CodeContext:
  34. "Display block context above the edit window."
  35. def __init__(self, editwin):
  36. """Initialize settings for context block.
  37. editwin is the Editor window for the context block.
  38. self.text is the editor window text widget.
  39. self.textfont is the editor window font.
  40. self.context displays the code context text above the editor text.
  41. Initially None, it is toggled via <<toggle-code-context>>.
  42. self.topvisible is the number of the top text line displayed.
  43. self.info is a list of (line number, indent level, line text,
  44. block keyword) tuples for the block structure above topvisible.
  45. self.info[0] is initialized with a 'dummy' line which
  46. starts the toplevel 'block' of the module.
  47. self.t1 and self.t2 are two timer events on the editor text widget to
  48. monitor for changes to the context text or editor font.
  49. """
  50. self.editwin = editwin
  51. self.text = editwin.text
  52. self.textfont = self.text["font"]
  53. self.contextcolors = CodeContext.colors
  54. self.context = None
  55. self.topvisible = 1
  56. self.info = [(0, -1, "", False)]
  57. # Start two update cycles, one for context lines, one for font changes.
  58. self.t1 = self.text.after(UPDATEINTERVAL, self.timer_event)
  59. self.t2 = self.text.after(CONFIGUPDATEINTERVAL, self.config_timer_event)
  60. @classmethod
  61. def reload(cls):
  62. "Load class variables from config."
  63. cls.context_depth = idleConf.GetOption("extensions", "CodeContext",
  64. "maxlines", type="int", default=15)
  65. cls.colors = idleConf.GetHighlight(idleConf.CurrentTheme(), 'context')
  66. def __del__(self):
  67. "Cancel scheduled events."
  68. try:
  69. self.text.after_cancel(self.t1)
  70. self.text.after_cancel(self.t2)
  71. except:
  72. pass
  73. def toggle_code_context_event(self, event=None):
  74. """Toggle code context display.
  75. If self.context doesn't exist, create it to match the size of the editor
  76. window text (toggle on). If it does exist, destroy it (toggle off).
  77. Return 'break' to complete the processing of the binding.
  78. """
  79. if not self.context:
  80. # Calculate the border width and horizontal padding required to
  81. # align the context with the text in the main Text widget.
  82. #
  83. # All values are passed through getint(), since some
  84. # values may be pixel objects, which can't simply be added to ints.
  85. widgets = self.editwin.text, self.editwin.text_frame
  86. # Calculate the required horizontal padding and border width.
  87. padx = 0
  88. border = 0
  89. for widget in widgets:
  90. padx += widget.tk.getint(widget.pack_info()['padx'])
  91. padx += widget.tk.getint(widget.cget('padx'))
  92. border += widget.tk.getint(widget.cget('border'))
  93. self.context = tkinter.Text(
  94. self.editwin.top, font=self.textfont,
  95. bg=self.contextcolors['background'],
  96. fg=self.contextcolors['foreground'],
  97. height=1,
  98. width=1, # Don't request more than we get.
  99. padx=padx, border=border, relief=SUNKEN, state='disabled')
  100. self.context.bind('<ButtonRelease-1>', self.jumptoline)
  101. # Pack the context widget before and above the text_frame widget,
  102. # thus ensuring that it will appear directly above text_frame.
  103. self.context.pack(side=TOP, fill=X, expand=False,
  104. before=self.editwin.text_frame)
  105. menu_status = 'Hide'
  106. else:
  107. self.context.destroy()
  108. self.context = None
  109. menu_status = 'Show'
  110. self.editwin.update_menu_label(menu='options', index='* Code Context',
  111. label=f'{menu_status} Code Context')
  112. return "break"
  113. def get_context(self, new_topvisible, stopline=1, stopindent=0):
  114. """Return a list of block line tuples and the 'last' indent.
  115. The tuple fields are (linenum, indent, text, opener).
  116. The list represents header lines from new_topvisible back to
  117. stopline with successively shorter indents > stopindent.
  118. The list is returned ordered by line number.
  119. Last indent returned is the smallest indent observed.
  120. """
  121. assert stopline > 0
  122. lines = []
  123. # The indentation level we are currently in.
  124. lastindent = INFINITY
  125. # For a line to be interesting, it must begin with a block opening
  126. # keyword, and have less indentation than lastindent.
  127. for linenum in range(new_topvisible, stopline-1, -1):
  128. codeline = self.text.get(f'{linenum}.0', f'{linenum}.end')
  129. indent, text, opener = get_line_info(codeline)
  130. if indent < lastindent:
  131. lastindent = indent
  132. if opener in ("else", "elif"):
  133. # Also show the if statement.
  134. lastindent += 1
  135. if opener and linenum < new_topvisible and indent >= stopindent:
  136. lines.append((linenum, indent, text, opener))
  137. if lastindent <= stopindent:
  138. break
  139. lines.reverse()
  140. return lines, lastindent
  141. def update_code_context(self):
  142. """Update context information and lines visible in the context pane.
  143. No update is done if the text hasn't been scrolled. If the text
  144. was scrolled, the lines that should be shown in the context will
  145. be retrieved and the context area will be updated with the code,
  146. up to the number of maxlines.
  147. """
  148. new_topvisible = int(self.text.index("@0,0").split('.')[0])
  149. if self.topvisible == new_topvisible: # Haven't scrolled.
  150. return
  151. if self.topvisible < new_topvisible: # Scroll down.
  152. lines, lastindent = self.get_context(new_topvisible,
  153. self.topvisible)
  154. # Retain only context info applicable to the region
  155. # between topvisible and new_topvisible.
  156. while self.info[-1][1] >= lastindent:
  157. del self.info[-1]
  158. else: # self.topvisible > new_topvisible: # Scroll up.
  159. stopindent = self.info[-1][1] + 1
  160. # Retain only context info associated
  161. # with lines above new_topvisible.
  162. while self.info[-1][0] >= new_topvisible:
  163. stopindent = self.info[-1][1]
  164. del self.info[-1]
  165. lines, lastindent = self.get_context(new_topvisible,
  166. self.info[-1][0]+1,
  167. stopindent)
  168. self.info.extend(lines)
  169. self.topvisible = new_topvisible
  170. # Last context_depth context lines.
  171. context_strings = [x[2] for x in self.info[-self.context_depth:]]
  172. showfirst = 0 if context_strings[0] else 1
  173. # Update widget.
  174. self.context['height'] = len(context_strings) - showfirst
  175. self.context['state'] = 'normal'
  176. self.context.delete('1.0', 'end')
  177. self.context.insert('end', '\n'.join(context_strings[showfirst:]))
  178. self.context['state'] = 'disabled'
  179. def jumptoline(self, event=None):
  180. "Show clicked context line at top of editor."
  181. lines = len(self.info)
  182. if lines == 1: # No context lines are showing.
  183. newtop = 1
  184. else:
  185. # Line number clicked.
  186. contextline = int(float(self.context.index('insert')))
  187. # Lines not displayed due to maxlines.
  188. offset = max(1, lines - self.context_depth) - 1
  189. newtop = self.info[offset + contextline][0]
  190. self.text.yview(f'{newtop}.0')
  191. self.update_code_context()
  192. def timer_event(self):
  193. "Event on editor text widget triggered every UPDATEINTERVAL ms."
  194. if self.context:
  195. self.update_code_context()
  196. self.t1 = self.text.after(UPDATEINTERVAL, self.timer_event)
  197. def config_timer_event(self):
  198. "Event on editor text widget triggered every CONFIGUPDATEINTERVAL ms."
  199. newtextfont = self.text["font"]
  200. if (self.context and (newtextfont != self.textfont or
  201. CodeContext.colors != self.contextcolors)):
  202. self.textfont = newtextfont
  203. self.contextcolors = CodeContext.colors
  204. self.context["font"] = self.textfont
  205. self.context['background'] = self.contextcolors['background']
  206. self.context['foreground'] = self.contextcolors['foreground']
  207. self.t2 = self.text.after(CONFIGUPDATEINTERVAL, self.config_timer_event)
  208. CodeContext.reload()
  209. if __name__ == "__main__":
  210. from unittest import main
  211. main('idlelib.idle_test.test_codecontext', verbosity=2, exit=False)
  212. # Add htest.