From d5663f513447a7908b5af7044c29f2788ba02e88 Mon Sep 17 00:00:00 2001 From: John Camelon Date: Tue, 3 May 2005 18:48:24 +0000 Subject: [PATCH] Patch for Devin Steffler. FIXED 72716- [Search] Search actions in "magic" include files do not work. --- .../cdt/core/search/DOMSearchUtil.java | 85 +++++++++++++++- .../eclipse/cdt/ui/tests/DOMAST/DOMAST.java | 9 ++ .../ui/search/actions/FindAction.java | 98 ++++++++++++------- .../ui/search/actions/FindRefsAction.java | 3 - .../actions/OpenDeclarationsAction.java | 67 ++++++++----- .../search/actions/OpenDefinitionAction.java | 67 ++++++++----- 6 files changed, 238 insertions(+), 91 deletions(-) diff --git a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/DOMSearchUtil.java b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/DOMSearchUtil.java index ebcf7003a8e..d4d3556e049 100644 --- a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/DOMSearchUtil.java +++ b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/DOMSearchUtil.java @@ -50,6 +50,7 @@ import org.eclipse.cdt.core.search.ICSearchConstants.SearchFor; import org.eclipse.cdt.internal.core.search.matching.CSearchPattern; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.IPath; /** * Utility class to have commonly used algorithms in one place for searching with the DOM. @@ -171,7 +172,75 @@ public class DOMSearchUtil { } /** - * This is used to get a List of selected names in an IFile based on the offset and length into that IFile. + * This is used to get an array of selected names in an IASTTranslationUnit based on the offset + * and length into that IASTTranslationUnit. + * + * ex: IASTTranslationUnit contains: int foo; + * then getSelectedNamesFrom(file, 4, 3) will return the IASTName corresponding to foo + * + * @param tu + * @param offset + * @param length + * @param lang + * @return + */ + public static IASTName[] getSelectedNamesFrom(IASTTranslationUnit tu, int offset, int length, ParserLanguage lang) { + IASTNode node = null; + try{ + node = tu.selectNodeForLocation(tu.getFilePath(), offset, length); // TODO Devin untested tu.getContainingFilename() ... + } + catch (ParseError er){} + catch ( VirtualMachineError vmErr){ + if (vmErr instanceof OutOfMemoryError){ + org.eclipse.cdt.internal.core.model.Util.log(null, "Open Declarations Out Of Memory error: " + vmErr.getMessage() + " on File: " + tu.getContainingFilename(), ICLogConstants.CDT); //$NON-NLS-1$ //$NON-NLS-2$ + } + } + catch (Exception ex){} + + finally{ + if (node == null){ + return EMPTY_NAME_LIST; + } + } + + if (node instanceof IASTName) { + IASTName[] results = new IASTName[1]; + results[0] = (IASTName)node; + return results; + } + + ASTVisitor collector = null; + if (lang == ParserLanguage.CPP) { + collector = new CPPNameCollector(); + } else { + collector = new CNameCollector(); + } + + node.accept( collector ); + + List names = null; + if (collector instanceof CPPNameCollector) { + names = ((CPPNameCollector)collector).nameList; + } else { + names = ((CNameCollector)collector).nameList; + } + + IASTName[] results = new IASTName[names.size()]; + for(int i=0; i 0 && selectedNames[0] != null) { // just right, only one name selected IASTName searchName = selectedNames[0]; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDefinitionAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDefinitionAction.java index a27f7f7a40b..7a7f758ad51 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDefinitionAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDefinitionAction.java @@ -13,9 +13,15 @@ package org.eclipse.cdt.internal.ui.search.actions; import java.util.Iterator; import java.util.Set; +import org.eclipse.cdt.core.dom.CDOM; +import org.eclipse.cdt.core.dom.IASTServiceProvider; +import org.eclipse.cdt.core.dom.IASTServiceProvider.UnsupportedDialectException; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.parser.ParserLanguage; import org.eclipse.cdt.core.parser.ParserUtil; import org.eclipse.cdt.core.search.DOMSearchUtil; import org.eclipse.cdt.core.search.ICSearchConstants; @@ -24,12 +30,10 @@ import org.eclipse.cdt.core.search.SearchEngine; import org.eclipse.cdt.internal.core.model.CProject; import org.eclipse.cdt.internal.ui.editor.CEditor; import org.eclipse.cdt.internal.ui.editor.CEditorMessages; -import org.eclipse.cdt.internal.ui.editor.ITranslationUnitEditorInput; import org.eclipse.cdt.internal.ui.util.ExternalEditorInput; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.dialogs.ProgressMonitorDialog; @@ -91,34 +95,47 @@ public class OpenDefinitionAction extends SelectionParseAction implements int selectionStart = selNode.selStart; int selectionLength = selNode.selEnd - selNode.selStart; - IFile resourceFile = null; - IASTName[] selectedNames = BLANK_NAME_ARRAY; - if (fEditor.getEditorInput() instanceof ExternalEditorInput) { - if( fEditor.getEditorInput() instanceof ITranslationUnitEditorInput ) - { - ITranslationUnitEditorInput ip = (ITranslationUnitEditorInput) fEditor.getEditorInput(); - IResource r = ip.getTranslationUnit().getUnderlyingResource(); - if( r.getType() == IResource.FILE ) - resourceFile = (IFile) r; - else - { - operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE); - return; - - } - } - } - else - resourceFile = fEditor.getInputFile(); + IASTTranslationUnit tu=null; + ParserLanguage lang=null; - - if (resourceFile != null) + if (fEditor.getEditorInput() instanceof ExternalEditorInput) { + ExternalEditorInput input = (ExternalEditorInput)fEditor.getEditorInput(); + try { + // get the project for the external editor input's translation unit + ICElement project = input.getTranslationUnit(); + while (!(project instanceof ICProject) && project != null) { + project = project.getParent(); + } + + if (project instanceof ICProject) { + tu = CDOM.getInstance().getASTService().getTranslationUnit(input.getStorage(), ((ICProject)project).getProject()); + lang = DOMSearchUtil.getLanguage(input.getStorage().getFullPath(), ((ICProject)project).getProject()); + projectName = ((ICProject)project).getElementName(); + } + } catch (UnsupportedDialectException e) { + operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE); + return; + } + } else { + IFile resourceFile = null; + resourceFile = fEditor.getInputFile(); + + try { + tu = CDOM.getInstance().getASTService().getTranslationUnit( + resourceFile, + CDOM.getInstance().getCodeReaderFactory( + CDOM.PARSE_WORKING_COPY_WHENEVER_POSSIBLE)); + } catch (IASTServiceProvider.UnsupportedDialectException e) { + operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE); + return; + } + lang = DOMSearchUtil.getLanguageFromFile(resourceFile); projectName = findProjectName(resourceFile); + } // step 1 starts here - if (resourceFile != null) - selectedNames = DOMSearchUtil.getSelectedNamesFrom(resourceFile, selectionStart, selectionLength); + selectedNames = DOMSearchUtil.getSelectedNamesFrom(tu, selectionStart, selectionLength, lang); if (selectedNames.length > 0 && selectedNames[0] != null) { // just right, only one name selected IASTName searchName = selectedNames[0];