2019-01-20 10:35:31 +00:00
|
|
|
'''Complete the current word before the cursor with words in the editor.
|
2016-04-14 19:54:42 +00:00
|
|
|
|
2019-01-20 10:35:31 +00:00
|
|
|
Each menu selection or shortcut key selection replaces the word with a
|
|
|
|
different word with the same prefix. The search for matches begins
|
|
|
|
before the target and moves toward the top of the editor. It then starts
|
|
|
|
after the cursor and moves down. It then returns to the original word and
|
|
|
|
the cycle starts again.
|
2016-04-14 19:54:42 +00:00
|
|
|
|
2019-01-20 10:35:31 +00:00
|
|
|
Changing the current text line or leaving the cursor in a different
|
|
|
|
place before requesting the next selection causes AutoExpand to reset
|
|
|
|
its state.
|
2016-04-14 19:54:42 +00:00
|
|
|
|
2019-01-20 10:35:31 +00:00
|
|
|
There is only one instance of Autoexpand.
|
|
|
|
'''
|
|
|
|
import re
|
|
|
|
import string
|
2016-04-14 19:54:42 +00:00
|
|
|
|
|
|
|
|
2019-01-20 10:35:31 +00:00
|
|
|
class AutoExpand:
|
2016-04-14 19:54:42 +00:00
|
|
|
wordchars = string.ascii_letters + string.digits + "_"
|
|
|
|
|
|
|
|
def __init__(self, editwin):
|
|
|
|
self.text = editwin.text
|
2019-01-20 10:35:31 +00:00
|
|
|
self.bell = self.text.bell
|
2016-04-14 19:54:42 +00:00
|
|
|
self.state = None
|
|
|
|
|
|
|
|
def expand_word_event(self, event):
|
2019-01-20 10:35:31 +00:00
|
|
|
"Replace the current word with the next expansion."
|
2016-04-14 19:54:42 +00:00
|
|
|
curinsert = self.text.index("insert")
|
|
|
|
curline = self.text.get("insert linestart", "insert lineend")
|
|
|
|
if not self.state:
|
|
|
|
words = self.getwords()
|
|
|
|
index = 0
|
|
|
|
else:
|
|
|
|
words, index, insert, line = self.state
|
|
|
|
if insert != curinsert or line != curline:
|
|
|
|
words = self.getwords()
|
|
|
|
index = 0
|
|
|
|
if not words:
|
2019-01-20 10:35:31 +00:00
|
|
|
self.bell()
|
2016-04-14 19:54:42 +00:00
|
|
|
return "break"
|
|
|
|
word = self.getprevword()
|
|
|
|
self.text.delete("insert - %d chars" % len(word), "insert")
|
|
|
|
newword = words[index]
|
|
|
|
index = (index + 1) % len(words)
|
|
|
|
if index == 0:
|
2019-01-20 10:35:31 +00:00
|
|
|
self.bell() # Warn we cycled around
|
2016-04-14 19:54:42 +00:00
|
|
|
self.text.insert("insert", newword)
|
|
|
|
curinsert = self.text.index("insert")
|
|
|
|
curline = self.text.get("insert linestart", "insert lineend")
|
|
|
|
self.state = words, index, curinsert, curline
|
|
|
|
return "break"
|
|
|
|
|
|
|
|
def getwords(self):
|
2019-01-20 10:35:31 +00:00
|
|
|
"Return a list of words that match the prefix before the cursor."
|
2016-04-14 19:54:42 +00:00
|
|
|
word = self.getprevword()
|
|
|
|
if not word:
|
|
|
|
return []
|
|
|
|
before = self.text.get("1.0", "insert wordstart")
|
|
|
|
wbefore = re.findall(r"\b" + word + r"\w+\b", before)
|
|
|
|
del before
|
|
|
|
after = self.text.get("insert wordend", "end")
|
|
|
|
wafter = re.findall(r"\b" + word + r"\w+\b", after)
|
|
|
|
del after
|
|
|
|
if not wbefore and not wafter:
|
|
|
|
return []
|
|
|
|
words = []
|
|
|
|
dict = {}
|
|
|
|
# search backwards through words before
|
|
|
|
wbefore.reverse()
|
|
|
|
for w in wbefore:
|
|
|
|
if dict.get(w):
|
|
|
|
continue
|
|
|
|
words.append(w)
|
|
|
|
dict[w] = w
|
|
|
|
# search onwards through words after
|
|
|
|
for w in wafter:
|
|
|
|
if dict.get(w):
|
|
|
|
continue
|
|
|
|
words.append(w)
|
|
|
|
dict[w] = w
|
|
|
|
words.append(word)
|
|
|
|
return words
|
|
|
|
|
|
|
|
def getprevword(self):
|
2019-01-20 10:35:31 +00:00
|
|
|
"Return the word prefix before the cursor."
|
2016-04-14 19:54:42 +00:00
|
|
|
line = self.text.get("insert linestart", "insert")
|
|
|
|
i = len(line)
|
|
|
|
while i > 0 and line[i-1] in self.wordchars:
|
|
|
|
i = i-1
|
|
|
|
return line[i:]
|
2019-01-20 10:35:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from unittest import main
|
|
|
|
main('idlelib.idle_test.test_autoexpand', verbosity=2)
|