ยปCore Development>Code coverage>Lib/idlelib/AutoExpand.py

Python code coverage for Lib/idlelib/AutoExpand.py

#countcontent
1n/aimport string
2n/aimport re
3n/a
4n/a###$ event <<expand-word>>
5n/a###$ win <Alt-slash>
6n/a###$ unix <Alt-slash>
7n/a
8n/aclass AutoExpand:
9n/a
10n/a menudefs = [
11n/a ('edit', [
12n/a ('E_xpand Word', '<<expand-word>>'),
13n/a ]),
14n/a ]
15n/a
16n/a wordchars = string.ascii_letters + string.digits + "_"
17n/a
18n/a def __init__(self, editwin):
19n/a self.text = editwin.text
20n/a self.state = None
21n/a
22n/a def expand_word_event(self, event):
23n/a curinsert = self.text.index("insert")
24n/a curline = self.text.get("insert linestart", "insert lineend")
25n/a if not self.state:
26n/a words = self.getwords()
27n/a index = 0
28n/a else:
29n/a words, index, insert, line = self.state
30n/a if insert != curinsert or line != curline:
31n/a words = self.getwords()
32n/a index = 0
33n/a if not words:
34n/a self.text.bell()
35n/a return "break"
36n/a word = self.getprevword()
37n/a self.text.delete("insert - %d chars" % len(word), "insert")
38n/a newword = words[index]
39n/a index = (index + 1) % len(words)
40n/a if index == 0:
41n/a self.text.bell() # Warn we cycled around
42n/a self.text.insert("insert", newword)
43n/a curinsert = self.text.index("insert")
44n/a curline = self.text.get("insert linestart", "insert lineend")
45n/a self.state = words, index, curinsert, curline
46n/a return "break"
47n/a
48n/a def getwords(self):
49n/a word = self.getprevword()
50n/a if not word:
51n/a return []
52n/a before = self.text.get("1.0", "insert wordstart")
53n/a wbefore = re.findall(r"\b" + word + r"\w+\b", before)
54n/a del before
55n/a after = self.text.get("insert wordend", "end")
56n/a wafter = re.findall(r"\b" + word + r"\w+\b", after)
57n/a del after
58n/a if not wbefore and not wafter:
59n/a return []
60n/a words = []
61n/a dict = {}
62n/a # search backwards through words before
63n/a wbefore.reverse()
64n/a for w in wbefore:
65n/a if dict.get(w):
66n/a continue
67n/a words.append(w)
68n/a dict[w] = w
69n/a # search onwards through words after
70n/a for w in wafter:
71n/a if dict.get(w):
72n/a continue
73n/a words.append(w)
74n/a dict[w] = w
75n/a words.append(word)
76n/a return words
77n/a
78n/a def getprevword(self):
79n/a line = self.text.get("insert linestart", "insert")
80n/a i = len(line)
81n/a while i > 0 and line[i-1] in self.wordchars:
82n/a i = i-1
83n/a return line[i:]