neo_doxygen: Manage top-level namespaces in `doxml`.
[nit.git] / contrib / neo_doxygen / src / doxml / doxyname.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 # Adds a methods to convert Doxygen’s names into short names.
16 module doxml::doxyname
17
18 import model
19
20 redef class Compound
21
22 # Separator used by Doxygen to separate name’s components.
23 protected fun doxyname_separator: String do return "::"
24
25 # Set the `name` using the specified name generated by Doxygen.
26 fun doxyname=(doxyname: String) do
27 name = doxyname.to_short_name(doxyname_separator)
28 end
29
30 # Declare an inner class.
31 #
32 # Note: Althought Doxygen indicates that both arguments are optional,
33 # declarations with an empty ID are not supported yet.
34 #
35 # Parameters:
36 #
37 # * `id`: `model_id` of the inner class.
38 # * `doxyname`: qualified name of the inner class, as generated by Doxygen.
39 # * `prot`: visibility (proctection).
40 #
41 # TODO: Handle cases where only the `doxyname` is available.
42 fun doxygen_declare_class(id: String, doxyname: String, prot: String) do
43 declare_class(id, doxyname.to_short_name(doxyname_separator), prot)
44 end
45 end
46
47 redef class Namespace
48 # Set the `name` and the `full_name` using the specified name generated by Doxygen.
49 #
50 # Warning: This method assumes that `model_id` is already set.
51 redef fun doxyname=(doxyname: String) do
52 full_name = doxyname
53 super
54 if doxyname == name and model_id != "" then
55 # Doxygen does not represent the root namespace.
56 # So, we have to link the root namespace with its children manually.
57 graph.by_id[""].as(Namespace).declare_namespace(model_id, doxyname)
58 end
59 end
60 end
61
62 redef class FileCompound
63 redef fun doxyname_separator do return "/"
64 end
65
66 redef class Text
67 # Return the substring that come after the last occurrence of `separator`.
68 #
69 # Return the whole string if `sperator` is not present.
70 private fun to_short_name(separator: String): SELFTYPE do
71 var m = search_last(separator)
72
73 if m == null then
74 return self
75 else
76 return substring_from(m.after)
77 end
78 end
79 end