ci: do not error when nothing with nitunit_some
[nit.git] / contrib / neo_doxygen / src / model / linked_text.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 # A text with links.
16 module model::linked_text
17
18 import graph
19
20 # A text with links.
21 abstract class LinkedText
22 super Entity
23
24 # All link in the text.
25 #
26 # Do not edit directly.
27 var links: Sequence[nullable Link] = new Array[nullable Link]
28
29 # Remove all the parts.
30 fun clear_parts do
31 self["text"] = null
32 links.clear
33 end
34
35 # Remove the first part.
36 fun shift_part do
37 var text = self["text"]
38 assert text isa JsonArray
39 text.shift
40 links.shift
41 if text.is_empty then
42 self["text"] = null
43 end
44 end
45
46 # Remove the last part.
47 fun pop_part do
48 var text = self["text"]
49 assert text isa JsonArray
50 text.pop
51 links.pop
52 if text.is_empty then
53 self["text"] = null
54 end
55 end
56
57 # Remove the part at the specified index.
58 fun remove_part_at(index: Int) do
59 var text = self["text"]
60 assert text isa JsonArray
61 text.remove_at(index)
62 links.remove_at(index)
63 if text.is_empty then
64 self["text"] = null
65 end
66 end
67
68 # Change a part of text.
69 #
70 # Parameters:
71 #
72 # * `index` : the index of the part.
73 # * `content` : textual content.
74 # * `refid` : `model_id` of the linked entity or `""`.
75 fun set_part(index: Int, content: String, refid: String) do
76 var text = self["text"]
77 assert text isa JsonArray
78 text[index] = content
79 if not refid.is_empty then
80 links[index] = create_link(links.length, refid)
81 else
82 links[index] = null
83 end
84 end
85
86 # Append a part of text.
87 #
88 # Parameters:
89 #
90 # * `content` : textual content.
91 # * `refid` : `model_id` of the linked entity or `""`.
92 fun add_part(content: String, refid: String) do
93 var text = self["text"]
94
95 if text == null then
96 text = new JsonArray
97 self["text"] = text
98 end
99 assert text isa JsonArray
100 text.add(content)
101 if not refid.is_empty then
102 links.add(create_link(links.length, refid))
103 else
104 links.add(null)
105 end
106 end
107
108 # Create a link to the specified entity.
109 protected fun create_link(rank:Int, refid: String): Link is abstract
110
111 redef fun to_s do
112 var text = self["text"]
113
114 if text isa JsonArray then
115 return text.join
116 else
117 return "UNDEFINED"
118 end
119 end
120
121 redef fun put_in_graph do
122 super
123 for link in links do
124 if link isa Link then
125 link.put_in_graph
126 end
127 end
128 end
129
130 redef fun put_edges do
131 super
132 for i in [0..links.length[ do
133 var link = links[i]
134 if link isa Link then
135 link["rank"] = i
136 graph.add_edge(self, "LINK", link)
137 end
138 end
139 end
140 end
141
142 # A link.
143 abstract class Link
144 super Entity
145
146 # * `refid` : `model_id` of the linked entity.
147 var refid: String
148
149 init do
150 super
151 self["rank"] = -1
152 end
153
154 redef fun put_edges do
155 graph.add_edge(self, "TARGET", graph.by_id[refid])
156 end
157
158 # Specify the rank (index) of the parameter in the signature.
159 #
160 # Called by `LinkedText.put_edges`.
161 private fun rank=(rank: Int) do
162 self["rank"] = rank
163 end
164 end