Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
AutoExpand.py96 linesDownload Raw Back to idle
1import re2import string3 4###$ event <<expand-word>>5###$ win <Alt-slash>6###$ unix <Alt-slash>7 8 9class AutoExpand:10    keydefs = {11        "<<expand-word>>": ["<Alt-slash>"],12    }13 14    unix_keydefs = {15        "<<expand-word>>": ["<Meta-slash>"],16    }17 18    menudefs = [19        (20            "edit",21            [22                ("E_xpand word", "<<expand-word>>"),23            ],24        ),25    ]26 27    wordchars = string.ascii_letters + string.digits + "_"28 29    def __init__(self, editwin):30        self.text = editwin.text31        self.text.wordlist = None  # XXX what is this?32        self.state = None33 34    def expand_word_event(self, event):35        curinsert = self.text.index("insert")36        curline = self.text.get("insert linestart", "insert lineend")37        if not self.state:38            words = self.getwords()39            index = 040        else:41            words, index, insert, line = self.state42            if insert != curinsert or line != curline:43                words = self.getwords()44                index = 045        if not words:46            self.text.bell()47            return "break"48        word = self.getprevword()49        self.text.delete("insert - %d chars" % len(word), "insert")50        newword = words[index]51        index = (index + 1) % len(words)52        if index == 0:53            self.text.bell()  # Warn we cycled around54        self.text.insert("insert", newword)55        curinsert = self.text.index("insert")56        curline = self.text.get("insert linestart", "insert lineend")57        self.state = words, index, curinsert, curline58        return "break"59 60    def getwords(self):61        word = self.getprevword()62        if not word:63            return []64        before = self.text.get("1.0", "insert wordstart")65        wbefore = re.findall(r"\b" + word + r"\w+\b", before)66        del before67        after = self.text.get("insert wordend", "end")68        wafter = re.findall(r"\b" + word + r"\w+\b", after)69        del after70        if not wbefore and not wafter:71            return []72        words = []73        dict = {}74        # search backwards through words before75        wbefore.reverse()76        for w in wbefore:77            if dict.get(w):78                continue79            words.append(w)80            dict[w] = w81        # search onwards through words after82        for w in wafter:83            if dict.get(w):84                continue85            words.append(w)86            dict[w] = w87        words.append(word)88        return words89 90    def getprevword(self):91        line = self.text.get("insert linestart", "insert")92        i = len(line)93        while i > 0 and line[i - 1] in self.wordchars:94            i = i - 195        return line[i:]96 
codekingpro/portable-devtools · Team Ai