Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3ae0660a9f4270a346290896e0645fdb99324f46 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*******************************************************************************
 * Copyright (c) 2000, 2015 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *     David Perryman (IPL Information Processing Limited)
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.text.correction;

import java.util.Comparator;
import java.util.Iterator;
import java.util.ResourceBundle;
import java.util.TreeMap;

import org.eclipse.cdt.internal.ui.editor.OverrideIndicatorManager;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextOperationTarget;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationAccessExtension;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRulerInfo;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
import org.eclipse.ui.texteditor.AnnotationPreference;
import org.eclipse.ui.texteditor.AnnotationPreferenceLookup;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.ITextEditorExtension;
import org.eclipse.ui.texteditor.SelectMarkerRulerAction;

/**
 * Action which gets triggered when selecting (annotations) in the vertical ruler.
 * based upon org.eclipse.jdt.internal.ui.javaeditor.JavaSelectMarkerRulerAction
 */
public class CSelectAnnotationRulerAction extends SelectMarkerRulerAction {

	private ITextEditor fTextEditor;
	private Position fPosition;
	private AnnotationPreferenceLookup fAnnotationPreferenceLookup;
	private IPreferenceStore fStore;
	private ResourceBundle fBundle;
	// Annotations at the ruler's current line of activity, keyed by their presentation layer,
	// in decreasing order (i.e. top to bottom).
	private static Comparator<Integer> decreasingOrder = new Comparator<Integer>() {
		@Override
		public int compare(Integer a, Integer b) {
			return b - a;
		}
	};
	private TreeMap<Integer, Annotation> fAnnotations = new TreeMap<>(decreasingOrder);
	// For each layer, whether the annotation at that layer has a correction.
	private TreeMap<Integer, Boolean> fHasCorrection = new TreeMap<>(decreasingOrder);

	public CSelectAnnotationRulerAction(ResourceBundle bundle, String prefix, ITextEditor editor,
			IVerticalRulerInfo ruler) {
		super(bundle, prefix, editor, ruler);
		fBundle = bundle;
		fTextEditor = editor;

		fAnnotationPreferenceLookup = EditorsUI.getAnnotationPreferenceLookup();
		fStore = CUIPlugin.getDefault().getCombinedPreferenceStore();
	}

	@Override
	public void run() {
		// is there an equivalent preference for the C Editor?
		// if (fStore.getBoolean(PreferenceConstants.EDITOR_ANNOTATION_ROLL_OVER))
		//     return;

		runWithEvent(null);
	}

	/*
	 * @see org.eclipse.jface.action.IAction#runWithEvent(org.eclipse.swt.widgets.Event)
	 */
	@Override
	public void runWithEvent(Event event) {
		// Give each annotation at the current line, from top to bottom, a chance to handle
		// the action. If there are no takers, fall back to the super class implementation.
		for (Integer layer : fAnnotations.keySet()) {
			Annotation annotation = fAnnotations.get(layer);
			if (annotation instanceof OverrideIndicatorManager.OverrideIndicator) {
				((OverrideIndicatorManager.OverrideIndicator) annotation).open();
				return;
			}

			if (fHasCorrection.get(layer)) {
				ITextOperationTarget operation = fTextEditor.getAdapter(ITextOperationTarget.class);
				final int opCode = ISourceViewer.QUICK_ASSIST;
				if (operation != null && operation.canDoOperation(opCode)) {
					fTextEditor.selectAndReveal(fPosition.getOffset(), fPosition.getLength());
					operation.doOperation(opCode);
				}
				return;
			}
		}

		super.run();
	}

	@Override
	public void update() {
		findCAnnotation();
		setEnabled(true);

		for (Integer layer : fAnnotations.keySet()) {
			Annotation annotation = fAnnotations.get(layer);
			if (annotation instanceof OverrideIndicatorManager.OverrideIndicator) {
				initialize(fBundle, "CSelectAnnotationRulerAction.OpenSuperImplementation."); //$NON-NLS-1$
				return;
			}
			if (fHasCorrection.get(layer)) {
				initialize(fBundle, "CSelectAnnotationRulerAction.QuickFix."); //$NON-NLS-1$
				return;
			}
		}

		initialize(fBundle, "CSelectAnnotationRulerAction.GotoAnnotation."); //$NON-NLS-1$;
		super.update();
	}

	private void findCAnnotation() {
		fPosition = null;
		fAnnotations.clear();
		fHasCorrection.clear();

		AbstractMarkerAnnotationModel model = getAnnotationModel();
		IAnnotationAccessExtension annotationAccess = getAnnotationAccessExtension();

		IDocument document = getDocument();
		if (model == null)
			return;

		Iterator<?> iter = model.getAnnotationIterator();

		while (iter.hasNext()) {
			Annotation annotation = (Annotation) iter.next();
			if (annotation.isMarkedDeleted())
				continue;

			int layer = IAnnotationAccessExtension.DEFAULT_LAYER;
			if (annotationAccess != null) {
				layer = annotationAccess.getLayer(annotation);
			}

			Position position = model.getPosition(annotation);
			if (!includesRulerLine(position, document))
				continue;

			boolean isReadOnly = fTextEditor instanceof ITextEditorExtension
					&& ((ITextEditorExtension) fTextEditor).isEditorInputReadOnly();

			if (!isReadOnly && CCorrectionProcessor.hasCorrections(annotation)) {

				fPosition = position;
				fAnnotations.put(layer, annotation);
				fHasCorrection.put(layer, true);
				continue;
			}
			AnnotationPreference preference = fAnnotationPreferenceLookup.getAnnotationPreference(annotation);
			if (preference == null)
				continue;

			String key = preference.getVerticalRulerPreferenceKey();
			if (key == null)
				continue;

			if (fStore.getBoolean(key)) {
				fPosition = position;
				fAnnotations.put(layer, annotation);
				fHasCorrection.put(layer, false);
			}
		}
	}
}

Back to the top