diff options
author | Sergey Prigogin | 2011-02-19 21:38:56 +0000 |
---|---|---|
committer | Sergey Prigogin | 2011-02-19 21:38:56 +0000 |
commit | 3715cdbbe824aa84bccfbde2335e90ae7e09bef1 (patch) | |
tree | 49d6cba4c52ec701a02de3c70bd1767df45cc092 /core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ToggleSourceAndHeaderAction.java | |
parent | 29552b6bcfec6a69d5a9ef3a4a50991981097533 (diff) | |
download | org.eclipse.cdt-3715cdbbe824aa84bccfbde2335e90ae7e09bef1.tar.gz org.eclipse.cdt-3715cdbbe824aa84bccfbde2335e90ae7e09bef1.tar.xz org.eclipse.cdt-3715cdbbe824aa84bccfbde2335e90ae7e09bef1.zip |
Bug 337040 - MethodDefinitionInsertLocationFinder2 does not find implementation file when no definition already present. Patch by Marc-Andre Laperle.
Diffstat (limited to 'core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ToggleSourceAndHeaderAction.java')
-rw-r--r-- | core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ToggleSourceAndHeaderAction.java | 272 |
1 files changed, 1 insertions, 271 deletions
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ToggleSourceAndHeaderAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ToggleSourceAndHeaderAction.java index 9a4261ab0cb..beafb9ea386 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ToggleSourceAndHeaderAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ToggleSourceAndHeaderAction.java @@ -12,53 +12,20 @@ package org.eclipse.cdt.internal.ui.editor; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; import java.util.ResourceBundle; -import org.eclipse.core.resources.IContainer; -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.resources.IResourceProxy; -import org.eclipse.core.resources.IResourceProxyVisitor; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.core.runtime.Path; -import org.eclipse.core.runtime.Platform; -import org.eclipse.core.runtime.Status; -import org.eclipse.core.runtime.content.IContentType; -import org.eclipse.core.runtime.content.IContentTypeManager; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.PartInitException; import org.eclipse.ui.texteditor.ITextEditor; import org.eclipse.ui.texteditor.TextEditorAction; -import org.eclipse.cdt.core.CCorePlugin; -import org.eclipse.cdt.core.dom.ast.ASTVisitor; -import org.eclipse.cdt.core.dom.ast.IASTDeclarator; -import org.eclipse.cdt.core.dom.ast.IASTFileLocation; -import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator; -import org.eclipse.cdt.core.dom.ast.IASTName; -import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; -import org.eclipse.cdt.core.dom.ast.IBinding; -import org.eclipse.cdt.core.dom.ast.IProblemBinding; -import org.eclipse.cdt.core.index.IIndex; -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.model.ILanguage; import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.core.model.IWorkingCopy; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.IWorkingCopyManager; -import org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable; - import org.eclipse.cdt.internal.ui.util.EditorUtility; /** @@ -68,119 +35,6 @@ import org.eclipse.cdt.internal.ui.util.EditorUtility; */ public class ToggleSourceAndHeaderAction extends TextEditorAction { - private static class Counter { - public int fCount; - } - - /** - * Compute the partner file for a translation unit. - * The partner file is the corresponding source or header file - * based on heuristics. - * - * @since 4.0 - */ - private static class PartnerFileComputer extends ASTVisitor implements ASTRunnable { - /** - * When this many times the same partner file is hit, - * we are confident enough to take it. - */ - private static final int CONFIDENCE_LIMIT = 15; - /** - * When this many times no match was found in the index, - * we suspect that we won't get a good partner. - */ - private static final int SUSPECT_LIMIT = 15; - - private IIndex fIndex; - private IPath fFilePath; - private Map<IPath, Counter> fMap; - /** The confidence level == number of hits */ - private int fConfidence; - /** Suspect level == number of no index matches */ - private int fSuspect; - /** The current favorite partner file */ - private IPath fFavoriteLocation; - - { - shouldVisitDeclarators= true; - } - public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) { - if (ast != null) { - fIndex= ast.getIndex(); - fFilePath= Path.fromOSString(ast.getFilePath()); - fMap= new HashMap<IPath, Counter>(); - if (fIndex != null) { - ast.accept(this); - } - } - return Status.OK_STATUS; - } - - public IPath getPartnerFileLocation() { - return fFavoriteLocation; - } - - /* - * @see org.eclipse.cdt.core.dom.ast.ASTVisitor#visit(org.eclipse.cdt.core.dom.ast.IASTDeclarator) - */ - @Override - public int visit(IASTDeclarator declarator) { - if (declarator instanceof IASTFunctionDeclarator) { - IASTName name= declarator.getName(); - if (name != null && declarator.getNestedDeclarator() == null) { - IBinding binding= name.resolveBinding(); - if (binding != null && !(binding instanceof IProblemBinding)) { - boolean isDefinition= name.isDefinition(); - final IIndexName[] partnerNames; - try { - if (isDefinition) { - partnerNames= fIndex.findNames(binding, IIndex.FIND_DECLARATIONS | IIndex.SEARCH_ACROSS_LANGUAGE_BOUNDARIES); - } else { - partnerNames= fIndex.findNames(binding, IIndex.FIND_DEFINITIONS | IIndex.SEARCH_ACROSS_LANGUAGE_BOUNDARIES); - } - if (partnerNames.length == 0) { - ++fSuspect; - if (fSuspect == SUSPECT_LIMIT) { - fFavoriteLocation= null; - return PROCESS_ABORT; - } - } - for (int i= 0; i < partnerNames.length; i++) { - IIndexName partnerName= partnerNames[i]; - IASTFileLocation partnerLocation= partnerName.getFileLocation(); - if (partnerLocation != null) { - IPath partnerFileLocation= Path.fromOSString(partnerLocation.getFileName()); - if (!fFilePath.equals(partnerFileLocation)) { - addPotentialPartnerFileLocation(partnerFileLocation); - if (fConfidence == CONFIDENCE_LIMIT) { - return PROCESS_ABORT; - } - } - } - } - } catch (CoreException exc) { - CUIPlugin.log(exc.getStatus()); - } - } - } - } - return PROCESS_SKIP; - } - - private void addPotentialPartnerFileLocation(IPath partnerFileLocation) { - Counter counter= fMap.get(partnerFileLocation); - if (counter == null) { - counter= new Counter(); - fMap.put(partnerFileLocation, counter); - } - ++counter.fCount; - if (counter.fCount > fConfidence) { - fConfidence= counter.fCount; - fFavoriteLocation= partnerFileLocation; - } - } - } - private static ITranslationUnit fgLastPartnerUnit; private static ITranslationUnit fgLastSourceUnit; @@ -261,130 +115,6 @@ public class ToggleSourceAndHeaderAction extends TextEditorAction { } // search partner file based on filename/extension - ITranslationUnit partnerUnit= getPartnerFileFromFilename(tUnit); - - if (partnerUnit == null) { - // search partner file based on definition/declaration association - IProgressMonitor monitor= new NullProgressMonitor(); - PartnerFileComputer computer= new PartnerFileComputer(); - ASTProvider.getASTProvider().runOnAST(tUnit, ASTProvider.WAIT_ACTIVE_ONLY, monitor, computer); - IPath partnerFileLoation= computer.getPartnerFileLocation(); - if (partnerFileLoation != null) { - partnerUnit= (ITranslationUnit) CoreModel.getDefault().create(partnerFileLoation); - if (partnerUnit == null) { - partnerUnit= CoreModel.getDefault().createTranslationUnitFrom(tUnit.getCProject(), partnerFileLoation); - } - } - } - return partnerUnit; - } - - /** - * Find a partner translation unit based on filename/extension matching. - * - * @param a partner translation unit or <code>null</code> - */ - private ITranslationUnit getPartnerFileFromFilename(ITranslationUnit tUnit) { - IPath sourceFileLocation= tUnit.getLocation(); - if (sourceFileLocation == null) { - return null; - } - IPath partnerBasePath= sourceFileLocation.removeFileExtension(); - IContentType[] contentTypes= getPartnerContentTypes(tUnit.getContentTypeId()); - HashSet<String> extensionsTried= new HashSet<String>(); - for (int j = 0; j < contentTypes.length; j++) { - IContentType contentType= contentTypes[j]; - String[] partnerExtensions; - partnerExtensions= contentType.getFileSpecs(IContentType.FILE_EXTENSION_SPEC); - for (int i= 0; i < partnerExtensions.length; i++) { - String ext= partnerExtensions[i]; - if (extensionsTried.add(ext)) { - String partnerFileBasename= partnerBasePath.addFileExtension(ext).lastSegment(); - - IFile partnerFile= null; - if (tUnit.getResource() != null) { - partnerFile= findInContainer(tUnit.getResource().getParent(), partnerFileBasename); - } - if (partnerFile == null) { - partnerFile= findInContainer(tUnit.getCProject().getProject(), partnerFileBasename); - } - if (partnerFile != null) { - ITranslationUnit partnerUnit= (ITranslationUnit) CoreModel.getDefault().create(partnerFile); - if (partnerUnit != null) { - return partnerUnit; - } - } - // external tanslation unit - try in same directory - if (tUnit.getResource() == null) { - IPath partnerFileLoation= partnerBasePath.removeLastSegments(1).append(partnerFileBasename); - ITranslationUnit partnerUnit= CoreModel.getDefault().createTranslationUnitFrom(tUnit.getCProject(), partnerFileLoation); - if (partnerUnit != null) { - return partnerUnit; - } - } - } - } - } - return null; - } - - /** - * Find a file in the given resource container for the given basename. - * - * @param container - * @param basename - * @return a matching {@link IFile} or <code>null</code>, if no matching file was found - */ - private IFile findInContainer(IContainer container, final String basename) { - final IFile[] result= { null }; - IResourceProxyVisitor visitor= new IResourceProxyVisitor() { - public boolean visit(IResourceProxy proxy) throws CoreException { - if (result[0] != null) { - return false; - } - if (!proxy.isAccessible()) { - return false; - } - if (proxy.getType() == IResource.FILE && proxy.getName().equals(basename)) { - result[0]= (IFile)proxy.requestResource(); - return false; - } - return true; - }}; - try { - container.accept(visitor, 0); - } catch (CoreException exc) { - // ignore - } - return result[0]; - } - - private IContentType[] getPartnerContentTypes(String contentTypeId) { - IContentTypeManager mgr= Platform.getContentTypeManager(); - if (contentTypeId.equals(CCorePlugin.CONTENT_TYPE_CHEADER)) { - return new IContentType[] { - mgr.getContentType(CCorePlugin.CONTENT_TYPE_CSOURCE), - mgr.getContentType(CCorePlugin.CONTENT_TYPE_CXXSOURCE) - }; - } - if (contentTypeId.equals(CCorePlugin.CONTENT_TYPE_CSOURCE)) { - return new IContentType[] { - mgr.getContentType(CCorePlugin.CONTENT_TYPE_CHEADER), - mgr.getContentType(CCorePlugin.CONTENT_TYPE_CXXHEADER) - }; - } - if (contentTypeId.equals(CCorePlugin.CONTENT_TYPE_CXXHEADER)) { - return new IContentType[] { - mgr.getContentType(CCorePlugin.CONTENT_TYPE_CXXSOURCE), - mgr.getContentType(CCorePlugin.CONTENT_TYPE_CSOURCE) - }; - } - if (contentTypeId.equals(CCorePlugin.CONTENT_TYPE_CXXSOURCE)) { - return new IContentType[] { - mgr.getContentType(CCorePlugin.CONTENT_TYPE_CXXHEADER), - mgr.getContentType(CCorePlugin.CONTENT_TYPE_CHEADER) - }; - } - return new IContentType[0]; + return SourceHeaderPartnerFinder.getPartnerTranslationUnit(tUnit); } } |