Skip to content

Commit 0e5dae7

Browse files
authored
fix: 🐛 match any character between func name and ( (#495)
* fix: 🐛 match any character between func name and `(` * refactor: ♻️ use non-capturing group * fix: 🐛 match white space before `(` * fix: Ignore comments * refactor: 🔥 removed redundant whitespace token moved the regex string to the top and added a description * fix: 🐛 match space before `(` and word boundary for the method name * refactor: ♻️ removed `(?<!#)` It only handled one specific case, `#func`. Something like `# ` was still matched. To avoid giving a false impression, I removed it, and this case is now also handled by `is_top_level_func()`. * fix: 🐛 more word boundaries
1 parent 64bcd83 commit 0e5dae7

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

addons/mod_loader/internal/mod_hook_preprocessor.gd

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ const HASH_COLLISION_ERROR := \
1414
const MOD_LOADER_HOOKS_START_STRING := \
1515
"\n# ModLoader Hooks - The following code has been automatically added by the Godot Mod Loader."
1616

17+
## \\bfunc\\b\\s+ -> Match the word 'func' and one or more whitespace characters
18+
## \\b%s\\b -> the function name
19+
## (?:.*\\n*)*?\\s*\\( -> Match any character between zero and unlimited times, but be lazy
20+
## and only do this until a '(' is found.
21+
const REGEX_MATCH_FUNC_WITH_WHITESPACE := "\\bfunc\\b\\s+\\b%s\\b(?:.*\\n*)*?\\s*\\("
1722

1823
## finds function names used as setters and getters (excluding inline definitions)
1924
## group 2 and 4 contain the xetter names
@@ -338,7 +343,7 @@ func match_method_body(method_name: String, func_body_start_index: int, text: St
338343

339344
static func match_func_with_whitespace(method_name: String, text: String, offset := 0) -> RegExMatch:
340345
# Dynamically create the new regex for that specific name
341-
var func_with_whitespace := RegEx.create_from_string("func\\s+%s[\\\\\\s]*\\(" % method_name)
346+
var func_with_whitespace := RegEx.create_from_string(REGEX_MATCH_FUNC_WITH_WHITESPACE % method_name)
342347
return func_with_whitespace.search(text, offset)
343348

344349

0 commit comments

Comments
 (0)