From ce35f1444b7d2cc9ee6b379c3a6447a39f52d206 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Fri, 13 Oct 2006 09:51:12 +0000 Subject: [PATCH] Ported index-view actions to the new api --- .../ui/indexview/FindDeclarationsAction.java | 19 ++-- .../ui/indexview/FindReferencesAction.java | 8 +- .../ui/indexview/OpenDefinitionAction.java | 105 ++++++++++-------- 3 files changed, 71 insertions(+), 61 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/indexview/FindDeclarationsAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/indexview/FindDeclarationsAction.java index e8e375f0ca5..5eeb79baefe 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/indexview/FindDeclarationsAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/indexview/FindDeclarationsAction.java @@ -11,16 +11,17 @@ package org.eclipse.cdt.internal.ui.indexview; -import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding; -import org.eclipse.cdt.internal.ui.search.PDOMSearchBindingQuery; -import org.eclipse.cdt.internal.ui.search.PDOMSearchQuery; - -import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.search.ui.NewSearchUI; +import org.eclipse.cdt.core.index.IIndexBinding; +import org.eclipse.cdt.ui.CUIPlugin; + +import org.eclipse.cdt.internal.ui.search.PDOMSearchBindingQuery; +import org.eclipse.cdt.internal.ui.search.PDOMSearchQuery; + /** * @author Doug Schaefer * @@ -31,17 +32,17 @@ public class FindDeclarationsAction extends IndexAction { super(viewer, CUIPlugin.getResourceString("IndexView.findDeclarations.name")); //$NON-NLS-1$ } - private PDOMBinding getBinding() { + private IIndexBinding getBinding() { ISelection selection = viewer.getSelection(); if (!(selection instanceof IStructuredSelection)) return null; Object[] objs = ((IStructuredSelection)selection).toArray(); - return (objs.length == 1 && objs[0] instanceof PDOMBinding) - ? (PDOMBinding)objs[0] : null; + return (objs.length == 1 && objs[0] instanceof IIndexBinding) + ? (IIndexBinding)objs[0] : null; } public void run() { - PDOMBinding binding = getBinding(); + IIndexBinding binding = getBinding(); PDOMSearchBindingQuery query = new PDOMSearchBindingQuery( null, binding, diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/indexview/FindReferencesAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/indexview/FindReferencesAction.java index 6cd7803c2d9..0bce71a5607 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/indexview/FindReferencesAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/indexview/FindReferencesAction.java @@ -11,10 +11,10 @@ package org.eclipse.cdt.internal.ui.indexview; -import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding; import org.eclipse.cdt.internal.ui.search.PDOMSearchBindingQuery; import org.eclipse.cdt.internal.ui.search.PDOMSearchQuery; +import org.eclipse.cdt.core.index.IIndexBinding; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; @@ -31,13 +31,13 @@ public class FindReferencesAction extends IndexAction { super(viewer, CUIPlugin.getResourceString("IndexView.findReferences.name")); //$NON-NLS-1$ } - private PDOMBinding getBinding() { + private IIndexBinding getBinding() { ISelection selection = viewer.getSelection(); if (!(selection instanceof IStructuredSelection)) return null; Object[] objs = ((IStructuredSelection)selection).toArray(); - return (objs.length == 1 && objs[0] instanceof PDOMBinding) - ? (PDOMBinding)objs[0] : null; + return (objs.length == 1 && objs[0] instanceof IIndexBinding) + ? (IIndexBinding)objs[0] : null; } public void run() { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/indexview/OpenDefinitionAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/indexview/OpenDefinitionAction.java index fa4d8b5bb20..13b8d647e02 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/indexview/OpenDefinitionAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/indexview/OpenDefinitionAction.java @@ -11,12 +11,6 @@ package org.eclipse.cdt.internal.ui.indexview; -import org.eclipse.cdt.core.dom.ast.IASTFileLocation; -import org.eclipse.cdt.core.resources.FileStorage; -import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding; -import org.eclipse.cdt.internal.core.pdom.dom.PDOMName; -import org.eclipse.cdt.internal.ui.util.EditorUtility; -import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; @@ -27,8 +21,20 @@ import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.PartInitException; import org.eclipse.ui.texteditor.ITextEditor; +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.index.IIndex; +import org.eclipse.cdt.core.index.IIndexBinding; +import org.eclipse.cdt.core.index.IIndexName; +import org.eclipse.cdt.core.model.CModelException; +import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.resources.FileStorage; +import org.eclipse.cdt.ui.CUIPlugin; + +import org.eclipse.cdt.internal.ui.util.EditorUtility; + /** * @author Doug Schaefer * @@ -44,50 +50,53 @@ public class OpenDefinitionAction extends IndexAction { if (!(selection instanceof IStructuredSelection)) return; - Object[] objs = ((IStructuredSelection)selection).toArray(); - for (int i = 0; i < objs.length; ++i) { - if (!(objs[i] instanceof PDOMBinding)) - continue; - - try { - PDOMBinding binding = (PDOMBinding)objs[i]; - PDOMName name = binding.getFirstDefinition(); - if (name == null) - name = binding.getFirstDeclaration(); - if (name == null) + try { + IIndex index= CCorePlugin.getIndexManager().getIndex(CoreModel.getDefault().getCModel().getCProjects()); + Object[] objs = ((IStructuredSelection)selection).toArray(); + for (int i = 0; i < objs.length; ++i) { + if (!(objs[i] instanceof IIndexBinding)) continue; - - IASTFileLocation location = name.getFileLocation(); - IPath path = new Path(location.getFileName()); - Object input = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path); - if (input == null) - input = new FileStorage(path); - - IEditorPart editor = EditorUtility.openInEditor(input); - if (editor != null && editor instanceof ITextEditor) { - ITextEditor textEditor = (ITextEditor)editor; - int nodeOffset = location.getNodeOffset(); - int nodeLength = location.getNodeLength(); - int offset; - int length; - if (nodeLength == -1) { - // This means the offset is actually a line number - try { - IDocument document = textEditor.getDocumentProvider().getDocument(editor.getEditorInput()); - offset = document.getLineOffset(nodeOffset); - length = document.getLineLength(nodeOffset); - } catch (BadLocationException e) { - CUIPlugin.getDefault().log(e); - return; - } - } else { - offset = nodeOffset; - length = nodeLength; + + index.acquireReadLock(); + try { + IIndexBinding binding = (IIndexBinding)objs[i]; + IIndexName[] defs= index.findDefinitions(binding); + for (int j = 0; j < defs.length; j++) { + IIndexName name = defs[j]; + showInEditor(name); } - - textEditor.selectAndReveal(offset, length); + } finally { + index.releaseReadLock(); } - } catch (CoreException e) { + } + } + catch (CoreException e) { + CUIPlugin.getDefault().log(e); + } + catch (InterruptedException e) { + } + } + + private void showInEditor(IIndexName name) throws CModelException, PartInitException { + IPath path = new Path(name.getFileName()); + Object input = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path); + if (input == null) + input = new FileStorage(path); + + IEditorPart editor = EditorUtility.openInEditor(input); + if (editor != null && editor instanceof ITextEditor) { + ITextEditor textEditor = (ITextEditor)editor; + int nodeOffset = name.getNodeOffset(); + int nodeLength = name.getNodeLength(); + try { + if (nodeLength == -1) { + // This means the offset is actually a line number + IDocument document = textEditor.getDocumentProvider().getDocument(editor.getEditorInput()); + nodeOffset = document.getLineOffset(nodeOffset); + nodeLength = document.getLineLength(nodeOffset); + } + textEditor.selectAndReveal(nodeOffset, nodeLength); + } catch (BadLocationException e) { CUIPlugin.getDefault().log(e); } } @@ -99,7 +108,7 @@ public class OpenDefinitionAction extends IndexAction { return false; Object[] objs = ((IStructuredSelection)selection).toArray(); for (int i = 0; i < objs.length; ++i) - if (objs[i] instanceof PDOMBinding) + if (objs[i] instanceof IIndexBinding) return true; return false; }