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.
19 # A list of filters that can be applied on a MEntity
22 # By default ModelFilter accepts all mentity.
25 # var filter = new ModelFilter
26 # assert filter.accept_mentity(my_mentity) == true
29 # To quickly configure the filters, options can be passed to the constructor:
31 # var filter = new ModelFilter(
32 # min_visibility = protected_visibility,
33 # accept_fictive = false,
34 # accept_test = false,
35 # accept_redef = false,
36 # accept_extern = false,
37 # accept_attribute = false,
38 # accept_empty_doc = false
43 # Accept `mentity` based on all the options from `self`?
45 # If one of the filter returns `false` then the `mentity` is not accepted.
46 fun accept_mentity
(mentity
: MEntity): Bool do
47 if not accept_mentity_visibility
(mentity
) then return false
48 if not accept_mentity_fictive
(mentity
) then return false
49 if not accept_mentity_test
(mentity
) then return false
50 if not accept_mentity_redef
(mentity
) then return false
51 if not accept_mentity_extern
(mentity
) then return false
52 if not accept_mentity_attribute
(mentity
) then return false
53 if not accept_mentity_empty_doc
(mentity
) then return false
54 if not accept_mentity_inherited
(mentity
) then return false
55 if not accept_mentity_full_name
(mentity
) then return false
59 # Minimum visibility an entity must have to be accepted
61 # Default is `private_visibility`.
62 var min_visibility
: MVisibility = private_visibility
is optional
, writable
64 # Accept `mentity` if its visibility is above `min_visibility`
65 fun accept_mentity_visibility
(mentity
: MEntity): Bool do
66 return mentity
.visibility
>= min_visibility
69 # Accept fictive entities?
72 var accept_fictive
= true is optional
, writable
74 # Accept only non-fictive entities
76 # See `MEntity::is_fictive`.
77 fun accept_mentity_fictive
(mentity
: MEntity): Bool do
78 if accept_fictive
then return true
79 return not mentity
.is_fictive
82 # Accept nitunit test suites?
85 var accept_test
= true is optional
, writable
87 # Accept only entities that are not `nitunit` related
88 fun accept_mentity_test
(mentity
: MEntity): Bool do
89 if accept_test
then return true
90 if mentity
isa MProperty then return accept_mentity
(mentity
.intro
)
91 if mentity
isa MMethodDef then
92 if mentity
.is_before
then return false
93 if mentity
.is_before_all
then return false
94 if mentity
.is_after
then return false
95 if mentity
.is_after_all
then return false
97 return not mentity
.is_test
100 # Accept redef classdefs and propdefs?
103 var accept_redef
= true is optional
, writable
105 # Accept a MClassDefs and MPropeDefs onyl if they are an introduction
107 # See `MClassDef::is_intro` and `MPropDef::is_intro`.
108 fun accept_mentity_redef
(mentity
: MEntity): Bool do
109 if accept_redef
then return true
110 if mentity
isa MClassDef then
111 return mentity
.is_intro
112 else if mentity
isa MPropDef then
113 return mentity
.is_intro
118 # Accept extern entities?
121 var accept_extern
= true is optional
, writable
123 # Accept only non- extern entities
125 # See `MEntity::is_extern`.
126 fun accept_mentity_extern
(mentity
: MEntity): Bool do
127 if accept_extern
then return true
128 if mentity
isa MMethodDef then
129 return not mentity
.is_extern
134 # Accept `MAttribute` and `MAttributeDef` instances?
137 var accept_attribute
= true is optional
, writable
139 # Accept only entities that are not a `MAttribute` or `MAttributeDef`
140 fun accept_mentity_attribute
(mentity
: MEntity): Bool do
141 if accept_attribute
then return true
142 if mentity
isa MAttribute then return false
143 if mentity
isa MAttributeDef then return false
147 # Accept entities with empty documentation?
150 var accept_empty_doc
= true is optional
, writable
152 # Accept only entities with documentation
153 fun accept_mentity_empty_doc
(mentity
: MEntity): Bool do
154 if accept_empty_doc
then return true
155 return mentity
.mdoc_or_fallback
!= null
158 # If set, accept only entities local to `accept_inherited`
159 var accept_inherited
: nullable MEntity = null is optional
161 # Accept only entities local to `accept_inherited`
163 # This means no imported or inherited entities.
164 fun accept_mentity_inherited
(mentity
: MEntity): Bool do
165 var context
= accept_inherited
166 if context
== null then return true
167 if context
isa MPackage then
168 if mentity
isa MGroup then return mentity
.mpackage
== context
169 if mentity
isa MModule then return mentity
.mpackage
== context
171 if context
isa MGroup then
172 if mentity
isa MModule then return mentity
.mgroup
== context
174 if context
isa MModule then
175 if mentity
isa MClass then return mentity
.intro
.mmodule
== context
176 if mentity
isa MClassDef then return mentity
.mmodule
== context
178 if context
isa MClass then
179 if mentity
isa MProperty then return mentity
.intro_mclassdef
.mclass
== context
180 if mentity
isa MPropDef then return mentity
.mclassdef
.mclass
== context
182 if context
isa MClassDef then
183 if mentity
isa MProperty then return mentity
.intro_mclassdef
== context
184 if mentity
isa MPropDef then return mentity
.mclassdef
== context
189 # If set, accept only entities where `MEntity::full_name` contains `string`
190 var accept_full_name
: nullable String = null is optional
, writable
192 # Accept only entities where `MEntity::full_name` contains `string`
193 fun accept_mentity_full_name
(mentity
: MEntity): Bool do
194 var string
= accept_full_name
195 if string
== null then return true
196 return mentity
.full_name
.has
(string
)