From 872058b5e565aeba0efb4e855d15e0466196048c Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Fri, 14 Mar 2008 13:38:14 +0000 Subject: [PATCH] 222728: [editor] Register C/C++ editor with common hyperlinking preferences --- core/org.eclipse.cdt.ui/plugin.properties | 6 +- core/org.eclipse.cdt.ui/plugin.xml | 22 +++- .../ui/editor/CElementHyperlinkDetector.java | 113 ++++++++---------- .../ui/text/CSourceViewerConfiguration.java | 41 ++----- 4 files changed, 90 insertions(+), 92 deletions(-) diff --git a/core/org.eclipse.cdt.ui/plugin.properties b/core/org.eclipse.cdt.ui/plugin.properties index d116fb1eb73..382666db8a2 100644 --- a/core/org.eclipse.cdt.ui/plugin.properties +++ b/core/org.eclipse.cdt.ui/plugin.properties @@ -474,4 +474,8 @@ OccurrenceAnnotation.label= C/C++ Occurrences DocCommentOwner.name = DocCommentOwner Doxygen.name = Doxygen -indexedFilesDecorator.label = C/C++ Indexed Files \ No newline at end of file +indexedFilesDecorator.label = C/C++ Indexed Files + +# Hyperlinking +cEditorHyperlinkTarget= C/C++ Editor +cElementHyperlinkDetector= C/C++ Elements diff --git a/core/org.eclipse.cdt.ui/plugin.xml b/core/org.eclipse.cdt.ui/plugin.xml index 435a5956b27..8d033b36da2 100644 --- a/core/org.eclipse.cdt.ui/plugin.xml +++ b/core/org.eclipse.cdt.ui/plugin.xml @@ -2677,4 +2677,24 @@ - + + + + + + + + + + + + + diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CElementHyperlinkDetector.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CElementHyperlinkDetector.java index 7826f90de56..1e30c8e987c 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CElementHyperlinkDetector.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CElementHyperlinkDetector.java @@ -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; + + // 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(fTextEditor.getEditorInput()); + 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); + else { + 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; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CSourceViewerConfiguration.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CSourceViewerConfiguration.java index 992a529285e..56dac58fc87 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CSourceViewerConfiguration.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CSourceViewerConfiguration.java @@ -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 { } }; } -} \ No newline at end of file + + /* + * @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; + } + +}