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

Python code coverage for Lib/idlelib/search.py

#countcontent
1n/afrom tkinter import TclError
2n/a
3n/afrom idlelib import searchengine
4n/afrom idlelib.searchbase import SearchDialogBase
5n/a
6n/adef _setup(text):
7n/a "Create or find the singleton SearchDialog instance."
8n/a root = text._root()
9n/a engine = searchengine.get(root)
10n/a if not hasattr(engine, "_searchdialog"):
11n/a engine._searchdialog = SearchDialog(root, engine)
12n/a return engine._searchdialog
13n/a
14n/adef find(text):
15n/a "Handle the editor edit menu item and corresponding event."
16n/a pat = text.get("sel.first", "sel.last")
17n/a return _setup(text).open(text, pat) # Open is inherited from SDBase.
18n/a
19n/adef find_again(text):
20n/a "Handle the editor edit menu item and corresponding event."
21n/a return _setup(text).find_again(text)
22n/a
23n/adef find_selection(text):
24n/a "Handle the editor edit menu item and corresponding event."
25n/a return _setup(text).find_selection(text)
26n/a
27n/a
28n/aclass SearchDialog(SearchDialogBase):
29n/a
30n/a def create_widgets(self):
31n/a SearchDialogBase.create_widgets(self)
32n/a self.make_button("Find Next", self.default_command, 1)
33n/a
34n/a def default_command(self, event=None):
35n/a if not self.engine.getprog():
36n/a return
37n/a self.find_again(self.text)
38n/a
39n/a def find_again(self, text):
40n/a if not self.engine.getpat():
41n/a self.open(text)
42n/a return False
43n/a if not self.engine.getprog():
44n/a return False
45n/a res = self.engine.search_text(text)
46n/a if res:
47n/a line, m = res
48n/a i, j = m.span()
49n/a first = "%d.%d" % (line, i)
50n/a last = "%d.%d" % (line, j)
51n/a try:
52n/a selfirst = text.index("sel.first")
53n/a sellast = text.index("sel.last")
54n/a if selfirst == first and sellast == last:
55n/a self.bell()
56n/a return False
57n/a except TclError:
58n/a pass
59n/a text.tag_remove("sel", "1.0", "end")
60n/a text.tag_add("sel", first, last)
61n/a text.mark_set("insert", self.engine.isback() and first or last)
62n/a text.see("insert")
63n/a return True
64n/a else:
65n/a self.bell()
66n/a return False
67n/a
68n/a def find_selection(self, text):
69n/a pat = text.get("sel.first", "sel.last")
70n/a if pat:
71n/a self.engine.setcookedpat(pat)
72n/a return self.find_again(text)
73n/a
74n/a
75n/adef _search_dialog(parent): # htest #
76n/a "Display search test box."
77n/a from tkinter import Toplevel, Text
78n/a from tkinter.ttk import Button
79n/a
80n/a box = Toplevel(parent)
81n/a box.title("Test SearchDialog")
82n/a x, y = map(int, parent.geometry().split('+')[1:])
83n/a box.geometry("+%d+%d" % (x, y + 175))
84n/a text = Text(box, inactiveselectbackground='gray')
85n/a text.pack()
86n/a text.insert("insert","This is a sample string.\n"*5)
87n/a
88n/a def show_find():
89n/a text.tag_add('sel', '1.0', 'end')
90n/a _setup(text).open(text)
91n/a text.tag_remove('sel', '1.0', 'end')
92n/a
93n/a button = Button(box, text="Search (selection ignored)", command=show_find)
94n/a button.pack()
95n/a
96n/aif __name__ == '__main__':
97n/a import unittest
98n/a unittest.main('idlelib.idle_test.test_search',
99n/a verbosity=2, exit=False)
100n/a
101n/a from idlelib.idle_test.htest import run
102n/a run(_search_dialog)