Skip to main content
summaryrefslogtreecommitdiffstats
blob: c526d80b5d447d708bdd68a7bca8f3f7a2c1437c (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*******************************************************************************
 * Copyright (c) 2000, 2015 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.texteditor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import org.osgi.framework.Bundle;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;

import org.eclipse.jface.dialogs.ErrorDialog;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationAccess;
import org.eclipse.jface.text.source.IAnnotationAccessExtension;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.IVerticalRulerInfo;

import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IGotoMarker;
import org.eclipse.ui.views.markers.MarkerViewUtil;


/**
 * A ruler action which can select the textual range of a marker that has a visual representation in
 * a vertical ruler.
 * <p>
 * This class may be instantiated but is not intended for sub-classing.
 * </p>
 * 
 * @since 2.0, allowed to be subclassed since 3.5
 */
public class SelectMarkerRulerAction extends ResourceAction implements IUpdate {

	/** The vertical ruler info of the action's editor. */
	private IVerticalRulerInfo fRuler;
	/** The associated editor. */
	private ITextEditor fTextEditor;
	/** The action's resource bundle. */
	private ResourceBundle fBundle;
	/** The prefix for resource bundle lookups. */
	private String fPrefix;

	/**
	 * Creates a new action for the given ruler and editor. The action configures
	 * its visual representation from the given resource bundle.
	 *
	 * @param bundle the resource bundle
	 * @param prefix a prefix to be prepended to the various resource keys
	 *   (described in <code>ResourceAction</code> constructor), or  <code>null</code> if none
	 * @param editor the editor
	 * @param ruler the ruler
	 *
	 * @see ResourceAction#ResourceAction(ResourceBundle, String)
	 */
	public SelectMarkerRulerAction(ResourceBundle bundle, String prefix, ITextEditor editor, IVerticalRulerInfo ruler) {
		super(bundle, prefix);
		fRuler= ruler;
		fTextEditor= editor;

		fBundle= bundle;
		fPrefix= prefix;
	}

	/**
	 * Creates a new action for the given ruler and editor. The action configures
	 * its visual representation from the given resource bundle.
	 *
	 * @param bundle the resource bundle
	 * @param prefix a prefix to be prepended to the various resource keys
	 * @param ruler the ruler
	 * @param editor the editor
	 * @deprecated As of 3.0, replaced by {@link #SelectMarkerRulerAction(ResourceBundle, String, ITextEditor, IVerticalRulerInfo)}
	 */
	@Deprecated
	public SelectMarkerRulerAction(ResourceBundle bundle, String prefix, IVerticalRuler ruler, ITextEditor editor) {
		this(bundle, prefix, editor, ruler);
	}

	@Override
	public void update() {
		setEnabled(hasMarkers());
	}

	@Override
	public void run() {

		IMarker marker= chooseMarker(getMarkers());
		if (marker == null)
			return;

		IWorkbenchPage page= fTextEditor.getSite().getPage();
		MarkerViewUtil.showMarker(page, marker, false);

		gotoMarker(marker);
	}

	private void gotoMarker(IMarker marker) {

		// Use the provided adapter if any
		IGotoMarker gotoMarkerAdapter= fTextEditor.getAdapter(IGotoMarker.class);
		if (gotoMarkerAdapter != null) {
			gotoMarkerAdapter.gotoMarker(marker);
			return;
		}

		int start= MarkerUtilities.getCharStart(marker);
		int end= MarkerUtilities.getCharEnd(marker);

		boolean selectLine= start < 0 || end < 0;

		IDocumentProvider documentProvider= fTextEditor.getDocumentProvider();
		IEditorInput editorInput= fTextEditor.getEditorInput();

		// look up the current range of the marker when the document has been edited
		IAnnotationModel model= documentProvider.getAnnotationModel(editorInput);
		if (model instanceof AbstractMarkerAnnotationModel) {

			AbstractMarkerAnnotationModel markerModel= (AbstractMarkerAnnotationModel) model;
			Position pos= markerModel.getMarkerPosition(marker);
			if (pos != null && !pos.isDeleted()) {
				// use position instead of marker values
				start= pos.getOffset();
				end= pos.getOffset() + pos.getLength();
			}

			if (pos != null && pos.isDeleted()) {
				// do nothing if position has been deleted
				return;
			}
		}

		IDocument document= documentProvider.getDocument(editorInput);

		if (selectLine) {
			int line;
			try {
				if (start >= 0)
					line= document.getLineOfOffset(start);
				else {
					line= MarkerUtilities.getLineNumber(marker);
					// Marker line numbers are 1-based
					-- line;
				}
				end= start + document.getLineLength(line) - 1;
			} catch (BadLocationException e) {
				return;
			}
		}

		int length= document.getLength();
		if (end - 1 < length && start < length)
			fTextEditor.selectAndReveal(start, end - start);
	}


	/**
	 * Chooses the marker with the highest layer. If there are multiple
	 * markers at the found layer, the first marker is taken.
	 *
	 * @param markers the list of markers to choose from
	 * @return the chosen marker or <code>null</code> if none of the given markers has a marker annotation in the model
	 */
	protected final IMarker chooseMarker(List<? extends IMarker> markers) {

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

		IMarker marker= null;
		int maxLayer= 0;

		Iterator<? extends IMarker> iter= markers.iterator();
		while (iter.hasNext()) {
			IMarker m= iter.next();
			Annotation a= model.getMarkerAnnotation(m);
			if (a != null) {
				if (access == null) {
					marker= m;
					break;
				}
				int l= access.getLayer(a);
				if (l == maxLayer) {
					if (marker == null)
						marker= m;
				} else if (l > maxLayer) {
					maxLayer= l;
					marker= m;
				}
			}
		}

		return marker;
	}

	/**
	 * Returns the annotation access extension.
	 *
	 * @return the annotation access extension or <code>null</code> if
	 * 			this action's editor has no such extension
	 * @since 3.0
	 */
	protected final IAnnotationAccessExtension getAnnotationAccessExtension() {
		Object adapter= fTextEditor.getAdapter(IAnnotationAccess.class);
		if (adapter instanceof IAnnotationAccessExtension)
			return (IAnnotationAccessExtension)adapter;

		return null;
	}

	/**
	 * Returns the resource for which to create the marker,
	 * or <code>null</code> if there is no applicable resource.
	 *
	 * @return the resource for which to create the marker or <code>null</code>
	 */
	protected final IResource getResource() {
		IEditorInput input= fTextEditor.getEditorInput();

		IResource resource= input.getAdapter(IFile.class);

		if (resource == null)
			resource= input.getAdapter(IResource.class);

		return resource;
	}

	/**
	 * Returns the <code>AbstractMarkerAnnotationModel</code> of the editor's input.
	 *
	 * @return the marker annotation model or <code>null</code> if there's none
	 */
	protected final AbstractMarkerAnnotationModel getAnnotationModel() {
		IDocumentProvider provider= fTextEditor.getDocumentProvider();
		IAnnotationModel model= provider.getAnnotationModel(fTextEditor.getEditorInput());
		if (model instanceof AbstractMarkerAnnotationModel)
			return (AbstractMarkerAnnotationModel) model;
		return null;
	}

	/**
	 * Returns the <code>IDocument</code> of the editor's input.
	 *
	 * @return the document of the editor's input
	 */
	protected final IDocument getDocument() {
		IDocumentProvider provider= fTextEditor.getDocumentProvider();
		return provider.getDocument(fTextEditor.getEditorInput());
	}

	/**
	 * Checks whether a position includes the ruler's line of activity.
	 *
	 * @param position the position to be checked
	 * @param document the document the position refers to
	 * @return <code>true</code> if the line is included by the given position
	 */
	protected final boolean includesRulerLine(Position position, IDocument document) {

		if (position != null) {
			try {
				int markerLine= document.getLineOfOffset(position.getOffset());
				int line= fRuler.getLineOfLastMouseButtonActivity();
				if (line == markerLine)
					return true;
				// commented because of "1GEUOZ9: ITPJUI:ALL - Confusing UI for multi-line Bookmarks and Tasks"
				// return (markerLine <= line && line <= document.getLineOfOffset(position.getOffset() + position.getLength()));
			} catch (BadLocationException x) {
			}
		}

		return false;
	}

	/**
	 * Checks whether a position includes the ruler's line of activity.
	 *
	 * @param position the position to be checked
	 * @param document the document the position refers to
	 * @param line the line of the last ruler activity
	 * @return <code>true</code> if the line is included by the given position
	 * @since 3.3
	 */
	private boolean includesLine(Position position, IDocument document, int line) {

		if (position != null) {
			try {
				int markerLine= document.getLineOfOffset(position.getOffset());
				if (line == markerLine)
					return true;
				// commented because of "1GEUOZ9: ITPJUI:ALL - Confusing UI for multi-line Bookmarks and Tasks"
				// return (markerLine <= line && line <= document.getLineOfOffset(position.getOffset() + position.getLength()));
			} catch (BadLocationException x) {
			}
		}

		return false;
	}

	/**
	 * Handles core exceptions. This implementation logs the exceptions
	 * with the workbench plug-in and shows an error dialog.
	 *
	 * @param exception the exception to be handled
	 * @param message the message to be logged with the given exception
	 */
	protected final void handleCoreException(CoreException exception, String message) {
		IStatus status;
		if (message != null)
			status= new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.OK, message, exception);
		else
			status= exception.getStatus();
		logException(status);

		Shell shell= fTextEditor.getSite().getShell();
		String title= getString(fBundle, fPrefix + "error.dialog.title", fPrefix + "error.dialog.title"); //$NON-NLS-2$ //$NON-NLS-1$
		String msg= getString(fBundle, fPrefix + "error.dialog.message", fPrefix + "error.dialog.message"); //$NON-NLS-2$ //$NON-NLS-1$

		ErrorDialog.openError(shell, title, msg, exception.getStatus());
	}

	/**
	 * Log status.
	 *
	 * @param status the status to log
	 * @since 3.4
	 */
	private void logException(IStatus status) {
		Bundle bundle = Platform.getBundle(PlatformUI.PLUGIN_ID);
		ILog log= Platform.getLog(bundle);
		log.log(status);
	}

	/**
	 * Returns all markers which include the ruler's line of activity.
	 *
	 * @return an unmodifiable list with all markers which include the ruler's line of activity
	 */
	protected final List<IMarker> getMarkers() {
		final IResource resource= getResource();
		if (resource == null || !resource.exists())
			return Collections.emptyList();

		final IDocument document= getDocument();
		if (document == null)
			return Collections.emptyList();

		final AbstractMarkerAnnotationModel model= getAnnotationModel();
		if (model == null)
			return Collections.emptyList();

		final IMarker[] allMarkers;
		try {
			allMarkers= resource.findMarkers(null, true, IResource.DEPTH_ZERO);
		} catch (CoreException x) {
			handleCoreException(x, TextEditorMessages.SelectMarkerRulerAction_getMarker);
			return Collections.emptyList();
		}

		if (allMarkers.length == 0)
			return Collections.emptyList();

		final int activeLine= fRuler.getLineOfLastMouseButtonActivity();
		if (activeLine == -1)
			return Collections.emptyList();

		Iterator<Annotation> it;
		try {
			IRegion line= document.getLineInformation(activeLine);
			it= model.getAnnotationIterator(line.getOffset(), line.getLength() + 1, true, true);
		} catch (BadLocationException e) {
			Bundle bundle= Platform.getBundle(PlatformUI.PLUGIN_ID);
			Platform.getLog(bundle).log(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.OK, e.getLocalizedMessage(), e));

			it= model.getAnnotationIterator();
		}

		List<IMarker> markers= null;
		while (it.hasNext()) {
			Annotation annotation= it.next();
			if (annotation instanceof MarkerAnnotation) {
				Position position= model.getPosition(annotation);
				if (includesLine(position, document, activeLine)) {
					if (markers == null)
						markers= new ArrayList<>(10);

					markers.add(((MarkerAnnotation) annotation).getMarker());
				}
			}
		}

		if (markers == null)
			return Collections.emptyList();

		return Collections.unmodifiableList(markers);
	}

	/**
	 * Returns <code>true</code> iff there are any markers which include the ruler's line of activity.
	 *
	 * @return <code>true</code> iff there are any markers which include the ruler's line of activity.
	 * @since 3.3
	 */
	protected final boolean hasMarkers() {
		final IResource resource= getResource();
		if (resource == null || !resource.exists())
			return false;

		final IDocument document= getDocument();
		if (document == null)
			return false;

		final AbstractMarkerAnnotationModel model= getAnnotationModel();
		if (model == null)
			return false;

		final IMarker[] allMarkers;
		try {
			allMarkers= resource.findMarkers(null, true, IResource.DEPTH_ZERO);
		} catch (CoreException x) {
			handleCoreException(x, TextEditorMessages.SelectMarkerRulerAction_getMarker);
			return false;
		}

		if (allMarkers.length == 0)
			return false;

		final int activeLine= fRuler.getLineOfLastMouseButtonActivity();
		if (activeLine == -1)
			return false;

		Iterator<Annotation> it;
		try {
			IRegion line= document.getLineInformation(activeLine);
			it= model.getAnnotationIterator(line.getOffset(), line.getLength() + 1, true, true);
		} catch (BadLocationException e) {
			logException(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.OK, e.getLocalizedMessage(), e));
			it= model.getAnnotationIterator();
		}

		while (it.hasNext()) {
			Annotation annotation= it.next();
			if (annotation instanceof MarkerAnnotation) {
				Position position= model.getPosition(annotation);
				if (includesLine(position, document, activeLine)) {
					return true;
				}
			}
		}

		return false;
	}
}

Back to the top