summaryrefslogtreecommitdiff
path: root/libvaladoc/documentation/importerhelper.vala
blob: 98100d308e51b8ce82fac3f2313a509c6c1cabd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* importhelper.vala
 *
 * Copyright (C) 2014 Florian Brosch
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 *  Florian Brosch <flo.brosch@gmail.com>
 */


using Valadoc.Content;

namespace Valadoc.ImporterHelper {

	//
	// resolve-parameter-ctype:
	//

	internal string? resolve_parameter_ctype (Api.Tree tree, Api.Node element, string parameter_name,
		out string? param_name, out string? param_array_name, out bool is_return_type_len)
	{
		string[]? parts = split_type_name (parameter_name);
		is_return_type_len = false;
		param_array_name = null;

		Api.Parameter? param = null; // type parameter or formal parameter
		foreach (Api.Node node in element.get_children_by_type (Api.NodeType.FORMAL_PARAMETER, false)) {
			if (node.name == parts[0]) {
				param = node as Api.Parameter;
				break;
			}

			if (((Api.Parameter) node).implicit_array_length_cparameter_name == parts[0]) {
				param_array_name = ((Api.Parameter) node).name;
				break;
			}
		}

		if (element is Api.Callable
			&& ((Api.Callable) element).implicit_array_length_cparameter_name == parts[0])
		{
			is_return_type_len = true;
		}

		if (parts.length == 1) {
			param_name = parameter_name;
			return null;
		}


		Api.Item? inner = null;

		if (param_array_name != null || is_return_type_len) {
			inner = tree.search_symbol_str (null, "int");
		} else if (param != null) {
			inner = param.parameter_type;
		}

		while (inner != null) {
			if (inner is Api.TypeReference) {
				inner = ((Api.TypeReference) inner).data_type;
			} else if (inner is Api.Pointer) {
				inner = ((Api.Pointer) inner).data_type;
			} else if (inner is Api.Array) {
				inner = ((Api.Array) inner).data_type;
			} else {
				break ;
			}
		}


		if (inner == null) {
			param_name = parameter_name;
			return null;
		}

		string? cname = null;
		if (inner is Api.ErrorDomain) {
			cname = ((Api.ErrorDomain) inner).get_cname ();
		} else if (inner is Api.Struct) {
			cname = ((Api.Struct) inner).get_cname ();
		} else if (inner is Api.Class) {
			cname = ((Api.Class) inner).get_cname ();
		} else if (inner is Api.Enum) {
			cname = ((Api.Enum) inner).get_cname ();
		} else {
			assert_not_reached ();
		}

		param_name = (owned) parts[0];
		return "c::" + cname + parts[1] + parts[2];
	}


	private string[]? split_type_name (string id) {
		unichar c;

		for (unowned string pos = id; (c = pos.get_char ()) != '\0'; pos = pos.next_char ()) {
			switch (c) {
			case '-': // ->
				return {id.substring (0, (long) (((char*) pos) - ((char*) id))), "->", (string) (((char*) pos) + 2)};

			case ':': // : or ::
				string sep = (pos.next_char ().get_char () == ':')? "::" : ":";
				return {id.substring (0, (long) (((char*) pos) - ((char*) id))), sep, (string) (((char*) pos) + sep.length)};

			case '.':
				return {id.substring (0, (long) (((char*) pos) - ((char*) id))), ".", (string) (((char*) pos) + 1)};
			}
		}

		return {id};
	}



	//
	// extract-short-desc:
	//

	internal void extract_short_desc (Comment comment, ContentFactory factory) {
		if (comment.content.size == 0) {
			return ;
		}

		Paragraph? first_paragraph = comment.content[0] as Paragraph;
		if (first_paragraph == null) {
			// add empty paragraph to avoid non-text as short descriptions
			comment.content.insert (1, factory.create_paragraph ());
			return ;
		}


		// avoid fancy stuff in short descriptions:
		first_paragraph.horizontal_align = HorizontalAlign.NONE;
		first_paragraph.vertical_align = VerticalAlign.NONE;
		first_paragraph.style = null;


		Paragraph? second_paragraph = split_paragraph (first_paragraph, factory);
		if (second_paragraph == null) {
			return ;
		}

		if (second_paragraph.is_empty () == false) {
			comment.content.insert (1, second_paragraph);
		}
	}

	private inline Text? split_text (Text text, ContentFactory factory) {
		int offset = 0;
		while ((offset = text.content.index_of_char ('.', offset)) >= 0) {
			if (offset >= 2) {
				// ignore "e.g."
				unowned string cmp4 = ((string) (((char*) text.content) + offset - 2));
				if (cmp4.has_prefix (" e.g.") || cmp4.has_prefix ("(e.g.")) {
					offset = offset + 3;
					continue;
				}

				// ignore "i.e."
				if (cmp4.has_prefix (" i.e.") || cmp4.has_prefix ("(i.e.")) {
					offset = offset + 3;
					continue;
				}
			}

			unowned string cmp0 = ((string) (((char*) text.content) + offset));

			// ignore ... (varargs)
			if (cmp0.has_prefix ("...")) {
				offset = offset + 3;
				continue;
			}

			// ignore numbers
			if (cmp0.get (1).isdigit ()) {
				offset = offset + 2;
				continue;
			}


			Text sec = factory.create_text (text.content.substring (offset+1, -1));
			text.content = text.content.substring (0, offset+1);
			return sec;
		}

		return null;
	}

	private inline Run? split_run (Run run, ContentFactory factory) {
		if (run.style != Run.Style.NONE) {
			return null;
		}

		Run? sec = null;


		Vala.Iterator<Inline> iter = run.content.iterator ();
		for (bool has_next = iter.next (); has_next; has_next = iter.next ()) {
			Inline item = iter.get ();
			if (sec == null) {
				Inline? tmp = split_inline (item, factory);
				if (tmp != null) {
					sec = factory.create_run (run.style);
					sec.content.add (tmp);
				}
			} else {
				sec.content.add (item);
				iter.remove ();
			}
		}

		return sec;
	}

	private inline Inline? split_inline (Inline item, ContentFactory factory) {
		if (item is Text) {
			return split_text ((Text) item, factory);
		} else if (item is Run) {
			return split_run ((Run) item, factory);
		}

		return null;
	}

	private inline Paragraph? split_paragraph (Paragraph p, ContentFactory factory) {
		Paragraph? sec = null;

		Vala.Iterator<Inline> iter = p.content.iterator ();
		for (bool has_next = iter.next (); has_next; has_next = iter.next ()) {
			Inline item = iter.get ();
			if (sec == null) {
				Inline? tmp = split_inline (item, factory);
				if (tmp != null) {
					sec = factory.create_paragraph ();
					sec.horizontal_align = p.horizontal_align;
					sec.vertical_align = p.vertical_align;
					sec.style = p.style;
					sec.content.add (tmp);
				}
			} else {
				sec.content.add (item);
				iter.remove ();
			}
		}

		return sec;
	}

}