Terry Jan Reedy | 13f4aba | 2014-06-04 20:54:43 -0400 | [diff] [blame] | 1 | '''Complete the current word before the cursor with words in the editor. |
| 2 | |
| 3 | Each menu selection or shortcut key selection replaces the word with a |
| 4 | different word with the same prefix. The search for matches begins |
| 5 | before the target and moves toward the top of the editor. It then starts |
| 6 | after the cursor and moves down. It then returns to the original word and |
| 7 | the cycle starts again. |
| 8 | |
| 9 | Changing the current text line or leaving the cursor in a different |
| 10 | place before requesting the next selection causes AutoExpand to reset |
| 11 | its state. |
| 12 | |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 13 | There is only one instance of Autoexpand. |
Terry Jan Reedy | 13f4aba | 2014-06-04 20:54:43 -0400 | [diff] [blame] | 14 | ''' |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 15 | import re |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 16 | import string |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 17 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 18 | |
| 19 | class AutoExpand: |
Kurt B. Kaiser | 87807a6 | 2002-09-15 20:50:02 +0000 | [diff] [blame] | 20 | wordchars = string.ascii_letters + string.digits + "_" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 21 | |
| 22 | def __init__(self, editwin): |
| 23 | self.text = editwin.text |
Terry Jan Reedy | 3ff55a8 | 2016-08-10 23:44:54 -0400 | [diff] [blame] | 24 | self.bell = self.text.bell |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 25 | self.state = None |
| 26 | |
| 27 | def expand_word_event(self, event): |
Terry Jan Reedy | 13f4aba | 2014-06-04 20:54:43 -0400 | [diff] [blame] | 28 | "Replace the current word with the next expansion." |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 29 | curinsert = self.text.index("insert") |
| 30 | curline = self.text.get("insert linestart", "insert lineend") |
| 31 | if not self.state: |
| 32 | words = self.getwords() |
| 33 | index = 0 |
| 34 | else: |
| 35 | words, index, insert, line = self.state |
| 36 | if insert != curinsert or line != curline: |
| 37 | words = self.getwords() |
| 38 | index = 0 |
| 39 | if not words: |
Terry Jan Reedy | 3ff55a8 | 2016-08-10 23:44:54 -0400 | [diff] [blame] | 40 | self.bell() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 41 | return "break" |
| 42 | word = self.getprevword() |
| 43 | self.text.delete("insert - %d chars" % len(word), "insert") |
| 44 | newword = words[index] |
| 45 | index = (index + 1) % len(words) |
| 46 | if index == 0: |
Terry Jan Reedy | 3ff55a8 | 2016-08-10 23:44:54 -0400 | [diff] [blame] | 47 | self.bell() # Warn we cycled around |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 48 | self.text.insert("insert", newword) |
| 49 | curinsert = self.text.index("insert") |
| 50 | curline = self.text.get("insert linestart", "insert lineend") |
| 51 | self.state = words, index, curinsert, curline |
| 52 | return "break" |
| 53 | |
| 54 | def getwords(self): |
Terry Jan Reedy | 13f4aba | 2014-06-04 20:54:43 -0400 | [diff] [blame] | 55 | "Return a list of words that match the prefix before the cursor." |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 56 | word = self.getprevword() |
| 57 | if not word: |
| 58 | return [] |
| 59 | before = self.text.get("1.0", "insert wordstart") |
| 60 | wbefore = re.findall(r"\b" + word + r"\w+\b", before) |
| 61 | del before |
| 62 | after = self.text.get("insert wordend", "end") |
| 63 | wafter = re.findall(r"\b" + word + r"\w+\b", after) |
| 64 | del after |
| 65 | if not wbefore and not wafter: |
| 66 | return [] |
| 67 | words = [] |
| 68 | dict = {} |
| 69 | # search backwards through words before |
| 70 | wbefore.reverse() |
| 71 | for w in wbefore: |
| 72 | if dict.get(w): |
| 73 | continue |
| 74 | words.append(w) |
| 75 | dict[w] = w |
| 76 | # search onwards through words after |
| 77 | for w in wafter: |
| 78 | if dict.get(w): |
| 79 | continue |
| 80 | words.append(w) |
| 81 | dict[w] = w |
| 82 | words.append(word) |
| 83 | return words |
| 84 | |
| 85 | def getprevword(self): |
Terry Jan Reedy | 13f4aba | 2014-06-04 20:54:43 -0400 | [diff] [blame] | 86 | "Return the word prefix before the cursor." |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 87 | line = self.text.get("insert linestart", "insert") |
| 88 | i = len(line) |
| 89 | while i > 0 and line[i-1] in self.wordchars: |
| 90 | i = i-1 |
| 91 | return line[i:] |
Terry Jan Reedy | 13f4aba | 2014-06-04 20:54:43 -0400 | [diff] [blame] | 92 | |
wohlganger | 58fc71c | 2017-09-10 16:19:47 -0500 | [diff] [blame] | 93 | |
Terry Jan Reedy | 13f4aba | 2014-06-04 20:54:43 -0400 | [diff] [blame] | 94 | if __name__ == '__main__': |
Terry Jan Reedy | 4d92158 | 2018-06-19 19:12:52 -0400 | [diff] [blame^] | 95 | from unittest import main |
| 96 | main('idlelib.idle_test.test_autoexpand', verbosity=2) |