Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 867d548e02371d85b52bad9f1b1c2b73541b0f41 (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
/*******************************************************************************
 * Copyright (c) 2017 Red Hat Inc. and others.
 *
 * 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:
 *   Mickael Istria (Red Hat Inc.) - initial implementation
 *******************************************************************************/
package org.eclipse.ui.internal.genericeditor.markers;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.ITextHoverExtension;
import org.eclipse.jface.text.ITextHoverExtension2;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.ISourceViewerExtension2;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.texteditor.AnnotationPreference;
import org.eclipse.ui.texteditor.MarkerAnnotation;

public class MarkerAnnotationHover implements ITextHoverExtension, ITextHoverExtension2, ITextHover {

	protected static boolean isIncluded(Annotation annotation) {
		if (!(annotation instanceof MarkerAnnotation)) {
			return false;
		}
		AnnotationPreference preference= EditorsUI.getAnnotationPreferenceLookup().getAnnotationPreference(annotation);
		if (preference == null) {
			return false;
		}
		String key= preference.getTextPreferenceKey();
		if (key != null) {
			if (!EditorsUI.getPreferenceStore().getBoolean(key))
				return false;
		} else {
			key= preference.getHighlightPreferenceKey();
			if (key == null || !EditorsUI.getPreferenceStore().getBoolean(key))
				return false;
		}
		return true;
	}


	@Override
	public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
		Object hoverInfo = getHoverInfo2(textViewer, hoverRegion);
		if (hoverInfo == null) {
			return null;
		}
		return hoverInfo.toString();
	}

	@Override
	public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
		if (!(textViewer instanceof ISourceViewerExtension2)) {
			return null;
		}
		ISourceViewerExtension2 viewer = (ISourceViewerExtension2)textViewer;
		List<MarkerAnnotation> annotations = findMarkerAnnotations(viewer, new Region(offset, 0));
		if (annotations.isEmpty()) {
			return null;
		}
		// find intersection of regions
		int highestOffsetStart = 0;
		int lowestOffsetEnd = Integer.MAX_VALUE;
		IAnnotationModel annotationModel = viewer.getVisualAnnotationModel();
		for (Annotation annotation : annotations) {
			Position position = annotationModel.getPosition(annotation);
			highestOffsetStart = Math.max(highestOffsetStart, position.getOffset());
			lowestOffsetEnd = Math.min(lowestOffsetEnd, position.getOffset() + position.getLength());
		}
		return new Region(highestOffsetStart, Math.max(0, lowestOffsetEnd - highestOffsetStart));
	}

	@Override
	public List<IMarker> getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
		if (!(textViewer instanceof ISourceViewerExtension2)) {
			return null;
		}
		List<MarkerAnnotation> annotations = findMarkerAnnotations((ISourceViewerExtension2)textViewer, hoverRegion);
		if (annotations.isEmpty()) {
			return null;
		}
		List<IMarker> markers = new ArrayList<>(annotations.size());
		for (MarkerAnnotation annotation : annotations) {
			markers.add(annotation.getMarker());
		}
		return markers;
	}
	
	private static List<MarkerAnnotation> findMarkerAnnotations(ISourceViewerExtension2 viewer, IRegion region) {
		List<MarkerAnnotation> res = new ArrayList<>();
		IAnnotationModel annotationModel = viewer.getVisualAnnotationModel();
		annotationModel.getAnnotationIterator().forEachRemaining(annotation -> {
			if (isIncluded(annotation)) {
				Position position = annotationModel.getPosition(annotation);
				if (region.getOffset() >= position.getOffset() && region.getOffset() + region.getLength() <= position.getOffset() + position.getLength()) {
					res.add((MarkerAnnotation)annotation);
				}
			}
		});
		return res;
	}

	@Override
	public IInformationControlCreator getHoverControlCreator() {
		return new MarkerHoverControlCreator();
	}

}

Back to the top