mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-09 10:46:02 +02:00
222728: [editor] Register C/C++ editor with common hyperlinking preferences
This commit is contained in:
parent
7b37526690
commit
872058b5e5
4 changed files with 90 additions and 92 deletions
|
@ -475,3 +475,7 @@ DocCommentOwner.name = DocCommentOwner
|
|||
Doxygen.name = Doxygen
|
||||
|
||||
indexedFilesDecorator.label = C/C++ Indexed Files
|
||||
|
||||
# Hyperlinking
|
||||
cEditorHyperlinkTarget= C/C++ Editor
|
||||
cElementHyperlinkDetector= C/C++ Elements
|
||||
|
|
|
@ -2677,4 +2677,24 @@
|
|||
</enablement>
|
||||
</decorator>
|
||||
</extension>
|
||||
</plugin>
|
||||
|
||||
<!-- Hyperlinking support -->
|
||||
<extension
|
||||
point="org.eclipse.ui.workbench.texteditor.hyperlinkDetectorTargets">
|
||||
<target
|
||||
id="org.eclipse.cdt.ui.cCode"
|
||||
name="%cEditorHyperlinkTarget">
|
||||
<context type="org.eclipse.ui.texteditor.ITextEditor"/>
|
||||
</target>
|
||||
</extension>
|
||||
<extension
|
||||
point="org.eclipse.ui.workbench.texteditor.hyperlinkDetectors">
|
||||
<hyperlinkDetector
|
||||
class="org.eclipse.cdt.internal.ui.editor.CElementHyperlinkDetector"
|
||||
id="org.eclipse.cdt.ui.editor.CElementHyperlinkDetector"
|
||||
name="%cElementHyperlinkDetector"
|
||||
targetId="org.eclipse.cdt.ui.cCode">
|
||||
</hyperlinkDetector>
|
||||
</extension>
|
||||
|
||||
</plugin>
|
||||
|
|
|
@ -12,107 +12,96 @@
|
|||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.ui.editor;
|
||||
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IStatus;
|
||||
import org.eclipse.core.runtime.Status;
|
||||
import org.eclipse.jface.action.IAction;
|
||||
import org.eclipse.jface.text.BadLocationException;
|
||||
import org.eclipse.jface.text.IDocument;
|
||||
import org.eclipse.jface.text.IRegion;
|
||||
import org.eclipse.jface.text.ITextViewer;
|
||||
import org.eclipse.jface.text.Region;
|
||||
import org.eclipse.jface.text.TextUtilities;
|
||||
import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector;
|
||||
import org.eclipse.jface.text.hyperlink.IHyperlink;
|
||||
import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
|
||||
import org.eclipse.ui.texteditor.ITextEditor;
|
||||
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||
import org.eclipse.cdt.core.index.IIndex;
|
||||
import org.eclipse.cdt.core.index.IIndexManager;
|
||||
import org.eclipse.cdt.core.model.ILanguage;
|
||||
import org.eclipse.cdt.core.model.IWorkingCopy;
|
||||
import org.eclipse.cdt.ui.CUIPlugin;
|
||||
import org.eclipse.cdt.ui.text.ICPartitions;
|
||||
|
||||
import org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable;
|
||||
|
||||
public class CElementHyperlinkDetector implements IHyperlinkDetector {
|
||||
public class CElementHyperlinkDetector extends AbstractHyperlinkDetector {
|
||||
|
||||
private ITextEditor fTextEditor;
|
||||
|
||||
public CElementHyperlinkDetector(ITextEditor editor) {
|
||||
fTextEditor= editor;
|
||||
public CElementHyperlinkDetector() {
|
||||
}
|
||||
|
||||
|
||||
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, final IRegion region, boolean canShowMultipleHyperlinks) {
|
||||
if (region == null || canShowMultipleHyperlinks || fTextEditor == null)
|
||||
ITextEditor textEditor= (ITextEditor)getAdapter(ITextEditor.class);
|
||||
if (region == null || canShowMultipleHyperlinks || !(textEditor instanceof CEditor))
|
||||
return null;
|
||||
|
||||
final IAction openAction= fTextEditor.getAction("OpenDeclarations"); //$NON-NLS-1$
|
||||
final IAction openAction= textEditor.getAction("OpenDeclarations"); //$NON-NLS-1$
|
||||
if (openAction == null)
|
||||
return null;
|
||||
|
||||
final IWorkingCopy workingCopy = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(fTextEditor.getEditorInput());
|
||||
// check partition type
|
||||
try {
|
||||
String partitionType= TextUtilities.getContentType(textViewer.getDocument(), ICPartitions.C_PARTITIONING, region.getOffset(), false);
|
||||
if (!IDocument.DEFAULT_CONTENT_TYPE.equals(partitionType) && !ICPartitions.C_PREPROCESSOR.equals(partitionType)) {
|
||||
return null;
|
||||
}
|
||||
} catch (BadLocationException exc) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final IWorkingCopy workingCopy = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(textEditor.getEditorInput());
|
||||
if (workingCopy == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
IIndex index;
|
||||
try {
|
||||
index = CCorePlugin.getIndexManager().getIndex(workingCopy.getCProject(),
|
||||
IIndexManager.ADD_DEPENDENCIES | IIndexManager.ADD_DEPENDENT);
|
||||
} catch(CoreException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
index.acquireReadLock();
|
||||
} catch (InterruptedException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final IHyperlink[] result= {null};
|
||||
try {
|
||||
IStatus status= ASTProvider.getASTProvider().runOnAST(workingCopy, ASTProvider.WAIT_YES, null, new ASTRunnable() {
|
||||
public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) {
|
||||
if (ast != null) {
|
||||
final int offset= region.getOffset();
|
||||
final int length= Math.max(1, region.getLength());
|
||||
final IASTNodeSelector nodeSelector= ast.getNodeSelector(null);
|
||||
IASTName selectedName= nodeSelector.findEnclosingName(offset, length);
|
||||
IASTFileLocation linkLocation= null;
|
||||
if (selectedName != null) { // found a name
|
||||
// prefer include statement over the include name
|
||||
if (selectedName.getParent() instanceof IASTPreprocessorIncludeStatement) {
|
||||
linkLocation= selectedName.getParent().getFileLocation();
|
||||
}
|
||||
else {
|
||||
linkLocation= selectedName.getFileLocation();
|
||||
}
|
||||
IStatus status= ASTProvider.getASTProvider().runOnAST(workingCopy, ASTProvider.WAIT_YES, null, new ASTRunnable() {
|
||||
public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) {
|
||||
if (ast != null) {
|
||||
final int offset= region.getOffset();
|
||||
final int length= Math.max(1, region.getLength());
|
||||
final IASTNodeSelector nodeSelector= ast.getNodeSelector(null);
|
||||
IASTName selectedName= nodeSelector.findEnclosingName(offset, length);
|
||||
IASTFileLocation linkLocation= null;
|
||||
if (selectedName != null) { // found a name
|
||||
// prefer include statement over the include name
|
||||
if (selectedName.getParent() instanceof IASTPreprocessorIncludeStatement) {
|
||||
linkLocation= selectedName.getParent().getFileLocation();
|
||||
}
|
||||
else {
|
||||
// search for include statement
|
||||
final IASTNode cand= nodeSelector.findEnclosingNode(offset, length);
|
||||
if (cand instanceof IASTPreprocessorIncludeStatement) {
|
||||
linkLocation= cand.getFileLocation();
|
||||
}
|
||||
}
|
||||
if (linkLocation != null) {
|
||||
result[0]= new CElementHyperlink(
|
||||
new Region(linkLocation.getNodeOffset(), linkLocation.getNodeLength()), openAction);
|
||||
linkLocation= selectedName.getFileLocation();
|
||||
}
|
||||
}
|
||||
return Status.OK_STATUS;
|
||||
else {
|
||||
// search for include statement
|
||||
final IASTNode cand= nodeSelector.findEnclosingNode(offset, length);
|
||||
if (cand instanceof IASTPreprocessorIncludeStatement) {
|
||||
linkLocation= cand.getFileLocation();
|
||||
}
|
||||
}
|
||||
if (linkLocation != null) {
|
||||
result[0]= new CElementHyperlink(
|
||||
new Region(linkLocation.getNodeOffset(), linkLocation.getNodeLength()), openAction);
|
||||
}
|
||||
}
|
||||
});
|
||||
if (!status.isOK()) {
|
||||
CUIPlugin.getDefault().log(status);
|
||||
return Status.OK_STATUS;
|
||||
}
|
||||
} finally {
|
||||
index.releaseReadLock();
|
||||
});
|
||||
if (!status.isOK()) {
|
||||
CUIPlugin.log(status);
|
||||
}
|
||||
|
||||
return result[0] == null ? null : result;
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package org.eclipse.cdt.internal.ui.text;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
|
@ -40,7 +41,6 @@ import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
|
|||
import org.eclipse.jface.text.contentassist.IContentAssistant;
|
||||
import org.eclipse.jface.text.formatter.IContentFormatter;
|
||||
import org.eclipse.jface.text.formatter.MultiPassContentFormatter;
|
||||
import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
|
||||
import org.eclipse.jface.text.information.IInformationPresenter;
|
||||
import org.eclipse.jface.text.information.IInformationProvider;
|
||||
import org.eclipse.jface.text.information.InformationPresenter;
|
||||
|
@ -62,7 +62,6 @@ import org.eclipse.ui.IPathEditorInput;
|
|||
import org.eclipse.ui.editors.text.ILocationProvider;
|
||||
import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
|
||||
import org.eclipse.ui.ide.ResourceUtil;
|
||||
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
|
||||
import org.eclipse.ui.texteditor.IDocumentProvider;
|
||||
import org.eclipse.ui.texteditor.ITextEditor;
|
||||
|
||||
|
@ -89,7 +88,6 @@ import org.eclipse.cdt.ui.text.doctools.IDocCommentViewerConfiguration;
|
|||
import org.eclipse.cdt.internal.corext.util.CodeFormatterUtil;
|
||||
|
||||
import org.eclipse.cdt.internal.ui.editor.CDocumentProvider;
|
||||
import org.eclipse.cdt.internal.ui.editor.CElementHyperlinkDetector;
|
||||
import org.eclipse.cdt.internal.ui.text.c.hover.CEditorTextHoverDescriptor;
|
||||
import org.eclipse.cdt.internal.ui.text.c.hover.CEditorTextHoverProxy;
|
||||
import org.eclipse.cdt.internal.ui.text.c.hover.CInformationProvider;
|
||||
|
@ -810,29 +808,6 @@ public class CSourceViewerConfiguration extends TextSourceViewerConfiguration {
|
|||
fPreprocessorScanner.adaptToPreferenceChange(event);
|
||||
}
|
||||
|
||||
/*
|
||||
* @see org.eclipse.jface.text.source.SourceViewerConfiguration#getHyperlinkDetectors(org.eclipse.jface.text.source.ISourceViewer)
|
||||
* @since 3.1
|
||||
*/
|
||||
public IHyperlinkDetector[] getHyperlinkDetectors(ISourceViewer sourceViewer) {
|
||||
if (!fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINKS_ENABLED))
|
||||
return null;
|
||||
|
||||
IHyperlinkDetector[] inheritedDetectors= super.getHyperlinkDetectors(sourceViewer);
|
||||
|
||||
if (fTextEditor == null)
|
||||
return inheritedDetectors;
|
||||
|
||||
int inheritedDetectorsLength= inheritedDetectors != null ? inheritedDetectors.length : 0;
|
||||
IHyperlinkDetector[] detectors= new IHyperlinkDetector[inheritedDetectorsLength + 1];
|
||||
detectors[0]= new CElementHyperlinkDetector(fTextEditor);
|
||||
for (int i= 0; i < inheritedDetectorsLength; i++) {
|
||||
detectors[i+1]= inheritedDetectors[i];
|
||||
}
|
||||
|
||||
return detectors;
|
||||
}
|
||||
|
||||
/*
|
||||
* @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredDocumentPartitioning(org.eclipse.jface.text.source.ISourceViewer)
|
||||
*/
|
||||
|
@ -887,7 +862,7 @@ public class CSourceViewerConfiguration extends TextSourceViewerConfiguration {
|
|||
try {
|
||||
return ((ITranslationUnit)element).getLanguage();
|
||||
} catch (CoreException e) {
|
||||
CUIPlugin.getDefault().log(e);
|
||||
CUIPlugin.log(e);
|
||||
}
|
||||
} else {
|
||||
// compute the language from the plain editor input
|
||||
|
@ -978,4 +953,14 @@ public class CSourceViewerConfiguration extends TextSourceViewerConfiguration {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
* @see org.eclipse.jface.text.source.SourceViewerConfiguration#getHyperlinkDetectorTargets(org.eclipse.jface.text.source.ISourceViewer)
|
||||
*/
|
||||
protected Map getHyperlinkDetectorTargets(ISourceViewer sourceViewer) {
|
||||
Map targets= super.getHyperlinkDetectorTargets(sourceViewer);
|
||||
targets.put("org.eclipse.cdt.ui.cCode", fTextEditor); //$NON-NLS-1$
|
||||
return targets;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue