Merge: names and concerns in model
authorJean Privat <jean@pryen.org>
Thu, 26 Jun 2014 01:10:11 +0000 (21:10 -0400)
committerJean Privat <jean@pryen.org>
Thu, 26 Jun 2014 01:10:11 +0000 (21:10 -0400)
First commit adapt the MEntity implementation to it's documentation by moving up the `name` property.

Second commit introduce a new kind of MEntity: MConcern that will be used in documentation tools as a common interface for MProject, MGroup and MModule.

Pull-Request: #522
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Jean Privat <jean@pryen.org>

src/model/mmodule.nit
src/model/model.nit
src/model/model_base.nit
src/model/mproject.nit

index 60a7c92..18e835d 100644 (file)
@@ -69,7 +69,7 @@ end
 
 # A Nit module is usually associated with a Nit source file.
 class MModule
-       super MEntity
+       super MConcern
 
        # The model considered
        var model: Model
@@ -84,7 +84,7 @@ class MModule
        var mgroup: nullable MGroup
 
        # The short name of the module
-       var name: String
+       redef var name: String
 
        # The origin of the definition
        var location: Location
@@ -221,4 +221,6 @@ class MModule
                        abort
                end
        end
+
+       redef fun parent_concern do return mgroup
 end
index 6732a16..7481338 100644 (file)
@@ -33,6 +33,7 @@ import poset
 import location
 import mmodule
 import mdoc
+import ordered_tree
 private import more_collections
 
 redef class Model
@@ -106,6 +107,35 @@ redef class Model
 
        # The only null type
        var null_type: MNullType = new MNullType(self)
+
+       # Build an ordered tree with from `concerns`
+       fun concerns_tree(mconcerns: Collection[MConcern]): ConcernsTree do
+               var seen = new HashSet[MConcern]
+               var res = new ConcernsTree
+
+               var todo = new Array[MConcern]
+               todo.add_all mconcerns
+
+               while not todo.is_empty do
+                       var c = todo.pop
+                       if seen.has(c) then continue
+                       var pc = c.parent_concern
+                       if pc == null then
+                               res.add(null, c)
+                       else
+                               res.add(pc, c)
+                               todo.add(pc)
+                       end
+                       seen.add(c)
+               end
+
+               return res
+       end
+end
+
+# An OrderedTree that can be easily refined for display purposes
+class ConcernsTree
+       super OrderedTree[MConcern]
 end
 
 redef class MModule
@@ -302,7 +332,7 @@ class MClass
 
        # The short name of the class
        # In Nit, the name of a class cannot evolve in refinements
-       var name: String
+       redef var name: String
 
        # The canonical name of the class
        # Example: `"owner::module::MyClass"`
@@ -463,6 +493,9 @@ class MClassDef
                self.to_s = "{mmodule}#{mclass}"
        end
 
+       # Actually the name of the `mclass`
+       redef fun name do return mclass.name
+
        # All declared super-types
        # FIXME: quite ugly but not better idea yet
        var supertypes: Array[MClassType] = new Array[MClassType]
@@ -1514,7 +1547,7 @@ abstract class MProperty
        var intro_mclassdef: MClassDef
 
        # The (short) name of the property
-       var name: String
+       redef var name: String
 
        # The canonical name of the property
        # Example: "owner::my_module::MyClass::my_method"
@@ -1799,6 +1832,9 @@ abstract class MPropDef
                self.to_s = "{mclassdef}#{mproperty}"
        end
 
+       # Actually the name of the `mproperty`
+       redef fun name do return mproperty.name
+
        # Internal name combining the module, the class and the property
        # Example: "mymodule#MyClass#mymethod"
        redef var to_s: String
index 39df50b..666ce8e 100644 (file)
@@ -25,6 +25,15 @@ end
 # A named and possibly documented entity in the model.
 # This class is usefull to generalize presentation of entities to the human.
 abstract class MEntity
+       # The short (unqualified) name of this model entity
+       fun name: String is abstract
+end
+
+# Something that represents a concern
+abstract class MConcern
+       super MEntity
+       # The concern that contains `self` or null if `self` is the root of the concern hierarchy
+       fun parent_concern: nullable MConcern is abstract
 end
 
 # A visibility (for modules, class and properties)
index cad2e39..7bd502e 100644 (file)
@@ -21,10 +21,10 @@ import poset
 
 # A Nit project, thas encompass a product
 class MProject
-       super MEntity
+       super MConcern
 
        # The name of the project
-       var name: String
+       redef var name: String
 
        # The model of the project
        var model: Model
@@ -44,15 +44,18 @@ class MProject
                model.mprojects.add(self)
                model.mproject_by_name.add_one(name, self)
        end
+
+       # MProject are always roots of the concerns hierarchy
+       redef fun parent_concern do return null
 end
 
 # A group of modules in a project
 class MGroup
-       super MEntity
+       super MConcern
 
        # The name of the group
        # empty name for a default group in a single-module project
-       var name: String
+       redef var name: String
 
        # The englobing project
        var mproject: MProject
@@ -74,6 +77,9 @@ class MGroup
        # nesting group (see `parent`) is bigger
        var in_nesting: POSetElement[MGroup]
 
+       # Is `self` the root of its project?
+       fun is_root: Bool do return mproject.root == self
+
        # The filepath (usualy a directory) of the group, if any
        var filepath: nullable String writable
 
@@ -89,6 +95,11 @@ class MGroup
                end
        end
 
+       redef fun parent_concern do
+               if not is_root then return parent
+               return mproject
+       end
+
        redef fun to_s do return name
 end