tests: fix tests with repeated types warnings
[nit.git] / src / doc / doc_phases / doc_poset.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 # Importation and inheritance POSet for pages.
16 module doc_poset
17
18 import doc_pages
19
20 # This phase computes importation and inheritance POSet for pages.
21 class POSetPhase
22 super DocPhase
23
24 # Populates the given DocModel.
25 redef fun apply do
26 for page in doc.pages do
27 if page isa MEntityPage then page.build_poset(self, doc)
28 end
29 end
30 end
31
32 redef class MEntityPage
33
34 # The poset associated with this page.
35 #
36 # FIXME should be defined in subclasses
37 # as the POSet can contains other types than `SELF`
38 var poset = new POSet[MENTITY]
39
40 # Build the POSet for this page.
41 private fun build_poset(v: POSetPhase, doc: DocModel) do end
42 end
43
44 redef class MModulePage
45
46 # Imported modules that should appear in the documentation.
47 var imports = new HashSet[MModule]
48
49 # Clients modules that shjould appear in the documentation.
50 var clients = new HashSet[MModule]
51
52 redef fun build_poset(v, doc) do
53 # collect importation
54 for dep in mentity.in_importation.greaters do
55 if dep == mentity then continue
56 if not doc.mmodules.has(dep) then continue
57 imports.add dep
58 end
59 # FIXME avoid diff
60 #if imports.length > 10 then
61 if mentity.in_importation.greaters.length > 10 then
62 imports.clear
63 for dep in mentity.in_importation.direct_greaters do
64 if dep == mentity then continue
65 if not doc.mmodules.has(dep) then continue
66 imports.add dep
67 end
68 end
69 # collect clients
70 for dep in mentity.in_importation.smallers do
71 if dep == mentity then continue
72 if not doc.mmodules.has(dep) then continue
73 clients.add dep
74 end
75 if clients.length > 10 then
76 clients.clear
77 for dep in mentity.in_importation.direct_smallers do
78 if dep == mentity then continue
79 if not doc.mmodules.has(dep) then continue
80 clients.add dep
81 end
82 end
83 # make poset
84 var mmodules = new HashSet[MModule]
85 mmodules.add_all mentity.nested_mmodules
86 mmodules.add_all imports
87 if clients.length < 10 then mmodules.add_all clients
88 mmodules.add mentity
89 build_importation_poset(doc, mmodules)
90 end
91
92 # Build the POSet of importation from a list of `mmodules`.
93 private fun build_importation_poset(doc: DocModel, mmodules: Set[MModule]): POSet[MModule] do
94 for mmodule in mmodules do
95 if not doc.mmodules.has(mmodule) then continue
96 poset.add_node mmodule
97 for omodule in mmodules do
98 if not doc.mmodules.has(omodule) then continue
99 poset.add_node mmodule
100 if mmodule.in_importation < omodule then
101 poset.add_edge(mmodule, omodule)
102 end
103 end
104 end
105 return poset
106 end
107 end
108
109 redef class MClassPage
110
111 # Direct parents classes to document.
112 var parents = new HashSet[MClass]
113
114 # Transitive ancestors classes to document.
115 #
116 # Does not contain the direct ancestors.
117 # See `parents` for that.
118 var ancestors = new HashSet[MClass]
119
120 # Direct children classes to document.
121 var children = new HashSet[MClass]
122
123 # All descendants classes to document.
124 #
125 # Does not contain the direct children.
126 # See `children` for that.
127 var descendants = new HashSet[MClass]
128
129 redef fun build_poset(v, doc) do
130 poset.add_node mentity
131
132 var h = mentity.in_hierarchy(doc.mainmodule)
133 # parents
134 for mclass in h.direct_greaters do
135 if doc.mclasses.has(mclass) then parents.add mclass
136 end
137 # ancestors
138 for mclass in h.greaters do
139 if mclass == mentity then continue
140 if not doc.mclasses.has(mclass) then continue
141 if parents.has(mclass) then continue
142 ancestors.add mclass
143 end
144 # children
145 for mclass in h.direct_smallers do
146 if doc.mclasses.has(mclass) then children.add mclass
147 end
148 # descendants
149 for mclass in h.smallers do
150 if mclass == mentity then continue
151 if not doc.mclasses.has(mclass) then continue
152 if children.has(mclass) then continue
153 descendants.add mclass
154 end
155 # poset
156 var mclasses = new HashSet[MClass]
157 mclasses.add_all ancestors
158 mclasses.add_all parents
159 mclasses.add_all children
160 mclasses.add_all descendants
161 mclasses.add mentity
162 build_inheritance_poset(v, doc, mclasses)
163 end
164
165 private fun build_inheritance_poset(v: POSetPhase, doc: DocModel, mclasses: Set[MClass]): POSet[MClass] do
166 for mclass in mclasses do
167 poset.add_node mclass
168 for oclass in mclasses do
169 if mclass == oclass then continue
170 poset.add_node oclass
171 if mclass.in_hierarchy(doc.mainmodule) < oclass then
172 poset.add_edge(mclass, oclass)
173 end
174 end
175 end
176 return poset
177 end
178 end