mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 18:24:30 -04:00
Merge pull request #1 from un-pogaz/template_highlighter
Improve Highlighter
This commit is contained in:
commit
dfcdf7c131
@ -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])),
|
|
||||||
|
a(
|
||||||
|
"|".join([r"\b%s\b" % keyword for keyword in self.KEYWORDS_PYTHON]),
|
||||||
"keyword")
|
"keyword")
|
||||||
a(re.compile(
|
a(
|
||||||
"|".join([r"\b%s\b" % builtin for builtin in self.BUILTINS_PYTHON])),
|
"|".join([r"\b%s\b" % builtin for builtin in self.BUILTINS_PYTHON]),
|
||||||
"builtin")
|
"builtin")
|
||||||
a(re.compile(
|
a(
|
||||||
"|".join([r"\b%s\b" % constant
|
"|".join([r"\b%s\b" % constant
|
||||||
for constant in self.CONSTANTS_PYTHON])), "constant")
|
for constant in self.CONSTANTS_PYTHON]), "constant")
|
||||||
a(re.compile(
|
|
||||||
r"\bPyQt6\b|\bQt?[A-Z][a-z]\w+\b"), "pyqt")
|
a(r"\bPyQt6\b|\bqt.core\b|\bQt?[A-Z][a-z]\w+\b", "pyqt")
|
||||||
a(re.compile(r"\b@\w+\b"), "decorator")
|
a(r"@\w+(\.\w+)?\b", "decorator")
|
||||||
stringRe = re.compile(r"""(?:'[^']*?'|"[^"]*?")""")
|
|
||||||
|
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,7 +144,9 @@ 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 (
|
|
||||||
|
all_formats = (
|
||||||
|
# name, color, bold, italic
|
||||||
("normal", None, False, False),
|
("normal", None, False, False),
|
||||||
("keyword", app_palette.color(QPalette.ColorRole.Link).name(), True, False),
|
("keyword", app_palette.color(QPalette.ColorRole.Link).name(), True, False),
|
||||||
("builtin", app_palette.color(QPalette.ColorRole.Link).name(), False, False),
|
("builtin", app_palette.color(QPalette.ColorRole.Link).name(), False, False),
|
||||||
@ -149,8 +155,12 @@ class TemplateHighlighter(QSyntaxHighlighter):
|
|||||||
("comment", "#007F00", False, True),
|
("comment", "#007F00", False, True),
|
||||||
("string", "#808000", False, False),
|
("string", "#808000", False, False),
|
||||||
("number", "#924900", False, False),
|
("number", "#924900", False, False),
|
||||||
|
("decorator", "#FF8000", False, True),
|
||||||
|
("pyqt", None, False, False),
|
||||||
("lparen", None, True, True),
|
("lparen", None, True, True),
|
||||||
("rparen", 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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user