Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 352e8d4bad77a3e85285a63d7c0859808a7acb61 (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
104
105
106
107
108
109
/*******************************************************************************
 * Copyright (c) 2009, 2015 Red Hat Inc..
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Red Hat Incorporated - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.autotools.ui.editors.automake;

import org.eclipse.cdt.autotools.ui.AutotoolsUIPlugin;
import org.eclipse.cdt.make.core.makefile.IDirective;
import org.eclipse.cdt.make.core.makefile.IMakefile;
import org.eclipse.cdt.make.core.makefile.IParent;
import org.eclipse.core.runtime.AssertionFailedException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
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 AutomakeErrorHandler {
	public static final String AUTOMAKE_ERROR_MARKER_ID = AutotoolsUIPlugin.PLUGIN_ID + ".parsefileerror"; //$NON-NLS-1$

	private IDocument document;
	private AnnotationModel fAnnotationModel;

	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$

	// TODO: no quickfixes yet implemented, but maybe in the future
	private static class AutomakeAnnotation extends Annotation implements IQuickFixableAnnotation {
		public AutomakeAnnotation(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;
		}

	}

	public AutomakeErrorHandler(IEditorInput input) {
		this.document = AutomakeEditorFactory.getDefault().getAutomakefileDocumentProvider().getDocument(input);
		this.fAnnotationModel = (AnnotationModel) AutomakeEditorFactory.getDefault().getAutomakefileDocumentProvider()
				.getAnnotationModel(input);
	}

	public void update(IMakefile makefile) {
		removeExistingMarkers();

		// Recursively process all the directives in the Makefile
		checkChildren(makefile);
	}

	private void checkChildren(IParent parent) {
		IDirective[] directives = parent.getDirectives();
		for (int i = 0; i < directives.length; i++) {
			IDirective directive = directives[i];
			if (directive instanceof IParent) {
				checkChildren((IParent) directive);
			} else if (directive instanceof BadDirective) {
				int lineNumber = directive.getStartLine();
				Integer charStart = getCharOffset(lineNumber - 1, 0);
				Integer charEnd = getCharOffset(directive.getEndLine() - 1, -1);

				String annotationType = CDT_ANNOTATION_ERROR;
				Annotation annotation = new AutomakeAnnotation(annotationType, true, "Bad directive"); //$NON-NLS-1$
				Position p = new Position(charStart.intValue(), charEnd.intValue() - charStart.intValue());
				fAnnotationModel.addAnnotation(annotation, p);
			}
		}
		return;
	}

	public void removeExistingMarkers() {
		fAnnotationModel.removeAllAnnotations();
	}

	private Integer getCharOffset(int lineNumber, int columnNumber) {
		try {
			if (columnNumber >= 0)
				return Integer.valueOf(document.getLineOffset(lineNumber) + columnNumber);
			return Integer.valueOf(document.getLineOffset(lineNumber) + document.getLineLength(lineNumber));
		} catch (BadLocationException e) {
			e.printStackTrace();
			return null;
		}
	}
}

Back to the top