1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 import parse_annotations
20 # A list of filters that can be applied on a MEntity
23 # By default ModelFilter accepts all mentity.
26 # var filter = new ModelFilter
27 # assert filter.accept_mentity(my_mentity) == true
30 # To quickly configure the filters, options can be passed to the constructor:
32 # var filter = new ModelFilter(
33 # min_visibility = protected_visibility,
34 # accept_fictive = false,
35 # accept_test = false,
36 # accept_redef = false,
37 # accept_extern = false,
38 # accept_attribute = false,
39 # accept_empty_doc = false
44 # Accept `mentity` based on all the options from `self`?
46 # If one of the filter returns `false` then the `mentity` is not accepted.
47 fun accept_mentity
(mentity
: MEntity): Bool do
48 if not accept_mentity_broken
(mentity
) then return false
49 if not accept_mentity_visibility
(mentity
) then return false
50 if not accept_mentity_fictive
(mentity
) then return false
51 if not accept_mentity_generated
(mentity
) then return false
52 if not accept_mentity_test
(mentity
) then return false
53 if not accept_mentity_redef
(mentity
) then return false
54 if not accept_mentity_extern
(mentity
) then return false
55 if not accept_mentity_example
(mentity
) then return false
56 if not accept_mentity_attribute
(mentity
) then return false
57 if not accept_mentity_empty_doc
(mentity
) then return false
58 if not accept_mentity_inherited
(mentity
) then return false
59 if not accept_mentity_full_name
(mentity
) then return false
63 # Minimum visibility an entity must have to be accepted
65 # Default is `private_visibility`.
66 var min_visibility
: MVisibility = private_visibility
is optional
, writable
68 # Accept `mentity` if its visibility is above `min_visibility`
69 fun accept_mentity_visibility
(mentity
: MEntity): Bool do
70 return mentity
.visibility
>= min_visibility
73 # Accept fictive entities?
76 var accept_fictive
= true is optional
, writable
78 # Accept only non-fictive entities
80 # See `MEntity::is_fictive`.
81 fun accept_mentity_fictive
(mentity
: MEntity): Bool do
82 if accept_fictive
then return true
83 return not mentity
.is_fictive
86 # Accept generated entities?
89 var accept_generated
= true is optional
, writable
91 # Accept only non-generated entities
93 # See `MEntity::is_generated`.
94 fun accept_mentity_generated
(mentity
: MEntity): Bool do
95 if accept_generated
then return true
96 if mentity
isa MClass then mentity
= mentity
.intro
97 if mentity
isa MProperty then mentity
= mentity
.intro
98 if mentity
isa MModule then
99 return not mentity
.has_annotation
("generated")
100 else if mentity
isa MClassDef then
101 return not mentity
.has_annotation
("generated")
102 else if mentity
isa MPropDef then
103 return not mentity
.has_annotation
("generated")
108 # Accept nitunit test suites?
111 var accept_test
= true is optional
, writable
113 # Accept only entities that are not `nitunit` related
114 fun accept_mentity_test
(mentity
: MEntity): Bool do
115 if accept_test
then return true
116 if mentity
isa MProperty then return accept_mentity
(mentity
.intro
)
117 if mentity
isa MMethodDef then
118 if mentity
.is_before
then return false
119 if mentity
.is_before_all
then return false
120 if mentity
.is_after
then return false
121 if mentity
.is_after_all
then return false
123 return not mentity
.is_test
126 # Accept redef classdefs and propdefs?
129 var accept_redef
= true is optional
, writable
131 # Accept a MClassDefs and MPropeDefs onyl if they are an introduction
133 # See `MClassDef::is_intro` and `MPropDef::is_intro`.
134 fun accept_mentity_redef
(mentity
: MEntity): Bool do
135 if accept_redef
then return true
136 if mentity
isa MClassDef then
137 return mentity
.is_intro
138 else if mentity
isa MPropDef then
139 return mentity
.is_intro
144 # Accept extern entities?
147 var accept_extern
= true is optional
, writable
149 # Accept only non- extern entities
151 # See `MEntity::is_extern`.
152 fun accept_mentity_extern
(mentity
: MEntity): Bool do
153 if accept_extern
then return true
154 if mentity
isa MMethodDef then
155 return not mentity
.is_extern
160 # Accept `MAttribute` and `MAttributeDef` instances?
163 var accept_attribute
= true is optional
, writable
165 # Accept only entities that are not a `MAttribute` or `MAttributeDef`
166 fun accept_mentity_attribute
(mentity
: MEntity): Bool do
167 if accept_attribute
then return true
168 if mentity
isa MAttribute then return false
169 if mentity
isa MAttributeDef then return false
173 # Accept entities with empty documentation?
176 var accept_empty_doc
= true is optional
, writable
178 # Accept only entities with documentation
179 fun accept_mentity_empty_doc
(mentity
: MEntity): Bool do
180 if accept_empty_doc
then return true
181 return mentity
.mdoc_or_fallback
!= null
187 var accept_example
= true is optional
189 # Accept only entities that are not example related
190 fun accept_mentity_example
(mentity
: MEntity): Bool do
191 if accept_example
then return true
192 return not mentity
.is_example
195 # If set, accept only entities local to `accept_inherited`
196 var accept_inherited
: nullable MEntity = null is optional
198 # Accept only entities local to `accept_inherited`
200 # This means no imported or inherited entities.
201 fun accept_mentity_inherited
(mentity
: MEntity): Bool do
202 var context
= accept_inherited
203 if context
== null then return true
204 if context
isa MPackage then
205 if mentity
isa MGroup then return mentity
.mpackage
== context
206 if mentity
isa MModule then return mentity
.mpackage
== context
208 if context
isa MGroup then
209 if mentity
isa MModule then return mentity
.mgroup
== context
211 if context
isa MModule then
212 if mentity
isa MClass then return mentity
.intro
.mmodule
== context
213 if mentity
isa MClassDef then return mentity
.mmodule
== context
215 if context
isa MClass then
216 if mentity
isa MProperty then return mentity
.intro_mclassdef
.mclass
== context
217 if mentity
isa MPropDef then return mentity
.mclassdef
.mclass
== context
219 if context
isa MClassDef then
220 if mentity
isa MProperty then return mentity
.intro_mclassdef
== context
221 if mentity
isa MPropDef then return mentity
.mclassdef
== context
226 # If set, accept only entities where `MEntity::full_name` contains `string`
227 var accept_full_name
: nullable String = null is optional
, writable
229 # Accept only entities where `MEntity::full_name` contains `string`
230 fun accept_mentity_full_name
(mentity
: MEntity): Bool do
231 var string
= accept_full_name
232 if string
== null then return true
233 return mentity
.full_name
.has
(string
)
236 # Accept broken classes and properties?
238 # Default is `false`.
239 var accept_broken
= false is optional
, writable
241 # Accept only non broken entities
242 fun accept_mentity_broken
(mentity
: MEntity): Bool do
243 if accept_broken
then return true
244 return not mentity
.is_broken