src/doc/doc_down: move base MDoc services to `mdoc` module
[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 import parse_annotations
19
20 # A list of filters that can be applied on a MEntity
21 #
22 #
23 # By default ModelFilter accepts all mentity.
24 #
25 # ~~~nitish
26 # var filter = new ModelFilter
27 # assert filter.accept_mentity(my_mentity) == true
28 # ~~~
29 #
30 # To quickly configure the filters, options can be passed to the constructor:
31 # ~~~
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
40 # )
41 # ~~~
42 class ModelFilter
43
44 # Accept `mentity` based on all the options from `self`?
45 #
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
60 return true
61 end
62
63 # Minimum visibility an entity must have to be accepted
64 #
65 # Default is `private_visibility`.
66 var min_visibility: MVisibility = private_visibility is optional, writable
67
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
71 end
72
73 # Accept fictive entities?
74 #
75 # Default is `true`.
76 var accept_fictive = true is optional, writable
77
78 # Accept only non-fictive entities
79 #
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
84 end
85
86 # Accept generated entities?
87 #
88 # Default is `true`.
89 var accept_generated = true is optional, writable
90
91 # Accept only non-generated entities
92 #
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")
104 end
105 return true
106 end
107
108 # Accept nitunit test suites?
109 #
110 # Default is `true`.
111 var accept_test = true is optional, writable
112
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
122 end
123 return not mentity.is_test
124 end
125
126 # Accept redef classdefs and propdefs?
127 #
128 # Default is `true`.
129 var accept_redef = true is optional, writable
130
131 # Accept a MClassDefs and MPropeDefs onyl if they are an introduction
132 #
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
140 end
141 return true
142 end
143
144 # Accept extern entities?
145 #
146 # Default is `true`.
147 var accept_extern = true is optional, writable
148
149 # Accept only non- extern entities
150 #
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
156 end
157 return true
158 end
159
160 # Accept `MAttribute` and `MAttributeDef` instances?
161 #
162 # Default is `true`.
163 var accept_attribute = true is optional, writable
164
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
170 return true
171 end
172
173 # Accept entities with empty documentation?
174 #
175 # Default is `true`.
176 var accept_empty_doc = true is optional, writable
177
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
182 end
183
184 # Accept examples?
185 #
186 # Default is `true`.
187 var accept_example = true is optional
188
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
193 end
194
195 # If set, accept only entities local to `accept_inherited`
196 var accept_inherited: nullable MEntity = null is optional
197
198 # Accept only entities local to `accept_inherited`
199 #
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
207 end
208 if context isa MGroup then
209 if mentity isa MModule then return mentity.mgroup == context
210 end
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
214 end
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
218 end
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
222 end
223 return true
224 end
225
226 # If set, accept only entities where `MEntity::full_name` contains `string`
227 var accept_full_name: nullable String = null is optional, writable
228
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)
234 end
235
236 # Accept broken classes and properties?
237 #
238 # Default is `false`.
239 var accept_broken = false is optional, writable
240
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
245 end
246 end