Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f8d29a38ae37ba11ea2e3170b3843eaf7f5037a7 (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
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
/*******************************************************************************
 * Copyright (c) 2007, 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.autotools.ui.editors.parser;

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

import org.eclipse.cdt.autotools.ui.AutotoolsUIPlugin;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.texteditor.MarkerUtilities;

public class AutoconfOutlineErrorHandler {

	public static final String PARSE_ERROR_MARKER_ID = AutotoolsUIPlugin.PLUGIN_ID + ".outlineparsefileerror"; //$NON-NLS-1$

	private IFile file;
	private IDocument document;

	public AutoconfOutlineErrorHandler(IStorageEditorInput input, IDocument document) {
		this.document = document;
		IWorkspaceRoot root = CCorePlugin.getWorkspace().getRoot();
		try {
			IPath absPath = input.getStorage().getFullPath();
			IPath rootPath = root.getLocation();
			IPath relPath = new Path(""); //$NON-NLS-1$

			for (int i = 0; i < rootPath.segmentCount(); ++i) {
				relPath = relPath.append("../"); //$NON-NLS-1$
			}
			relPath = relPath.append(absPath);
			this.file = root.getFileForLocation(relPath);
			if (this.file == null) {
				this.file = root.getFile(relPath);
			}
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}

	public IDocument getDocument() {
		return document;
	}

	public void handleError(ParseException e) {
		if (!file.exists())
			return;

		int lineNumber = e.getLineNumber();

		Map<String, Object> map = new HashMap<>();
		MarkerUtilities.setLineNumber(map, lineNumber);
		MarkerUtilities.setMessage(map, e.getMessage());
		map.put(IMarker.MESSAGE, e.getMessage());
		map.put(IMarker.LOCATION, file.getFullPath().toString());

		Integer charStart = getCharOffset(lineNumber, e.getStartColumn());
		if (charStart != null) {
			map.put(IMarker.CHAR_START, charStart);
		}
		Integer charEnd = getCharOffset(lineNumber, e.getEndColumn());
		if (charEnd != null) {
			map.put(IMarker.CHAR_END, charEnd);
		}

		// FIXME:  add severity level
		map.put(IMarker.SEVERITY, Integer.valueOf(e.getSeverity()));

		try {
			MarkerUtilities.createMarker(file, map, PARSE_ERROR_MARKER_ID);
		} catch (CoreException ee) {
			ee.printStackTrace();
		}
		return;
	}

	public void removeAllExistingMarkers() {
		if (!file.exists())
			return;

		try {
			file.deleteMarkers(PARSE_ERROR_MARKER_ID, true, IResource.DEPTH_ZERO);
		} catch (CoreException e1) {
			e1.printStackTrace();
		}
	}

	public void removeExistingMarkers(int offset, int length) {
		if (!file.exists())
			return;

		try {
			IMarker[] markers = file.findMarkers(PARSE_ERROR_MARKER_ID, true, IResource.DEPTH_ZERO);
			// Delete all markers that start in the given document range.
			for (int i = 0; i < markers.length; ++i) {
				IMarker marker = markers[i];
				int charEnd = MarkerUtilities.getCharEnd(marker);
				int charStart = MarkerUtilities.getCharStart(marker);
				if (charStart >= offset && charEnd <= (offset + length))
					marker.delete();
			}
		} catch (CoreException e1) {
			e1.printStackTrace();
		}
	}

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

}

Back to the top