2021-01-05 05:18:02 -04:00
|
|
|
"""Example extension, also used for testing.
|
|
|
|
|
|
|
|
See extend.txt for more details on creating an extension.
|
|
|
|
See config-extension.def for configuring an extension.
|
|
|
|
"""
|
2017-09-10 18:19:47 -03:00
|
|
|
|
|
|
|
from idlelib.config import idleConf
|
2021-01-05 05:18:02 -04:00
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
|
|
|
|
def format_selection(format_line):
|
|
|
|
"Apply a formatting function to all of the selected lines."
|
|
|
|
|
|
|
|
@wraps(format_line)
|
|
|
|
def apply(self, event=None):
|
|
|
|
head, tail, chars, lines = self.formatter.get_region()
|
|
|
|
for pos in range(len(lines) - 1):
|
|
|
|
line = lines[pos]
|
|
|
|
lines[pos] = format_line(self, line)
|
|
|
|
self.formatter.set_region(head, tail, chars, lines)
|
|
|
|
return 'break'
|
2017-09-10 18:19:47 -03:00
|
|
|
|
2021-01-05 05:18:02 -04:00
|
|
|
return apply
|
2017-09-10 18:19:47 -03:00
|
|
|
|
|
|
|
|
|
|
|
class ZzDummy:
|
2021-01-05 05:18:02 -04:00
|
|
|
"""Prepend or remove initial text from selected lines."""
|
2017-09-10 18:19:47 -03:00
|
|
|
|
2021-01-05 05:18:02 -04:00
|
|
|
# Extend the format menu.
|
|
|
|
menudefs = [
|
|
|
|
('format', [
|
|
|
|
('Z in', '<<z-in>>'),
|
|
|
|
('Z out', '<<z-out>>'),
|
|
|
|
] )
|
|
|
|
]
|
2017-09-10 18:19:47 -03:00
|
|
|
|
|
|
|
def __init__(self, editwin):
|
2021-01-05 05:18:02 -04:00
|
|
|
"Initialize the settings for this extension."
|
|
|
|
self.editwin = editwin
|
2017-09-10 18:19:47 -03:00
|
|
|
self.text = editwin.text
|
2021-01-05 05:18:02 -04:00
|
|
|
self.formatter = editwin.fregion
|
2017-09-10 18:19:47 -03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def reload(cls):
|
2021-01-05 05:18:02 -04:00
|
|
|
"Load class variables from config."
|
2017-09-10 18:19:47 -03:00
|
|
|
cls.ztext = idleConf.GetOption('extensions', 'ZzDummy', 'z-text')
|
|
|
|
|
2021-01-05 05:18:02 -04:00
|
|
|
@format_selection
|
|
|
|
def z_in_event(self, line):
|
|
|
|
"""Insert text at the beginning of each selected line.
|
|
|
|
|
|
|
|
This is bound to the <<z-in>> virtual event when the extensions
|
|
|
|
are loaded.
|
2017-09-10 18:19:47 -03:00
|
|
|
"""
|
2021-01-05 05:18:02 -04:00
|
|
|
return f'{self.ztext}{line}'
|
|
|
|
|
|
|
|
@format_selection
|
|
|
|
def z_out_event(self, line):
|
|
|
|
"""Remove specific text from the beginning of each selected line.
|
|
|
|
|
|
|
|
This is bound to the <<z-out>> virtual event when the extensions
|
|
|
|
are loaded.
|
2017-09-10 18:19:47 -03:00
|
|
|
"""
|
2021-01-05 05:18:02 -04:00
|
|
|
zlength = 0 if not line.startswith(self.ztext) else len(self.ztext)
|
|
|
|
return line[zlength:]
|
2017-09-10 18:19:47 -03:00
|
|
|
|
|
|
|
|
|
|
|
ZzDummy.reload()
|
|
|
|
|
2021-01-05 05:18:02 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import unittest
|
|
|
|
unittest.main('idlelib.idle_test.test_zzdummy', verbosity=2, exit=False)
|