diff options
Diffstat (limited to 'core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/togglefunction/ToggleRefactoringContext.java')
-rw-r--r-- | core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/togglefunction/ToggleRefactoringContext.java | 114 |
1 files changed, 66 insertions, 48 deletions
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/togglefunction/ToggleRefactoringContext.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/togglefunction/ToggleRefactoringContext.java index b32e43375e1..2e0c1cefa33 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/togglefunction/ToggleRefactoringContext.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/togglefunction/ToggleRefactoringContext.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2011, 2012 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 @@ -7,14 +7,15 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Martin Schwab & Thomas Kallenberg - initial API and implementation + * Martin Schwab & Thomas Kallenberg - initial API and implementation + * Sergey Prigogin (Google) ******************************************************************************/ package org.eclipse.cdt.internal.ui.refactoring.togglefunction; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jface.text.ITextSelection; import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator; @@ -27,30 +28,38 @@ import org.eclipse.cdt.core.dom.ast.IBinding; 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.CoreModelUtil; +import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; +import org.eclipse.cdt.internal.corext.util.CModelUtil; +import org.eclipse.cdt.internal.ui.editor.SourceHeaderPartnerFinder; +import org.eclipse.cdt.internal.ui.refactoring.CRefactoringContext; import org.eclipse.cdt.internal.ui.refactoring.IndexToASTNameHelper; -import org.eclipse.cdt.internal.ui.refactoring.utils.TranslationUnitHelper; public class ToggleRefactoringContext { private IASTFunctionDefinition targetDefinition; private IASTFunctionDeclarator targetDeclaration; - private IASTTranslationUnit targetDefinitionUnit; - private IASTTranslationUnit targetDeclarationUnit; - private IIndex index; - private IASTTranslationUnit selectionUnit; - private IFile selectionFile; + private IASTTranslationUnit targetDefinitionAST; + private IASTTranslationUnit targetDeclarationAST; + private final CRefactoringContext refactoringContext; + private final IIndex index; + private final ITranslationUnit selectionTU; + private IASTTranslationUnit selectionAST; private IBinding binding; private IASTName selectionName; private boolean defaultAnswer; private boolean settedDefaultAnswer; - public ToggleRefactoringContext(IIndex index, IFile file, ITextSelection selection) { + public ToggleRefactoringContext(CRefactoringContext refactoringContext, IIndex index, + ITranslationUnit translationUnit, ITextSelection selection) + throws OperationCanceledException, CoreException { + this.refactoringContext = refactoringContext; this.index = index; - this.selectionFile = file; - findSelectionUnit(); + this.selectionTU = translationUnit; + findSelectionAST(); findSelectedFunctionDeclarator(selection); findBinding(); findDeclaration(); @@ -58,7 +67,7 @@ public class ToggleRefactoringContext { } public void findSelectedFunctionDeclarator(ITextSelection selection) { - selectionName = new DeclaratorFinder(selection, selectionUnit).getName(); + selectionName = new DeclaratorFinder(selection, selectionAST).getName(); } public void findBinding() { @@ -80,12 +89,12 @@ public class ToggleRefactoringContext { throw new NotSupportedException( Messages.ToggleRefactoringContext_MultipleDeclarations); for (IIndexName iname : decnames) { - selectionUnit = getTUForNameInFile(iname); + selectionAST = getASTForIndexName(iname); IASTName astname = IndexToASTNameHelper.findMatchingASTName( - selectionUnit, iname, index); + selectionAST, iname, index); if (astname != null) { targetDeclaration = findFunctionDeclarator(astname); - targetDeclarationUnit = selectionUnit; + targetDeclarationAST = selectionAST; break; } } @@ -101,11 +110,11 @@ public class ToggleRefactoringContext { throw new NotSupportedException(Messages.ToggleRefactoringContext_MultipleDefinitions); } for (IIndexName iname : defnames) { - IASTTranslationUnit unit = getTUForNameInFile(iname); + IASTTranslationUnit unit = getASTForIndexName(iname); IASTName astname = IndexToASTNameHelper.findMatchingASTName(unit, iname, index); if (astname != null) { targetDefinition = findFunctionDefinition(astname); - targetDefinitionUnit = unit; + targetDefinitionAST = unit; break; } } @@ -124,51 +133,51 @@ public class ToggleRefactoringContext { return targetDefinition; } - public IASTTranslationUnit getDeclarationUnit() { - return targetDeclarationUnit; + public IASTTranslationUnit getDeclarationAST() { + return targetDeclarationAST; } - public IASTTranslationUnit getDefinitionUnit() { - return targetDefinitionUnit; + public IASTTranslationUnit getDefinitionAST() { + return targetDefinitionAST; + } + + public ITranslationUnit getSelectionTU() { + return selectionTU; } public IFile getSelectionFile() { - return selectionFile; + return (IFile) selectionTU.getResource(); } - public IASTTranslationUnit getTUForSiblingFile() { - IASTTranslationUnit unit = getDeclarationUnit(); - if (unit == null) - unit = getDefinitionUnit(); - try { - return ToggleNodeHelper.getSiblingFile(getSelectionFile(), unit); - } catch (CoreException e) { - CUIPlugin.log(e); + public IASTTranslationUnit getASTForPartnerFile() throws CoreException { + ITranslationUnit tu = + SourceHeaderPartnerFinder.getPartnerTranslationUnit(selectionTU, refactoringContext); + if (tu == null) return null; - } + return refactoringContext.getAST(tu, null); } - - private void findSelectionUnit() { - try { - selectionUnit = TranslationUnitHelper.loadTranslationUnit(selectionFile, true); - } catch (Exception e) { - } - if (selectionUnit == null) + + private void findSelectionAST() throws OperationCanceledException, CoreException { + selectionAST = refactoringContext.getAST(selectionTU, null); + if (selectionAST == null) throw new NotSupportedException(Messages.ToggleRefactoringContext_NoTuFound); } - private IASTTranslationUnit getTUForNameInFile(IIndexName iname) + private IASTTranslationUnit getASTForIndexName(IIndexName indexName) throws CModelException, CoreException { - if (isSameFileAsInTU(iname)) { - return selectionUnit; + if (isSameFileAsInTU(indexName)) { + return selectionAST; } - IPath path = new Path(iname.getFileLocation().getFileName()); - return TranslationUnitHelper.loadTranslationUnit(path.toString(), true); + ITranslationUnit tu = CoreModelUtil.findTranslationUnitForLocation( + indexName.getFile().getLocation(), null); + if (tu == null) + return null; + return refactoringContext.getAST(tu, null); } - private boolean isSameFileAsInTU(IIndexName iname) { - return iname.getFileLocation().getFileName().equals( - selectionUnit.getFileLocation().getFileName()); + private boolean isSameFileAsInTU(IIndexName indexName) { + return indexName.getFileLocation().getFileName().equals( + selectionAST.getFileLocation().getFileName()); } private IASTFunctionDeclarator findFunctionDeclarator(IASTNode node) { @@ -197,4 +206,13 @@ public class ToggleRefactoringContext { public boolean isSettedDefaultAnswer() { return settedDefaultAnswer; } + + public IASTTranslationUnit getAST(IFile file, IProgressMonitor pm) + throws OperationCanceledException, CoreException { + ITranslationUnit tu = CoreModelUtil.findTranslationUnit(file); + if (tu == null) + return null; + tu = CModelUtil.toWorkingCopy(tu); + return refactoringContext.getAST(tu, pm); + } } |