mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Add support for dynamic help context provider
This commit is contained in:
parent
dcc302a00d
commit
6f2a059c1a
4 changed files with 94 additions and 9 deletions
|
@ -28,6 +28,7 @@ import org.eclipse.core.runtime.CoreException;
|
|||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
import org.eclipse.core.runtime.ListenerList;
|
||||
import org.eclipse.core.runtime.content.IContentType;
|
||||
import org.eclipse.help.IContextProvider;
|
||||
import org.eclipse.jface.action.GroupMarker;
|
||||
import org.eclipse.jface.action.IAction;
|
||||
import org.eclipse.jface.action.IMenuManager;
|
||||
|
@ -1262,6 +1263,9 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IR
|
|||
return adapter;
|
||||
}
|
||||
}
|
||||
if (IContextProvider.class.equals(required)) {
|
||||
return new CUIHelp.CUIHelpContextProvider(this);
|
||||
}
|
||||
return super.getAdapter(required);
|
||||
}
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2000, 2006 QNX Software Systems and others.
|
||||
* Copyright (c) 2000, 2007 QNX Software Systems 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
|
||||
|
@ -7,6 +7,7 @@
|
|||
*
|
||||
* Contributors:
|
||||
* QNX Software Systems - Initial API and implementation
|
||||
* Anton Leherbauer (Wind River Systems)
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.ui.text.c.hover;
|
||||
|
||||
|
@ -18,6 +19,7 @@ import org.eclipse.cdt.internal.ui.text.CWordFinder;
|
|||
import org.eclipse.cdt.internal.ui.text.HTMLPrinter;
|
||||
import org.eclipse.cdt.ui.CUIPlugin;
|
||||
import org.eclipse.cdt.ui.IFunctionSummary;
|
||||
import org.eclipse.cdt.ui.IFunctionSummary.IFunctionPrototypeSummary;
|
||||
import org.eclipse.cdt.ui.text.ICHelpInvocationContext;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.jface.text.IRegion;
|
||||
|
@ -72,8 +74,11 @@ public class CDocHover extends AbstractCEditorTextHover {
|
|||
if (fs != null) {
|
||||
buffer.append(CEditorMessages.getString("DefaultCEditorTextHover.html.name")); //$NON-NLS-1$
|
||||
buffer.append(HTMLPrinter.convertToHTMLContent(fs.getName()));
|
||||
buffer.append(CEditorMessages.getString("DefaultCEditorTextHover.html.prototype")); //$NON-NLS-1$
|
||||
buffer.append(HTMLPrinter.convertToHTMLContent(fs.getPrototype().getPrototypeString(false)));
|
||||
final IFunctionPrototypeSummary prototype = fs.getPrototype();
|
||||
if (prototype != null) {
|
||||
buffer.append(CEditorMessages.getString("DefaultCEditorTextHover.html.prototype")); //$NON-NLS-1$
|
||||
buffer.append(HTMLPrinter.convertToHTMLContent(prototype.getPrototypeString(false)));
|
||||
}
|
||||
if(fs.getDescription() != null) {
|
||||
buffer.append(CEditorMessages.getString("DefaultCEditorTextHover.html.description")); //$NON-NLS-1$
|
||||
//Don't convert this description since it could already be formatted
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**********************************************************************
|
||||
* Copyright (c) 2004, 2005 Intel Corporation and others.
|
||||
* Copyright (c) 2004, 2007 Intel 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
|
||||
|
@ -7,6 +7,7 @@
|
|||
*
|
||||
* Contributors:
|
||||
* Intel Corporation - Initial API and implementation
|
||||
* Anton Leherbauer (Wind River Systems)
|
||||
**********************************************************************/
|
||||
package org.eclipse.cdt.internal.ui.util;
|
||||
|
||||
|
@ -24,6 +25,7 @@ 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;
|
||||
|
||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||
import org.eclipse.cdt.ui.CUIPlugin;
|
||||
|
@ -31,7 +33,6 @@ import org.eclipse.cdt.ui.ICHelpResourceDescriptor;
|
|||
import org.eclipse.cdt.ui.text.ICHelpInvocationContext;
|
||||
|
||||
import org.eclipse.cdt.internal.ui.CHelpProviderManager;
|
||||
import org.eclipse.cdt.internal.ui.editor.CEditor;
|
||||
import org.eclipse.cdt.internal.ui.text.CWordFinder;
|
||||
|
||||
/**
|
||||
|
@ -39,10 +40,11 @@ import org.eclipse.cdt.internal.ui.text.CWordFinder;
|
|||
* @since 2.1
|
||||
*/
|
||||
public class CHelpDisplayContext implements IContext {
|
||||
|
||||
private IHelpResource[] fHelpResources;
|
||||
private String fText;
|
||||
|
||||
public static void displayHelp(String contextId, CEditor editor) throws CoreException {
|
||||
public static void displayHelp(String contextId, ITextEditor editor) throws CoreException {
|
||||
String selected = getSelectedString(editor);
|
||||
IContext context= HelpSystem.getContext(contextId);
|
||||
if (context != null) {
|
||||
|
@ -53,7 +55,7 @@ public class CHelpDisplayContext implements IContext {
|
|||
}
|
||||
}
|
||||
|
||||
private static String getSelectedString(CEditor editor){
|
||||
private static String getSelectedString(ITextEditor editor){
|
||||
String expression = null;
|
||||
try{
|
||||
ITextSelection selection = (ITextSelection)editor.getSite().getSelectionProvider().getSelection();
|
||||
|
@ -66,7 +68,7 @@ public class CHelpDisplayContext implements IContext {
|
|||
return expression;
|
||||
}
|
||||
|
||||
public CHelpDisplayContext(IContext context, final CEditor editor , String selected) throws CoreException {
|
||||
public CHelpDisplayContext(IContext context, final ITextEditor editor , String selected) throws CoreException {
|
||||
|
||||
List helpResources= new ArrayList();
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**********************************************************************
|
||||
* Copyright (c) 2004, 2005 Intel Corporation and others.
|
||||
* Copyright (c) 2004, 2007 Intel 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
|
||||
|
@ -7,16 +7,27 @@
|
|||
*
|
||||
* Contributors:
|
||||
* Intel Corporation - Initial API and implementation
|
||||
* Anton Leherbauer (Wind River Systems)
|
||||
**********************************************************************/
|
||||
|
||||
package org.eclipse.cdt.internal.ui.util;
|
||||
|
||||
import org.eclipse.cdt.internal.ui.ICHelpContextIds;
|
||||
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.core.runtime.CoreException;
|
||||
import org.eclipse.help.HelpSystem;
|
||||
import org.eclipse.help.IContext;
|
||||
import org.eclipse.help.IContextProvider;
|
||||
import org.eclipse.jface.text.IDocument;
|
||||
import org.eclipse.jface.text.IRegion;
|
||||
import org.eclipse.jface.text.ITextSelection;
|
||||
import org.eclipse.swt.custom.StyledText;
|
||||
import org.eclipse.swt.events.HelpEvent;
|
||||
import org.eclipse.swt.events.HelpListener;
|
||||
import org.eclipse.ui.texteditor.ITextEditor;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -52,4 +63,67 @@ public class CUIHelp {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A dynamic help context provider.
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public static final class CUIHelpContextProvider implements IContextProvider {
|
||||
|
||||
private final ITextEditor fEditor;
|
||||
|
||||
/**
|
||||
* Creates a context provider for the given text editor.
|
||||
* @param editor
|
||||
*/
|
||||
public CUIHelpContextProvider(ITextEditor editor) {
|
||||
fEditor= editor;
|
||||
}
|
||||
|
||||
/*
|
||||
* @see org.eclipse.help.IContextProvider#getContext(java.lang.Object)
|
||||
*/
|
||||
public IContext getContext(Object target) {
|
||||
String selected = getSelectedString(fEditor);
|
||||
IContext context= HelpSystem.getContext(ICHelpContextIds.CEDITOR_VIEW);
|
||||
if (context != null) {
|
||||
if (selected != null && selected.length() > 0) {
|
||||
try {
|
||||
context= new CHelpDisplayContext(context, fEditor, selected);
|
||||
} catch (CoreException exc) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
/*
|
||||
* @see org.eclipse.help.IContextProvider#getContextChangeMask()
|
||||
*/
|
||||
public int getContextChangeMask() {
|
||||
return SELECTION;
|
||||
}
|
||||
|
||||
/*
|
||||
* @see org.eclipse.help.IContextProvider#getSearchExpression(java.lang.Object)
|
||||
*/
|
||||
public String getSearchExpression(Object target) {
|
||||
return getSelectedString(fEditor);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue