blob: 92f5c84eb6f40195c1bfa989002304a6c8d76cad [file] [log] [blame]
Terry Jan Reedy13f4aba2014-06-04 20:54:43 -04001'''Complete the current word before the cursor with words in the editor.
2
3Each menu selection or shortcut key selection replaces the word with a
4different word with the same prefix. The search for matches begins
5before the target and moves toward the top of the editor. It then starts
6after the cursor and moves down. It then returns to the original word and
7the cycle starts again.
8
9Changing the current text line or leaving the cursor in a different
10place before requesting the next selection causes AutoExpand to reset
11its state.
12
wohlganger58fc71c2017-09-10 16:19:47 -050013There is only one instance of Autoexpand.
Terry Jan Reedy13f4aba2014-06-04 20:54:43 -040014'''
David Scherer7aced172000-08-15 01:13:23 +000015import re
Terry Jan Reedybfbaa6b2016-08-31 00:50:55 -040016import string
David Scherer7aced172000-08-15 01:13:23 +000017
David Scherer7aced172000-08-15 01:13:23 +000018
19class AutoExpand:
Kurt B. Kaiser87807a62002-09-15 20:50:02 +000020 wordchars = string.ascii_letters + string.digits + "_"
David Scherer7aced172000-08-15 01:13:23 +000021
22 def __init__(self, editwin):
23 self.text = editwin.text
Terry Jan Reedy3ff55a82016-08-10 23:44:54 -040024 self.bell = self.text.bell
David Scherer7aced172000-08-15 01:13:23 +000025 self.state = None
26
27 def expand_word_event(self, event):
Terry Jan Reedy13f4aba2014-06-04 20:54:43 -040028 "Replace the current word with the next expansion."
David Scherer7aced172000-08-15 01:13:23 +000029 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 Reedy3ff55a82016-08-10 23:44:54 -040040 self.bell()
David Scherer7aced172000-08-15 01:13:23 +000041 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 Reedy3ff55a82016-08-10 23:44:54 -040047 self.bell() # Warn we cycled around
David Scherer7aced172000-08-15 01:13:23 +000048 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 Reedy13f4aba2014-06-04 20:54:43 -040055 "Return a list of words that match the prefix before the cursor."
David Scherer7aced172000-08-15 01:13:23 +000056 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 Reedy13f4aba2014-06-04 20:54:43 -040086 "Return the word prefix before the cursor."
David Scherer7aced172000-08-15 01:13:23 +000087 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 Reedy13f4aba2014-06-04 20:54:43 -040092
wohlganger58fc71c2017-09-10 16:19:47 -050093
Terry Jan Reedy13f4aba2014-06-04 20:54:43 -040094if __name__ == '__main__':
Terry Jan Reedy4d921582018-06-19 19:12:52 -040095 from unittest import main
96 main('idlelib.idle_test.test_autoexpand', verbosity=2)