7864b279dcf508ff5f62c19a293c1e2957a020eb
[nit.git] / src / doc / doc_phases / doc_lin.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 # Add linearization lists to DefinitionArticle found in MClass pages.
16 module doc_lin
17
18 import doc_structure
19
20 # LinPhase populates the DocPage content with linearization data.
21 class LinListPhase
22 super DocPhase
23
24 # Used to sort list by linearization order
25 private var lin_sorter = new MEntityNameSorter
26
27 redef fun apply do
28 for page in doc.pages.values do page.apply_linearization(self, doc)
29 end
30 end
31
32 redef class DocPage
33
34 # Populates `self` with linearization data.
35 #
36 # See `LinListPhase`.
37 fun apply_linearization(v: LinListPhase, doc: DocModel) do end
38 end
39
40 redef class MClassPage
41 redef fun apply_linearization(v, doc) do
42 root.apply_linearization(v, doc, self)
43 end
44 end
45
46 redef class DocComposite
47
48 # Populates `self` with linearization data.
49 #
50 # For now, it's only used for mpropdefs linearization in MClassPage.
51 #
52 # See `LinListPhase`.
53 private fun apply_linearization(v: LinListPhase, doc: DocModel, page: DocPage) do
54 for child in children do child.apply_linearization(v, doc, page)
55 end
56 end
57
58 redef class DefinitionArticle
59 redef fun apply_linearization(v, doc, page) do
60 var mentity = self.mentity
61 if not mentity isa MPropDef then return
62 # Add linearization
63 var all_defs = new HashSet[MPropDef]
64 for local_def in local_defs(page.as(MClassPage), mentity.mproperty) do
65 all_defs.add local_def
66 var smpropdef = local_def
67 while not smpropdef.is_intro do
68 smpropdef = smpropdef.lookup_next_definition(
69 doc.mainmodule, smpropdef.mclassdef.bound_mtype)
70 all_defs.add smpropdef
71 end
72 end
73 var lin = all_defs.to_a
74 doc.mainmodule.linearize_mpropdefs(lin)
75 if lin.length > 1 then
76 add_child new DefinitionLinArticle("{mentity.nitdoc_id}.lin", "Linearization", lin)
77 end
78 end
79
80 # Filter `page.mpropdefs` for this `mpropertie`.
81 #
82 # FIXME compatability with current templates.
83 private fun local_defs(page: MClassPage, mproperty: MProperty): HashSet[MPropDef] do
84 var mpropdefs = new HashSet[MPropDef]
85 for mpropdef in page.mpropdefs do
86 if mpropdef.mproperty == mproperty then
87 mpropdefs.add mpropdef
88 end
89 end
90 return mpropdefs
91 end
92 end
93
94 # Display a linearized list of definitions.
95 class DefinitionLinArticle
96 super DocArticle
97
98 # The linearized list to display.
99 var mentities: Array[MEntity]
100
101 redef fun is_hidden do return mentities.is_empty
102 redef var is_toc_hidden = true
103 end