mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Renamed DefinitionFinder2 to DefinitionFinder.
This commit is contained in:
parent
0ae7869ae4
commit
f40f3e9288
3 changed files with 80 additions and 190 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -1,110 +1,108 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008, 2009 Institute for Software, HSR Hochschule fuer Technik
|
||||
* Rapperswil, University of applied sciences and others
|
||||
* 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:
|
||||
* Institute for Software - initial API and implementation
|
||||
* 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<String, IASTTranslationUnit> parsedFiles = new TreeMap<String, IASTTranslationUnit>();
|
||||
|
||||
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);
|
||||
if (index == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
pdomref= index.findDefinitions(bind);
|
||||
} finally {
|
||||
index.releaseReadLock();
|
||||
}
|
||||
|
||||
if (pdomref == null || pdomref.length < 1) {
|
||||
IIndexBinding binding = index.adaptBinding(declarator.getName().resolveBinding());
|
||||
if (binding == 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());
|
||||
}
|
||||
return findDefinitionInTranslationUnit(transUnit, pdomref[0]);
|
||||
return getDefinition(binding, astCache, index, pm);
|
||||
}
|
||||
|
||||
private static IASTName findDefinitionInTranslationUnit(IASTTranslationUnit transUnit,
|
||||
final IIndexName indexName) {
|
||||
final Container<IASTName> defName = new Container<IASTName>();
|
||||
transUnit.accept(new ASTVisitor() {
|
||||
{
|
||||
shouldVisitNames = true;
|
||||
private static IASTName getDefinition(IIndexBinding binding,
|
||||
RefactoringASTCache astCache, IIndex index, IProgressMonitor pm) throws CoreException {
|
||||
Set<String> searchedFiles = new HashSet<String>();
|
||||
List<IASTName> definitions = new ArrayList<IASTName>();
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
@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;
|
||||
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 ASTVisitor.PROCESS_CONTINUE;
|
||||
}
|
||||
|
||||
});
|
||||
return defName.getObject();
|
||||
return definitions.size() == 1 ? definitions.get(0) : null;
|
||||
}
|
||||
|
||||
private static void findDefinitionsInTranslationUnit(IIndexBinding binding, ITranslationUnit tu,
|
||||
RefactoringASTCache astCache, List<IASTName> 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<IASTName> definitions) {
|
||||
for (IName definition : ast.getDefinitions(binding)) {
|
||||
if (definition instanceof IASTName) {
|
||||
definitions.add((IASTName) definition);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> searchedFiles = new HashSet<String>();
|
||||
List<IASTName> definitions = new ArrayList<IASTName>();
|
||||
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<IASTName> 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<IASTName> definitions) {
|
||||
for (IName definition : ast.getDefinitions(binding)) {
|
||||
if (definition instanceof IASTName) {
|
||||
definitions.add((IASTName) definition);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue