Patch 1479219 - Tal Einat

1. 'as' highlighted as builtin in comment string on import line
2. Comments such as "#False identity" which start with a keyword immediately
   after the '#' character aren't colored as comments.
3. u or U beginning unicode string not correctly highlighted

Closes bug 1325071
This commit is contained in:
Kurt B. Kaiser 2006-07-17 21:59:27 +00:00
parent 7a752e7ad4
commit a2f60a47b5
1 changed files with 18 additions and 12 deletions

View File

@ -8,28 +8,29 @@ from configHandler import idleConf
DEBUG = False DEBUG = False
def any(name, list): def any(name, alternates):
return "(?P<%s>" % name + "|".join(list) + ")" "Return a named group pattern matching list of alternates."
return "(?P<%s>" % name + "|".join(alternates) + ")"
def make_pat(): def make_pat():
kw = r"\b" + any("KEYWORD", keyword.kwlist) + r"\b" kw = r"\b" + any("KEYWORD", keyword.kwlist) + r"\b"
builtinlist = [str(name) for name in dir(__builtin__) builtinlist = [str(name) for name in dir(__builtin__)
if not name.startswith('_')] if not name.startswith('_')]
# self.file = file("file") : # self.file = file("file") :
# 1st 'file' colorized normal, 2nd as builtin, 3rd as comment # 1st 'file' colorized normal, 2nd as builtin, 3rd as string
builtin = r"([^.'\"\\]\b|^)" + any("BUILTIN", builtinlist) + r"\b" builtin = r"([^.'\"\\#]\b|^)" + any("BUILTIN", builtinlist) + r"\b"
comment = any("COMMENT", [r"#[^\n]*"]) comment = any("COMMENT", [r"#[^\n]*"])
sqstring = r"(\b[rR])?'[^'\\\n]*(\\.[^'\\\n]*)*'?" sqstring = r"(\b[rRuU])?'[^'\\\n]*(\\.[^'\\\n]*)*'?"
dqstring = r'(\b[rR])?"[^"\\\n]*(\\.[^"\\\n]*)*"?' dqstring = r'(\b[rRuU])?"[^"\\\n]*(\\.[^"\\\n]*)*"?'
sq3string = r"(\b[rR])?'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?" sq3string = r"(\b[rRuU])?'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?"
dq3string = r'(\b[rR])?"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?' dq3string = r'(\b[rRuU])?"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?'
string = any("STRING", [sq3string, dq3string, sqstring, dqstring]) string = any("STRING", [sq3string, dq3string, sqstring, dqstring])
return kw + "|" + builtin + "|" + comment + "|" + string +\ return kw + "|" + builtin + "|" + comment + "|" + string +\
"|" + any("SYNC", [r"\n"]) "|" + any("SYNC", [r"\n"])
prog = re.compile(make_pat(), re.S) prog = re.compile(make_pat(), re.S)
idprog = re.compile(r"\s+(\w+)", re.S) idprog = re.compile(r"\s+(\w+)", re.S)
asprog = re.compile(r".*?\b(as)\b", re.S) asprog = re.compile(r".*?\b(as)\b")
class ColorDelegator(Delegator): class ColorDelegator(Delegator):
@ -208,10 +209,15 @@ class ColorDelegator(Delegator):
head + "+%dc" % a, head + "+%dc" % a,
head + "+%dc" % b) head + "+%dc" % b)
elif value == "import": elif value == "import":
# color all the "as" words on same line; # color all the "as" words on same line, except
# cheap approximation to the truth # if in a comment; cheap approximation to the
# truth
if '#' in chars:
endpos = chars.index('#')
else:
endpos = len(chars)
while True: while True:
m1 = self.asprog.match(chars, b) m1 = self.asprog.match(chars, b, endpos)
if not m1: if not m1:
break break
a, b = m1.span(1) a, b = m1.span(1)