Skip to content

Commit 53d9964

Browse files
committed
gcodelexer - added highlighting for comment commands
highlights commands in comments listed in the https://linuxcnc.org/docs/html/gcode/overview.html using color[5] (used by "Other" group in lexer). Only highlights the last comment command on the line as requested by Greg Carl.
1 parent 041ba71 commit 53d9964

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

lib/python/qtvcp/widgets/gcode_editor.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,35 +114,48 @@ def styleText(self, start, end):
114114
return
115115

116116
re_tokens = {
117-
1: r"(?:[N]\d+|\(.*\)|;.*)", # LineNo and Comment
117+
1: r"(?:[N]\d+|\(.*?\)|;.*)", # LineNo and Comment
118118
2: r"[G]\d{1,4}\.?\d*", # Gcode
119119
3: r"[M]\d{1,4}\.?\d*", # Mcode
120120
4: r"[XYZABCUVW]{1}(?:[+-]?[\d\.]+|\#\<.*?\>|\[.*?\]|\#\d+)", # Axis
121121
5: r"[EFHIJKDQLRPST]{1}(?:[+-]?[\d\.]+|\#\<.*?\>|\[.*?\]|\#\d+)", # Other (feed,rpm,radius,etc)
122-
0: r"\s+|\w+|\W", # Default (fallback)
122+
0: r"\s+|\w+|\W", # Default (fallback)
123123
}
124124

125+
re_comment_cmd = r"(?:\(\s*(?:print|debug|msg|logopen|logappend|logclose|log|pyrun|pyreload|abort|probeopen|probeclose)|\;py)(?:,|\s*)"
126+
125127
re_string = "|".join(re_tokens.values())
126128
p = re.compile(re_string, re.IGNORECASE)
127129

128-
token_list = [(token, len(bytearray(token, "utf-8"))) for token in p.findall(source)]
129-
130-
for token in token_list:
131-
if re.match(re_tokens[self.Comment], token[0], re.IGNORECASE):
132-
self.setStyling(token[1], self.Comment)
133-
# print("{}:{} ({})".format(token[0], token[1], "Comment"))
134-
elif re.match(re_tokens[self.Gcode], token[0], re.IGNORECASE):
135-
self.setStyling(token[1], self.Gcode)
136-
elif re.match(re_tokens[self.Mcode], token[0], re.IGNORECASE):
137-
self.setStyling(token[1], self.Mcode)
138-
elif re.match(re_tokens[self.Axis], token[0], re.IGNORECASE):
139-
self.setStyling(1, self.Axis)
140-
self.setStyling(token[1] - 1, self.AxisValue)
141-
elif re.match(re_tokens[self.Other], token[0], re.IGNORECASE):
142-
self.setStyling(1, self.Other)
143-
self.setStyling(token[1] - 1, self.OtherValue)
144-
else:
145-
self.setStyling(token[1], self.Default)
130+
for line in source.splitlines(True):
131+
token_list = [(token, len(bytearray(token, "utf-8"))) for token in p.findall(line)]
132+
num_comment_cmds = len(re.findall(re_comment_cmd, line, re.IGNORECASE))
133+
134+
for token in token_list:
135+
if re.match(re_tokens[self.Comment], token[0], re.IGNORECASE):
136+
m = re.search(re_comment_cmd, token[0], re.IGNORECASE)
137+
if m:
138+
num_comment_cmds -= 1
139+
140+
if m and num_comment_cmds == 0:
141+
# Only highlight last comment_cmd on line
142+
self.setStyling(1, self.Comment)
143+
self.setStyling(m.span()[1] - 1, self.Other)
144+
self.setStyling(len(token[0]) - m.span()[1], self.Comment)
145+
else:
146+
self.setStyling(token[1], self.Comment)
147+
elif re.match(re_tokens[self.Gcode], token[0], re.IGNORECASE):
148+
self.setStyling(token[1], self.Gcode)
149+
elif re.match(re_tokens[self.Mcode], token[0], re.IGNORECASE):
150+
self.setStyling(token[1], self.Mcode)
151+
elif re.match(re_tokens[self.Axis], token[0], re.IGNORECASE):
152+
self.setStyling(1, self.Axis)
153+
self.setStyling(token[1] - 1, self.AxisValue)
154+
elif re.match(re_tokens[self.Other], token[0], re.IGNORECASE):
155+
self.setStyling(1, self.Other)
156+
self.setStyling(token[1] - 1, self.OtherValue)
157+
else:
158+
self.setStyling(token[1], self.Default)
146159

147160

148161
##########################################################
@@ -157,10 +170,10 @@ class EditorBase(QsciScintilla):
157170
# styleFont[0] and _styleColor[0] as defaults when they are not
158171
# set in the dict manually, or by CSS properties.
159172
_styleFont = {
160-
0: QFont("Deja Vu Sans Mono", 10), # Default Font
161-
# 2: QFont("Deja Vu Sans Mono", 20, weight=QFont.Bold), # Gcode
162-
# 5: QFont("Deja Vu Sans Mono", 11, weight=QFont.Bold), # Other
163-
# "Margins": QFont("Deja Vu Sans Mono", 9), # Margins
173+
0: QFont("Courier", 11), # Default Font
174+
# 2: QFont("Courier", 20, weight=QFont.Bold), # Gcode
175+
# 5: QFont("Courier", 11, weight=QFont.Bold), # Other
176+
# "Margins": QFont("Courier", 9), # Margins
164177
}
165178

166179
_styleColor = {

0 commit comments

Comments
 (0)