File: tree.vala

package info (click to toggle)
vala 0.56.18-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 57,520 kB
  • sloc: ansic: 581,293; sh: 5,357; makefile: 3,980; xml: 3,161; yacc: 1,219; lex: 374; javascript: 23
file content (350 lines) | stat: -rw-r--r-- 8,647 bytes parent folder | download | duplicates (5)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* tree.vala
 *
 * Copyright (C) 2008-2011  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.Importer;

/**
 * The root of the code tree.
 */
public class Valadoc.Api.Tree {
	private Vala.List<InheritDocContainer> inheritdocs = new Vala.ArrayList<InheritDocContainer> ();
	private Vala.ArrayList<string> external_c_files = new Vala.ArrayList<string>(str_equal);
	private Vala.ArrayList<Package> packages = new Vala.ArrayList<Package>();
	private Package source_package = null;
	private Settings settings;
	private ErrorReporter reporter;
	private Highlighter.Highlighter _highlighter;
	private CTypeResolver _cresolver = null;
	private Package _source_package;


	private class InheritDocContainer {
		public unowned Taglets.InheritDoc taglet;
		public unowned Api.Node taglet_container;

		public InheritDocContainer (Api.Node taglet_container, Taglets.InheritDoc taglet) {
			this.taglet_container = taglet_container;
			this.taglet = taglet;
		}
	}


	public void add_package(Package package) {
		this.packages.add (package);
	}

	public Vala.CodeContext context {
		set;
		get;
	}

	public Highlighter.Highlighter highlighter {
		get {
			if (_highlighter == null) {
				_highlighter = new Highlighter.Highlighter ();
			}

			return _highlighter;
		}
	}

	/**
	 * The root of the wiki tree.
	 */
	public WikiPageTree? wikitree {
		private set;
		get;
	}

	/**
	 * Returns a list of C source files.
	 *
	 * @return list of C source files
	 */
	public Vala.Collection<string> get_external_c_files () {
		return external_c_files;
	}

	public void add_external_c_files (string name) {
		external_c_files.add (name);
	}


	/**
	 * Returns a list of all packages in the tree
	 *
	 * @return list of all packages
	 */
	public Vala.Collection<Package> get_package_list () {
		return this.packages;
	}

	private void add_dependencies_to_source_package () {
		if ( this.source_package != null ) {
			Vala.ArrayList<Package> deplst = new Vala.ArrayList<Package> ();
			foreach (Package pkg in this.packages) {
				if (pkg != this.source_package) {
					deplst.add (pkg);
				}
			}
			this.source_package.set_dependency_list (deplst);
		}
	}

	/**
	 * Visits this node with the specified Visitor.
	 *
	 * @param visitor the visitor to be called while traversing
	 */
	public void accept (Visitor visitor) {
		visitor.visit_tree (this);
	}

	/**
	 * Visits all children of this node with the given types with the specified Visitor.
	 *
	 * @param visitor the visitor to be called while traversing
	 */
	public void accept_children (Visitor visitor) {
		foreach (Node node in packages) {
			node.accept (visitor);
		}
	}

	private Node? search_relative_to (Node element, string[] path) {
		Api.Node? node = element;

		foreach (string name in path) {
			node = node.find_by_name (name);
			if (node == null) {
				break;
			}
		}

		if (node == null && element.parent != null) {
			node = search_relative_to ((Node) element.parent, path);
		}

		return node;
	}

	public Node? search_symbol_path (Node? element, string[] path) {
		Api.Node? node = null;

		// relative to element
		if (element != null) {
			node = search_relative_to (element, path);
			if (node != null) {
				return node;
			}
		}


		// absolute
		foreach (Package package in packages) {
			// search in root namespace

			Node? global = package.find_by_name ("");
			if (global != null) {
				node = search_relative_to (global, path);
				if (node != null) {
					return node;
				}
			}
		}

		return null;
	}

	public TypeSymbol? search_symbol_type_cstr (string cname) {
		if (_cresolver == null) {
			_cresolver = new CTypeResolver (this);
		}

		return _cresolver.resolve_symbol_type (cname);
	}

	public Node? search_symbol_cstr (Node? element, string cname) {
		if (_cresolver == null) {
			_cresolver = new CTypeResolver (this);
		}

		return _cresolver.resolve_symbol (element, cname);
	}

	public Node? search_symbol_str (Node? element, string symname) {
		string[] path = split_name (symname);

		var node = search_symbol_path (element, path);
		if (node != null) {
			return node;
		}

		if (path.length >= 2 && path[path.length-2] == path[path.length-2]) {
			path[path.length-2] = path[path.length-2]+"."+path[path.length-1];
			path.resize (path.length-1);
			return search_symbol_path (element, path);
		}

		return null;
	}

	private string[] split_name (string full_name) {
		string[] params = (full_name).split (".", -1);
		int i = 0; while (params[i] != null) i++;
		params.length = i;
		return params;
	}

	public Tree (ErrorReporter reporter, Settings settings, Vala.CodeContext context) {
		this.settings = settings;
		this.reporter = reporter;
		this.context = context;
	}

	// copied from valacodecontext.vala
	private string? get_file_path (string basename, string[] directories) {
		string filename = null;

		if (directories != null) {
			foreach (string dir in directories) {
				filename = Path.build_filename (dir, basename);
				if (FileUtils.test (filename, FileTest.EXISTS)) {
					return filename;
				}
			}
		}

		foreach (string dir in Environment.get_system_data_dirs ()) {
			filename = Path.build_filename (dir, basename);
			if (FileUtils.test (filename, FileTest.EXISTS)) {
				return filename;
			}
		}

		return null;
	}

	public bool create_tree ( ) {
		this.add_dependencies_to_source_package ();
		return true;
	}

	private Package? get_source_package () {
		if (_source_package == null) {
			foreach (Package pkg in packages) {
				if (!pkg.is_package) {
					_source_package = pkg;
					break;
				}
			}
		}

		return _source_package;
	}

	private void parse_wiki (DocumentationParser docparser) {
		this.wikitree = new WikiPageTree ();
		var pkg = get_source_package ();
		if (pkg != null) {
			wikitree.parse (settings, docparser, pkg, reporter);
		}
	}

	private void check_wiki (DocumentationParser docparser) {
		Package pkg = get_source_package ();
		if (pkg != null) {
			wikitree.check (settings, docparser, pkg);
		}
	}

	public void parse_comments (DocumentationParser docparser) {
		parse_wiki (docparser);

		foreach (Package pkg in this.packages) {
			if (pkg.is_browsable (settings)) {
				pkg.parse_comments (settings, docparser);
			}
		}
	}

	public void check_comments (DocumentationParser docparser) {
		check_wiki (docparser);

		foreach (Package pkg in this.packages) {
			if (pkg.is_browsable (settings)) {
				pkg.check_comments (settings, docparser);
				postprocess_inheritdoc (docparser);
			}
		}
	}

	internal void register_inheritdoc (Api.Node container, Taglets.InheritDoc taglet) {
		inheritdocs.add (new InheritDocContainer (container, taglet));
	}

	private void postprocess_inheritdoc (DocumentationParser docparser) {
		while (!this.inheritdocs.is_empty) {
			InheritDocContainer container = this.inheritdocs.remove_at (0);

			docparser.transform_inheritdoc (container.taglet_container, container.taglet);
		}
	}


	/**
	 * Import documentation from various sources
	 *
	 * @param importers a list of importers
	 * @param packages sources
	 * @param import_directories List of directories where to find the files
	 */
	public void import_comments (DocumentationImporter[] importers, string[] packages,
									  string[] import_directories)
	{
		Vala.HashSet<string> processed = new Vala.HashSet<string> ();
		foreach (string pkg_name in packages) {
			bool imported = false;
			foreach (DocumentationImporter importer in importers) {
				string? path = get_file_path ("%s.%s".printf (pkg_name, importer.file_extension),
											  import_directories);
				if (path == null) {
					continue;
				}

				path = Vala.CodeContext.realpath (path);
				imported = true;

				if (!processed.contains (path)) {
					importer.process (path);
					processed.add (path);
				}
			}

			if (imported == false) {
				reporter.simple_error (null, "'%s' not found in specified import directories", pkg_name);
			}
		}
	}
}