Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 11e144a2d647c3e33d54d0ec1a7e67e1bec2fb4a (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
/*******************************************************************************
 * Copyright (c) 2008, 2015 Nokia 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:
 *     Nokia - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.actions;

import java.util.Arrays;
import java.util.Comparator;
import java.util.ResourceBundle;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.util.OpenStrategy;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.MarkerUtilities;
import org.eclipse.ui.texteditor.TextEditorAction;

/**
 * Find and goto the next bookmark in the currently selected file.
 */
public class GotoNextBookmarkAction extends TextEditorAction {

	public static final String NEXT_BOOKMARK = "GotoNextBookmark"; //$NON-NLS-1$

	/**
	 * Private class to handle comparison of markers using their line numbers.
	 */
	private class CompareMarker implements Comparator<IMarker> {
		@Override
		public int compare(IMarker m1, IMarker m2) {
			int l1 = MarkerUtilities.getLineNumber(m1);
			int l2 = MarkerUtilities.getLineNumber(m2);
			if (l1 > l2)
				return 1;
			if (l1 < l2)
				return -1;
			return 0;
		}
	}

	/**
	 * Creates new action.
	 */
	public GotoNextBookmarkAction(ResourceBundle bundle, String prefix, ITextEditor editor) {
		super(bundle, prefix, editor);
	}

	@Override
	public void run() {
		ITextEditor editor = getTextEditor();
		if (editor == null)
			return;

		ISelectionProvider provider = editor.getSelectionProvider();
		if (provider == null)
			return;

		ITextSelection selection = (ITextSelection) provider.getSelection();
		if (selection == null || selection.isEmpty())
			return;

		IEditorInput input = editor.getEditorInput();
		if (input == null)
			return;

		IResource resource = (input).getAdapter(IResource.class);
		if (resource == null || !(resource instanceof IFile))
			return;

		try {
			IMarker[] bookmarks = resource.findMarkers(IMarker.BOOKMARK, true, IResource.DEPTH_ONE);
			if (bookmarks.length == 0)
				return;

			// sort bookmarks by line number
			CompareMarker comparator = new CompareMarker();
			Arrays.sort(bookmarks, comparator);

			// marker line numbers are 1-based
			int line = selection.getStartLine() + 1;
			IMarker lastBookmark = bookmarks[bookmarks.length - 1];

			// start from the beginning of file if reached or went beyond last bookmark
			if (line >= MarkerUtilities.getLineNumber(lastBookmark)) {
				line = 1;
			}

			// find the next bookmark and goto it
			for (int i = 0; i < bookmarks.length; i++) {
				IMarker bookmark = bookmarks[i];
				if (MarkerUtilities.getLineNumber(bookmark) > line) {
					IDE.openEditor(getTextEditor().getSite().getPage(), bookmark, OpenStrategy.activateOnOpen());
					break;
				}
			}
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}

}

Back to the top