Add `\` & `\\` to TBlElementQuery to more easily navigate through BlElement hierarchy
Similar methods are defined in GtSmaCCNodeQuery & SmaCCParseNode and are very useful and make code very concise. I find myself needing to search up the BlElement parent hierarchy with as much or more frequency than needing to search down the hierarchy into an element's children.
We have // and /
I like the idea of adding \ and \ to navigate the parent hierarchy! We needed it too! I will add
@syrel This has been open for a while but still think useful for scripter tests and just generally. Below is a rather naive POC implementation of BlElementEagerQuery>>#'\\' I'll keep as an extension method but couldn't really use it in any public code as would be a dependency, proper support would be great.
\\ aSelector
self result size = 1
ifFalse: [ self
error: 'Parent queries can only be ran on individual elements, not query results with more than one element.' ].
^ self result anyOne
allParentsDetect: [ :anElement |
| return |
return := false.
aSelector isClass ifTrue: [ return := anElement isKindOf: aSelector ].
aSelector isSymbol ifTrue: [ return := anElement id asSymbol = aSelector ].
aSelector isBlock ifTrue: [ return := aSelector value: anElement ].
return ]
ifFound: [ :found | found query ]
ifNone: [ self // UndefinedObject ]
The limitation of only being able to run on queries where the result is a single element could probably be relaxed with a proper implementation. It just helped to simplify this prototype and seems like the most obvious for the use cases I can think of.
When one inspects the below element, the various queries work as expected.
BrFrame new
id: #test;
addChild: BlBasicExamples new circle
Search for parents by class:
self query // BlElement \\ BrFrame
Search for parent by id:
self query // BlElement \\ #test
Search for parent with a closure
self query // BlElement \\ [:anElement | anElement children size = 1 ]
Failing query (the result reflects a failed child query as that's the only direction the current semantics support)
self query // BlElement \\ #nonExistentID