blob: ca22190e2ec0d49e0552c5b4e356d06745ac7fc0 (
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
|
/**********************************************************************
* Copyright (c) 2004, 2012 Intel 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:
* Intel Corporation - Initial API and implementation
* Anton Leherbauer (Wind River Systems)
**********************************************************************/
package org.eclipse.cdt.internal.ui.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.ui.CHelpProviderManager;
import org.eclipse.cdt.internal.ui.text.CWordFinder;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.ICHelpResourceDescriptor;
import org.eclipse.cdt.ui.text.ICHelpInvocationContext;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.help.HelpSystem;
import org.eclipse.help.IContext;
import org.eclipse.help.IHelpResource;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.ITextEditor;
/**
*
* @since 2.1
*/
public class CHelpDisplayContext implements IContext {
private IHelpResource[] fHelpResources;
private String fText;
public static void displayHelp(String contextId, ITextEditor editor) throws CoreException {
String selected = getSelectedString(editor);
IContext context = HelpSystem.getContext(contextId);
if (context != null) {
if (selected != null && selected.length() > 0) {
context = new CHelpDisplayContext(context, editor, selected);
}
PlatformUI.getWorkbench().getHelpSystem().displayHelp(context);
}
}
private static String getSelectedString(ITextEditor editor) {
String expression = null;
try {
ITextSelection selection = (ITextSelection) editor.getSite().getSelectionProvider().getSelection();
IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
IRegion region = CWordFinder.findWord(document, selection.getOffset());
expression = document.get(region.getOffset(), region.getLength());
} catch (Exception e) {
}
return expression;
}
public CHelpDisplayContext(IContext context, final ITextEditor editor, String selected) throws CoreException {
List<IHelpResource> helpResources = new ArrayList<IHelpResource>();
ICHelpInvocationContext invocationContext = new ICHelpInvocationContext() {
@Override
public IProject getProject() {
ITranslationUnit unit = getTranslationUnit();
if (unit != null) {
return unit.getCProject().getProject();
}
return null;
}
@Override
public ITranslationUnit getTranslationUnit() {
IEditorInput editorInput = editor.getEditorInput();
return CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editorInput);
}
};
if (context != null) {
IHelpResource[] resources = context.getRelatedTopics();
if (resources != null) {
helpResources.addAll(Arrays.asList(resources));
}
}
ICHelpResourceDescriptor providerResources[] = CHelpProviderManager.getDefault()
.getHelpResources(invocationContext, selected);
if (providerResources != null) {
for (int i = 0; i < providerResources.length; i++) {
helpResources.addAll(Arrays.asList(providerResources[i].getHelpResources()));
}
}
fHelpResources = helpResources.toArray(new IHelpResource[helpResources.size()]);
if (fText == null || fText.length() == 0) {
if (context != null) {
fText = context.getText();
}
}
if (fText != null && fText.length() == 0) {
fText = null;
}
}
@Override
public IHelpResource[] getRelatedTopics() {
return fHelpResources;
}
@Override
public String getText() {
return fText;
}
}
|