Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cdc416cfcbaa2d82ad55a31cca67f76e0008eb84 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2007, 2009 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat Incorporated - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.autotools.ui.editors;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.cdt.autotools.ui.editors.parser.IAutoconfErrorHandler;
import org.eclipse.cdt.autotools.ui.editors.parser.ParseException;
import org.eclipse.core.runtime.AssertionFailedException;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.quickassist.IQuickFixableAnnotation;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.AnnotationModel;
import org.eclipse.ui.IEditorInput;


public class AutoconfErrorHandler implements IAutoconfErrorHandler {
	
	public static final String CDT_ANNOTATION_INFO = "org.eclipse.cdt.ui.info"; //$NON-NLS-1$
	public static final String CDT_ANNOTATION_WARNING = "org.eclipse.cdt.ui.warning"; //$NON-NLS-1$
	public static final String CDT_ANNOTATION_ERROR = "org.eclipse.cdt.ui.error"; //$NON-NLS-1$
	
	private int CDT_WARNING = 1;
	private int CDT_ERROR = 2;
	
	private Map<Position, Annotation> annotations = new HashMap<>();
	private AnnotationModel fAnnotationModel;
	
	public AutoconfErrorHandler(IEditorInput input) {
		this.fAnnotationModel = (AnnotationModel)AutoconfEditor.getAutoconfDocumentProvider().getAnnotationModel(input);
	}
	
	// TODO: no quickfixes yet implemented, but maybe in the future
	private static class AutoconfAnnotation extends Annotation implements IQuickFixableAnnotation {
		public AutoconfAnnotation(String annotationType, boolean persist, String message) {
			super(annotationType, persist, message);
		}

		@Override
		public void setQuickFixable(boolean state) {
			// do nothing
		}

		@Override
		public boolean isQuickFixableStateSet() {
			return true;
		}

		@Override
		public boolean isQuickFixable() throws AssertionFailedException {
			return false;
		}

	}
	
	@Override
	public void handleError(ParseException e) {
		Integer charStart = Integer.valueOf(e.getStartOffset());
		Integer charEnd = Integer.valueOf(e.getEndOffset());
		
		String annotationType = CDT_ANNOTATION_INFO;
		if (e.getSeverity() == CDT_ERROR)
			annotationType = CDT_ANNOTATION_ERROR;
		else if (e.getSeverity() == CDT_WARNING)
			annotationType = CDT_ANNOTATION_WARNING;
		Annotation annotation = new AutoconfAnnotation(annotationType, true, e.getLocalizedMessage());
		Position p = new Position(charStart.intValue(),charEnd.intValue() - charStart.intValue());
		fAnnotationModel.addAnnotation(annotation, p);
		annotations.put(p, annotation);
	}
	
	public void removeAllExistingMarkers()
	{
		fAnnotationModel.removeAllAnnotations();
		annotations.clear();
	}

	public void removeExistingMarkers(int offset, int length)
	{	
		@SuppressWarnings("rawtypes")
		Iterator i = fAnnotationModel.getAnnotationIterator();
		while (i.hasNext()) {
			Annotation annotation = (Annotation)i.next();
			Position p = fAnnotationModel.getPosition(annotation);
			int pStart = p.getOffset();
			if (pStart >= offset && pStart < (offset + length)) {
				// Remove directly from model instead of using
				// iterator so position will be removed from document.
				fAnnotationModel.removeAnnotation(annotation);
			}
		}
	}
}

Back to the top