/opt/alt/python37/lib64/python3.7/idlelib
'''Complete the current word before the cursor with words in the editor. 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. Changing the current text line or leaving the cursor in a different place before requesting the next selection causes AutoExpand to reset its state. There is only one instance of Autoexpand. ''' import re import string class AutoExpand: wordchars = string.ascii_letters + string.digits + "_" def __init__(self, editwin): self.text = editwin.text self.bell = self.text.bell self.state = None def expand_word_event(self, event): "Replace the current word with the next expansion." 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: self.bell() 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: self.bell() # Warn we cycled around 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): "Return a list of words that match the prefix before the cursor." 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): "Return the word prefix before the cursor." 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:] if __name__ == '__main__': from unittest import main main('idlelib.idle_test.test_autoexpand', verbosity=2)
.
Edit
..
Edit
CREDITS.txt
Edit
ChangeLog
Edit
HISTORY.txt
Edit
Icons
Edit
NEWS.txt
Edit
NEWS2x.txt
Edit
README.txt
Edit
TODO.txt
Edit
__init__.py
Edit
__main__.py
Edit
__pycache__
Edit
autocomplete.py
Edit
autocomplete_w.py
Edit
autoexpand.py
Edit
browser.py
Edit
calltip.py
Edit
calltip_w.py
Edit
codecontext.py
Edit
colorizer.py
Edit
config-extensions.def
Edit
config-highlight.def
Edit
config-keys.def
Edit
config-main.def
Edit
config.py
Edit
config_key.py
Edit
configdialog.py
Edit
debugger.py
Edit
debugger_r.py
Edit
debugobj.py
Edit
debugobj_r.py
Edit
delegator.py
Edit
dynoption.py
Edit
editor.py
Edit
extend.txt
Edit
filelist.py
Edit
format.py
Edit
grep.py
Edit
help.html
Edit
help.py
Edit
help_about.py
Edit
history.py
Edit
hyperparser.py
Edit
idle.py
Edit
idle.pyw
Edit
idle_test
Edit
iomenu.py
Edit
macosx.py
Edit
mainmenu.py
Edit
multicall.py
Edit
outwin.py
Edit
parenmatch.py
Edit
pathbrowser.py
Edit
percolator.py
Edit
pyparse.py
Edit
pyshell.py
Edit
query.py
Edit
redirector.py
Edit
replace.py
Edit
rpc.py
Edit
run.py
Edit
runscript.py
Edit
scrolledlist.py
Edit
search.py
Edit
searchbase.py
Edit
searchengine.py
Edit
sidebar.py
Edit
squeezer.py
Edit
stackviewer.py
Edit
statusbar.py
Edit
textview.py
Edit
tooltip.py
Edit
tree.py
Edit
undo.py
Edit
window.py
Edit
zoomheight.py
Edit
zzdummy.py
Edit