model: filter examples from views
[nit.git] / src / model / model_filters.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 module model_filters
16
17 import model_examples
18
19 # A list of filters that can be applied on a MEntity
20 #
21 #
22 # By default ModelFilter accepts all mentity.
23 #
24 # ~~~nitish
25 # var filter = new ModelFilter
26 # assert filter.accept_mentity(my_mentity) == true
27 # ~~~
28 #
29 # To quickly configure the filters, options can be passed to the constructor:
30 # ~~~
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
39 # )
40 # ~~~
41 class ModelFilter
42
43 # Accept `mentity` based on all the options from `self`?
44 #
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_example(mentity) then return false
53 if not accept_mentity_attribute(mentity) then return false
54 if not accept_mentity_empty_doc(mentity) then return false
55 if not accept_mentity_inherited(mentity) then return false
56 if not accept_mentity_full_name(mentity) then return false
57 return true
58 end
59
60 # Minimum visibility an entity must have to be accepted
61 #
62 # Default is `private_visibility`.
63 var min_visibility: MVisibility = private_visibility is optional, writable
64
65 # Accept `mentity` if its visibility is above `min_visibility`
66 fun accept_mentity_visibility(mentity: MEntity): Bool do
67 return mentity.visibility >= min_visibility
68 end
69
70 # Accept fictive entities?
71 #
72 # Default is `true`.
73 var accept_fictive = true is optional, writable
74
75 # Accept only non-fictive entities
76 #
77 # See `MEntity::is_fictive`.
78 fun accept_mentity_fictive(mentity: MEntity): Bool do
79 if accept_fictive then return true
80 return not mentity.is_fictive
81 end
82
83 # Accept nitunit test suites?
84 #
85 # Default is `true`.
86 var accept_test = true is optional, writable
87
88 # Accept only entities that are not `nitunit` related
89 fun accept_mentity_test(mentity: MEntity): Bool do
90 if accept_test then return true
91 if mentity isa MProperty then return accept_mentity(mentity.intro)
92 if mentity isa MMethodDef then
93 if mentity.is_before then return false
94 if mentity.is_before_all then return false
95 if mentity.is_after then return false
96 if mentity.is_after_all then return false
97 end
98 return not mentity.is_test
99 end
100
101 # Accept redef classdefs and propdefs?
102 #
103 # Default is `true`.
104 var accept_redef = true is optional, writable
105
106 # Accept a MClassDefs and MPropeDefs onyl if they are an introduction
107 #
108 # See `MClassDef::is_intro` and `MPropDef::is_intro`.
109 fun accept_mentity_redef(mentity: MEntity): Bool do
110 if accept_redef then return true
111 if mentity isa MClassDef then
112 return mentity.is_intro
113 else if mentity isa MPropDef then
114 return mentity.is_intro
115 end
116 return true
117 end
118
119 # Accept extern entities?
120 #
121 # Default is `true`.
122 var accept_extern = true is optional, writable
123
124 # Accept only non- extern entities
125 #
126 # See `MEntity::is_extern`.
127 fun accept_mentity_extern(mentity: MEntity): Bool do
128 if accept_extern then return true
129 if mentity isa MMethodDef then
130 return not mentity.is_extern
131 end
132 return true
133 end
134
135 # Accept `MAttribute` and `MAttributeDef` instances?
136 #
137 # Default is `true`.
138 var accept_attribute = true is optional, writable
139
140 # Accept only entities that are not a `MAttribute` or `MAttributeDef`
141 fun accept_mentity_attribute(mentity: MEntity): Bool do
142 if accept_attribute then return true
143 if mentity isa MAttribute then return false
144 if mentity isa MAttributeDef then return false
145 return true
146 end
147
148 # Accept entities with empty documentation?
149 #
150 # Default is `true`.
151 var accept_empty_doc = true is optional, writable
152
153 # Accept only entities with documentation
154 fun accept_mentity_empty_doc(mentity: MEntity): Bool do
155 if accept_empty_doc then return true
156 return mentity.mdoc_or_fallback != null
157 end
158
159 # Accept examples?
160 #
161 # Default is `true`.
162 var accept_example = true is optional
163
164 # Accept only entities that are not example related
165 fun accept_mentity_example(mentity: MEntity): Bool do
166 if accept_example then return true
167 return not mentity.is_example
168 end
169
170 # If set, accept only entities local to `accept_inherited`
171 var accept_inherited: nullable MEntity = null is optional
172
173 # Accept only entities local to `accept_inherited`
174 #
175 # This means no imported or inherited entities.
176 fun accept_mentity_inherited(mentity: MEntity): Bool do
177 var context = accept_inherited
178 if context == null then return true
179 if context isa MPackage then
180 if mentity isa MGroup then return mentity.mpackage == context
181 if mentity isa MModule then return mentity.mpackage == context
182 end
183 if context isa MGroup then
184 if mentity isa MModule then return mentity.mgroup == context
185 end
186 if context isa MModule then
187 if mentity isa MClass then return mentity.intro.mmodule == context
188 if mentity isa MClassDef then return mentity.mmodule == context
189 end
190 if context isa MClass then
191 if mentity isa MProperty then return mentity.intro_mclassdef.mclass == context
192 if mentity isa MPropDef then return mentity.mclassdef.mclass == context
193 end
194 if context isa MClassDef then
195 if mentity isa MProperty then return mentity.intro_mclassdef == context
196 if mentity isa MPropDef then return mentity.mclassdef == context
197 end
198 return true
199 end
200
201 # If set, accept only entities where `MEntity::full_name` contains `string`
202 var accept_full_name: nullable String = null is optional, writable
203
204 # Accept only entities where `MEntity::full_name` contains `string`
205 fun accept_mentity_full_name(mentity: MEntity): Bool do
206 var string = accept_full_name
207 if string == null then return true
208 return mentity.full_name.has(string)
209 end
210 end