diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/MethodDefinitionInsertLocationFinder.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/MethodDefinitionInsertLocationFinder.java index adbb6d4fed5..8bb794931a9 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/MethodDefinitionInsertLocationFinder.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/MethodDefinitionInsertLocationFinder.java @@ -35,7 +35,7 @@ import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.internal.ui.editor.SourceHeaderPartnerFinder; import org.eclipse.cdt.internal.ui.refactoring.RefactoringASTCache; -import org.eclipse.cdt.internal.ui.refactoring.utils.DefinitionFinder2; +import org.eclipse.cdt.internal.ui.refactoring.utils.DefinitionFinder; import org.eclipse.cdt.internal.ui.refactoring.utils.NodeHelper; /** @@ -72,7 +72,7 @@ public class MethodDefinitionInsertLocationFinder { if (cachedDeclarationToDefinition.containsKey(simpleDeclaration)) { definition = cachedDeclarationToDefinition.get(simpleDeclaration); } else { - definition = DefinitionFinder2.getDefinition(simpleDeclaration, astCache, pm); + definition = DefinitionFinder.getDefinition(simpleDeclaration, astCache, pm); if (definition != null) { cachedDeclarationToDefinition.put(simpleDeclaration, definition); } @@ -93,7 +93,7 @@ public class MethodDefinitionInsertLocationFinder { if (cachedDeclarationToDefinition.containsKey(simpleDeclaration)) { definition = cachedDeclarationToDefinition.get(simpleDeclaration); } else { - definition = DefinitionFinder2.getDefinition(simpleDeclaration, astCache, pm); + definition = DefinitionFinder.getDefinition(simpleDeclaration, astCache, pm); if (definition != null) { cachedDeclarationToDefinition.put(simpleDeclaration, definition); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/DefinitionFinder.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/DefinitionFinder.java index 7e290131b98..3f2b19739c0 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/DefinitionFinder.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/DefinitionFinder.java @@ -1,110 +1,108 @@ /******************************************************************************* - * Copyright (c) 2008, 2009 Institute for Software, HSR Hochschule fuer Technik - * Rapperswil, University of applied sciences 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 - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Institute for Software - initial API and implementation + * Copyright (c) 2011 Google, Inc 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Sergey Prigogin (Google) - initial API and implementation *******************************************************************************/ package org.eclipse.cdt.internal.ui.refactoring.utils; -import java.util.TreeMap; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; -import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Path; -import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorPart; -import org.eclipse.cdt.core.CCorePlugin; -import org.eclipse.cdt.core.dom.ast.ASTVisitor; +import org.eclipse.cdt.core.dom.IName; import org.eclipse.cdt.core.dom.ast.IASTDeclarator; import org.eclipse.cdt.core.dom.ast.IASTName; -import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; -import org.eclipse.cdt.core.dom.ast.IBinding; 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.CoreModelUtil; import org.eclipse.cdt.core.model.ITranslationUnit; -import org.eclipse.cdt.ui.CUIPlugin; -import org.eclipse.cdt.internal.ui.refactoring.Container; +import org.eclipse.cdt.internal.corext.util.CModelUtil; + +import org.eclipse.cdt.internal.ui.editor.ITranslationUnitEditorInput; +import org.eclipse.cdt.internal.ui.refactoring.RefactoringASTCache; +import org.eclipse.cdt.internal.ui.util.EditorUtility; /** - * Helper class to find definitions. - * - * @author Lukas Felber + * Helper class to find definitions. This class is intended as a replacement for DefinitionFinder. */ public class DefinitionFinder { - public static IASTName getDefinition(IASTSimpleDeclaration simpleDeclaration, IFile file) - throws CoreException{ + public static IASTName getDefinition(IASTSimpleDeclaration simpleDeclaration, + RefactoringASTCache astCache, IProgressMonitor pm) throws CoreException { + IIndex index = astCache.getIndex(); IASTDeclarator declarator = simpleDeclaration.getDeclarators()[0]; - IBinding resolveBinding = declarator.getName().resolveBinding(); - return DefinitionFinder.getDefinition(declarator.getName(), resolveBinding, file); - } - - public static IASTName getDefinition(IASTName methodName, IBinding bind, IFile file) throws CoreException { - TreeMap parsedFiles = new TreeMap(); - - ITranslationUnit tu = (ITranslationUnit) CCorePlugin.getDefault().getCoreModel().create(file); - IIndex index = CCorePlugin.getIndexManager().getIndex(tu.getCProject()); - IIndexName[] pdomref = null; - - try { - index.acquireReadLock(); - } catch (InterruptedException e) { - IStatus status = new Status(IStatus.WARNING, CUIPlugin.PLUGIN_ID, IStatus.OK, e.getMessage(), e); - CUIPlugin.log(status); - return null; - } - try { - pdomref= index.findDefinitions(bind); - } finally { - index.releaseReadLock(); - } - - if (pdomref == null || pdomref.length < 1) { + if (index == null) { return null; } - - IASTTranslationUnit transUnit; - if (!parsedFiles.containsKey(pdomref[0].getFileLocation().getFileName())) { - String filename = pdomref[0].getFileLocation().getFileName(); - transUnit = TranslationUnitHelper.loadTranslationUnit(filename, false); - } else { - transUnit = parsedFiles.get(pdomref[0].getFileLocation().getFileName()); + IIndexBinding binding = index.adaptBinding(declarator.getName().resolveBinding()); + if (binding == null) { + return null; } - return findDefinitionInTranslationUnit(transUnit, pdomref[0]); + return getDefinition(binding, astCache, index, pm); } - private static IASTName findDefinitionInTranslationUnit(IASTTranslationUnit transUnit, - final IIndexName indexName) { - final Container defName = new Container(); - transUnit.accept(new ASTVisitor() { - { - shouldVisitNames = true; + private static IASTName getDefinition(IIndexBinding binding, + RefactoringASTCache astCache, IIndex index, IProgressMonitor pm) throws CoreException { + Set searchedFiles = new HashSet(); + List definitions = new ArrayList(); + IEditorPart[] dirtyEditors = EditorUtility.getDirtyEditors(true); + for (IEditorPart editor : dirtyEditors) { + if (pm != null && pm.isCanceled()) { + throw new OperationCanceledException(); } - - @Override - public int visit(IASTName name) { - if (name.isDefinition() && name.getNodeLocations().length > 0) { - IASTNodeLocation nodeLocation = name.getNodeLocations()[0]; - if (indexName.getNodeOffset() == nodeLocation.getNodeOffset() - && indexName.getNodeLength() == nodeLocation.getNodeLength() - && new Path(indexName.getFileLocation().getFileName()).equals(new Path(nodeLocation.asFileLocation().getFileName()))) { - defName.setObject(name); - return ASTVisitor.PROCESS_ABORT; - } - } - return ASTVisitor.PROCESS_CONTINUE; + IEditorInput editorInput = editor.getEditorInput(); + if (editorInput instanceof ITranslationUnitEditorInput) { + ITranslationUnit tu = + CModelUtil.toWorkingCopy(((ITranslationUnitEditorInput) editorInput).getTranslationUnit()); + findDefinitionsInTranslationUnit(binding, tu, astCache, definitions, null); + searchedFiles.add(tu.getLocation().toOSString()); } + } + + IIndexName[] definitionsFromIndex = index.findDefinitions(binding); + for (IIndexName name : definitionsFromIndex) { + if (pm != null && pm.isCanceled()) { + throw new OperationCanceledException(); + } + ITranslationUnit tu = CoreModelUtil.findTranslationUnitForLocation( + name.getFile().getLocation(), null); + if (searchedFiles.add(tu.getLocation().toOSString())) { + findDefinitionsInTranslationUnit(binding, tu, astCache, definitions, pm); + } + } - }); - return defName.getObject(); + return definitions.size() == 1 ? definitions.get(0) : null; + } + + private static void findDefinitionsInTranslationUnit(IIndexBinding binding, ITranslationUnit tu, + RefactoringASTCache astCache, List definitions, IProgressMonitor pm) + throws OperationCanceledException, CoreException { + IASTTranslationUnit ast = astCache.getAST(tu, pm); + findDefinitionsInAST(binding, ast, tu, definitions); + } + + private static void findDefinitionsInAST(IIndexBinding binding, IASTTranslationUnit ast, + ITranslationUnit tu, List definitions) { + for (IName definition : ast.getDefinitions(binding)) { + if (definition instanceof IASTName) { + definitions.add((IASTName) definition); + } + } } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/DefinitionFinder2.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/DefinitionFinder2.java deleted file mode 100644 index ec9fe02dfb5..00000000000 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/DefinitionFinder2.java +++ /dev/null @@ -1,108 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2011 Google, Inc 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 - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Sergey Prigogin (Google) - initial API and implementation - *******************************************************************************/ -package org.eclipse.cdt.internal.ui.refactoring.utils; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.OperationCanceledException; -import org.eclipse.ui.IEditorInput; -import org.eclipse.ui.IEditorPart; - -import org.eclipse.cdt.core.dom.IName; -import org.eclipse.cdt.core.dom.ast.IASTDeclarator; -import org.eclipse.cdt.core.dom.ast.IASTName; -import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; -import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; -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.CoreModelUtil; -import org.eclipse.cdt.core.model.ITranslationUnit; - -import org.eclipse.cdt.internal.corext.util.CModelUtil; - -import org.eclipse.cdt.internal.ui.editor.ITranslationUnitEditorInput; -import org.eclipse.cdt.internal.ui.refactoring.RefactoringASTCache; -import org.eclipse.cdt.internal.ui.util.EditorUtility; - -/** - * Helper class to find definitions. This class is intended as a replacement for DefinitionFinder. - */ -public class DefinitionFinder2 { - - public static IASTName getDefinition(IASTSimpleDeclaration simpleDeclaration, - RefactoringASTCache astCache, IProgressMonitor pm) throws CoreException { - IIndex index = astCache.getIndex(); - IASTDeclarator declarator = simpleDeclaration.getDeclarators()[0]; - if (index == null) { - return null; - } - IIndexBinding binding = index.adaptBinding(declarator.getName().resolveBinding()); - if (binding == null) { - return null; - } - return getDefinition(binding, astCache, index, pm); - } - - private static IASTName getDefinition(IIndexBinding binding, - RefactoringASTCache astCache, IIndex index, IProgressMonitor pm) throws CoreException { - Set searchedFiles = new HashSet(); - List definitions = new ArrayList(); - IEditorPart[] dirtyEditors = EditorUtility.getDirtyEditors(true); - for (IEditorPart editor : dirtyEditors) { - if (pm != null && pm.isCanceled()) { - throw new OperationCanceledException(); - } - IEditorInput editorInput = editor.getEditorInput(); - if (editorInput instanceof ITranslationUnitEditorInput) { - ITranslationUnit tu = - CModelUtil.toWorkingCopy(((ITranslationUnitEditorInput) editorInput).getTranslationUnit()); - findDefinitionsInTranslationUnit(binding, tu, astCache, definitions, null); - searchedFiles.add(tu.getLocation().toOSString()); - } - } - - IIndexName[] definitionsFromIndex = index.findDefinitions(binding); - for (IIndexName name : definitionsFromIndex) { - if (pm != null && pm.isCanceled()) { - throw new OperationCanceledException(); - } - ITranslationUnit tu = CoreModelUtil.findTranslationUnitForLocation( - name.getFile().getLocation(), null); - if (searchedFiles.add(tu.getLocation().toOSString())) { - findDefinitionsInTranslationUnit(binding, tu, astCache, definitions, pm); - } - } - - return definitions.size() == 1 ? definitions.get(0) : null; - } - - private static void findDefinitionsInTranslationUnit(IIndexBinding binding, ITranslationUnit tu, - RefactoringASTCache astCache, List definitions, IProgressMonitor pm) - throws OperationCanceledException, CoreException { - IASTTranslationUnit ast = astCache.getAST(tu, pm); - findDefinitionsInAST(binding, ast, tu, definitions); - } - - private static void findDefinitionsInAST(IIndexBinding binding, IASTTranslationUnit ast, - ITranslationUnit tu, List definitions) { - for (IName definition : ast.getDefinitions(binding)) { - if (definition instanceof IASTName) { - definitions.add((IASTName) definition); - } - } - } -}