MCPcopy Index your code
hub / github.com/python/cpython / LineNumbers

Class LineNumbers

Lib/idlelib/sidebar.py:283–360  ·  view source on GitHub ↗

Line numbers support for editor windows.

Source from the content-addressed store, hash-verified

281
282
283class LineNumbers(BaseSideBar):
284 """Line numbers support for editor windows."""
285 def __init__(self, editwin):
286 super().__init__(editwin)
287
288 end_line_delegator = EndLineDelegator(self.update_sidebar_text)
289 # Insert the delegator after the undo delegator, so that line numbers
290 # are properly updated after undo and redo actions.
291 self.editwin.per.insertfilterafter(end_line_delegator,
292 after=self.editwin.undo)
293
294 def init_widgets(self):
295 _padx, pady = get_widget_padding(self.text)
296 self.sidebar_text = tk.Text(self.parent, width=1, wrap=tk.NONE,
297 padx=2, pady=pady,
298 borderwidth=0, highlightthickness=0)
299 self.sidebar_text.config(state=tk.DISABLED)
300
301 self.prev_end = 1
302 self._sidebar_width_type = type(self.sidebar_text['width'])
303 with temp_enable_text_widget(self.sidebar_text):
304 self.sidebar_text.insert('insert', '1', 'linenumber')
305 self.sidebar_text.config(takefocus=False, exportselection=False)
306 self.sidebar_text.tag_config('linenumber', justify=tk.RIGHT)
307
308 end = get_end_linenumber(self.text)
309 self.update_sidebar_text(end)
310
311 return self.sidebar_text
312
313 def grid(self):
314 self.sidebar_text.grid(row=1, column=0, sticky=tk.NSEW)
315
316 def update_font(self):
317 font = idleConf.GetFont(self.text, 'main', 'EditorWindow')
318 self.sidebar_text['font'] = font
319
320 def update_colors(self):
321 """Update the sidebar text colors, usually after config changes."""
322 colors = idleConf.GetHighlight(idleConf.CurrentTheme(), 'linenumber')
323 foreground = colors['foreground']
324 background = colors['background']
325 self.sidebar_text.config(
326 fg=foreground, bg=background,
327 selectforeground=foreground, selectbackground=background,
328 inactiveselectbackground=background,
329 )
330
331 def update_sidebar_text(self, end):
332 """
333 Perform the following action:
334 Each line sidebar_text contains the linenumber for that line
335 Synchronize with editwin.text so that both sidebar_text and
336 editwin.text contain the same number of lines"""
337 if end == self.prev_end:
338 return
339
340 width_difference = len(str(end)) - len(str(self.prev_end))

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…