Reviewed template_dialog.py. I made cosmetic changes in some variable names to make the code easier (for me) to understand.

This commit is contained in:
Charles Haley 2021-11-20 10:52:34 +00:00
parent de477900ac
commit b73125904a

View File

@ -49,7 +49,7 @@ class TemplateHighlighter(QSyntaxHighlighter):
def __init__(self, parent=None, builtin_functions=None): def __init__(self, parent=None, builtin_functions=None):
super().__init__(parent) super().__init__(parent)
self.initializeFormats() self.initialize_formats()
self.initialize_rules(builtin_functions) self.initialize_rules(builtin_functions)
self.regenerate_paren_positions() self.regenerate_paren_positions()
self.highlighted_paren = False self.highlighted_paren = False
@ -87,7 +87,7 @@ class TemplateHighlighter(QSyntaxHighlighter):
a(r'\)', "rparen") a(r'\)', "rparen")
self.Rules = tuple(r) self.Rules = tuple(r)
def initializeFormats(self): def initialize_formats(self):
font_name = gprefs.get('gpm_template_editor_font', None) font_name = gprefs.get('gpm_template_editor_font', None)
size = gprefs['gpm_template_editor_font_size'] size = gprefs['gpm_template_editor_font_size']
if font_name is None: if font_name is None:
@ -95,37 +95,37 @@ class TemplateHighlighter(QSyntaxHighlighter):
font.setFixedPitch(True) font.setFixedPitch(True)
font.setPointSize(size) font.setPointSize(size)
font_name = font.family() font_name = font.family()
Config = self.Config = {} config = self.Config = {}
Config["fontfamily"] = font_name config["fontfamily"] = font_name
pal = QApplication.instance().palette() app_palette = QApplication.instance().palette()
for name, color, bold, italic in ( for name, color, bold, italic in (
("normal", None, False, False), ("normal", None, False, False),
("keyword", pal.color(QPalette.ColorRole.Link).name(), True, False), ("keyword", app_palette.color(QPalette.ColorRole.Link).name(), True, False),
("builtin", pal.color(QPalette.ColorRole.Link).name(), False, False), ("builtin", app_palette.color(QPalette.ColorRole.Link).name(), False, False),
("identifier", None, False, True), ("identifier", None, False, True),
("comment", "#007F00", False, True), ("comment", "#007F00", False, True),
("string", "#808000", False, False), ("string", "#808000", False, False),
("number", "#924900", False, False), ("number", "#924900", False, False),
("lparen", None, True, True), ("lparen", None, True, True),
("rparen", None, True, True)): ("rparen", None, True, True)):
Config["%sfontcolor" % name] = color config["%sfontcolor" % name] = color
Config["%sfontbold" % name] = bold config["%sfontbold" % name] = bold
Config["%sfontitalic" % name] = italic config["%sfontitalic" % name] = italic
baseFormat = QTextCharFormat() base_format = QTextCharFormat()
baseFormat.setFontFamily(Config["fontfamily"]) base_format.setFontFamily(config["fontfamily"])
Config["fontsize"] = size config["fontsize"] = size
baseFormat.setFontPointSize(Config["fontsize"]) base_format.setFontPointSize(config["fontsize"])
self.Formats = {}
self.Formats = {}
for name in ("normal", "keyword", "builtin", "comment", "identifier", for name in ("normal", "keyword", "builtin", "comment", "identifier",
"string", "number", "lparen", "rparen"): "string", "number", "lparen", "rparen"):
format_ = QTextCharFormat(baseFormat) format_ = QTextCharFormat(base_format)
col = Config["%sfontcolor" % name] color = config["%sfontcolor" % name]
if col: if color:
format_.setForeground(QColor(col)) format_.setForeground(QColor(color))
if Config["%sfontbold" % name]: if config["%sfontbold" % name]:
format_.setFontWeight(QFont.Weight.Bold) format_.setFontWeight(QFont.Weight.Bold)
format_.setFontItalic(Config["%sfontitalic" % name]) format_.setFontItalic(config["%sfontitalic" % name])
self.Formats[name] = format_ self.Formats[name] = format_
def find_paren(self, bn, pos): def find_paren(self, bn, pos):
@ -157,17 +157,17 @@ class TemplateHighlighter(QSyntaxHighlighter):
if self.generate_paren_positions: if self.generate_paren_positions:
t = str(text) t = str(text)
i = 0 i = 0
foundQuote = False found_quote = False
while i < len(t): while i < len(t):
c = t[i] c = t[i]
if c == ':': if c == ':':
# Deal with the funky syntax of template program mode. # Deal with the funky syntax of template program mode.
# This won't work if there are more than one template # This won't work if there are more than one template
# expression in the document. # expression in the document.
if not foundQuote and i+1 < len(t) and t[i+1] == "'": if not found_quote and i+1 < len(t) and t[i+1] == "'":
i += 2 i += 2
elif c in ["'", '"']: elif c in ["'", '"']:
foundQuote = True found_quote = True
i += 1 i += 1
j = t[i:].find(c) j = t[i:].find(c)
if j < 0: if j < 0:
@ -186,11 +186,11 @@ class TemplateHighlighter(QSyntaxHighlighter):
QApplication.restoreOverrideCursor() QApplication.restoreOverrideCursor()
def check_cursor_pos(self, chr_, block, pos_in_block): def check_cursor_pos(self, chr_, block, pos_in_block):
found_pp = -1 paren_pos = -1
for i, pp in enumerate(self.paren_positions): for i, pp in enumerate(self.paren_positions):
pp.set_highlight(False) pp.set_highlight(False)
if pp.block == block and pp.pos == pos_in_block: if pp.block == block and pp.pos == pos_in_block:
found_pp = i paren_pos = i
if chr_ not in ('(', ')'): if chr_ not in ('(', ')'):
if self.highlighted_paren: if self.highlighted_paren:
@ -198,12 +198,12 @@ class TemplateHighlighter(QSyntaxHighlighter):
self.highlighted_paren = False self.highlighted_paren = False
return return
if found_pp >= 0: if paren_pos >= 0:
stack = 0 stack = 0
if chr_ == '(': if chr_ == '(':
list_ = self.paren_positions[found_pp+1:] list_ = self.paren_positions[paren_pos+1:]
else: else:
list_ = reversed(self.paren_positions[0:found_pp]) list_ = reversed(self.paren_positions[0:paren_pos])
for pp in list_: for pp in list_:
if pp.paren == chr_: if pp.paren == chr_:
stack += 1 stack += 1
@ -211,7 +211,7 @@ class TemplateHighlighter(QSyntaxHighlighter):
stack -= 1 stack -= 1
else: else:
pp.set_highlight(True) pp.set_highlight(True)
self.paren_positions[found_pp].set_highlight(True) self.paren_positions[paren_pos].set_highlight(True)
break break
self.highlighted_paren = True self.highlighted_paren = True
self.rehighlight() self.rehighlight()
@ -544,7 +544,7 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
self.textbox.setTabStopDistance(w) self.textbox.setTabStopDistance(w)
self.source_code.setTabStopDistance(w) self.source_code.setTabStopDistance(w)
self.textbox.setFont(font) self.textbox.setFont(font)
self.highlighter.initializeFormats() self.highlighter.initialize_formats()
self.highlighter.rehighlight() self.highlighter.rehighlight()
def set_up_font_boxes(self): def set_up_font_boxes(self):