Skip to content

Commit a222117

Browse files
committed
fix: rewrite code highlight algorithm for nicer highlighting of symbols and numbers
fix #776
1 parent 5630b1c commit a222117

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

autoload/TextUtils.gd

+28-23
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,19 @@ var _REGEX_REPLACE_MAP := {}
3939
func _init() -> void:
4040
_REGEXES["code"] = RegEx.new()
4141
_REGEXES["func"] = RegEx.new()
42-
_REGEXES["method_call"] = RegEx.new()
4342
_REGEXES["number"] = RegEx.new()
4443
_REGEXES["symbol"] = RegEx.new()
44+
_REGEXES["format"] = RegEx.new()
45+
4546

4647
_REGEXES["code"].compile("\\[code\\](.+?)\\[\\/code\\]")
4748
_REGEXES["func"].compile("(?<func>func)")
48-
_REGEXES["method_call"].compile("(?<method_name>\\w[\\w\\d]*)(?<method_args>\\(.*?\\))")
4949
_REGEXES["number"].compile("(?<number>\\d+(\\.\\d+)?)")
50-
_REGEXES["symbol"].compile("(?<symbol>[a-zA-Z][a-zA-Z0-9_]+)")
50+
_REGEXES["symbol"].compile("(?<symbol>[a-zA-Z][a-zA-Z0-9_]+|[a-zA-Z])")
51+
_REGEXES["format"].compile("\\d+(\\.\\d+)?|[a-zA-Z0-9_]+")
5152

5253
_REGEX_REPLACE_MAP = {
5354
"func": "[color=#%s]$func[/color]" % CodeEditorEnhancer.COLOR_KEYWORD.to_html(false),
54-
"method_call":
55-
(
56-
"[color=#%s]$method_name[/color]$method_args"
57-
% CodeEditorEnhancer.COLOR_MEMBER.to_html(false)
58-
),
5955
"number": "[color=#%s]$number[/color]" % CodeEditorEnhancer.COLOR_NUMBERS.to_html(false),
6056
"symbol": "[color=#%s]$symbol[/color]" % CodeEditorEnhancer.COLOR_MEMBER.to_html(false)
6157
}
@@ -71,21 +67,30 @@ func bbcode_add_code_color(bbcode_text := "") -> String:
7167
var match_string: String = regex_match.strings[1]
7268

7369
var colored_string := ""
74-
for regex_type in [
75-
"func",
76-
"method_call",
77-
"number",
78-
"symbol",
79-
]:
80-
var replaced: String = _REGEXES[regex_type].sub(
81-
match_string, _REGEX_REPLACE_MAP[regex_type], false
82-
)
83-
if match_string != replaced:
84-
colored_string = replaced
85-
break
86-
87-
if colored_string == "":
88-
colored_string = match_string
70+
# The algorithm consists of finding all regex matches of a-zA-Z0-9_ and \d.\d
71+
# Then formatting these regex matches, and adding the parts in-between
72+
# matches to the formatted string.
73+
var to_format: Array = _REGEXES["format"].search_all(match_string)
74+
var last_match_end := -1
75+
for match_to_format in to_format:
76+
var match_start: int = match_to_format.get_start()
77+
if last_match_end != -1:
78+
colored_string += match_string.substr(last_match_end, match_start - last_match_end)
79+
var part: String = match_to_format.get_string()
80+
for regex_type in [
81+
"func",
82+
"symbol",
83+
"number",
84+
]:
85+
var replaced: String = _REGEXES[regex_type].sub(
86+
part, _REGEX_REPLACE_MAP[regex_type], false
87+
)
88+
if part != replaced:
89+
colored_string += replaced
90+
last_match_end = match_to_format.get_end()
91+
break
92+
93+
colored_string += match_string.substr(last_match_end)
8994
colored_string = "[code]" + colored_string + "[/code]"
9095
bbcode_text.erase(index_offset, initial_length)
9196
bbcode_text = bbcode_text.insert(index_offset, colored_string)

0 commit comments

Comments
 (0)