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.editor.SourceHeaderPartnerFinder;
|
||||||
import org.eclipse.cdt.internal.ui.refactoring.RefactoringASTCache;
|
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;
|
import org.eclipse.cdt.internal.ui.refactoring.utils.NodeHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -72,7 +72,7 @@ public class MethodDefinitionInsertLocationFinder {
|
||||||
if (cachedDeclarationToDefinition.containsKey(simpleDeclaration)) {
|
if (cachedDeclarationToDefinition.containsKey(simpleDeclaration)) {
|
||||||
definition = cachedDeclarationToDefinition.get(simpleDeclaration);
|
definition = cachedDeclarationToDefinition.get(simpleDeclaration);
|
||||||
} else {
|
} else {
|
||||||
definition = DefinitionFinder2.getDefinition(simpleDeclaration, astCache, pm);
|
definition = DefinitionFinder.getDefinition(simpleDeclaration, astCache, pm);
|
||||||
if (definition != null) {
|
if (definition != null) {
|
||||||
cachedDeclarationToDefinition.put(simpleDeclaration, definition);
|
cachedDeclarationToDefinition.put(simpleDeclaration, definition);
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ public class MethodDefinitionInsertLocationFinder {
|
||||||
if (cachedDeclarationToDefinition.containsKey(simpleDeclaration)) {
|
if (cachedDeclarationToDefinition.containsKey(simpleDeclaration)) {
|
||||||
definition = cachedDeclarationToDefinition.get(simpleDeclaration);
|
definition = cachedDeclarationToDefinition.get(simpleDeclaration);
|
||||||
} else {
|
} else {
|
||||||
definition = DefinitionFinder2.getDefinition(simpleDeclaration, astCache, pm);
|
definition = DefinitionFinder.getDefinition(simpleDeclaration, astCache, pm);
|
||||||
if (definition != null) {
|
if (definition != null) {
|
||||||
cachedDeclarationToDefinition.put(simpleDeclaration, definition);
|
cachedDeclarationToDefinition.put(simpleDeclaration, definition);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,110 +1,108 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2008, 2009 Institute for Software, HSR Hochschule fuer Technik
|
* Copyright (c) 2011 Google, Inc and others.
|
||||||
* Rapperswil, University of applied sciences and others
|
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
*
|
*
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* Institute for Software - initial API and implementation
|
* Sergey Prigogin (Google) - initial API and implementation
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.ui.refactoring.utils;
|
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.CoreException;
|
||||||
import org.eclipse.core.runtime.IStatus;
|
import org.eclipse.core.runtime.IProgressMonitor;
|
||||||
import org.eclipse.core.runtime.Path;
|
import org.eclipse.core.runtime.OperationCanceledException;
|
||||||
import org.eclipse.core.runtime.Status;
|
import org.eclipse.ui.IEditorInput;
|
||||||
|
import org.eclipse.ui.IEditorPart;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.CCorePlugin;
|
import org.eclipse.cdt.core.dom.IName;
|
||||||
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
|
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
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.IASTSimpleDeclaration;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
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.IIndex;
|
||||||
|
import org.eclipse.cdt.core.index.IIndexBinding;
|
||||||
import org.eclipse.cdt.core.index.IIndexName;
|
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.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.
|
* Helper class to find definitions. This class is intended as a replacement for DefinitionFinder.
|
||||||
*
|
|
||||||
* @author Lukas Felber
|
|
||||||
*/
|
*/
|
||||||
public class DefinitionFinder {
|
public class DefinitionFinder {
|
||||||
|
|
||||||
public static IASTName getDefinition(IASTSimpleDeclaration simpleDeclaration, IFile file)
|
public static IASTName getDefinition(IASTSimpleDeclaration simpleDeclaration,
|
||||||
throws CoreException{
|
RefactoringASTCache astCache, IProgressMonitor pm) throws CoreException {
|
||||||
|
IIndex index = astCache.getIndex();
|
||||||
IASTDeclarator declarator = simpleDeclaration.getDeclarators()[0];
|
IASTDeclarator declarator = simpleDeclaration.getDeclarators()[0];
|
||||||
IBinding resolveBinding = declarator.getName().resolveBinding();
|
if (index == null) {
|
||||||
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);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
IIndexBinding binding = index.adaptBinding(declarator.getName().resolveBinding());
|
||||||
pdomref= index.findDefinitions(bind);
|
if (binding == null) {
|
||||||
} finally {
|
|
||||||
index.releaseReadLock();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pdomref == null || pdomref.length < 1) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
return getDefinition(binding, astCache, index, pm);
|
||||||
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]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IASTName findDefinitionInTranslationUnit(IASTTranslationUnit transUnit,
|
private static IASTName getDefinition(IIndexBinding binding,
|
||||||
final IIndexName indexName) {
|
RefactoringASTCache astCache, IIndex index, IProgressMonitor pm) throws CoreException {
|
||||||
final Container<IASTName> defName = new Container<IASTName>();
|
Set<String> searchedFiles = new HashSet<String>();
|
||||||
transUnit.accept(new ASTVisitor() {
|
List<IASTName> definitions = new ArrayList<IASTName>();
|
||||||
{
|
IEditorPart[] dirtyEditors = EditorUtility.getDirtyEditors(true);
|
||||||
shouldVisitNames = 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
|
IIndexName[] definitionsFromIndex = index.findDefinitions(binding);
|
||||||
public int visit(IASTName name) {
|
for (IIndexName name : definitionsFromIndex) {
|
||||||
if (name.isDefinition() && name.getNodeLocations().length > 0) {
|
if (pm != null && pm.isCanceled()) {
|
||||||
IASTNodeLocation nodeLocation = name.getNodeLocations()[0];
|
throw new OperationCanceledException();
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
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 definitions.size() == 1 ? definitions.get(0) : null;
|
||||||
return defName.getObject();
|
}
|
||||||
|
|
||||||
|
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