Merge: highlight with ansi color (for console)
[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
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_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
56 return true
57 end
58
59 # Minimum visibility an entity must have to be accepted
60 #
61 # Default is `private_visibility`.
62 var min_visibility: MVisibility = private_visibility is optional, writable
63
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
67 end
68
69 # Accept fictive entities?
70 #
71 # Default is `true`.
72 var accept_fictive = true is optional, writable
73
74 # Accept only non-fictive entities
75 #
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
80 end
81
82 # Accept nitunit test suites?
83 #
84 # Default is `true`.
85 var accept_test = true is optional, writable
86
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
96 end
97 return not mentity.is_test
98 end
99
100 # Accept redef classdefs and propdefs?
101 #
102 # Default is `true`.
103 var accept_redef = true is optional, writable
104
105 # Accept a MClassDefs and MPropeDefs onyl if they are an introduction
106 #
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
114 end
115 return true
116 end
117
118 # Accept extern entities?
119 #
120 # Default is `true`.
121 var accept_extern = true is optional, writable
122
123 # Accept only non- extern entities
124 #
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
130 end
131 return true
132 end
133
134 # Accept `MAttribute` and `MAttributeDef` instances?
135 #
136 # Default is `true`.
137 var accept_attribute = true is optional, writable
138
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
144 return true
145 end
146
147 # Accept entities with empty documentation?
148 #
149 # Default is `true`.
150 var accept_empty_doc = true is optional, writable
151
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
156 end
157
158 # If set, accept only entities local to `accept_inherited`
159 var accept_inherited: nullable MEntity = null is optional
160
161 # Accept only entities local to `accept_inherited`
162 #
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
170 end
171 if context isa MGroup then
172 if mentity isa MModule then return mentity.mgroup == context
173 end
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
177 end
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
181 end
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
185 end
186 return true
187 end
188
189 # If set, accept only entities where `MEntity::full_name` contains `string`
190 var accept_full_name: nullable String = null is optional, writable
191
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)
197 end
198 end