Skip to content

Commit bbbc8b9

Browse files
committed
Refactor expr_contains function to normalize representations and improve pattern matching
1 parent 1a35d1f commit bbbc8b9

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

src/inspect.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function replace_curly_braces_in_symbols(expr)
6666
result = Expr(:curly, result, content)
6767
end
6868

69-
remaining = rest
69+
remaining = something(rest, "")
7070
end
7171

7272
return result === nothing ? expr : result
@@ -123,16 +123,21 @@ end
123123
Check if `expr` contains `sym` matching `pattern` (nothing = any timing).
124124
"""
125125
function expr_contains(expr, sym::Symbol, pattern)
126+
normalize_repr(x) = replace(string(x), "" => "{", "" => "}")
127+
sym_str = normalize_repr(sym)
128+
pattern_str = pattern === nothing ? "" : normalize_repr(pattern)
129+
126130
found = Ref(false)
127131
postwalk(expr) do x
128132
if pattern === nothing
129133
# Match symbol anywhere (as ref base or standalone)
130-
if x === sym || (x isa Expr && x.head == :ref && x.args[1] === sym)
134+
if normalize_repr(x) == sym_str ||
135+
(x isa Expr && x.head == :ref && normalize_repr(x.args[1]) == sym_str)
131136
found[] = true
132137
end
133138
else
134139
# Match exact expression pattern
135-
x == pattern && (found[] = true)
140+
normalize_repr(x) == pattern_str && (found[] = true)
136141
end
137142
x
138143
end
@@ -347,8 +352,8 @@ function get_dynamic_equations(𝓂::ℳ; filter::Union{Symbol, String, Nothing}
347352

348353
# Parse filter term (uses user-friendly format with [-1], [0], etc.)
349354
sym, pattern = parse_filter_term(filter)
350-
351-
return [expr for (expr, orig) in zip(exprs, 𝓂.equations.dynamic) if expr_contains(orig, sym, pattern)]
355+
356+
return [expr for expr in exprs if expr_contains(expr, sym, pattern)]
352357
end
353358

354359

0 commit comments

Comments
 (0)