Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7477cbb2e67fd6b6798917959a2f6723b765004e (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
/*******************************************************************************
 * Copyright (c) 2013 Ericsson AB 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:
 *     Alvaro Sanchez-Leon (Ericsson AB) - Support for Step into selection (bug 244865)
 *******************************************************************************/
package org.eclipse.cdt.dsf.debug.ui.actions;

import org.eclipse.cdt.core.model.ICLanguageKeywords;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.debug.core.model.IStepIntoSelectionHandler;
import org.eclipse.cdt.dsf.datamodel.DMContexts;
import org.eclipse.cdt.dsf.datamodel.IDMContext;
import org.eclipse.cdt.dsf.debug.internal.ui.Messages;
import org.eclipse.cdt.dsf.debug.internal.ui.sourcelookup.DsfSourceSelectionResolver;
import org.eclipse.cdt.dsf.debug.internal.ui.sourcelookup.DsfSourceSelectionResolver.LineLocation;
import org.eclipse.cdt.dsf.debug.service.IRunControl.IExecutionDMContext;
import org.eclipse.cdt.dsf.internal.ui.DsfUIPlugin;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.cdt.dsf.ui.viewmodel.datamodel.IDMVMContext;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.text.CWordFinder;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.text.ICPartitions;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.ui.texteditor.ITextEditor;

/**
 * @since 2.4
 * 
 */
public class DsfStepIntoSelectionHyperlinkDetector extends AbstractHyperlinkDetector {
	private class DsfStepIntoSelectionHyperlink implements IHyperlink {

		private ITextSelection fSelection = null;
		private IDsfStepIntoSelection fStepIntoSelectionCommand = null;
		private DsfSourceSelectionResolver fSelectionResolver = null;

		/**
		 * Constructor
		 * 
		 * @param stepIntoSelectionCommand
		 * @param region
		 */
		public DsfStepIntoSelectionHyperlink(DsfSourceSelectionResolver selectionResolver, IDsfStepIntoSelection stepIntoSelectionCommand) {
			fSelection = selectionResolver.resolveSelection();
			fStepIntoSelectionCommand = stepIntoSelectionCommand;
			fSelectionResolver = selectionResolver;
		}

		@Override
		public IRegion getHyperlinkRegion() {
			return new Region(fSelection.getOffset(), fSelection.getLength());
		}

		@Override
		public String getHyperlinkText() {
			return Messages.DsfUIStepIntoEditorSelection;
		}

		@Override
		public String getTypeLabel() {
			return null;
		}

		@Override
		public void open() {
			// Resolve the debug context
			final IExecutionDMContext dmc = resolveDebugContext();
			if (fSelectionResolver.isSuccessful() && dmc != null) {
				LineLocation location = fSelectionResolver.getLineLocation();
				fStepIntoSelectionCommand.runToSelection(location.getFileName(), location.getLineNumber(), fSelectionResolver.getFunction(), dmc);
			} else {
				String message = null;
				if (dmc == null) {
					message = "DSfStepIntoSelection: Unable to resolve the debug context"; //$NON-NLS-1$
				} else {
					message = "DSfStepIntoSelection: Unable to resolve a selected function"; //$NON-NLS-1$
				}
				DsfUIPlugin.debug(message);
			}
		}
	}

	@Override
	public IHyperlink[] detectHyperlinks(ITextViewer textViewer, final IRegion region, boolean canShowMultipleHyperlinks) {
		// Only valid in the context of a selection within the CEditor
		ITextEditor editor = getAdapter(ITextEditor.class);
		if (editor == null || region == null || !(editor instanceof CEditor))
			return null;

		ITextSelection selection = resolveSelection(editor, region);
		if (selection == null) {
			return null;
		}

		// Shall only enable hyper link step into selection within a cdt debug execution context
		IExecutionDMContext dmc = resolveDebugContext();
		if (dmc == null) {
			return null;
		}

		final DsfSession session = DsfSession.getSession(dmc.getSessionId());
		if (session == null || !session.isActive()) {
			return null;
		}

		IDsfStepIntoSelection stepIntoSelectionCommand = null;
		IStepIntoSelectionHandler stepIntoSelectionHandler = (IStepIntoSelectionHandler) session.getModelAdapter(IStepIntoSelectionHandler.class);
		if (stepIntoSelectionHandler instanceof IDsfStepIntoSelection) {
			stepIntoSelectionCommand = (IDsfStepIntoSelection)stepIntoSelectionHandler;
		} else {
			return null;
		}

		if (!stepIntoSelectionCommand.isExecutable(dmc)) {
			return null;
		}

		DsfSourceSelectionResolver functionResolver = new DsfSourceSelectionResolver(editor, selection);
		functionResolver.run();
		// Resolve to a selected function
		if (!functionResolver.isSuccessful()) {
			// We are not pointing to a valid function
			return null;
		}

		return new IHyperlink[] { new DsfStepIntoSelectionHyperlink(functionResolver, stepIntoSelectionCommand) };
	}

	private ITextSelection resolveSelection(ITextEditor editor, IRegion region) {
		ITextSelection selection = null;
		if (editor != null) {
			IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
			final IWorkingCopy workingCopy = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editor.getEditorInput());

			if (document != null && workingCopy != null) {
				// Check partition type.
				String partitionType;
				try {
					partitionType = TextUtilities.getContentType(document, ICPartitions.C_PARTITIONING, region.getOffset(), false);
					if (IDocument.DEFAULT_CONTENT_TYPE.equals(partitionType)) {
						// Regular code i.e. Not a Preprocessor directive.
						IRegion wregion = getIdentifier(document, region.getOffset(), workingCopy.getLanguage());
						if (wregion != null) {
							selection = new TextSelection(document, wregion.getOffset(), wregion.getLength());
						}
					}
				} catch (BadLocationException e) {
					// Ignore to return null
				} catch (CoreException e) {
					// Ignore to return null
				}
			}
		}

		return selection;
	}

	/**
	 * Returns the identifier at the given offset, or {@code null} if the there is no identifier at the offset.
	 */
	private static IRegion getIdentifier(IDocument document, int offset, ILanguage language) throws BadLocationException {
		IRegion wordRegion = CWordFinder.findWord(document, offset);
		if (wordRegion != null && wordRegion.getLength() > 0) {
			String word = document.get(wordRegion.getOffset(), wordRegion.getLength());
			if (!Character.isDigit(word.charAt(0)) && !isLanguageKeyword(language, word)) {
				return wordRegion;
			}
		}
		return null;
	}

	private static boolean isLanguageKeyword(ILanguage lang, String word) {
		ICLanguageKeywords keywords = lang.getAdapter(ICLanguageKeywords.class);
		if (keywords != null) {
			for (String keyword : keywords.getKeywords()) {
				if (keyword.equals(word))
					return true;
			}
			for (String type : keywords.getBuiltinTypes()) {
				if (type.equals(word))
					return true;
			}
			for (String keyword : keywords.getPreprocessorKeywords()) {
				if (keyword.charAt(0) == '#' && keyword.length() == word.length() + 1 && keyword.regionMatches(1, word, 0, word.length())) {
					return true;
				}
			}
		}
		return false;
	}

	private IExecutionDMContext resolveDebugContext() {
		IExecutionDMContext execContext = null;
		IAdaptable adaptableContext = DebugUITools.getDebugContext();
		IDMContext debugContext = null;
		if (adaptableContext instanceof IDMVMContext) {
			debugContext = ((IDMVMContext) adaptableContext).getDMContext();
		}

		if (debugContext != null) {
			execContext = DMContexts.getAncestorOfType(debugContext, IExecutionDMContext.class);
		}

		return execContext;
	}
}

Back to the top