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

Python code coverage for Lib/idlelib/ReplaceDialog.py

#countcontent
1n/afrom tkinter import *
2n/a
3n/afrom idlelib import SearchEngine
4n/afrom idlelib.SearchDialogBase import SearchDialogBase
5n/aimport re
6n/a
7n/a
8n/adef replace(text):
9n/a root = text._root()
10n/a engine = SearchEngine.get(root)
11n/a if not hasattr(engine, "_replacedialog"):
12n/a engine._replacedialog = ReplaceDialog(root, engine)
13n/a dialog = engine._replacedialog
14n/a dialog.open(text)
15n/a
16n/a
17n/aclass ReplaceDialog(SearchDialogBase):
18n/a
19n/a title = "Replace Dialog"
20n/a icon = "Replace"
21n/a
22n/a def __init__(self, root, engine):
23n/a SearchDialogBase.__init__(self, root, engine)
24n/a self.replvar = StringVar(root)
25n/a
26n/a def open(self, text):
27n/a SearchDialogBase.open(self, text)
28n/a try:
29n/a first = text.index("sel.first")
30n/a except TclError:
31n/a first = None
32n/a try:
33n/a last = text.index("sel.last")
34n/a except TclError:
35n/a last = None
36n/a first = first or text.index("insert")
37n/a last = last or first
38n/a self.show_hit(first, last)
39n/a self.ok = 1
40n/a
41n/a def create_entries(self):
42n/a SearchDialogBase.create_entries(self)
43n/a self.replent = self.make_entry("Replace with:", self.replvar)
44n/a
45n/a def create_command_buttons(self):
46n/a SearchDialogBase.create_command_buttons(self)
47n/a self.make_button("Find", self.find_it)
48n/a self.make_button("Replace", self.replace_it)
49n/a self.make_button("Replace+Find", self.default_command, 1)
50n/a self.make_button("Replace All", self.replace_all)
51n/a
52n/a def find_it(self, event=None):
53n/a self.do_find(0)
54n/a
55n/a def replace_it(self, event=None):
56n/a if self.do_find(self.ok):
57n/a self.do_replace()
58n/a
59n/a def default_command(self, event=None):
60n/a if self.do_find(self.ok):
61n/a if self.do_replace(): # Only find next match if replace succeeded.
62n/a # A bad re can cause a it to fail.
63n/a self.do_find(0)
64n/a
65n/a def _replace_expand(self, m, repl):
66n/a """ Helper function for expanding a regular expression
67n/a in the replace field, if needed. """
68n/a if self.engine.isre():
69n/a try:
70n/a new = m.expand(repl)
71n/a except re.error:
72n/a self.engine.report_error(repl, 'Invalid Replace Expression')
73n/a new = None
74n/a else:
75n/a new = repl
76n/a
77n/a return new
78n/a
79n/a def replace_all(self, event=None):
80n/a prog = self.engine.getprog()
81n/a if not prog:
82n/a return
83n/a repl = self.replvar.get()
84n/a text = self.text
85n/a res = self.engine.search_text(text, prog)
86n/a if not res:
87n/a text.bell()
88n/a return
89n/a text.tag_remove("sel", "1.0", "end")
90n/a text.tag_remove("hit", "1.0", "end")
91n/a line = res[0]
92n/a col = res[1].start()
93n/a if self.engine.iswrap():
94n/a line = 1
95n/a col = 0
96n/a ok = 1
97n/a first = last = None
98n/a # XXX ought to replace circular instead of top-to-bottom when wrapping
99n/a text.undo_block_start()
100n/a while 1:
101n/a res = self.engine.search_forward(text, prog, line, col, 0, ok)
102n/a if not res:
103n/a break
104n/a line, m = res
105n/a chars = text.get("%d.0" % line, "%d.0" % (line+1))
106n/a orig = m.group()
107n/a new = self._replace_expand(m, repl)
108n/a if new is None:
109n/a break
110n/a i, j = m.span()
111n/a first = "%d.%d" % (line, i)
112n/a last = "%d.%d" % (line, j)
113n/a if new == orig:
114n/a text.mark_set("insert", last)
115n/a else:
116n/a text.mark_set("insert", first)
117n/a if first != last:
118n/a text.delete(first, last)
119n/a if new:
120n/a text.insert(first, new)
121n/a col = i + len(new)
122n/a ok = 0
123n/a text.undo_block_stop()
124n/a if first and last:
125n/a self.show_hit(first, last)
126n/a self.close()
127n/a
128n/a def do_find(self, ok=0):
129n/a if not self.engine.getprog():
130n/a return False
131n/a text = self.text
132n/a res = self.engine.search_text(text, None, ok)
133n/a if not res:
134n/a text.bell()
135n/a return False
136n/a line, m = res
137n/a i, j = m.span()
138n/a first = "%d.%d" % (line, i)
139n/a last = "%d.%d" % (line, j)
140n/a self.show_hit(first, last)
141n/a self.ok = 1
142n/a return True
143n/a
144n/a def do_replace(self):
145n/a prog = self.engine.getprog()
146n/a if not prog:
147n/a return False
148n/a text = self.text
149n/a try:
150n/a first = pos = text.index("sel.first")
151n/a last = text.index("sel.last")
152n/a except TclError:
153n/a pos = None
154n/a if not pos:
155n/a first = last = pos = text.index("insert")
156n/a line, col = SearchEngine.get_line_col(pos)
157n/a chars = text.get("%d.0" % line, "%d.0" % (line+1))
158n/a m = prog.match(chars, col)
159n/a if not prog:
160n/a return False
161n/a new = self._replace_expand(m, self.replvar.get())
162n/a if new is None:
163n/a return False
164n/a text.mark_set("insert", first)
165n/a text.undo_block_start()
166n/a if m.group():
167n/a text.delete(first, last)
168n/a if new:
169n/a text.insert(first, new)
170n/a text.undo_block_stop()
171n/a self.show_hit(first, text.index("insert"))
172n/a self.ok = 0
173n/a return True
174n/a
175n/a def show_hit(self, first, last):
176n/a text = self.text
177n/a text.mark_set("insert", first)
178n/a text.tag_remove("sel", "1.0", "end")
179n/a text.tag_add("sel", first, last)
180n/a text.tag_remove("hit", "1.0", "end")
181n/a if first == last:
182n/a text.tag_add("hit", first)
183n/a else:
184n/a text.tag_add("hit", first, last)
185n/a text.see("insert")
186n/a text.update_idletasks()
187n/a
188n/a def close(self, event=None):
189n/a SearchDialogBase.close(self, event)
190n/a self.text.tag_remove("hit", "1.0", "end")