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

Python code coverage for Lib/idlelib/autoexpand.py

#countcontent
1n/a'''Complete the current word before the cursor with words in the editor.
2n/a
3n/aEach menu selection or shortcut key selection replaces the word with a
4n/adifferent word with the same prefix. The search for matches begins
5n/abefore the target and moves toward the top of the editor. It then starts
6n/aafter the cursor and moves down. It then returns to the original word and
7n/athe cycle starts again.
8n/a
9n/aChanging the current text line or leaving the cursor in a different
10n/aplace before requesting the next selection causes AutoExpand to reset
11n/aits state.
12n/a
13n/aThis is an extension file and there is only one instance of AutoExpand.
14n/a'''
15n/aimport re
16n/aimport string
17n/a
18n/a###$ event <<expand-word>>
19n/a###$ win <Alt-slash>
20n/a###$ unix <Alt-slash>
21n/a
22n/aclass AutoExpand:
23n/a
24n/a menudefs = [
25n/a ('edit', [
26n/a ('E_xpand Word', '<<expand-word>>'),
27n/a ]),
28n/a ]
29n/a
30n/a wordchars = string.ascii_letters + string.digits + "_"
31n/a
32n/a def __init__(self, editwin):
33n/a self.text = editwin.text
34n/a self.bell = self.text.bell
35n/a self.state = None
36n/a
37n/a def expand_word_event(self, event):
38n/a "Replace the current word with the next expansion."
39n/a curinsert = self.text.index("insert")
40n/a curline = self.text.get("insert linestart", "insert lineend")
41n/a if not self.state:
42n/a words = self.getwords()
43n/a index = 0
44n/a else:
45n/a words, index, insert, line = self.state
46n/a if insert != curinsert or line != curline:
47n/a words = self.getwords()
48n/a index = 0
49n/a if not words:
50n/a self.bell()
51n/a return "break"
52n/a word = self.getprevword()
53n/a self.text.delete("insert - %d chars" % len(word), "insert")
54n/a newword = words[index]
55n/a index = (index + 1) % len(words)
56n/a if index == 0:
57n/a self.bell() # Warn we cycled around
58n/a self.text.insert("insert", newword)
59n/a curinsert = self.text.index("insert")
60n/a curline = self.text.get("insert linestart", "insert lineend")
61n/a self.state = words, index, curinsert, curline
62n/a return "break"
63n/a
64n/a def getwords(self):
65n/a "Return a list of words that match the prefix before the cursor."
66n/a word = self.getprevword()
67n/a if not word:
68n/a return []
69n/a before = self.text.get("1.0", "insert wordstart")
70n/a wbefore = re.findall(r"\b" + word + r"\w+\b", before)
71n/a del before
72n/a after = self.text.get("insert wordend", "end")
73n/a wafter = re.findall(r"\b" + word + r"\w+\b", after)
74n/a del after
75n/a if not wbefore and not wafter:
76n/a return []
77n/a words = []
78n/a dict = {}
79n/a # search backwards through words before
80n/a wbefore.reverse()
81n/a for w in wbefore:
82n/a if dict.get(w):
83n/a continue
84n/a words.append(w)
85n/a dict[w] = w
86n/a # search onwards through words after
87n/a for w in wafter:
88n/a if dict.get(w):
89n/a continue
90n/a words.append(w)
91n/a dict[w] = w
92n/a words.append(word)
93n/a return words
94n/a
95n/a def getprevword(self):
96n/a "Return the word prefix before the cursor."
97n/a line = self.text.get("insert linestart", "insert")
98n/a i = len(line)
99n/a while i > 0 and line[i-1] in self.wordchars:
100n/a i = i-1
101n/a return line[i:]
102n/a
103n/aif __name__ == '__main__':
104n/a import unittest
105n/a unittest.main('idlelib.idle_test.test_autoexpand', verbosity=2)