Cheryl Sabella | e40e2a2 | 2021-01-05 02:26:43 -0500 | [diff] [blame] | 1 | """Example extension, also used for testing. |
| 2 | |
| 3 | See extend.txt for more details on creating an extension. |
| 4 | See config-extension.def for configuring an extension. |
| 5 | """ |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 6 | |
| 7 | from idlelib.config import idleConf |
Cheryl Sabella | e40e2a2 | 2021-01-05 02:26:43 -0500 | [diff] [blame] | 8 | from functools import wraps |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 9 | |
Cheryl Sabella | e40e2a2 | 2021-01-05 02:26:43 -0500 | [diff] [blame] | 10 | |
| 11 | def format_selection(format_line): |
| 12 | "Apply a formatting function to all of the selected lines." |
| 13 | |
| 14 | @wraps(format_line) |
| 15 | def apply(self, event=None): |
| 16 | head, tail, chars, lines = self.formatter.get_region() |
| 17 | for pos in range(len(lines) - 1): |
| 18 | line = lines[pos] |
| 19 | lines[pos] = format_line(self, line) |
| 20 | self.formatter.set_region(head, tail, chars, lines) |
| 21 | return 'break' |
| 22 | |
| 23 | return apply |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 24 | |
| 25 | |
| 26 | class ZzDummy: |
Cheryl Sabella | e40e2a2 | 2021-01-05 02:26:43 -0500 | [diff] [blame] | 27 | """Prepend or remove initial text from selected lines.""" |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 28 | |
Cheryl Sabella | e40e2a2 | 2021-01-05 02:26:43 -0500 | [diff] [blame] | 29 | # Extend the format menu. |
| 30 | menudefs = [ |
| 31 | ('format', [ |
| 32 | ('Z in', '<<z-in>>'), |
| 33 | ('Z out', '<<z-out>>'), |
| 34 | ] ) |
| 35 | ] |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 36 | |
| 37 | def __init__(self, editwin): |
Cheryl Sabella | e40e2a2 | 2021-01-05 02:26:43 -0500 | [diff] [blame] | 38 | "Initialize the settings for this extension." |
| 39 | self.editwin = editwin |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 40 | self.text = editwin.text |
Cheryl Sabella | e40e2a2 | 2021-01-05 02:26:43 -0500 | [diff] [blame] | 41 | self.formatter = editwin.fregion |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 42 | |
| 43 | @classmethod |
| 44 | def reload(cls): |
Cheryl Sabella | e40e2a2 | 2021-01-05 02:26:43 -0500 | [diff] [blame] | 45 | "Load class variables from config." |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 46 | cls.ztext = idleConf.GetOption('extensions', 'ZzDummy', 'z-text') |
| 47 | |
Cheryl Sabella | e40e2a2 | 2021-01-05 02:26:43 -0500 | [diff] [blame] | 48 | @format_selection |
| 49 | def z_in_event(self, line): |
| 50 | """Insert text at the beginning of each selected line. |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 51 | |
Cheryl Sabella | e40e2a2 | 2021-01-05 02:26:43 -0500 | [diff] [blame] | 52 | This is bound to the <<z-in>> virtual event when the extensions |
| 53 | are loaded. |
| 54 | """ |
| 55 | return f'{self.ztext}{line}' |
| 56 | |
| 57 | @format_selection |
| 58 | def z_out_event(self, line): |
| 59 | """Remove specific text from the beginning of each selected line. |
| 60 | |
| 61 | This is bound to the <<z-out>> virtual event when the extensions |
| 62 | are loaded. |
| 63 | """ |
| 64 | zlength = 0 if not line.startswith(self.ztext) else len(self.ztext) |
| 65 | return line[zlength:] |
| 66 | |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 67 | |
| 68 | ZzDummy.reload() |
| 69 | |
Cheryl Sabella | e40e2a2 | 2021-01-05 02:26:43 -0500 | [diff] [blame] | 70 | |
| 71 | if __name__ == "__main__": |
| 72 | import unittest |
| 73 | unittest.main('idlelib.idle_test.test_zzdummy', verbosity=2, exit=False) |