Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 92b5ca3

Browse files
committed
M ColorDelegator.py
M PyShell.py M ScriptBinding.py 1. Update ScriptBinding.py to highlight a syntax error in the Edit window, and place the cursor on the error. Add a syntax check to the Run Script event instead of waiting until the script tries to run and raises a syntax error in the shell, forcing the user to navigate back to the Edit window to fix it. 2. Modify tag_config's appropriately in PyShell.py and ColorDelegator.py 3. Some minor clean-up in ScriptBinding.py
1 parent 51cd8a2 commit 92b5ca3

3 files changed

Lines changed: 40 additions & 22 deletions

File tree

Lib/idlelib/ColorDelegator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def LoadTagDefs(self):
6363
"SYNC": {'background':None,'foreground':None},
6464
"TODO": {'background':None,'foreground':None},
6565
"BREAK": idleConf.GetHighlight(theme, "break"),
66+
"ERROR": idleConf.GetHighlight(theme, "error"),
6667
# The following is used by ReplaceDialog:
6768
"hit": idleConf.GetHighlight(theme, "hit"),
6869
}

Lib/idlelib/PyShell.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ def LoadTagDefs(self):
261261
"stdout": idleConf.GetHighlight(theme, "stdout"),
262262
"stderr": idleConf.GetHighlight(theme, "stderr"),
263263
"console": idleConf.GetHighlight(theme, "console"),
264-
"ERROR": idleConf.GetHighlight(theme, "error"),
265264
None: idleConf.GetHighlight(theme, "normal"),
266265
})
267266

Lib/idlelib/ScriptBinding.py

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,28 @@
33
This adds the following commands:
44
55
- Check module does a full syntax check of the current module.
6-
It also runs the tabnanny to catch any inconsistent tabs.
6+
It also runs the tabnanny to catch any inconsistent tabs.
77
88
- Run module executes the module's code in the __main__ namespace. The window
9-
must have been saved previously. The module is added to sys.modules, and is
10-
also added to the __main__ namespace.
9+
must have been saved previously. The module is added to sys.modules, and is
10+
also added to the __main__ namespace.
1111
12-
XXX Redesign this interface (yet again) as follows:
12+
XXX GvR Redesign this interface (yet again) as follows:
1313
1414
- Present a dialog box for ``Run script''
1515
1616
- Allow specify command line arguments in the dialog box
1717
1818
"""
1919

20+
import re
21+
import string
22+
import tabnanny
23+
import tokenize
2024
import tkMessageBox
2125

26+
IDENTCHARS = string.ascii_letters + string.digits + "_"
27+
2228
indent_message = """Error: Inconsistent indentation detected!
2329
2430
This means that either:
@@ -32,13 +38,14 @@
3238
by Untabify Region (both in the Edit menu)."""
3339

3440

35-
# XXX TBD Implement stop-execution KBK 11Jun02
41+
# XXX 11Jun02 KBK TBD Implement stop-execution
42+
3643
class ScriptBinding:
3744

3845
menudefs = [
3946
('run', [None,
40-
('Check module', '<<check-module>>'),
41-
('Run script', '<<run-script>>'), ]), ]
47+
('Check Module', '<<check-module>>'),
48+
('Run Script', '<<run-script>>'), ]), ]
4249

4350
def __init__(self, editwin):
4451
self.editwin = editwin
@@ -53,12 +60,9 @@ def check_module_event(self, event):
5360
return
5461
if not self.tabnanny(filename):
5562
return
56-
if not self.checksyntax(filename):
57-
return
63+
self.checksyntax(filename)
5864

5965
def tabnanny(self, filename):
60-
import tabnanny
61-
import tokenize
6266
f = open(filename, 'r')
6367
try:
6468
tabnanny.process_tokens(tokenize.generate_tokens(f.readline))
@@ -77,31 +81,46 @@ def checksyntax(self, filename):
7781
source = f.read()
7882
f.close()
7983
if '\r' in source:
80-
import re
8184
source = re.sub(r"\r\n", "\n", source)
8285
if source and source[-1] != '\n':
8386
source = source + '\n'
8487
try:
85-
compile(source, filename, "exec")
88+
# If successful, return the compiled code
89+
return compile(source, filename, "exec")
8690
except (SyntaxError, OverflowError), err:
8791
try:
8892
msg, (errorfilename, lineno, offset, line) = err
8993
if not errorfilename:
9094
err.args = msg, (filename, lineno, offset, line)
9195
err.filename = filename
96+
self.colorize_syntax_error(msg, lineno, offset)
9297
except:
93-
lineno = None
9498
msg = "*** " + str(err)
95-
if lineno:
96-
self.editwin.gotoline(lineno)
9799
self.errorbox("Syntax error",
98100
"There's an error in your program:\n" + msg)
99-
return True
100-
101+
return False
102+
103+
def colorize_syntax_error(self, msg, lineno, offset):
104+
text = self.editwin.text
105+
pos = "0.0 + %d lines + %d chars" % (lineno-1, offset-1)
106+
text.tag_add("ERROR", pos)
107+
char = text.get(pos)
108+
if char and char in IDENTCHARS:
109+
text.tag_add("ERROR", pos + " wordstart", pos)
110+
if '\n' == text.get(pos): # error at line end
111+
text.mark_set("insert", pos)
112+
else:
113+
text.mark_set("insert", pos + "+1c")
114+
text.see(pos)
115+
101116
def run_script_event(self, event):
117+
"Check syntax, if ok run the script in the shell top level"
102118
filename = self.getfilename()
103119
if not filename:
104120
return
121+
code = self.checksyntax(filename)
122+
if not code:
123+
return
105124
flist = self.editwin.flist
106125
shell = flist.open_shell()
107126
interp = shell.interp
@@ -116,11 +135,10 @@ def run_script_event(self, event):
116135
from os.path import basename as _basename
117136
if (not _sys.argv or
118137
_basename(_sys.argv[0]) != _basename(_filename)):
119-
# XXX 25 July 2002 KBK should this be sys.argv not _sys.argv?
120138
_sys.argv = [_filename]
121-
del _filename, _sys, _basename
139+
del _filename, _sys, _basename
122140
\n""" % `filename`)
123-
interp.execfile(filename)
141+
interp.runcode(code)
124142

125143
def getfilename(self):
126144
# Logic to make sure we have a saved filename

0 commit comments

Comments
 (0)