model: visitor uses filters
authorAlexandre Terrasa <alexandre@moz-code.org>
Sun, 22 Oct 2017 21:06:04 +0000 (17:06 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Tue, 7 Nov 2017 17:07:30 +0000 (12:07 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/model/model_visitor.nit
src/test_model_visitor.nit

index 9244055..e5a7409 100644 (file)
 # ~~~
 module model_visitor
 
-import model
+import model_filters
 
 # The abstract model visitor template.
 #
 # Specific visitor must implement the `visit` method to perform the work.
 abstract class ModelVisitor
+
        # Visit the entity `e`.
        #
        # This method setups `current_entity` and call `visit`.
@@ -68,81 +69,30 @@ abstract class ModelVisitor
        # It should not be called directly but used by `enter_visit`
        protected fun visit(e: MEntity) is abstract
 
-       # Filter classes and method on the visibility.
-       #
-       # If set, only the classes and method with at least the given
-       # visibility level will be visited.
-       var min_visibility: nullable MVisibility = null is writable
-
-       # Can we accept this `mentity` in the view regarding its visibility?
-       fun accept_visibility(mentity: MEntity): Bool do
-               return mentity.accept_visibility(min_visibility)
-       end
-
-       # Include fictive entities?
-       #
-       # By default, fictive entities (see `MEntity::is_fictive`) are not visited.
-       var include_fictive = false is writable
-
-       # Can we accept this `mentity` in the view regarding its fictivity?
-       fun accept_fictive(mentity: MEntity): Bool do
-               if include_fictive then return true
-               return not mentity.is_fictive
-       end
-
-       # Should we accept mentities with empty documentation?
+       # Filters to apply when visiting the model.
        #
-       # Default is `true`.
-       var include_empty_doc = true is writable
-
-       # Can we accept this `mentity` regarding its documentation?
-       fun accept_empty_doc(mentity: MEntity): Bool do
-               if include_empty_doc then return true
-               return mentity.mdoc != null
+       # See ModelFilters for configuration.
+       var filter: ModelFilter is lazy, writable, optional do
+               return new ModelFilter(
+                       min_visibility = protected_visibility,
+                       accept_fictive = false,
+                       accept_test = false,
+                       accept_redef = true,
+                       accept_extern = true,
+                       accept_attribute = true,
+                       accept_empty_doc = true
+               )
        end
 
-       # Should we accept nitunit test suites?
+       # Should we accept this `mentity` from the view?
        #
-       # Default is `false`.
-       var include_test = false is writable
-
-       # Can we accept this `mentity` regarding its test suite status?
-       fun accept_test(mentity: MEntity): Bool do
-               if include_test then return true
-               if mentity isa MProperty then
-                       if mentity.is_before or mentity.is_before_all then return false
-                       if mentity.is_after or mentity.is_after_all then return false
+       # If no `override_filter` is passed then use `self.filter`.
+       fun accept_mentity(mentity: MEntity, override_filter: nullable ModelFilter): Bool do
+               if override_filter != null then
+                       return override_filter.accept_mentity(mentity)
                end
-               if mentity isa MPropDef then
-                       if mentity.is_before or mentity.is_before_all then return false
-                       if mentity.is_after or mentity.is_after_all then return false
-               end
-               return not mentity.is_test
-       end
-
-       # Should we accept `MAttribute` instances?
-       #
-       # Default is `true`.
-       var include_attribute = true is writable
-
-       # Can we accept this `mentity` regarding its type?
-       fun accept_attribute(mentity: MEntity): Bool do
-               if include_attribute then return true
-               if mentity isa MAttribute then return false
-               if mentity isa MAttributeDef then return false
-               return true
+               return filter.accept_mentity(mentity)
        end
-
-       # Should we accept this `mentity` from the view?
-       fun accept_mentity(mentity: MEntity): Bool do
-               if not accept_visibility(mentity) then return false
-               if not accept_fictive(mentity) then return false
-               if not accept_empty_doc(mentity) then return false
-               if not accept_test(mentity) then return false
-               if not accept_attribute(mentity) then return false
-               return true
-       end
-
 end
 
 redef class MEntity
@@ -150,11 +100,6 @@ redef class MEntity
        #
        # See the specific implementation in the subclasses.
        fun visit_all(v: ModelVisitor) do end
-
-       private fun accept_visibility(min_visibility: nullable MVisibility): Bool do
-               if min_visibility == null then return true
-               return visibility >= min_visibility
-       end
 end
 
 redef class Model
index d7cc947..9b8c62e 100644 (file)
@@ -55,42 +55,39 @@ do
        var model = modelbuilder.model
 
        print "All entities, including fictive ones:"
-       var v = new TestModelVisitor
-       v.min_visibility = private_visibility
-       v.include_fictive = true
+       var filters = new ModelFilters(private_visibility, accept_fictive = true)
+       var v = new TestModelVisitor(filters)
        v.enter_visit(model)
        v.cpt.print_elements(10)
        var names = v.names
 
        print "All entities:"
-       v = new TestModelVisitor
-       v.min_visibility = private_visibility
+       filters = new ModelFilters(private_visibility)
+       v = new TestModelVisitor(filters)
        v.enter_visit(model)
        v.cpt.print_elements(10)
 
        print "\nAll non-private entities:"
-       v = new TestModelVisitor
-       v.min_visibility = protected_visibility
+       filters = new ModelFilters(protected_visibility)
+       v = new TestModelVisitor(filters)
        v.enter_visit(model)
        v.cpt.print_elements(10)
 
        print "\nAll documented non-private entities:"
-       v = new TestModelVisitor
-       v.min_visibility = protected_visibility
-       v.include_empty_doc = false
+       filters = new ModelFilters(protected_visibility, accept_empty_doc = false)
+       v = new TestModelVisitor(filters)
        v.enter_visit(model)
        v.cpt.print_elements(10)
 
        print "\nAll public entities:"
-       v = new TestModelVisitor
-       v.min_visibility = public_visibility
+       filters = new ModelFilters(public_visibility)
+       v = new TestModelVisitor(filters)
        v.enter_visit(model)
        v.cpt.print_elements(10)
 
        print "\nAll documented public entities:"
-       v = new TestModelVisitor
-       v.min_visibility = public_visibility
-       v.include_empty_doc = false
+       filters = new ModelFilters(public_visibility, accept_empty_doc = false)
+       v = new TestModelVisitor(filters)
        v.enter_visit(model)
        v.cpt.print_elements(10)