1/*
2 * Copyright (C) 2008-2009 Abderrahim Kitouni
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /* A Vala.Report subclass for reporting errors in Anjuta UI */
19public class AnjutaReport : Vala.Report {
20	struct Error {
21		public Vala.SourceReference source;
22		public bool error;
23		public string message;
24	}
25	public IAnjuta.DocumentManager docman { get; set; }
26	Vala.List<Error?> errors_list = new Vala.ArrayList<Error?>();
27	bool general_error = false;
28
29	public void update_errors (IAnjuta.Editor editor) {
30		var ind = editor as IAnjuta.Indicable;
31		var mark = editor as IAnjuta.Markable;
32		if (ind == null && mark == null)
33			return;
34
35		if (ind != null)
36			ind.clear ();
37		if (mark != null)
38			mark.delete_all_markers (IAnjuta.MarkableMarker.MESSAGE);
39
40		foreach (var e in errors_list) {
41			if (e.source.file.filename.has_suffix (((IAnjuta.Document)editor).get_filename ())) {
42				if (ind != null) {
43					/* begin_iter should be one cell before to select the first character */
44					var begin_iter = editor.get_line_begin_position (e.source.begin.line);
45					for (var i = 1; i < e.source.begin.column; i++)
46						begin_iter.next ();
47					var end_iter = editor.get_line_begin_position (e.source.end.line);
48					for (var i = 0; i < e.source.end.column; i++)
49						end_iter.next ();
50					ind.set(begin_iter, end_iter, e.error ? IAnjuta.IndicableIndicator.CRITICAL :
51					                                        IAnjuta.IndicableIndicator.WARNING);
52				}
53				if (editor is IAnjuta.Markable) {
54					mark.mark(e.source.begin.line, IAnjuta.MarkableMarker.MESSAGE, e.message);
55				}
56			}
57
58		}
59	}
60	public void clear_error_indicators (Vala.SourceFile? file = null) {
61		if (file == null) {
62			errors_list = new Vala.ArrayList<Error?>();
63			errors = 0;
64		} else {
65			for (var i = 0; i < errors_list.size; i++) {
66				if (errors_list[i].source.file == file) {
67					if (errors_list[i].error)
68						errors --;
69					else
70						warnings --;
71
72					errors_list.remove_at (i);
73					i --;
74				}
75			}
76			assert (errors_list.size <= errors + warnings);
77		}
78
79		foreach (var doc in docman.get_doc_widgets ()) {
80			if (doc is IAnjuta.Indicable)
81				((IAnjuta.Indicable)doc).clear ();
82			if (doc is IAnjuta.Markable)
83				((IAnjuta.Markable)doc).delete_all_markers (IAnjuta.MarkableMarker.MESSAGE);
84		}
85	}
86	public override void warn (Vala.SourceReference? source, string message) {
87		warnings ++;
88
89		if (source == null)
90			return;
91
92		lock (errors_list) {
93			errors_list.add(Error () {source = source, message = message, error = false});
94		}
95	}
96	public override void err (Vala.SourceReference? source, string message) {
97		errors ++;
98
99		if (source == null) {
100			general_error = true;
101			return;
102		}
103
104		lock (errors_list) {
105			errors_list.add(Error () {source = source, message = message, error = true});
106		}
107	}
108}
109