neo_doxygen: Unlink `name` and `full_name`.
[nit.git] / contrib / neo_doxygen / src / model / module_compound.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 # Nodes for modules and files.
16 module model::module_compound
17
18 import graph
19 import class_compound
20 import namespace_members
21
22 # A source file.
23 #
24 # Creates one modules by inner namespace. The full name of the modules begin
25 # with the namespace’s full name, and end with the unqualified name of the file,
26 # without the extension.
27 #
28 # Note: If a module associated to the root namespace is needed, it is added to
29 # the graph only when `put_edges` is called.
30 class FileCompound
31 super Compound
32 super CodeBlock
33
34 # Modules corresponding to the namespaces defined/redefined in the file.
35 private var inner_namespaces = new Array[Module]
36
37 # `model_id` of the classes declared in the file.
38 private var inner_classes = new Array[String]
39
40 # The last component of the path, without the extension.
41 #
42 # Used as the unqualified name of the modules.
43 private var basename: String = ""
44
45 init do
46 super
47 end
48
49 redef fun location=(location: nullable Location) do
50 super
51 for m in inner_namespaces do m.location = location
52 end
53
54 redef fun name=(name: String) do
55 # Example: `MyClass.java`
56 super
57 var match = name.search_last(".")
58
59 if match == null then
60 basename = name
61 else
62 basename = name.substring(0, match.from)
63 end
64 # Update the modules’ name.
65 for m in inner_namespaces do m.update_name
66 end
67
68 redef fun declare_namespace(id: String, full_name: String) do
69 var m: Module
70
71 assert not full_name.is_empty or id.is_empty else
72 sys.stderr.write "Inner mamespace declarations without name are not yet supported (except for the root namespace).\n"
73 end
74 m = new Module(graph, self, new NamespaceRef(id, full_name))
75 m.location = self["location"].as(nullable Location)
76 inner_namespaces.add m
77 end
78
79 redef fun declare_class(id, name, prot) do
80 assert not id.is_empty else
81 sys.stderr.write "Inner class declarations without ID are not yet supported.\n"
82 end
83 inner_classes.add id
84 end
85
86 redef fun put_in_graph do
87 # Do not add `self` to the Neo4j graph...
88 # ... but add its modules...
89 for m in inner_namespaces do m.put_in_graph
90 # ... and add `self` to the indexes.
91 if model_id != "" then graph.by_id[model_id] = self
92 graph.files.add self
93 end
94
95 # If the file contains some classes in the root namespace, add an implicit
96 # module to handle them.
97 #
98 # This method is called by `ProjectGraph.add_global_modules` and assumes
99 # that all the namespaces are already fully set and put in the graph.
100 fun declare_root_namespace do
101 if has_globals then
102 declare_namespace("", "")
103 inner_namespaces.last.put_in_graph
104 end
105 end
106
107 # Does this file contain classes in the root namespace?
108 private fun has_globals: Bool do
109 var root = graph.by_id[""]
110 for c in inner_classes do
111 if graph.class_to_ns[c] == root then return true
112 end
113 return false
114 end
115 end
116
117 # A `MModule` node.
118 #
119 # For each file, there is one module by inner namespace.
120 private class Module
121 super Compound
122 super CodeBlock
123
124 # The file that declares the module.
125 var file_compound: FileCompound
126
127 # The namespace defined or redefined by the module.
128 var namespace: NamespaceRef
129
130 init do
131 super
132 self.labels.add("MModule")
133 update_name
134 end
135
136 # Update the `name`.
137 #
138 # Update the short name of the module to the `basename` of the file that
139 # declares it.
140 fun update_name do name = file_compound.basename
141
142 redef fun put_in_graph do
143 super
144 end
145
146 redef fun put_edges do
147 var ns_compound = namespace.seek_in(graph)
148 var self_class = ns_compound.self_class
149
150 graph.add_edge(ns_compound, "DECLARES", self)
151
152 for c in file_compound.inner_classes do
153 if graph.class_to_ns[c] != ns_compound then continue
154 var class_compound = graph.by_id[c].as(ClassCompound)
155 graph.add_edge(self, "INTRODUCES", class_compound)
156 graph.add_edge(self, "DEFINES", class_compound.class_def)
157 end
158
159 if self_class isa SelfClass then
160 # We assume that only one file is linked to the namespace.
161 # TODO When Doxygen will provide a way to know which file defines which member, use it.
162 self_class.location = file_compound.location
163 graph.add_edge(self, "INTRODUCES", self_class)
164 graph.add_edge(self, "DEFINES", self_class.class_def)
165 end
166 end
167 end
168
169 # Adds the `add_global_modules` phase to `ProjectGraph`.
170 redef class ProjectGraph
171
172 # Project’s source files.
173 var files: SimpleCollection[FileCompound] = new Array[FileCompound]
174
175 # Add the modules that define the root namespace.
176 #
177 # **Must** be called before any call to `put_edges`, and after all the
178 # namespaces are fully set and put in the graph.
179 #
180 # Note: This method is not idempotent so it has to be called only once.
181 fun add_global_modules do
182 for f in files do f.declare_root_namespace
183 end
184 end