Merge pull request #1 from un-pogaz/template_highlighter

Improve Highlighter
This commit is contained in:
Charles Haley 2022-10-10 18:54:36 +01:00 committed by GitHub
commit dfcdf7c131
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -46,7 +46,7 @@ class TemplateHighlighter(QSyntaxHighlighter):
BN_FACTOR = 1000 BN_FACTOR = 1000
KEYWORDS_GPM = ["program", 'if', 'then', 'else', 'elif', 'fi', 'for', 'rof', KEYWORDS_GPM = ['if', 'then', 'else', 'elif', 'fi', 'for', 'rof',
'separator', 'break', 'continue', 'return', 'in', 'inlist', 'separator', 'break', 'continue', 'return', 'in', 'inlist',
'def', 'fed', 'limit'] 'def', 'fed', 'limit']
@ -89,6 +89,7 @@ class TemplateHighlighter(QSyntaxHighlighter):
r"|\$+#?[a-zA-Z]\w*", r"|\$+#?[a-zA-Z]\w*",
"identifier") "identifier")
a(r"^\bprogram\b", "keyword")
a( a(
"|".join([r"\b%s\b" % keyword for keyword in self.KEYWORDS_GPM]), "|".join([r"\b%s\b" % keyword for keyword in self.KEYWORDS_GPM]),
"keyword") "keyword")
@ -101,22 +102,25 @@ class TemplateHighlighter(QSyntaxHighlighter):
a(r"""(?<!:)'[^']*'|"[^"]*\"""", "string") a(r"""(?<!:)'[^']*'|"[^"]*\"""", "string")
else: else:
a(re.compile( a(r"^\bpython\b", "keyword")
"|".join([r"\b%s\b" % keyword for keyword in self.KEYWORDS_PYTHON])),
"keyword") a(
a(re.compile( "|".join([r"\b%s\b" % keyword for keyword in self.KEYWORDS_PYTHON]),
"|".join([r"\b%s\b" % builtin for builtin in self.BUILTINS_PYTHON])), "keyword")
"builtin") a(
a(re.compile( "|".join([r"\b%s\b" % builtin for builtin in self.BUILTINS_PYTHON]),
"|".join([r"\b%s\b" % constant "builtin")
for constant in self.CONSTANTS_PYTHON])), "constant") a(
a(re.compile( "|".join([r"\b%s\b" % constant
r"\bPyQt6\b|\bQt?[A-Z][a-z]\w+\b"), "pyqt") for constant in self.CONSTANTS_PYTHON]), "constant")
a(re.compile(r"\b@\w+\b"), "decorator")
stringRe = re.compile(r"""(?:'[^']*?'|"[^"]*?")""") a(r"\bPyQt6\b|\bqt.core\b|\bQt?[A-Z][a-z]\w+\b", "pyqt")
a(r"@\w+(\.\w+)?\b", "decorator")
a(r"""(?:'[^']*?'|"[^"]*?")""", "string")
stringRe = r"""((:?"|'){3}).*?\1"""
a(stringRe, "string") a(stringRe, "string")
self.stringRe = re.compile(r"""(:?"["]".*?"["]"|'''.*?''')""") self.stringRe = re.compile(stringRe)
a(self.stringRe, "string")
self.tripleSingleRe = re.compile(r"""'''(?!")""") self.tripleSingleRe = re.compile(r"""'''(?!")""")
self.tripleDoubleRe = re.compile(r'''"""(?!')''') self.tripleDoubleRe = re.compile(r'''"""(?!')''')
a( a(
@ -140,17 +144,23 @@ class TemplateHighlighter(QSyntaxHighlighter):
config = self.Config = {} config = self.Config = {}
config["fontfamily"] = font_name config["fontfamily"] = font_name
app_palette = QApplication.instance().palette() app_palette = QApplication.instance().palette()
for name, color, bold, italic in (
("normal", None, False, False), all_formats = (
("keyword", app_palette.color(QPalette.ColorRole.Link).name(), True, False), # name, color, bold, italic
("builtin", app_palette.color(QPalette.ColorRole.Link).name(), False, False), ("normal", None, False, False),
("constant", app_palette.color(QPalette.ColorRole.Link).name(), False, False), ("keyword", app_palette.color(QPalette.ColorRole.Link).name(), True, False),
("identifier", None, False, True), ("builtin", app_palette.color(QPalette.ColorRole.Link).name(), False, False),
("comment", "#007F00", False, True), ("constant", app_palette.color(QPalette.ColorRole.Link).name(), False, False),
("string", "#808000", False, False), ("identifier", None, False, True),
("number", "#924900", False, False), ("comment", "#007F00", False, True),
("lparen", None, True, True), ("string", "#808000", False, False),
("rparen", None, True, True)): ("number", "#924900", False, False),
("decorator", "#FF8000", False, True),
("pyqt", None, False, False),
("lparen", None, True, True),
("rparen", None, True, True))
for name, color, bold, italic in all_formats:
config["%sfontcolor" % name] = color config["%sfontcolor" % name] = color
config["%sfontbold" % name] = bold config["%sfontbold" % name] = bold
config["%sfontitalic" % name] = italic config["%sfontitalic" % name] = italic
@ -160,8 +170,7 @@ class TemplateHighlighter(QSyntaxHighlighter):
base_format.setFontPointSize(config["fontsize"]) base_format.setFontPointSize(config["fontsize"])
self.Formats = {} self.Formats = {}
for name in ("normal", "keyword", "builtin", "comment", "identifier", for name, color, bold, italic in all_formats:
"string", "number", "lparen", "rparen", "constant"):
format_ = QTextCharFormat(base_format) format_ = QTextCharFormat(base_format)
color = config["%sfontcolor" % name] color = config["%sfontcolor" % name]
if color: if color:
@ -768,7 +777,6 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
self.highlighting_gpm = False self.highlighting_gpm = False
self.break_box.setChecked(False) self.break_box.setChecked(False)
self.break_box.setEnabled(False) self.break_box.setEnabled(False)
print(self.break_box.isEnabled())
elif not self.highlighting_gpm: elif not self.highlighting_gpm:
self.highlighter.initialize_rules(self.builtins, False) self.highlighter.initialize_rules(self.builtins, False)
self.highlighting_gpm = True self.highlighting_gpm = True