diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/rewrite/TypeHelper.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/rewrite/TypeHelper.java index 222d970da17..254059fb287 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/rewrite/TypeHelper.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/rewrite/TypeHelper.java @@ -10,14 +10,18 @@ *******************************************************************************/ package org.eclipse.cdt.core.dom.rewrite; +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.ICompositeType; import org.eclipse.cdt.core.dom.ast.IType; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; import org.eclipse.cdt.internal.core.dom.parser.ASTTranslationUnit; import org.eclipse.cdt.internal.core.dom.parser.SizeofCalculator; import org.eclipse.cdt.internal.core.dom.parser.SizeofCalculator.SizeAndAlignment; +import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor; import org.eclipse.cdt.internal.core.dom.parser.cpp.ClassTypeHelper; +import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil; /** @@ -59,4 +63,12 @@ public class TypeHelper { } return false; } + + public static IType createType(IASTDeclarator declarator) { + if (declarator.getTranslationUnit() instanceof ICPPASTTranslationUnit) { + return CPPVisitor.createType(declarator); + } else { + return CVisitor.createType(declarator); + } + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java index fb6a0564da6..f6c3e0b65ec 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java @@ -13,7 +13,11 @@ *******************************************************************************/ package org.eclipse.cdt.internal.core.dom.parser.cpp.semantics; -import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.*; +import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.ALLCVQ; +import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.CVTYPE; +import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.TDEF; +import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.getNestedType; +import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.getUltimateTypeUptoPointers; import java.util.ArrayList; import java.util.Collections; @@ -1864,6 +1868,8 @@ public class CPPVisitor extends ASTQueries { boolean isPackExpansion= false; if (parent instanceof IASTSimpleDeclaration) { declSpec = ((IASTSimpleDeclaration) parent).getDeclSpecifier(); + } else if (parent instanceof IASTParameterDeclaration) { + declSpec = ((IASTParameterDeclaration) parent).getDeclSpecifier(); } else if (parent instanceof IASTFunctionDefinition) { declSpec = ((IASTFunctionDefinition) parent).getDeclSpecifier(); } else if (parent instanceof ICPPASTTypeId) { @@ -2506,4 +2512,21 @@ public class CPPVisitor extends ASTQueries { public static ICPPASTDeclarator findInnermostDeclarator(ICPPASTDeclarator dtor) { return (ICPPASTDeclarator) ASTQueries.findInnermostDeclarator(dtor); } + + /** + * Traverses parent chain of the given node and returns the first node of the given type. + * @param node the start node + * @param type the type to look for + * @return the node itself or its closest ancestor that has the given type, or {@code null} + * if no such node is found. + */ + @SuppressWarnings("unchecked") + public static T findAncestorWithType(IASTNode node, Class type) { + do { + if (type.isInstance(node)) { + return (T) node; + } + } while ((node = node.getParent()) != null); + return null; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/DeclarationGeneratorImpl.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/DeclarationGeneratorImpl.java index ee4c9d86257..4718e1e11ac 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/DeclarationGeneratorImpl.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/DeclarationGeneratorImpl.java @@ -105,13 +105,13 @@ public class DeclarationGeneratorImpl extends DeclarationGenerator { returnedDeclSpec = declSpec; } else if (type instanceof ICPPTemplateInstance) { returnedDeclSpec = getDeclSpecForTemplate((ICPPTemplateInstance) type); - } else if (type instanceof IBinding) { /* ITypedef, ICompositeType... */ // BTW - we need to distinguish (and fail explicitly) on literal composites like: // struct { } aSingleInstance; returnedDeclSpec = getDeclSpecForBinding((IBinding) type); } + // TODO(sprigogin): Be honest and return null instead of void. // Fallback... if (returnedDeclSpec == null) { IASTSimpleDeclSpecifier specifier = factory.newSimpleDeclSpecifier(); @@ -129,7 +129,7 @@ public class DeclarationGeneratorImpl extends DeclarationGenerator { // Addition of pointer operators has to be in reverse order, so it's deferred until the end Map> pointerOperatorMap = new HashMap>(); - IASTName newName = (name != null) ? factory.newName(name) : factory.newName(); + IASTName newName = name != null ? factory.newName(name) : factory.newName(); // If the type is an array of something, create a declaration of a pointer to something instead // (to allow assignment, etc) @@ -311,8 +311,8 @@ public class DeclarationGeneratorImpl extends DeclarationGenerator { ICPPNodeFactory cppFactory = (ICPPNodeFactory) factory; ICPPASTTemplateId tempId = cppFactory.newTemplateId(templateName.copy()); for (ICPPTemplateArgument arg : type.getTemplateArguments()) { - IASTDeclSpecifier argDeclSpec = createDeclSpecFromType(arg.isTypeValue() ? arg - .getTypeValue() : arg.getTypeOfNonTypeValue()); + IASTDeclSpecifier argDeclSpec = createDeclSpecFromType(arg.isTypeValue() ? + arg.getTypeValue() : arg.getTypeOfNonTypeValue()); IASTTypeId typeId = cppFactory.newTypeId(argDeclSpec, null); tempId.addTemplateArgument(typeId); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriter.java index 83dd18fdf43..f9992e2c681 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriter.java @@ -39,7 +39,7 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap; * @author Emanuel Graf */ public class ASTWriter { - private ASTModificationStore modificationStore = new ASTModificationStore(); + private final ASTModificationStore modificationStore = new ASTModificationStore(); /** * Creates a ASTWriter. @@ -63,7 +63,7 @@ public class ASTWriter { * Generates the source code representing this node including comments. * * @param rootNode Node to write. - * @param commentMap Node Comment Map ASTCommenter + * @param commentMap comments for the translation unit * @return A String representing the source code for the node. * @throws ProblemRuntimeException if the node or one of it's children is * an IASTProblemNode. @@ -79,10 +79,6 @@ public class ASTWriter { return writer.toString(); } - public void setModificationStore(ASTModificationStore modificationStore) { - this.modificationStore = modificationStore; - } - /** * Returns true if the node should be separated by a blank line from the node * before it. diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java index cd1e5c363d9..ef08a0ba868 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java @@ -80,6 +80,13 @@ public class ASTWriterVisitor extends ASTVisitor { shouldVisitTypeIds = true; } + /** + * Creates a writer with an empty comment map. + */ + public ASTWriterVisitor() { + this(new NodeCommentMap()); + } + public ASTWriterVisitor(NodeCommentMap commentMap) { super(); init(commentMap); diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CConventions.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CConventions.java index f88b9830d4d..462cc7f39c8 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CConventions.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CConventions.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2001, 2011 IBM Corporation and others. + * Copyright (c) 2001, 2012 IBM Corporation 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 diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java index 711786b3bb1..ea9ac377b15 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java @@ -15,7 +15,6 @@ package org.eclipse.cdt.core.formatter; import java.util.Map; import org.eclipse.cdt.core.CCorePlugin; -import org.eclipse.cdt.internal.formatter.DefaultCodeFormatterOptions; import org.eclipse.cdt.internal.formatter.align.Alignment; diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/DefaultCodeFormatterOptions.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterOptions.java similarity index 99% rename from core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/DefaultCodeFormatterOptions.java rename to core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterOptions.java index dc1e040c11e..a62816e1ee7 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/DefaultCodeFormatterOptions.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterOptions.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2012 IBM Corporation 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 @@ -10,15 +10,20 @@ * Sergey Prigogin (Google) * Anton Leherbauer (Wind River Systems) *******************************************************************************/ -package org.eclipse.cdt.internal.formatter; +package org.eclipse.cdt.core.formatter; import java.util.HashMap; import java.util.Map; import org.eclipse.cdt.core.CCorePlugin; -import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; import org.eclipse.cdt.internal.formatter.align.Alignment; +/** + * Code formatter options. + * + * @noextend This class is not intended to be subclassed by clients. + * @since 5.4 + */ public class DefaultCodeFormatterOptions { public static final int TAB = 1; public static final int SPACE = 2; diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CCodeFormatter.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CCodeFormatter.java index bdc678c626a..2c891e50939 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CCodeFormatter.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CCodeFormatter.java @@ -21,6 +21,7 @@ import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage; import org.eclipse.cdt.core.formatter.CodeFormatter; import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; +import org.eclipse.cdt.core.formatter.DefaultCodeFormatterOptions; import org.eclipse.cdt.core.index.IIndex; import org.eclipse.cdt.core.model.CoreModel; import org.eclipse.cdt.core.model.ILanguage; diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java index cf6502358f7..30430639a17 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java @@ -141,6 +141,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTWhileStatement; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier; import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator; import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; +import org.eclipse.cdt.core.formatter.DefaultCodeFormatterOptions; import org.eclipse.cdt.core.parser.IToken; import org.eclipse.cdt.internal.formatter.align.Alignment; import org.eclipse.cdt.internal.formatter.align.AlignmentException; diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java index 85e3ff3c7c6..ffad22adcc7 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java @@ -18,6 +18,7 @@ import java.util.Iterator; import java.util.List; import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; +import org.eclipse.cdt.core.formatter.DefaultCodeFormatterOptions; import org.eclipse.cdt.internal.formatter.align.Alignment; import org.eclipse.cdt.internal.formatter.align.AlignmentException; import org.eclipse.cdt.internal.formatter.scanner.Scanner; diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/GenerateGettersAndSetters.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/GenerateGettersAndSetters.rts index a270920614c..ac0dfa784c4 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/GenerateGettersAndSetters.rts +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/GenerateGettersAndSetters.rts @@ -1502,8 +1502,8 @@ public: return mClass; } - void setClass(int _class) { - mClass = _class; + void setClass(int clazz) { + mClass = clazz; } private: diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractfunction/ExtractFunctionRefactoringTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractfunction/ExtractFunctionRefactoringTest.java index 9e3e46baaa9..d755f2ac8a5 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractfunction/ExtractFunctionRefactoringTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractfunction/ExtractFunctionRefactoringTest.java @@ -18,7 +18,7 @@ import org.eclipse.ltk.core.refactoring.Refactoring; import org.eclipse.cdt.ui.tests.refactoring.RefactoringTestBase; -import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation; +import org.eclipse.cdt.internal.ui.refactoring.NameInformation; import org.eclipse.cdt.internal.ui.refactoring.extractfunction.ExtractFunctionInformation; import org.eclipse.cdt.internal.ui.refactoring.extractfunction.ExtractFunctionRefactoring; import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum; @@ -27,6 +27,7 @@ import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum; * Tests for Extract Function refactoring. */ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { + private static final String NO_RETURN_VALUE = ""; private ExtractFunctionInformation refactoringInfo; private String extractedFunctionName = "extracted"; private String returnValue; @@ -59,11 +60,8 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { refactoringInfo.setReplaceDuplicates(replaceDuplicates); if (refactoringInfo.getMandatoryReturnVariable() == null) { if (returnValue != null) { - for (NameInformation nameInfo : refactoringInfo.getParameterCandidates()) { - if (returnValue.equals(String.valueOf(nameInfo.getName().getSimpleID()))) { - refactoringInfo.setReturnVariable(nameInfo); - break; - } + for (NameInformation nameInfo : refactoringInfo.getParameters()) { + nameInfo.setReturnValue(returnValue.equals(String.valueOf(nameInfo.getName().getSimpleID()))); } } } @@ -146,7 +144,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //int A::help() { // return 42; //} - public void testVariableDefinedInside() throws Exception { + public void testExtractedVariableDefinition() throws Exception { assertRefactoringSuccess(); } @@ -177,7 +175,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //private: // int help(); - // void extracted(int& i); + // int extracted(int i); //}; // //#endif /*A_H_*/ @@ -211,23 +209,24 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //A::~A() { //} // - //void A::extracted(int& i) { + //int A::extracted(int i) { // //comment // ++i; // help(); + // return i; //} // //int A::foo() { // int i = 2; // //comment - // extracted(i); + // i = extracted(i); // return i; //} // //int A::help() { // return 42; //} - public void testWithComment() throws Exception { + public void testComment() throws Exception { assertRefactoringSuccess(); } @@ -239,18 +238,19 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // return i; //} //==================== - //void extracted(int& i) { + //int extracted(int i) { // // Comment // i = 7; + // return i; //} // //int main() { // int i; // // Comment - // extracted(i); + // i = extracted(i); // return i; //} - public void testFirstExtractedStatementWithLeadingComment() throws Exception { + public void testLeadingComment() throws Exception { assertRefactoringSuccess(); } @@ -261,16 +261,17 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // return i; //} //==================== - //void extracted(int& i) { + //int extracted(int i) { // i = 7; // Comment + // return i; //} // //int main() { // int i; - // extracted(i); + // i = extracted(i); // return i; //} - public void testLastExtractedStatementWithTraillingComment() throws Exception { + public void testTraillingComment() throws Exception { assertRefactoringSuccess(); } @@ -312,7 +313,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //int A::help() { // return 42; //} - public void testWithTwoVariableDefinedInScope() throws Exception { + public void testTwoVariableDefinedInScope() throws Exception { assertRefactoringFailure(); } @@ -404,7 +405,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //}; // //#endif /*B_H_*/ - public void testWithNamedTypedField() throws Exception { + public void testNamedTypedField() throws Exception { assertRefactoringSuccess(); } @@ -496,413 +497,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //}; // //#endif /*B_H_*/ - public void testWithNamedTypedVariableDefinedInScope() throws Exception { - assertRefactoringSuccess(); - } - - //A.h - //#ifndef A_H_ - //#define A_H_ - // - //class A { - //public: - // A(); - // virtual ~A(); - // int foo(); - // - //private: - // int help(); - //}; - // - //#endif /*A_H_*/ - //==================== - //#ifndef A_H_ - //#define A_H_ - // - //class A { - //public: - // A(); - // virtual ~A(); - // int foo(); - // - //private: - // int help(); - // void extracted(int& i); - //}; - // - //#endif /*A_H_*/ - - //A.cpp - //#include "A.h" - // - //#define ZWO 2 - // - //A::A() { - //} - // - //A::~A() { - //} - // - //int A::foo() { - // int i = 2; - // /*$*/++i; - // i += ZWO; - // help();/*$$*/ - // return i; - //} - // - //int A::help() { - // return 42; - //} - //==================== - //#include "A.h" - // - //#define ZWO 2 - // - //A::A() { - //} - // - //A::~A() { - //} - // - //void A::extracted(int& i) { - // ++i; - // i += ZWO; - // help(); - //} - // - //int A::foo() { - // int i = 2; - // extracted(i); - // return i; - //} - // - //int A::help() { - // return 42; - //} - public void testWithObjectStyleMacro() throws Exception { - assertRefactoringSuccess(); - } - - //A.h - //#ifndef A_H_ - //#define A_H_ - // - //class A { - //public: - // A(); - // virtual ~A(); - // int foo(); - // - //private: - // int help(); - //}; - // - //#endif /*A_H_*/ - //==================== - //#ifndef A_H_ - //#define A_H_ - // - //class A { - //public: - // A(); - // virtual ~A(); - // int foo(); - // - //private: - // int help(); - // void extracted(int& i); - //}; - // - //#endif /*A_H_*/ - - //A.cpp - //#include "A.h" - // - //#define ADD(a,b) a + b + 2 - // - //A::A() { - //} - // - //A::~A() { - //} - // - //int A::foo() { - // int i = 2; - // /*$*/++i; - // i = ADD(i, 42); - // help();/*$$*/ - // return i; - //} - // - //int A::help() { - // return 42; - //} - //==================== - //#include "A.h" - // - //#define ADD(a,b) a + b + 2 - // - //A::A() { - //} - // - //A::~A() { - //} - // - //void A::extracted(int& i) { - // ++i; - // i = ADD(i, 42); - // help(); - //} - // - //int A::foo() { - // int i = 2; - // extracted(i); - // return i; - //} - // - //int A::help() { - // return 42; - //} - public void testWithFunctionStyleMacro() throws Exception { - assertRefactoringSuccess(); - } - - //A.h - //#ifndef A_H_ - //#define A_H_ - // - //class A { - //public: - // A(); - // virtual ~A(); - // int foo(); - // - //private: - // int help(); - //}; - // - //#endif /*A_H_*/ - //==================== - //#ifndef A_H_ - //#define A_H_ - // - //class A { - //public: - // A(); - // virtual ~A(); - // int foo(); - // - //private: - // int help(); - // void extracted(int* i); - //}; - // - //#endif /*A_H_*/ - - //A.cpp - //#include "A.h" - // - //A::A() { - //} - // - //A::~A() { - //} - // - //int A::foo() { - // int* i = new int(2); - // /*$*/++*i; - // help();/*$$*/ - // return *i; - //} - // - //int A::help() { - // return 42; - //} - //==================== - //#include "A.h" - // - //A::A() { - //} - // - //A::~A() { - //} - // - //void A::extracted(int* i) { - // ++*i; - // help(); - //} - // - //int A::foo() { - // int* i = new int(2); - // extracted(i); - // return *i; - //} - // - //int A::help() { - // return 42; - //} - public void testWithPointer() throws Exception { - assertRefactoringSuccess(); - } - - //A.h - //#ifndef A_H_ - //#define A_H_ - // - //class A { - //public: - // A(); - // virtual ~A(); - // int foo(); - // - //private: - // int help(); - //}; - // - //#endif /*A_H_*/ - //==================== - //#ifndef A_H_ - //#define A_H_ - // - //class A { - //public: - // A(); - // virtual ~A(); - // int foo(); - // - //private: - // int help(); - // void extracted(int* i); - //}; - // - //#endif /*A_H_*/ - - //A.cpp - //#include "A.h" - // - //A::A() { - //} - // - //A::~A() { - //} - // - //int A::foo() { - // int* i = new int(2); - // /*$*/++*i; - // help(); - // //A end-comment/*$$*/ - // return *i; - //} - // - //int A::help() { - // return 42; - //} - //==================== - //#include "A.h" - // - //A::A() { - //} - // - //A::~A() { - //} - // - //void A::extracted(int* i) { - // ++*i; - // help(); - //} - // - //int A::foo() { - // int* i = new int(2); - // extracted(i); - // //A end-comment - // return *i; - //} - // - //int A::help() { - // return 42; - //} - public void testWithPointerAndCommentAtTheEnd() throws Exception { - assertRefactoringSuccess(); - } - - //A.h - //#ifndef A_H_ - //#define A_H_ - // - //class A { - //public: - // A(); - // virtual ~A(); - // int foo(); - // - //private: - // int help(); - //}; - // - //#endif /*A_H_*/ - //==================== - //#ifndef A_H_ - //#define A_H_ - // - //class A { - //public: - // A(); - // virtual ~A(); - // int foo(); - // - //private: - // int help(); - // void extracted(int* i); - //}; - // - //#endif /*A_H_*/ - - //A.cpp - //#include "A.h" - // - //A::A() { - //} - // - //A::~A() { - //} - // - //int A::foo() { - // //A beautiful comment - // int* i = new int(2); - // /*$*/++*i; - // help();/*$$*/ - // return *i; - //} - // - //int A::help() { - // return 42; - //} - //==================== - //#include "A.h" - // - //A::A() { - //} - // - //A::~A() { - //} - // - //void A::extracted(int* i) { - // ++*i; - // help(); - //} - // - //int A::foo() { - // //A beautiful comment - // int* i = new int(2); - // extracted(i); - // return *i; - //} - // - //int A::help() { - // return 42; - //} - public void testWithPointerAndComment() throws Exception { + public void testNamedTypedVariableDefinedInScope() throws Exception { assertRefactoringSuccess(); } @@ -938,6 +533,254 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //#endif /*A_H_*/ + //A.cpp + //#include "A.h" + // + //#define ZWO 2 + // + //A::A() { + //} + // + //A::~A() { + //} + // + //int A::foo() { + // int i = 2; + // /*$*/++i; + // i += ZWO; + // help();/*$$*/ + // return i; + //} + // + //int A::help() { + // return 42; + //} + //==================== + //#include "A.h" + // + //#define ZWO 2 + // + //A::A() { + //} + // + //A::~A() { + //} + // + //int A::extracted(int i) { + // ++i; + // i += ZWO; + // help(); + // return i; + //} + // + //int A::foo() { + // int i = 2; + // i = extracted(i); + // return i; + //} + // + //int A::help() { + // return 42; + //} + public void testObjectStyleMacro() throws Exception { + assertRefactoringSuccess(); + } + + //A.h + //#ifndef A_H_ + //#define A_H_ + // + //class A { + //public: + // A(); + // virtual ~A(); + // int foo(); + // + //private: + // int help(); + //}; + // + //#endif /*A_H_*/ + //==================== + //#ifndef A_H_ + //#define A_H_ + // + //class A { + //public: + // A(); + // virtual ~A(); + // int foo(); + // + //private: + // int help(); + // int extracted(int i); + //}; + // + //#endif /*A_H_*/ + + //A.cpp + //#include "A.h" + // + //#define ADD(a,b) a + b + 2 + // + //A::A() { + //} + // + //A::~A() { + //} + // + //int A::foo() { + // int i = 2; + // /*$*/++i; + // i = ADD(i, 42); + // help();/*$$*/ + // return i; + //} + // + //int A::help() { + // return 42; + //} + //==================== + //#include "A.h" + // + //#define ADD(a,b) a + b + 2 + // + //A::A() { + //} + // + //A::~A() { + //} + // + //int A::extracted(int i) { + // ++i; + // i = ADD(i, 42); + // help(); + // return i; + //} + // + //int A::foo() { + // int i = 2; + // i = extracted(i); + // return i; + //} + // + //int A::help() { + // return 42; + //} + public void testFunctionStyleMacro() throws Exception { + assertRefactoringSuccess(); + } + + //A.h + //#ifndef A_H_ + //#define A_H_ + // + //class A { + //public: + // A(); + // virtual ~A(); + // int foo(); + // + //private: + // int help(); + //}; + // + //#endif /*A_H_*/ + //==================== + //#ifndef A_H_ + //#define A_H_ + // + //class A { + //public: + // A(); + // virtual ~A(); + // int foo(); + // + //private: + // int help(); + // void extracted(int* i); + //}; + // + //#endif /*A_H_*/ + + //A.cpp + //#include "A.h" + // + //A::A() { + //} + // + //A::~A() { + //} + // + //int A::foo() { + // int* i = new int(2); + // /*$*/++*i; + // help();/*$$*/ + // return *i; + //} + // + //int A::help() { + // return 42; + //} + //==================== + //#include "A.h" + // + //A::A() { + //} + // + //A::~A() { + //} + // + //void A::extracted(int* i) { + // ++*i; + // help(); + //} + // + //int A::foo() { + // int* i = new int(2); + // extracted(i); + // return *i; + //} + // + //int A::help() { + // return 42; + //} + public void testPointer() throws Exception { + assertRefactoringSuccess(); + } + + //A.h + //#ifndef A_H_ + //#define A_H_ + // + //class A { + //public: + // A(); + // virtual ~A(); + // int foo(); + // + //private: + // int help(); + //}; + // + //#endif /*A_H_*/ + //==================== + //#ifndef A_H_ + //#define A_H_ + // + //class A { + //public: + // A(); + // virtual ~A(); + // int foo(); + // + //private: + // int help(); + // void extracted(int& i); + //}; + // + //#endif /*A_H_*/ + //A.cpp //#include "A.h" // @@ -966,23 +809,22 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //A::~A() { //} // - //int A::extracted(int i) { + //void A::extracted(int& i) { // ++i; // help(); - // return i; //} // //int A::foo() { // int i = 2; - // i = extracted(i); + // extracted(i); // return i; //} // //int A::help() { // return 42; //} - public void testWithReturnValue() throws Exception { - returnValue = "i"; + public void testWithoutReturnValue() throws Exception { + returnValue = NO_RETURN_VALUE; assertRefactoringSuccess(); } @@ -1067,8 +909,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //int A::help() { // return 42; //} - public void testWithReturnValueAndRefParameter() throws Exception { - returnValue = "i"; + public void testRefParameter() throws Exception { assertRefactoringSuccess(); } @@ -1099,7 +940,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //private: // int help(); - // int extracted(int i, B* b, int y, float x); + // int extracted(int i, int y, float x, B* b); //}; // //#endif /*A_H_*/ @@ -1138,7 +979,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //A::~A() { //} // - //int A::extracted(int i, B* b, int y, float x) { + //int A::extracted(int i, int y, float x, B* b) { // ++i; // b->hello(y); // i = i + x; @@ -1151,7 +992,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // float x = i; // B* b = new B(); // int y = x + i; - // i = extracted(i, b, y, x); + // i = extracted(i, y, x, b); // ++x; // return i; //} @@ -1159,8 +1000,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //int A::help() { // return 42; //} - public void testWithReturnValueAndRefParameterAndSomeMoreNotUsedAfterwards() throws Exception { - returnValue = "i"; + public void testRefParameterAndSomeMoreNotUsedAfterwards() throws Exception { assertRefactoringSuccess(); } @@ -1195,7 +1035,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //private: // int help(); - // float extracted(int& i, B* b, int y, float x); + // float extracted(int& i, int y, float x, B* b); //}; // //#endif /*A_H_*/ @@ -1234,7 +1074,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //A::~A() { //} // - //float A::extracted(int& i, B* b, int y, float x) { + //float A::extracted(int& i, int y, float x, B* b) { // ++i; // b->hello(y); // i = i + x; @@ -1247,7 +1087,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // float x = i; // B* b = new B(); // int y = x + i; - // x = extracted(i, b, y, x); + // x = extracted(i, y, x, b); // ++x; // return i; //} @@ -1268,7 +1108,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //}; // //#endif /*B_H_*/ - public void testWithReturnValueTakeTheSecondAndRefParameterAndSomeMoreNotUsedAferwards() throws Exception { + public void testExplicitlyAssignedReturnValue() throws Exception { returnValue = "x"; assertRefactoringSuccess(); } @@ -1304,7 +1144,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //private: // int help(); - // int extracted(int i, B* b, int y, float x); + // bool extracted(bool y, float x, int& i, B* b); //}; // //#endif /*A_H_*/ @@ -1322,9 +1162,10 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // int i = 2; // float x = i; // B* b = new B(); - // int y = x + i; + // bool y = false; // /*$*/++i; // b->hello(y); + // y = !y; // i = i + x; // help();/*$$*/ // b->hello(y); @@ -1344,20 +1185,21 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //A::~A() { //} // - //int A::extracted(int i, B* b, int y, float x) { + //bool A::extracted(bool y, float x, int& i, B* b) { // ++i; // b->hello(y); + // y = !y; // i = i + x; // help(); - // return i; + // return y; //} // //int A::foo() { // int i = 2; // float x = i; // B* b = new B(); - // int y = x + i; - // i = extracted(i, b, y, x); + // bool y = false; + // y = extracted(y, x, i, b); // b->hello(y); // ++x; // return i; @@ -1375,12 +1217,11 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //public: // B(); // virtual ~B(); - // void hello(float y); + // void hello(bool y); //}; // //#endif /*B_H_*/ - public void testWithReturnValueAndALotRefParameter() throws Exception { - returnValue = "i"; + public void testReturnValueSelection() throws Exception { assertRefactoringSuccess(); } @@ -1415,7 +1256,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //private: // int help(); - // B* extracted(int& i, B* b, int y, float x); + // B* extracted(int& i, int y, float x, B* b); //}; // //#endif /*A_H_*/ @@ -1455,7 +1296,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //A::~A() { //} // - //B* A::extracted(int& i, B* b, int y, float x) { + //B* A::extracted(int& i, int y, float x, B* b) { // ++i; // b->hello(y); // i = i + x; @@ -1468,7 +1309,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // float x = i; // B* b = new B(); // int y = x + i; - // b = extracted(i, b, y, x); + // b = extracted(i, y, x, b); // b->hello(y); // ++x; // return i; @@ -1490,7 +1331,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //}; // //#endif /*B_H_*/ - public void testWithReturnValueTakeTheSecondAndRefParameter() throws Exception { + public void testExplicitlyAssignedReturnValueAndOutputParameter() throws Exception { returnValue = "b"; assertRefactoringSuccess(); } @@ -1521,7 +1362,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // int foo(); // //protected: - // void extracted(int& i); + // int extracted(int i); // //private: // int help(); @@ -1557,21 +1398,22 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //A::~A() { //} // - //void A::extracted(int& i) { + //int A::extracted(int i) { // ++i; // help(); + // return i; //} // //int A::foo() { // int i = 2; - // extracted(i); + // i = extracted(i); // return i; //} // //int A::help() { // return 42; //} - public void testWithProtectedVisibility() throws Exception { + public void testProtectedVisibility() throws Exception { visibility = VisibilityEnum.v_protected; assertRefactoringSuccess(); } @@ -1600,7 +1442,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // A(); // virtual ~A(); // int foo(); - // void extracted(int& i); + // int extracted(int i); // //private: // int help(); @@ -1636,21 +1478,22 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //A::~A() { //} // - //void A::extracted(int& i) { + //int A::extracted(int i) { // ++i; // help(); + // return i; //} // //int A::foo() { // int i = 2; - // extracted(i); + // i = extracted(i); // return i; //} // //int A::help() { // return 42; //} - public void testWithPublicVisibility() throws Exception { + public void testPublicVisibility() throws Exception { visibility = VisibilityEnum.v_public; assertRefactoringSuccess(); } @@ -1682,7 +1525,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //private: // int help(); - // void extracted(int& i) const; + // int extracted(int i) const; //}; // //#endif /*A_H_*/ @@ -1715,21 +1558,22 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //A::~A() { //} // - //void A::extracted(int& i) const { + //int A::extracted(int i) const { // ++i; // help(); + // return i; //} // //int A::foo() const { // int i = 2; - // extracted(i); + // i = extracted(i); // return i; //} // //int A::help() { // return 42; //} - public void testWithConstMethod() throws Exception { + public void testConstMethod() throws Exception { assertRefactoringSuccess(); } @@ -1762,7 +1606,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // return;/*$$*/ // //unreachable //} - public void testDontExtractCodeThatContainsReturn() throws Exception { + public void testDontExtractCodeContainingReturn() throws Exception { assertRefactoringFailure(); } @@ -1773,7 +1617,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // continue;/*$$*/ // } //} - public void testTestIfWeDontAllowToExtractContinue() throws Exception { + public void testDontExtractCodeContainingContinue() throws Exception { assertRefactoringFailure(); } @@ -1805,7 +1649,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // int i = int(); // runTest(i); //} - public void testExtractFunctionWithAMacroCallInSelectedCodeForgetsTheMacro() throws Exception { + public void testMacroCallInSelectedCodeForgetsTheMacro() throws Exception { extractedFunctionName = "runTest"; assertRefactoringSuccess(); } @@ -1834,7 +1678,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // int i = int(); // runTest(i); //} - public void testWithCommentsExtractFunctionWithAMacroCallInSelectedCodeForgetsTheMacro() throws Exception { + public void testCommentsWithMacroCallInSelectedCodeForgetsTheMacro() throws Exception { extractedFunctionName = "runTest"; assertRefactoringSuccess(); } @@ -1898,7 +1742,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // int foo(int& a); // //private: - // void extracted(int& a, int b, int c); + // void extracted(int b, int c, int& a); //}; // //#endif /*A_H_*/ @@ -1927,14 +1771,14 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //A::~A() { //} // - //void A::extracted(int& a, int b, int c) { + //void A::extracted(int b, int c, int& a) { // a = b + c; //} // //int A::foo(int& a) { // int b = 7; // int c = 8; - // extracted(a, b, c); + // extracted(b, c, a); // return a; //} public void testBug239059DoubleAmpersandInSignatureOfExtractedFunctions() throws Exception { @@ -2359,17 +2203,18 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // return 5; //} // - //void extracted(int& a) { + //int extracted(int a) { // try { // a = myFunc(); // } catch (const int&) { // a = 3; // } + // return a; //} // //int main() { // int a = 0; - // extracted(a); + // a = extracted(a); // return a; //} public void testBug281564ExtractFunctionFailsWhenCatchingAnUnnamedException() throws Exception { @@ -2403,13 +2248,14 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // return a; //} //==================== - //void extracted(int* a, int b) { + //int extracted(int a, int b) { // a = b * 2; + // return a; //} // //int main() { // int a, b; - // extracted(a, b); + // a = extracted(a, b); // return a; //} public void testBug288268CRefactoringCreatesCPPParameters() throws Exception { @@ -2440,7 +2286,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // A(); // virtual ~A(); // int foo(); - // virtual void extracted(int& i); + // virtual int extracted(int i); // //private: // int help(); @@ -2477,23 +2323,24 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //A::~A() { //} // - //void A::extracted(int& i) { + //int A::extracted(int i) { // //comment // ++i; // help(); + // return i; //} // //int A::foo() { // int i = 2; // //comment - // extracted(i); + // i = extracted(i); // return i; //} // //int A::help() { // return 42; //} - public void testWithVirtual() throws Exception { + public void testVirtual() throws Exception { visibility = VisibilityEnum.v_public; virtual = true; assertRefactoringSuccess(); @@ -2659,7 +2506,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //private: // int help(); - // void extracted(int& i); + // int extracted(int i); //}; // //#endif /*A_H_*/ @@ -2694,24 +2541,25 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //A::~A() { // int i = 2; - // extracted(i); + // i = extracted(i); //} // - //void A::extracted(int& i) { + //int A::extracted(int i) { // ++i; // help(); + // return i; //} // //int A::foo() { // int i = 2; - // extracted(i); + // i = extracted(i); // return i; //} // //int A::help() { // return 42; //} - public void testWithDuplicates() throws Exception { + public void testDuplicates() throws Exception { assertRefactoringSuccess(); } @@ -2742,7 +2590,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //private: // int help(); - // void extracted(int& i); + // int extracted(int i); //}; // //#endif /*A_H_*/ @@ -2777,17 +2625,18 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //A::~A() { // int oo = 99; - // extracted(oo); + // oo = extracted(oo); //} // - //void A::extracted(int& i) { + //int A::extracted(int i) { // ++i; // help(); + // return i; //} // //int A::foo() { // int i = 2; - // extracted(i); + // i = extracted(i); // return i; //} // @@ -2888,7 +2737,6 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // return 42; //} public void testDuplicateWithField() throws Exception { - returnValue = "j"; assertRefactoringSuccess(); } @@ -2988,7 +2836,6 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // return 42; //} public void testDuplicateWithFieldInMarkedScope() throws Exception { - returnValue = "j"; assertRefactoringSuccess(); } @@ -3084,7 +2931,6 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // return 42; //} public void testDuplicatesWithDifferentNamesAndReturnType() throws Exception { - returnValue = "i"; assertRefactoringSuccess(); } @@ -3174,7 +3020,8 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //int A::help() { // return 42; //} - public void testDuplicatesWithALotOfDifferentNamesAnVariableNotUsedAfterwardsInTheDuplicate() throws Exception { + public void testDuplicatesWithDifferentNamesAndOutputParameter() throws Exception { + returnValue = NO_RETURN_VALUE; assertRefactoringSuccess(); } @@ -3258,7 +3105,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //int A::help() { // return 42; //} - public void testWithDuplicateNameUsedAfterwardsInDuplicateButNotInOriginalSelectionThisIsNoDuplicate() throws Exception { + public void testDuplicateNameUsedAfterwardsInDuplicateButNotInOriginalSelectionThisIsNoDuplicate() throws Exception { assertRefactoringSuccess(); } @@ -3293,7 +3140,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //private: // int help(); - // int extracted(int i, B* b, int y, float x); + // int extracted(int i, int y, float x, B* b); //}; // //#endif /*A_H_*/ @@ -3346,13 +3193,13 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // float x = i; // B* b = new B(); // int y = x + i; - // i = extracted(i, b, y, x); + // i = extracted(i, y, x, b); // b->hello(y); // ++x; // i++; //} // - //int A::extracted(int i, B* b, int y, float x) { + //int A::extracted(int i, int y, float x, B* b) { // ++i; // b->hello(y); // i = i + x; @@ -3365,7 +3212,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // float x = i; // B* b = new B(); // int y = x + i; - // i = extracted(i, b, y, x); + // i = extracted(i, y, x, b); // b->hello(y); // ++x; // return i; @@ -3387,8 +3234,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //}; // //#endif /*B_H_*/ - public void testWithReturnValueAndALotRefParameterAndAMethodCall() throws Exception { - returnValue = "i"; + public void testALotRefParameterAndAMethodCall() throws Exception { assertRefactoringSuccess(); } @@ -3423,7 +3269,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //private: // int help(); - // int extracted(int i, B* b, int y, float x); + // int extracted(int i, int y, float x, B* b); //}; // //#endif /*A_H_*/ @@ -3475,13 +3321,13 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // float x = i; // B* b = new B(); // int y = x + i; - // i = extracted(i, b, y, x); + // i = extracted(i, y, x, b); // b->hello(y); // ++x; // i++; //} // - //int A::extracted(int i, B* b, int y, float x) { + //int A::extracted(int i, int y, float x, B* b) { // ++i; // b->hello(y); // i = i + x; @@ -3494,7 +3340,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // float x = i; // B* b = new B(); // int y = x + i; - // i = extracted(i, b, y, x); + // i = extracted(i, y, x, b); // b->hello(y); // return i; //} @@ -3515,8 +3361,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //}; // //#endif /*B_H_*/ - public void testWithReturnValueAndALotRefParameterAndAMethodCallDuplicateIsSimilar() throws Exception { - returnValue = "i"; + public void testALotRefParameterAndAMethodCallDuplicateIsSimilar() throws Exception { assertRefactoringSuccess(); } @@ -3547,7 +3392,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //private: // int help(); - // void extracted(int& i); + // int extracted(int i); //}; // //#endif /*A_H_*/ @@ -3582,24 +3427,25 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //A::~A() { // int i = 2; - // extracted(i); + // i = extracted(i); //} // - //void A::extracted(int& i) { + //int A::extracted(int i) { // ++i; // help(); + // return i; //} // //int A::foo() { // int i = 2; - // extracted(i); + // i = extracted(i); // return i; //} // //int A::help() { // return 42; //} - public void testWithDuplicatesAndComments() throws Exception { + public void testDuplicatesAndComments() throws Exception { assertRefactoringSuccess(); } @@ -3630,7 +3476,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //private: // int help(); - // void extracted(int& ii); + // int extracted(int ii); //}; // //#endif /*A_H_*/ @@ -3668,22 +3514,23 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //A::~A() { //} // - //void A::extracted(int& ii) { + //int A::extracted(int ii) { // ++ii; // ii = ADD(ii, 42); // help(); + // return ii; //} // //int A::foo() { // int ii = 2; - // extracted(ii); + // ii = extracted(ii); // return ii; //} // //int A::help() { // return 42; //} - public void testExtractFunctionRefactoringTestWithFunctionStyleMacro2() throws Exception { + public void testFunctionStyleMacro2() throws Exception { assertRefactoringSuccess(); } @@ -3862,8 +3709,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //int A::help() { // return 42; //} - public void testWithMultipleMacros() throws Exception { - returnValue = "bb"; + public void testMultipleMacros() throws Exception { assertRefactoringSuccess(); } @@ -3952,7 +3798,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // name="extracted" project="RegressionTestProject" selection="57,25" visibility="private"/> // // - public void testExtractFunctionHistoryRefactoringTestVariableDefinedInScope() throws Exception { + public void testHistoryWithVariableDefinedInScope() throws Exception { assertRefactoringSuccess(); } @@ -3983,7 +3829,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //private: // int help(); - // void extracted(int& i); + // int extracted(int i); //}; // //#endif /*A_H_*/ @@ -4017,16 +3863,17 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //A::~A() { //} // - //void A::extracted(int& i) { + //int A::extracted(int i) { // //comment // ++i; // help(); + // return i; //} // //int A::foo() { // int i = 2; // //comment - // extracted(i); + // i = extracted(i); // return i; //} // @@ -4042,7 +3889,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // flags="4" id="org.eclipse.cdt.internal.ui.refactoring.extractfunction.ExtractFunctionRefactoring" // name="extracted" project="RegressionTestProject" selection="69,24" visibility="private"/> // - public void testExtractFunctionHistoryRefactoringTest() throws Exception { + public void testHistory() throws Exception { assertRefactoringSuccess(); } @@ -4054,15 +3901,16 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // return i; //} //==================== - //void extracted(int& i) { + //int extracted(int i) { // // Comment // i = 7; + // return i; //} // //int main() { // int i; // // Comment - // extracted(i); + // i = extracted(i); // return i; //} @@ -4074,7 +3922,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // flags="4" id="org.eclipse.cdt.internal.ui.refactoring.extractfunction.ExtractFunctionRefactoring" // name="extracted" project="RegressionTestProject" selection="34,6" visibility="private"/> // - public void testHistoryFirstExtractedStatementWithLeadingComment() throws Exception { + public void testHistoryWithLeadingComment() throws Exception { assertRefactoringSuccess(); } @@ -4085,13 +3933,14 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // return i; //} //==================== - //void extracted(int& i) { + //int extracted(int i) { // i = 7; // Comment + // return i; //} // //int main() { // int i; - // extracted(i); + // i = extracted(i); // return i; //} @@ -4103,7 +3952,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // flags="4" id="org.eclipse.cdt.internal.ui.refactoring.extractfunction.ExtractFunctionRefactoring" // name="extracted" project="RegressionTestProject" selection="22,6" visibility="private"/> // - public void testHistoryExtractedStatementWithTrailingComment() throws Exception { + public void testHistoryWithTrailingComment() throws Exception { assertRefactoringSuccess(); } @@ -4134,7 +3983,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //private: // int help(); - // void extracted(int& i); + // int extracted(int i); //}; // //#endif /*A_H_*/ @@ -4169,17 +4018,18 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //A::~A() { // int oo = 99; - // extracted(oo); + // oo = extracted(oo); //} // - //void A::extracted(int& i) { + //int A::extracted(int i) { // ++i; // help(); + // return i; //} // //int A::foo() { // int i = 2; - // extracted(i); + // i = extracted(i); // return i; //} // @@ -4194,7 +4044,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // fileName="file:${projectPath}/A.cpp" flags="4" id="org.eclipse.cdt.internal.ui.refactoring.extractfunction.ExtractFunctionRefactoring" // name="extracted" project="RegressionTestProject" replaceDuplicates="true" selection="99,13" visibility="private"/> // - public void testExtractFunctionRefactoringTestDuplicatesWithDifferentNamesHistoryTest() throws Exception { + public void testHistoryWithDuplicatesWithDifferentNames() throws Exception { assertRefactoringSuccess(); } } diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/NameComposerTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/NameComposerTest.java index 907c6b036de..afaec7ed7b9 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/NameComposerTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/NameComposerTest.java @@ -12,6 +12,8 @@ package org.eclipse.cdt.ui.tests.refactoring.utils; import org.eclipse.cdt.ui.PreferenceConstants; +import org.eclipse.cdt.internal.corext.codemanipulation.StubUtility; + import junit.framework.TestCase; import org.eclipse.cdt.internal.ui.util.NameComposer; @@ -46,42 +48,42 @@ public class NameComposerTest extends TestCase { } public void testTrimFieldName() { - assertEquals("f", NameComposer.trimFieldName("f_")); - assertEquals("F", NameComposer.trimFieldName("F_")); - assertEquals("oo", NameComposer.trimFieldName("F_oo")); - assertEquals("o", NameComposer.trimFieldName("f_o")); + assertEquals("f", StubUtility.trimFieldName("f_")); + assertEquals("F", StubUtility.trimFieldName("F_")); + assertEquals("oo", StubUtility.trimFieldName("F_oo")); + assertEquals("o", StubUtility.trimFieldName("f_o")); - assertEquals("M", NameComposer.trimFieldName("a_M_")); - assertEquals("bs", NameComposer.trimFieldName("a_bs_")); - assertEquals("foo_bar", NameComposer.trimFieldName("foo_bar")); - assertEquals("foo_bar", NameComposer.trimFieldName("foo_bar_")); + assertEquals("M", StubUtility.trimFieldName("a_M_")); + assertEquals("bs", StubUtility.trimFieldName("a_bs_")); + assertEquals("foo_bar", StubUtility.trimFieldName("foo_bar")); + assertEquals("foo_bar", StubUtility.trimFieldName("foo_bar_")); - assertEquals("foo_b", NameComposer.trimFieldName("foo_b_")); + assertEquals("foo_b", StubUtility.trimFieldName("foo_b_")); - assertEquals("foo", NameComposer.trimFieldName("foo")); - assertEquals("foo", NameComposer.trimFieldName("_foo")); - assertEquals("bar", NameComposer.trimFieldName("_f_bar")); + assertEquals("foo", StubUtility.trimFieldName("foo")); + assertEquals("foo", StubUtility.trimFieldName("_foo")); + assertEquals("bar", StubUtility.trimFieldName("_f_bar")); - assertEquals("f", NameComposer.trimFieldName("f__")); - assertEquals("f", NameComposer.trimFieldName("__f")); - assertEquals("O__b", NameComposer.trimFieldName("fO__b")); - assertEquals("Oo", NameComposer.trimFieldName("fOo")); - assertEquals("O", NameComposer.trimFieldName("fO")); - assertEquals("MyStatic", NameComposer.trimFieldName("sMyStatic")); - assertEquals("MyMember", NameComposer.trimFieldName("mMyMember")); + assertEquals("f", StubUtility.trimFieldName("f__")); + assertEquals("f", StubUtility.trimFieldName("__f")); + assertEquals("O__b", StubUtility.trimFieldName("fO__b")); + assertEquals("Oo", StubUtility.trimFieldName("fOo")); + assertEquals("O", StubUtility.trimFieldName("fO")); + assertEquals("MyStatic", StubUtility.trimFieldName("sMyStatic")); + assertEquals("MyMember", StubUtility.trimFieldName("mMyMember")); - assertEquals("8", NameComposer.trimFieldName("_8")); + assertEquals("8", StubUtility.trimFieldName("_8")); - assertEquals("8bar", NameComposer.trimFieldName("_8bar_")); - assertEquals("8bar_8", NameComposer.trimFieldName("_8bar_8")); - assertEquals("8bAr", NameComposer.trimFieldName("_8bAr")); - assertEquals("b8Ar", NameComposer.trimFieldName("_b8Ar")); + assertEquals("8bar", StubUtility.trimFieldName("_8bar_")); + assertEquals("8bar_8", StubUtility.trimFieldName("_8bar_8")); + assertEquals("8bAr", StubUtility.trimFieldName("_8bAr")); + assertEquals("b8Ar", StubUtility.trimFieldName("_b8Ar")); - assertEquals("Id", NameComposer.trimFieldName("Id")); - assertEquals("ID", NameComposer.trimFieldName("ID")); - assertEquals("IDS", NameComposer.trimFieldName("IDS")); - assertEquals("ID", NameComposer.trimFieldName("bID")); - assertEquals("Id", NameComposer.trimFieldName("MId")); - assertEquals("IdA", NameComposer.trimFieldName("IdA")); + assertEquals("Id", StubUtility.trimFieldName("Id")); + assertEquals("ID", StubUtility.trimFieldName("ID")); + assertEquals("IDS", StubUtility.trimFieldName("IDS")); + assertEquals("ID", StubUtility.trimFieldName("bID")); + assertEquals("Id", StubUtility.trimFieldName("MId")); + assertEquals("IdA", StubUtility.trimFieldName("IdA")); } } diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java index 0d0288ebac4..ae482ea7d8b 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CAutoIndentTest.java @@ -34,12 +34,12 @@ import org.eclipse.jface.text.TabsToSpacesConverter; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; +import org.eclipse.cdt.core.formatter.DefaultCodeFormatterOptions; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.PreferenceConstants; import org.eclipse.cdt.ui.text.ICPartitions; import org.eclipse.cdt.ui.text.doctools.DefaultMultilineCommentAutoEditStrategy; -import org.eclipse.cdt.internal.formatter.DefaultCodeFormatterOptions; import org.eclipse.cdt.internal.ui.text.CAutoIndentStrategy; import org.eclipse.cdt.internal.ui.text.CTextTools; diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CIndenterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CIndenterTest.java index 9bf315743d8..9abe5b5291f 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CIndenterTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CIndenterTest.java @@ -22,9 +22,9 @@ import org.eclipse.jface.text.source.LineRange; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; +import org.eclipse.cdt.core.formatter.DefaultCodeFormatterOptions; import org.eclipse.cdt.ui.tests.BaseUITestCase; -import org.eclipse.cdt.internal.formatter.DefaultCodeFormatterOptions; import org.eclipse.cdt.internal.ui.editor.CDocumentSetupParticipant; import org.eclipse.cdt.internal.ui.editor.IndentUtil; diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java index 08d5345b1cc..190308599d8 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java @@ -27,10 +27,10 @@ import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage; import org.eclipse.cdt.core.formatter.CodeFormatter; import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; +import org.eclipse.cdt.core.formatter.DefaultCodeFormatterOptions; import org.eclipse.cdt.ui.tests.BaseUITestCase; import org.eclipse.cdt.internal.corext.util.CodeFormatterUtil; -import org.eclipse.cdt.internal.formatter.DefaultCodeFormatterOptions; import org.eclipse.cdt.internal.formatter.align.Alignment; /** diff --git a/core/org.eclipse.cdt.ui/icons/wizban/fieldrefact_wiz.gif b/core/org.eclipse.cdt.ui/icons/wizban/fieldrefact_wiz.gif deleted file mode 100644 index f3e9221cca9..00000000000 Binary files a/core/org.eclipse.cdt.ui/icons/wizban/fieldrefact_wiz.gif and /dev/null differ diff --git a/core/org.eclipse.cdt.ui/icons/wizban/methrefact_wiz.gif b/core/org.eclipse.cdt.ui/icons/wizban/methrefact_wiz.gif deleted file mode 100644 index ec154e963b2..00000000000 Binary files a/core/org.eclipse.cdt.ui/icons/wizban/methrefact_wiz.gif and /dev/null differ diff --git a/core/org.eclipse.cdt.ui/icons/wizban/refactor_field_wiz.png b/core/org.eclipse.cdt.ui/icons/wizban/refactor_field_wiz.png new file mode 100644 index 00000000000..ff2dae47294 Binary files /dev/null and b/core/org.eclipse.cdt.ui/icons/wizban/refactor_field_wiz.png differ diff --git a/core/org.eclipse.cdt.ui/icons/wizban/refactor_method_wiz.png b/core/org.eclipse.cdt.ui/icons/wizban/refactor_method_wiz.png new file mode 100644 index 00000000000..1114876d871 Binary files /dev/null and b/core/org.eclipse.cdt.ui/icons/wizban/refactor_method_wiz.png differ diff --git a/core/org.eclipse.cdt.ui/icons/wizban/refactor_tu_wiz.png b/core/org.eclipse.cdt.ui/icons/wizban/refactor_tu_wiz.png new file mode 100644 index 00000000000..6cd52a58946 Binary files /dev/null and b/core/org.eclipse.cdt.ui/icons/wizban/refactor_tu_wiz.png differ diff --git a/core/org.eclipse.cdt.ui/icons/wizban/refactor_type_wiz.png b/core/org.eclipse.cdt.ui/icons/wizban/refactor_type_wiz.png new file mode 100644 index 00000000000..6cd52a58946 Binary files /dev/null and b/core/org.eclipse.cdt.ui/icons/wizban/refactor_type_wiz.png differ diff --git a/core/org.eclipse.cdt.ui/icons/wizban/typerefact_wiz.gif b/core/org.eclipse.cdt.ui/icons/wizban/typerefact_wiz.gif deleted file mode 100644 index ed3b49f2565..00000000000 Binary files a/core/org.eclipse.cdt.ui/icons/wizban/typerefact_wiz.gif and /dev/null differ diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/codemanipulation/StubUtility.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/codemanipulation/StubUtility.java index e11f01fabd3..e116bb286ad 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/codemanipulation/StubUtility.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/codemanipulation/StubUtility.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.UUID; import org.eclipse.core.resources.IFile; @@ -27,6 +28,7 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.preferences.IPreferencesService; import org.eclipse.core.runtime.preferences.IScopeContext; import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.jface.text.BadLocationException; @@ -45,13 +47,19 @@ import org.eclipse.text.edits.InsertEdit; import org.eclipse.text.edits.MalformedTreeException; import org.eclipse.text.edits.MultiTextEdit; +import com.ibm.icu.text.BreakIterator; + +import org.eclipse.cdt.core.CConventions; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePreferenceConstants; +import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage; +import org.eclipse.cdt.core.dom.parser.AbstractCLikeLanguage; import org.eclipse.cdt.core.model.CModelException; import org.eclipse.cdt.core.model.CoreModel; import org.eclipse.cdt.core.model.IBuffer; import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.model.ILanguage; import org.eclipse.cdt.core.model.ISourceRoot; import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.ui.CUIPlugin; @@ -64,6 +72,8 @@ import org.eclipse.cdt.internal.corext.template.c.FileTemplateContext; import org.eclipse.cdt.internal.corext.template.c.FileTemplateContextType; import org.eclipse.cdt.internal.corext.util.Strings; +import org.eclipse.cdt.internal.ui.text.CBreakIterator; +import org.eclipse.cdt.internal.ui.util.NameComposer; import org.eclipse.cdt.internal.ui.viewsupport.ProjectTemplateStore; public class StubUtility { @@ -747,4 +757,205 @@ public class StubUtility { } return result.toArray(new Template[result.size()]); } + + /** + * Returns a suggested name for a getter that is guaranteed to be a valid identifier + * and not collide with a set of given names. + * + * @param baseName the name used as an inspiration + * @param bool true if the getter is for a boolean field + * @param excluded the set of excluded names, can be {@code null} + * @param context the translation unit for which the code is intended, can be {@code null} + * @return the suggested name, or {@code null} if all possible names are taken + */ + public static String suggestGetterName(String baseName, boolean bool, Set excluded, ITranslationUnit context) { + IPreferencesService preferences = Platform.getPreferencesService(); + int capitalization = preferences.getInt(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_GETTER_CAPITALIZATION, + PreferenceConstants.NAME_STYLE_CAPITALIZATION_CAMEL_CASE, null); + String wordDelimiter = preferences.getString(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_GETTER_WORD_DELIMITER, "", null); //$NON-NLS-1$ + String prefix = bool ? + preferences.getString(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_GETTER_PREFIX_FOR_BOOLEAN, "is", null) : //$NON-NLS-1$ + preferences.getString(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_GETTER_PREFIX, "get", null); //$NON-NLS-1$ + String suffix = preferences.getString(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_GETTER_SUFFIX, "", null); //$NON-NLS-1$ + NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix); + return adjustName(composer.compose(baseName), excluded, context); + } + + /** + * Returns a suggested name for a setter that is guaranteed to be a valid identifier + * and not collide with a set of given names. + * + * @param baseName the name used as an inspiration + * @param excluded the set of excluded names, can be {@code null} + * @param context the translation unit for which the code is intended, can be {@code null} + * @return the suggested name, or {@code null} if all possible names are taken + */ + public static String suggestSetterName(String baseName, Set excluded, ITranslationUnit context) { + IPreferencesService preferences = Platform.getPreferencesService(); + int capitalization = preferences.getInt(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_SETTER_CAPITALIZATION, + PreferenceConstants.NAME_STYLE_CAPITALIZATION_CAMEL_CASE, null); + String wordDelimiter = preferences.getString(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_SETTER_WORD_DELIMITER, "", null); //$NON-NLS-1$ + String prefix = preferences.getString(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_SETTER_PREFIX, "set", null); //$NON-NLS-1$ + String suffix = preferences.getString(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_SETTER_SUFFIX, "", null); //$NON-NLS-1$ + NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix); + return adjustName(composer.compose(baseName), excluded, context); + } + + /** + * Returns a suggested name for a function parameter that is guaranteed to be a valid identifier + * and not collide with a set of given names. + * + * @param baseName the name used as an inspiration + * @param excluded the set of excluded names, can be {@code null} + * @param context the translation unit for which the code is intended, can be {@code null} + * @return the suggested name, or {@code null} if all possible names are taken + */ + public static String suggestParameterName(String baseName, Set excluded, ITranslationUnit context) { + IPreferencesService preferences = Platform.getPreferencesService(); + int capitalization = preferences.getInt(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_VARIABLE_CAPITALIZATION, + PreferenceConstants.NAME_STYLE_CAPITALIZATION_ORIGINAL, null); + String wordDelimiter = preferences.getString(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_VARIABLE_WORD_DELIMITER, "", null); //$NON-NLS-1$ + String prefix = preferences.getString(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_VARIABLE_PREFIX, "", null); //$NON-NLS-1$ + String suffix = preferences.getString(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_VARIABLE_SUFFIX, "", null); //$NON-NLS-1$ + NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix); + return adjustName(composer.compose(baseName), excluded, context); + } + + /** + * Returns a suggested name for a method that is guaranteed to be a valid identifier + * and not collide with a set of given names. + * + * @param baseName the name used as an inspiration + * @param excluded the set of excluded names, can be {@code null} + * @param context the translation unit for which the code is intended, can be {@code null} + * @return the suggested name, or {@code null} if all possible names are taken + */ + public static String suggestMethodName(String baseName, Set excluded, ITranslationUnit context) { + IPreferencesService preferences = Platform.getPreferencesService(); + int capitalization = preferences.getInt(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_METHOD_CAPITALIZATION, + PreferenceConstants.NAME_STYLE_CAPITALIZATION_ORIGINAL, null); + String wordDelimiter = preferences.getString(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_METHOD_WORD_DELIMITER, "", null); //$NON-NLS-1$ + String prefix = preferences.getString(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_METHOD_PREFIX, "", null); //$NON-NLS-1$ + String suffix = preferences.getString(CUIPlugin.PLUGIN_ID, + PreferenceConstants.NAME_STYLE_METHOD_SUFFIX, "", null); //$NON-NLS-1$ + NameComposer composer = new NameComposer(capitalization, wordDelimiter, prefix, suffix); + return adjustName(composer.compose(baseName), excluded, context); + } + + /** + * Checks is the given name is valid and, if not, tries to adjust it by adding a numeric suffix + * to it. + * + * @param name the name to check and, possibly, adjust + * @param namesToAvoid the set of names to avoid + * @param context the translation unit, can be {@code null} + * @return the adjusted name, or null if a valid name could not be generated. + */ + private static String adjustName(String name, Set namesToAvoid, ITranslationUnit context) { + ILanguage language = null; + try { + if (context != null) + language = context.getLanguage(); + } catch (CoreException e) { + // Ignore + } + return adjustName(name, namesToAvoid, language); + } + + /** + * Checks is the given name is valid and, if not, tries to adjust it by adding a numeric suffix + * to it. + * + * @param name the name to check and, possibly, adjust + * @param namesToAvoid the set of names to avoid + * @param language the language of the translation unit, can be {@code null} + * @return the adjusted name, or null if a valid name could not be generated. + */ + private static String adjustName(String name, Set namesToAvoid, ILanguage language) { + if (language == null) { + language = GPPLanguage.getDefault(); + } + String originalName = name; + if (!isValidIdentifier(name, language)) { + if ("class".equals(name)) { //$NON-NLS-1$ + name = "clazz"; //$NON-NLS-1$ + } else { + name = '_' + name; + } + } + int numTries = namesToAvoid != null ? namesToAvoid.size() + 1 : 1; + for (int i = 1; i <= numTries; i++) { + if ((namesToAvoid == null || !namesToAvoid.contains(name)) && + isValidIdentifier(name, language)) { + return name; + } + name = originalName + i; + } + return null; + } + + private static boolean isValidIdentifier(String name, ILanguage language) { + if (language instanceof AbstractCLikeLanguage) { + return CConventions.validateIdentifier(name, (AbstractCLikeLanguage) language).isOK(); + } + return true; + } + + /** + * Returns the trimmed field name. Leading and trailing non-alphanumeric characters are trimmed. + * If the first word of the name consists of a single letter and the name contains more than + * one word, the first word is removed. + * + * @param fieldName a field name to trim + * @return the trimmed field name + */ + public static String trimFieldName(String fieldName) { + CBreakIterator iterator = new CBreakIterator(); + iterator.setText(fieldName); + int firstWordStart = -1; + int firstWordEnd = -1; + int secondWordStart = -1; + int lastWordEnd = -1; + int end; + for (int start = iterator.first(); (end = iterator.next()) != BreakIterator.DONE; start = end) { + if (Character.isLetterOrDigit(fieldName.charAt(start))) { + int pos = end; + while (--pos >= start && !Character.isLetterOrDigit(fieldName.charAt(pos))) { + } + lastWordEnd = pos + 1; + if (firstWordStart < 0) { + firstWordStart = start; + firstWordEnd = lastWordEnd; + } else if (secondWordStart < 0) { + secondWordStart = start; + } + } + } + // Skip the first word if it consists of a single letter and the name contains more than + // one word. + if (firstWordStart >= 0 && firstWordStart + 1 == firstWordEnd && secondWordStart >= 0) { + firstWordStart = secondWordStart; + } + if (firstWordStart < 0) { + return fieldName; + } else { + return fieldName.substring(firstWordStart, lastWordEnd); + } + } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CPluginImages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CPluginImages.java index c600ac5bba7..e506fbfdd7d 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CPluginImages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CPluginImages.java @@ -417,9 +417,10 @@ public class CPluginImages { /** @deprecated as of CDT 8.0. Use {@link CDTSharedImages#getImageDescriptor(String)}. */ @Deprecated public static final ImageDescriptor DESC_REFACTORING_INFO= createManaged ( T_OBJ, IMG_OBJS_REFACTORING_INFO); - public static final ImageDescriptor DESC_WIZBAN_REFACTOR_FIELD= createUnManaged(T_WIZBAN, "fieldrefact_wiz.gif"); //$NON-NLS-1$ - public static final ImageDescriptor DESC_WIZBAN_REFACTOR_METHOD= createUnManaged(T_WIZBAN, "methrefact_wiz.gif"); //$NON-NLS-1$ - public static final ImageDescriptor DESC_WIZBAN_REFACTOR_TYPE= createUnManaged(T_WIZBAN, "typerefact_wiz.gif"); //$NON-NLS-1$ + public static final ImageDescriptor DESC_WIZBAN_REFACTOR_TU= createUnManaged(T_WIZBAN, "refactor_tu_wiz.png"); //$NON-NLS-1$ + public static final ImageDescriptor DESC_WIZBAN_REFACTOR_FIELD= createUnManaged(T_WIZBAN, "refactor_field_wiz.png"); //$NON-NLS-1$ + public static final ImageDescriptor DESC_WIZBAN_REFACTOR_METHOD= createUnManaged(T_WIZBAN, "refactor_method_wiz.png"); //$NON-NLS-1$ + public static final ImageDescriptor DESC_WIZBAN_REFACTOR_TYPE= createUnManaged(T_WIZBAN, "refactor_type_wiz.png"); //$NON-NLS-1$ public static final ImageDescriptor DESC_OBJS_DEFAULT_CHANGE= createUnManaged(T_OBJ, "change.gif"); //$NON-NLS-1$ public static final ImageDescriptor DESC_OBJS_COMPOSITE_CHANGE= createUnManaged(T_OBJ, "composite_change.gif"); //$NON-NLS-1$ diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/ICHelpContextIds.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/ICHelpContextIds.java index c883bbb98ee..fd83696f119 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/ICHelpContextIds.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/ICHelpContextIds.java @@ -120,6 +120,7 @@ public interface ICHelpContextIds { public static final String RENAME_TYPE_WIZARD_PAGE= PREFIX + "rename_type_wizard_page_context"; //$NON-NLS-1$ public static final String RENAME_FIELD_WIZARD_PAGE= PREFIX + "rename_field_wizard_page_context"; //$NON-NLS-1$ public static final String RENAME_RESOURCE_WIZARD_PAGE= PREFIX + "rename_resource_wizard_page_context"; //$NON-NLS-1$ + public static final String EXTRACT_FUNCTION_WIZARD_PAGE= PREFIX + "extract_function_wizard_page_context"; //$NON-NLS-1$ // Dialogs public static final String EDIT_TEMPLATE_DIALOG = PREFIX + "edit_template_dialog_context"; //$NON-NLS-1$ diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/dialogs/TableTextCellEditor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/dialogs/TableTextCellEditor.java new file mode 100644 index 00000000000..194996302b8 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/dialogs/TableTextCellEditor.java @@ -0,0 +1,452 @@ +/******************************************************************************* + * Copyright (c) 2005, 2012 IBM Corporation 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: + * IBM Corporation - initial API and implementation + * Sergey Prigogin (Google) + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.dialogs; + +import org.eclipse.core.runtime.Assert; +import org.eclipse.jface.contentassist.SubjectControlContentAssistant; +import org.eclipse.jface.viewers.CellEditor; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.osgi.util.NLS; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.FocusAdapter; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.events.MouseAdapter; +import org.eclipse.swt.events.MouseEvent; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.TraverseEvent; +import org.eclipse.swt.events.TraverseListener; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Text; + +/** + * TableTextCellEditor is a copy of TextCellEditor, with the + * following changes: + * + *
    + *
  • modify events are sent out as the text is changed, and not only after + * editing is done
  • + * + *
  • a content assistant is supported
  • + * + *
  • the user can go to the next/previous row with up and down keys
  • + *
+ */ +public class TableTextCellEditor extends CellEditor { + public interface IActivationListener { + public void activate(); + } + + private final TableViewer fTableViewer; + private final int fColumn; + private final String fProperty; + /** + * The editor's value on activation. This value is reset to the + * cell when the editor is left via ESC key. + */ + String fOriginalValue; + SubjectControlContentAssistant fContentAssistant; + private IActivationListener fActivationListener; + + protected Text text; + + private boolean isSelection; + private boolean isDeleteable; + private boolean isSelectable; + + private static final int defaultStyle = SWT.SINGLE; + private ModifyListener fModifyListener; + + public TableTextCellEditor(TableViewer tableViewer, int column) { + super(tableViewer.getTable(), defaultStyle); + fTableViewer= tableViewer; + fColumn= column; + fProperty= (String) tableViewer.getColumnProperties()[column]; + } + + @Override + public void activate() { + super.activate(); + if (fActivationListener != null) + fActivationListener.activate(); + fOriginalValue= text.getText(); + } + + private void fireModifyEvent(Object newValue) { + fTableViewer.getCellModifier().modify( + ((IStructuredSelection) fTableViewer.getSelection()).getFirstElement(), + fProperty, newValue); + } + + @Override + protected void focusLost() { + if (fContentAssistant != null && fContentAssistant.hasProposalPopupFocus()) { + // skip focus lost if it went to the content assist popup + } else { + super.focusLost(); + } + } + + public void setContentAssistant(SubjectControlContentAssistant assistant) { + fContentAssistant= assistant; + } + + public void setActivationListener(IActivationListener listener) { + fActivationListener= listener; + } + + public Text getText() { + return text; + } + + protected void checkDeleteable() { + boolean oldIsDeleteable = isDeleteable; + isDeleteable = isDeleteEnabled(); + if (oldIsDeleteable != isDeleteable) { + fireEnablementChanged(DELETE); + } + } + + protected void checkSelectable() { + boolean oldIsSelectable = isSelectable; + isSelectable = isSelectAllEnabled(); + if (oldIsSelectable != isSelectable) { + fireEnablementChanged(SELECT_ALL); + } + } + + protected void checkSelection() { + boolean oldIsSelection = isSelection; + isSelection = text.getSelectionCount() > 0; + if (oldIsSelection != isSelection) { + fireEnablementChanged(COPY); + fireEnablementChanged(CUT); + } + } + + private ModifyListener getModifyListener() { + if (fModifyListener == null) { + fModifyListener = new ModifyListener() { + @Override + public void modifyText(ModifyEvent e) { + editOccured(e); + } + }; + } + return fModifyListener; + } + + /* (non-Javadoc) + * Method declared on CellEditor. + */ + @Override + protected Control createControl(Composite parent) { + text= new Text(parent, getStyle()); + text.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetDefaultSelected(SelectionEvent e) { + handleDefaultSelection(e); + } + }); + text.addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + // support switching rows while editing: + if (e.stateMask == SWT.MOD1 || e.stateMask == SWT.MOD2) { + if (e.keyCode == SWT.ARROW_UP || e.keyCode == SWT.ARROW_DOWN) { + // allow starting multi-selection even if in edit mode + deactivate(); + e.doit= false; + return; + } + } + + if (e.stateMask != SWT.NONE) + return; + + switch (e.keyCode) { + case SWT.ARROW_DOWN: + e.doit= false; + int nextRow= fTableViewer.getTable().getSelectionIndex() + 1; + if (nextRow >= fTableViewer.getTable().getItemCount()) + break; + editRow(nextRow); + break; + + case SWT.ARROW_UP: + e.doit= false; + int prevRow= fTableViewer.getTable().getSelectionIndex() - 1; + if (prevRow < 0) + break; + editRow(prevRow); + break; + + case SWT.F2: + e.doit= false; + deactivate(); + break; + } + } + + private void editRow(int row) { + fTableViewer.getTable().setSelection(row); + IStructuredSelection newSelection= (IStructuredSelection) fTableViewer.getSelection(); + if (newSelection.size() == 1) + fTableViewer.editElement(newSelection.getFirstElement(), fColumn); + } + }); + text.addKeyListener(new KeyAdapter() { + // hook key pressed - see PR 14201 + @Override + public void keyPressed(KeyEvent e) { + keyReleaseOccured(e); + + // as a result of processing the above call, clients may have + // disposed this cell editor + if ((getControl() == null) || getControl().isDisposed()) + return; + checkSelection(); // see explaination below + checkDeleteable(); + checkSelectable(); + } + }); + text.addTraverseListener(new TraverseListener() { + @Override + public void keyTraversed(TraverseEvent e) { + if (e.detail == SWT.TRAVERSE_ESCAPE + || e.detail == SWT.TRAVERSE_RETURN) { + e.doit = false; + } + } + }); + // We really want a selection listener but it is not supported so we + // use a key listener and a mouse listener to know when selection changes + // may have occurred + text.addMouseListener(new MouseAdapter() { + @Override + public void mouseUp(MouseEvent e) { + checkSelection(); + checkDeleteable(); + checkSelectable(); + } + }); + text.addFocusListener(new FocusAdapter() { + @Override + public void focusLost(FocusEvent e) { + e.display.asyncExec(new Runnable() { + @Override + public void run() { + // without the asyncExec, focus has not had a chance to go to the content assist proposals + TableTextCellEditor.this.focusLost(); + } + }); + } + }); + text.setFont(parent.getFont()); + text.setBackground(parent.getBackground()); + text.setText("");//$NON-NLS-1$ + text.addModifyListener(getModifyListener()); + + return text; + } + + @Override + protected void fireCancelEditor() { + /* bug 58540: change signature refactoring interaction: validate as you type [refactoring] */ + text.setText(fOriginalValue); + super.fireApplyEditorValue(); + } + + /** + * The TextCellEditor implementation of + * this CellEditor framework method returns + * the text string. + * + * @return the text string + */ + @Override + protected Object doGetValue() { + return text.getText(); + } + + @Override + protected void doSetFocus() { + if (text != null) { + text.selectAll(); + text.setFocus(); + checkSelection(); + checkDeleteable(); + checkSelectable(); + } + } + + /** + * The TextCellEditor2 implementation of + * this CellEditor framework method accepts + * a text string (type String). + * + * @param value a text string (type String) + */ + @Override + protected void doSetValue(Object value) { + Assert.isTrue(text != null && (value instanceof String)); + text.removeModifyListener(getModifyListener()); + text.setText((String) value); + text.addModifyListener(getModifyListener()); + } + + /** + * Processes a modify event that occurred in this text cell editor. + * This framework method performs validation and sets the error message + * accordingly, and then reports a change via fireEditorValueChanged. + * Subclasses should call this method at appropriate times. Subclasses + * may extend or reimplement. + * + * @param e the SWT modify event + */ + protected void editOccured(ModifyEvent e) { + String value = text.getText(); + boolean oldValidState = isValueValid(); + boolean newValidState = isCorrect(value); + if (!newValidState) { + // Try to insert the current value into the error message. + setErrorMessage(NLS.bind(getErrorMessage(), value)); + } + valueChanged(oldValidState, newValidState); + fireModifyEvent(text.getText()); // update model on-the-fly + } + + @Override + public LayoutData getLayoutData() { + return new LayoutData(); + } + + protected void handleDefaultSelection(SelectionEvent event) { + // same with enter-key handling code in keyReleaseOccured(e); + fireApplyEditorValue(); + deactivate(); + } + + @Override + public boolean isCopyEnabled() { + if (text == null || text.isDisposed()) + return false; + return text.getSelectionCount() > 0; + } + + @Override + public boolean isCutEnabled() { + if (text == null || text.isDisposed()) + return false; + return text.getSelectionCount() > 0; + } + + @Override + public boolean isDeleteEnabled() { + if (text == null || text.isDisposed()) + return false; + return text.getSelectionCount() > 0 + || text.getCaretPosition() < text.getCharCount(); + } + + @Override + public boolean isPasteEnabled() { + if (text == null || text.isDisposed()) + return false; + return true; + } + + @Override + public boolean isSelectAllEnabled() { + if (text == null || text.isDisposed()) + return false; + return text.getCharCount() > 0; + } + + @Override + protected void keyReleaseOccured(KeyEvent keyEvent) { + if (keyEvent.character == '\r') { // Return key + // Enter is handled in handleDefaultSelection. + // Do not apply the editor value in response to an Enter key event + // since this can be received from the IME when the intent is -not- + // to apply the value. + // See bug 39074 [CellEditors] [DBCS] canna input mode fires bogus event from Text Control + // + // An exception is made for Ctrl+Enter for multi-line texts, since + // a default selection event is not sent in this case. + if (text != null && !text.isDisposed() && (text.getStyle() & SWT.MULTI) != 0) { + if ((keyEvent.stateMask & SWT.CTRL) != 0) { + super.keyReleaseOccured(keyEvent); + } + } + return; + } + super.keyReleaseOccured(keyEvent); + } + + @Override + public void performCopy() { + text.copy(); + } + + @Override + public void performCut() { + text.cut(); + checkSelection(); + checkDeleteable(); + checkSelectable(); + } + + @Override + public void performDelete() { + if (text.getSelectionCount() > 0) { + // remove the contents of the current selection + text.insert(""); //$NON-NLS-1$ + } else { + // remove the next character + int pos = text.getCaretPosition(); + if (pos < text.getCharCount()) { + text.setSelection(pos, pos + 1); + text.insert(""); //$NON-NLS-1$ + } + } + checkSelection(); + checkDeleteable(); + checkSelectable(); + } + + @Override + public void performPaste() { + text.paste(); + checkSelection(); + checkDeleteable(); + checkSelectable(); + } + + @Override + public void performSelectAll() { + text.selectAll(); + checkSelection(); + checkDeleteable(); + } + + @Override + protected boolean dependsOnExternalFocusListener() { + return false; + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/dialogs/TextFieldNavigationHandler.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/dialogs/TextFieldNavigationHandler.java new file mode 100644 index 00000000000..26ddbab5c4c --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/dialogs/TextFieldNavigationHandler.java @@ -0,0 +1,527 @@ +/******************************************************************************* + * Copyright (c) 2005, 2011 IBM Corporation 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: + * IBM Corporation - initial API and implementation + * Sergey Prigogin (Google) + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.dialogs; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.commands.Command; +import org.eclipse.core.commands.CommandManager; +import org.eclipse.core.commands.ParameterizedCommand; +import org.eclipse.core.commands.common.NotDefinedException; +import org.eclipse.core.commands.contexts.ContextManager; +import org.eclipse.jface.bindings.BindingManager; +import org.eclipse.jface.bindings.Scheme; +import org.eclipse.jface.bindings.TriggerSequence; +import org.eclipse.jface.bindings.keys.KeySequence; +import org.eclipse.jface.bindings.keys.SWTKeySupport; +import org.eclipse.jface.util.Util; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.events.DisposeEvent; +import org.eclipse.swt.events.DisposeListener; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.events.FocusListener; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.events.MouseAdapter; +import org.eclipse.swt.events.MouseEvent; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Combo; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Text; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.commands.ICommandService; +import org.eclipse.ui.keys.IBindingService; +import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; + +import com.ibm.icu.text.BreakIterator; + +import org.eclipse.cdt.ui.CUIPlugin; + +import org.eclipse.cdt.internal.ui.text.CWordIterator; + +/** + * Support for camelCase-aware sub-word navigation in dialog fields. + */ +public class TextFieldNavigationHandler { + + public static void install(Text text) { + new FocusHandler(new TextNavigable(text)); + } + + public static void install(StyledText styledText) { + new FocusHandler(new StyledTextNavigable(styledText)); + } + + public static void install(Combo combo) { + new FocusHandler(new ComboNavigable(combo)); + } + + private abstract static class WorkaroundNavigable extends Navigable { + /* workarounds for: + * - bug 103630: Add API: Combo#getCaretPosition() + * - bug 106024: Text#setSelection(int, int) does not handle start > end with SWT.SINGLE + */ + Point fLastSelection; + int fCaretPosition; + + void selectionChanged() { + Point selection= getSelection(); + if (selection.equals(fLastSelection)) { + // leave caret position + } else if (selection.x == selection.y) { //empty range + fCaretPosition= selection.x; + } else if (fLastSelection.y == selection.y) { + fCaretPosition= selection.x; //same end -> assume caret at start + } else { + fCaretPosition= selection.y; + } + fLastSelection= selection; + } + } + + private abstract static class Navigable { + public abstract Control getControl(); + + public abstract String getText(); + + public abstract void setText(String text); + + public abstract Point getSelection(); + + public abstract void setSelection(int start, int end); + + public abstract int getCaretPosition(); + } + + private static class TextNavigable extends WorkaroundNavigable { + static final boolean BUG_106024_TEXT_SELECTION= + "win32".equals(SWT.getPlatform()) //$NON-NLS-1$ + // on carbon, getCaretPosition() always returns getSelection().x + || Util.isMac(); + + private final Text fText; + + public TextNavigable(Text text) { + fText= text; + // workaround for bug 106024: + if (BUG_106024_TEXT_SELECTION) { + fLastSelection= getSelection(); + fCaretPosition= fLastSelection.y; + fText.addKeyListener(new KeyAdapter() { + @Override + public void keyReleased(KeyEvent e) { + selectionChanged(); + } + }); + fText.addMouseListener(new MouseAdapter() { + @Override + public void mouseUp(MouseEvent e) { + selectionChanged(); + } + }); + } + } + + @Override + public Control getControl() { + return fText; + } + + @Override + public String getText() { + return fText.getText(); + } + + @Override + public void setText(String text) { + fText.setText(text); + } + + @Override + public Point getSelection() { + return fText.getSelection(); + } + + @Override + public int getCaretPosition() { + if (BUG_106024_TEXT_SELECTION) { + selectionChanged(); + return fCaretPosition; + } else { + return fText.getCaretPosition(); + } + } + + @Override + public void setSelection(int start, int end) { + fText.setSelection(start, end); + } + } + + private static class StyledTextNavigable extends Navigable { + private final StyledText fStyledText; + + public StyledTextNavigable(StyledText styledText) { + fStyledText= styledText; + } + + @Override + public Control getControl() { + return fStyledText; + } + + @Override + public String getText() { + return fStyledText.getText(); + } + + @Override + public void setText(String text) { + fStyledText.setText(text); + } + + @Override + public Point getSelection() { + return fStyledText.getSelection(); + } + + @Override + public int getCaretPosition() { + return fStyledText.getCaretOffset(); + } + + @Override + public void setSelection(int start, int end) { + fStyledText.setSelection(start, end); + } + } + + private static class ComboNavigable extends WorkaroundNavigable { + private final Combo fCombo; + + public ComboNavigable(Combo combo) { + fCombo= combo; + // workaround for bug 103630: + fLastSelection= getSelection(); + fCaretPosition= fLastSelection.y; + fCombo.addKeyListener(new KeyAdapter() { + @Override + public void keyReleased(KeyEvent e) { + selectionChanged(); + } + }); + fCombo.addMouseListener(new MouseAdapter() { + @Override + public void mouseUp(MouseEvent e) { + selectionChanged(); + } + }); + } + + @Override + public Control getControl() { + return fCombo; + } + + @Override + public String getText() { + return fCombo.getText(); + } + + @Override + public void setText(String text) { + fCombo.setText(text); + } + + @Override + public Point getSelection() { + return fCombo.getSelection(); + } + + @Override + public int getCaretPosition() { + selectionChanged(); + return fCaretPosition; +// return fCombo.getCaretPosition(); // not available: bug 103630 + } + + @Override + public void setSelection(int start, int end) { + fCombo.setSelection(new Point(start, end)); + } + } + + private static class FocusHandler implements FocusListener { + private static final String EMPTY_TEXT= ""; //$NON-NLS-1$ + + private final CWordIterator fIterator; + private final Navigable fNavigable; + private KeyAdapter fKeyListener; + + private FocusHandler(Navigable navigable) { + fIterator= new CWordIterator(); + fNavigable= navigable; + + Control control= navigable.getControl(); + control.addFocusListener(this); + if (control.isFocusControl()) + activate(); + control.addDisposeListener(new DisposeListener() { + @Override + public void widgetDisposed(DisposeEvent e) { + deactivate(); + } + }); + } + + @Override + public void focusGained(FocusEvent e) { + activate(); + } + + @Override + public void focusLost(FocusEvent e) { + deactivate(); + } + + private void activate() { + fNavigable.getControl().addKeyListener(getKeyListener()); + } + + private void deactivate() { + if (fKeyListener != null) { + Control control= fNavigable.getControl(); + if (! control.isDisposed()) + control.removeKeyListener(fKeyListener); + fKeyListener= null; + } + } + + private KeyAdapter getKeyListener() { + if (fKeyListener == null) { + fKeyListener= new KeyAdapter() { + private final boolean IS_WORKAROUND= (fNavigable instanceof ComboNavigable) + || (fNavigable instanceof TextNavigable && TextNavigable.BUG_106024_TEXT_SELECTION); + private List fSubmissions; + + @Override + public void keyPressed(KeyEvent e) { + if (IS_WORKAROUND) { + if (e.keyCode == SWT.ARROW_LEFT && e.stateMask == SWT.MOD2) { + int caretPosition= fNavigable.getCaretPosition(); + if (caretPosition != 0) { + Point selection= fNavigable.getSelection(); + if (caretPosition == selection.x) + fNavigable.setSelection(selection.y, caretPosition - 1); + else + fNavigable.setSelection(selection.x, caretPosition - 1); + } + e.doit= false; + return; + + } else if (e.keyCode == SWT.ARROW_RIGHT && e.stateMask == SWT.MOD2) { + String text= fNavigable.getText(); + int caretPosition= fNavigable.getCaretPosition(); + if (caretPosition != text.length()) { + Point selection= fNavigable.getSelection(); + if (caretPosition == selection.y) + fNavigable.setSelection(selection.x, caretPosition + 1); + else + fNavigable.setSelection(selection.y, caretPosition + 1); + } + e.doit= false; + return; + } + } + int accelerator = SWTKeySupport.convertEventToUnmodifiedAccelerator(e); + KeySequence keySequence = KeySequence.getInstance(SWTKeySupport.convertAcceleratorToKeyStroke(accelerator)); + for (Submission submission : getSubmissions()) { + TriggerSequence[] triggerSequences= submission.getTriggerSequences(); + for (int i= 0; i < triggerSequences.length; i++) { + if (triggerSequences[i].equals(keySequence)) { // XXX does not work for multi-stroke bindings + e.doit= false; + submission.execute(); + return; + } + } + } + } + + private List getSubmissions() { + if (fSubmissions != null) + return fSubmissions; + + fSubmissions= new ArrayList(); + + ICommandService commandService= (ICommandService) PlatformUI.getWorkbench().getAdapter(ICommandService.class); + IBindingService bindingService= (IBindingService) PlatformUI.getWorkbench().getAdapter(IBindingService.class); + if (commandService == null || bindingService == null) + return fSubmissions; + + // Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=184502 , + // similar to CodeAssistAdvancedConfigurationBlock.getKeyboardShortcut(..): + BindingManager localBindingManager= new BindingManager(new ContextManager(), new CommandManager()); + final Scheme[] definedSchemes= bindingService.getDefinedSchemes(); + if (definedSchemes != null) { + try { + for (int i = 0; i < definedSchemes.length; i++) { + Scheme scheme= definedSchemes[i]; + Scheme localSchemeCopy= localBindingManager.getScheme(scheme.getId()); + localSchemeCopy.define(scheme.getName(), scheme.getDescription(), scheme.getParentId()); + } + } catch (final NotDefinedException e) { + CUIPlugin.log(e); + } + } + localBindingManager.setLocale(bindingService.getLocale()); + localBindingManager.setPlatform(bindingService.getPlatform()); + + localBindingManager.setBindings(bindingService.getBindings()); + try { + Scheme activeScheme= bindingService.getActiveScheme(); + if (activeScheme != null) + localBindingManager.setActiveScheme(activeScheme); + } catch (NotDefinedException e) { + CUIPlugin.log(e); + } + + fSubmissions.add(new Submission(getKeyBindings(localBindingManager, commandService, ITextEditorActionDefinitionIds.SELECT_WORD_NEXT)) { + @Override + public void execute() { + fIterator.setText(fNavigable.getText()); + int caretPosition= fNavigable.getCaretPosition(); + int newCaret= fIterator.following(caretPosition); + if (newCaret != BreakIterator.DONE) { + Point selection= fNavigable.getSelection(); + if (caretPosition == selection.y) + fNavigable.setSelection(selection.x, newCaret); + else + fNavigable.setSelection(selection.y, newCaret); + } + fIterator.setText(EMPTY_TEXT); + } + }); + fSubmissions.add(new Submission(getKeyBindings(localBindingManager, commandService, ITextEditorActionDefinitionIds.SELECT_WORD_PREVIOUS)) { + @Override + public void execute() { + fIterator.setText(fNavigable.getText()); + int caretPosition= fNavigable.getCaretPosition(); + int newCaret= fIterator.preceding(caretPosition); + if (newCaret != BreakIterator.DONE) { + Point selection= fNavigable.getSelection(); + if (caretPosition == selection.x) + fNavigable.setSelection(selection.y, newCaret); + else + fNavigable.setSelection(selection.x, newCaret); + } + fIterator.setText(EMPTY_TEXT); + } + }); + fSubmissions.add(new Submission(getKeyBindings(localBindingManager, commandService, ITextEditorActionDefinitionIds.WORD_NEXT)) { + @Override + public void execute() { + fIterator.setText(fNavigable.getText()); + int caretPosition= fNavigable.getCaretPosition(); + int newCaret= fIterator.following(caretPosition); + if (newCaret != BreakIterator.DONE) + fNavigable.setSelection(newCaret, newCaret); + fIterator.setText(EMPTY_TEXT); + } + }); + fSubmissions.add(new Submission(getKeyBindings(localBindingManager, commandService, ITextEditorActionDefinitionIds.WORD_PREVIOUS)) { + @Override + public void execute() { + fIterator.setText(fNavigable.getText()); + int caretPosition= fNavigable.getCaretPosition(); + int newCaret= fIterator.preceding(caretPosition); + if (newCaret != BreakIterator.DONE) + fNavigable.setSelection(newCaret, newCaret); + fIterator.setText(EMPTY_TEXT); + } + }); + fSubmissions.add(new Submission(getKeyBindings(localBindingManager, commandService, ITextEditorActionDefinitionIds.DELETE_NEXT_WORD)) { + @Override + public void execute() { + Point selection= fNavigable.getSelection(); + String text= fNavigable.getText(); + int start; + int end; + if (selection.x != selection.y) { + start= selection.x; + end= selection.y; + } else { + fIterator.setText(text); + start= fNavigable.getCaretPosition(); + end= fIterator.following(start); + fIterator.setText(EMPTY_TEXT); + if (end == BreakIterator.DONE) + return; + } + fNavigable.setText(text.substring(0, start) + text.substring(end)); + fNavigable.setSelection(start, start); + } + }); + fSubmissions.add(new Submission(getKeyBindings(localBindingManager, commandService, ITextEditorActionDefinitionIds.DELETE_PREVIOUS_WORD)) { + @Override + public void execute() { + Point selection= fNavigable.getSelection(); + String text= fNavigable.getText(); + int start; + int end; + if (selection.x != selection.y) { + start= selection.x; + end= selection.y; + } else { + fIterator.setText(text); + end= fNavigable.getCaretPosition(); + start= fIterator.preceding(end); + fIterator.setText(EMPTY_TEXT); + if (start == BreakIterator.DONE) + return; + } + fNavigable.setText(text.substring(0, start) + text.substring(end)); + fNavigable.setSelection(start, start); + } + }); + + return fSubmissions; + } + + private TriggerSequence[] getKeyBindings(BindingManager localBindingManager, ICommandService commandService, String commandID) { + Command command= commandService.getCommand(commandID); + ParameterizedCommand pCmd= new ParameterizedCommand(command, null); + return localBindingManager.getActiveBindingsDisregardingContextFor(pCmd); + } + + }; + } + return fKeyListener; + } + } + + private abstract static class Submission { + private TriggerSequence[] fTriggerSequences; + + public Submission(TriggerSequence[] triggerSequences) { + fTriggerSequences= triggerSequences; + } + + public TriggerSequence[] getTriggerSequences() { + return fTriggerSequences; + } + + public abstract void execute(); + } + +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CSourceViewer.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CSourceViewer.java index 022816a5f6c..956df220a00 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CSourceViewer.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CSourceViewer.java @@ -7,7 +7,7 @@ * * Contributors: * QNX Software Systems - initial API and implementation - * Sergey Prigogin, Google + * Sergey Prigogin (Google) * Anton Leherbauer (Wind River Systems) * Markus Schorn (Wind River Systems) *******************************************************************************/ @@ -43,19 +43,23 @@ import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants; import org.eclipse.ui.texteditor.AbstractTextEditor; +import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.PreferenceConstants; import org.eclipse.cdt.ui.text.CSourceViewerConfiguration; +import org.eclipse.cdt.ui.text.ICColorConstants; +import org.eclipse.cdt.ui.text.IColorManager; +import org.eclipse.cdt.internal.ui.text.CTextTools; /** * Source viewer for C/C++ et al. */ public class CSourceViewer extends ProjectionViewer implements IPropertyChangeListener { - /** Show outline operation id. */ public static final int SHOW_OUTLINE= 101; /** Show type hierarchy operation id. */ @@ -620,4 +624,32 @@ public class CSourceViewer extends ProjectionViewer implements IPropertyChangeLi cmd.event= null; cmd.text= null; } + + /** + * Sets the viewer's background color to the given control's background color. + * The background color is only set if it's visibly distinct from the + * default Java source text color. + * + * @param control the control with the default background color + */ + public void adaptBackgroundColor(Control control) { + // Workaround for dark editor background color, see https://bugs.eclipse.org/330680 + Color defaultColor= control.getBackground(); + float[] defaultBgHSB= defaultColor.getRGB().getHSB(); + + CTextTools textTools= CUIPlugin.getDefault().getTextTools(); + IColorManager manager= textTools.getColorManager(); + Color cDefaultColor= manager.getColor(ICColorConstants.C_DEFAULT); + RGB cDefaultRGB= cDefaultColor != null ? + cDefaultColor.getRGB() : new RGB(255, 255, 255); + float[] javaDefaultHSB= cDefaultRGB.getHSB(); + + if (Math.abs(defaultBgHSB[2] - javaDefaultHSB[2]) >= 0.5f) { + getTextWidget().setBackground(defaultColor); + if (fBackgroundColor != null) { + fBackgroundColor.dispose(); + fBackgroundColor= null; + } + } + } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/NameStyleBlock.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/NameStyleBlock.java index f8356f1405a..6ef9c311a3a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/NameStyleBlock.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/NameStyleBlock.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 Google, Inc and others. + * Copyright (c) 2011, 2012 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 @@ -34,6 +34,8 @@ import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer; import org.eclipse.cdt.ui.PreferenceConstants; import org.eclipse.cdt.utils.ui.controls.ControlFactory; +import org.eclipse.cdt.internal.corext.codemanipulation.StubUtility; + import org.eclipse.cdt.internal.ui.dialogs.IStatusChangeListener; import org.eclipse.cdt.internal.ui.dialogs.StatusInfo; import org.eclipse.cdt.internal.ui.util.NameComposer; @@ -51,6 +53,7 @@ public class NameStyleBlock extends OptionsConfigurationBlock { private static final String EXAMPLE_CONSTANT_NAME = "MY_CONSTANT"; //$NON-NLS-1$ private static final String EXAMPLE_VARIABLE_NAME = "myVariable"; //$NON-NLS-1$ private static final String EXAMPLE_FIELD_NAME = "myField"; //$NON-NLS-1$ + private static final String EXAMPLE_METHOD_NAME = "myMethod"; //$NON-NLS-1$ private static final String EXAMPLE_CLASS_NAME = "MyClass"; //$NON-NLS-1$ private final String[] CAPITALIZATION_VALUES = { @@ -81,6 +84,10 @@ public class NameStyleBlock extends OptionsConfigurationBlock { private static final Key KEY_FIELD_WORD_DELIMITER = getCDTUIKey(PreferenceConstants.NAME_STYLE_FIELD_WORD_DELIMITER); private static final Key KEY_FIELD_PREFIX = getCDTUIKey(PreferenceConstants.NAME_STYLE_FIELD_PREFIX); private static final Key KEY_FIELD_SUFFIX = getCDTUIKey(PreferenceConstants.NAME_STYLE_FIELD_SUFFIX); + private static final Key KEY_METHOD_CAPITALIZATION = getCDTUIKey(PreferenceConstants.NAME_STYLE_METHOD_CAPITALIZATION); + private static final Key KEY_METHOD_WORD_DELIMITER = getCDTUIKey(PreferenceConstants.NAME_STYLE_METHOD_WORD_DELIMITER); + private static final Key KEY_METHOD_PREFIX = getCDTUIKey(PreferenceConstants.NAME_STYLE_METHOD_PREFIX); + private static final Key KEY_METHOD_SUFFIX = getCDTUIKey(PreferenceConstants.NAME_STYLE_METHOD_SUFFIX); private static final Key KEY_GETTER_CAPITALIZATION = getCDTUIKey(PreferenceConstants.NAME_STYLE_GETTER_CAPITALIZATION); private static final Key KEY_GETTER_WORD_DELIMITER = getCDTUIKey(PreferenceConstants.NAME_STYLE_GETTER_WORD_DELIMITER); private static final Key KEY_GETTER_PREFIX = getCDTUIKey(PreferenceConstants.NAME_STYLE_GETTER_PREFIX); @@ -120,6 +127,10 @@ public class NameStyleBlock extends OptionsConfigurationBlock { KEY_FIELD_WORD_DELIMITER, KEY_FIELD_PREFIX, KEY_FIELD_SUFFIX, + KEY_METHOD_CAPITALIZATION, + KEY_METHOD_WORD_DELIMITER, + KEY_METHOD_PREFIX, + KEY_METHOD_SUFFIX, KEY_GETTER_CAPITALIZATION, KEY_GETTER_WORD_DELIMITER, KEY_GETTER_PREFIX, @@ -182,6 +193,14 @@ public class NameStyleBlock extends OptionsConfigurationBlock { .setPrefixKey(KEY_FIELD_PREFIX) .setSuffixKey(KEY_FIELD_SUFFIX) .setNameValidator(IDENTIFIER_VALIDATOR); + new Category(PreferencesMessages.NameStyleBlock_method_node, + PreferencesMessages.NameStyleBlock_method_node_description, EXAMPLE_METHOD_NAME, + codeCategory) + .setCapitalizationKey(KEY_METHOD_CAPITALIZATION) + .setWordDelimiterKey(KEY_METHOD_WORD_DELIMITER) + .setPrefixKey(KEY_METHOD_PREFIX) + .setSuffixKey(KEY_METHOD_SUFFIX) + .setNameValidator(IDENTIFIER_VALIDATOR); new Category(PreferencesMessages.NameStyleBlock_getter_node, PreferencesMessages.NameStyleBlock_getter_node_description, EXAMPLE_FIELD_NAME, codeCategory) @@ -572,7 +591,7 @@ public class NameStyleBlock extends OptionsConfigurationBlock { String name = seedNameGenerator != null ? seedNameGenerator.composeExampleName(settings) : seedName; if (trimFieldName) { - name = NameComposer.trimFieldName(name); + name = StubUtility.trimFieldName(name); } return composer.compose(name); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.java index a4d76943d35..97fa545a614 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.java @@ -367,6 +367,8 @@ public final class PreferencesMessages extends NLS { public static String NameStyleBlock_variable_node_description; public static String NameStyleBlock_field_node; public static String NameStyleBlock_field_node_description; + public static String NameStyleBlock_method_node; + public static String NameStyleBlock_method_node_description; public static String NameStyleBlock_getter_node; public static String NameStyleBlock_getter_node_description; public static String NameStyleBlock_setter_node; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties index aa5574d29d8..b7cf751fbfe 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties @@ -420,8 +420,10 @@ NameStyleBlock_constant_node=Constant NameStyleBlock_constant_node_description=Constant name NameStyleBlock_variable_node=Variable NameStyleBlock_variable_node_description=Variable name -NameStyleBlock_field_node=Class field +NameStyleBlock_field_node=Class Field NameStyleBlock_field_node_description=Class field name +NameStyleBlock_method_node=Class Method +NameStyleBlock_method_node_description=Class method name NameStyleBlock_getter_node=Getter Method NameStyleBlock_getter_node_description=Getter name based on the field name NameStyleBlock_setter_node=Setter Method diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/CRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/CRefactoring.java index eac0efc7cbb..98a5f5a1656 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/CRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/CRefactoring.java @@ -66,6 +66,7 @@ public abstract class CRefactoring extends Refactoring { protected String name = Messages.Refactoring_name; protected IFile file; + protected final ITranslationUnit tu; protected Region region; protected RefactoringStatus initStatus; protected IASTTranslationUnit ast; @@ -76,11 +77,10 @@ public abstract class CRefactoring extends Refactoring { project = proj; if (element instanceof ISourceReference) { ISourceReference sourceRef= (ISourceReference) element; - ITranslationUnit tu = sourceRef.getTranslationUnit(); + this.tu = sourceRef.getTranslationUnit(); IResource res= tu.getResource(); if (res instanceof IFile) this.file= (IFile) res; - try { final ISourceRange sourceRange = sourceRef.getSourceRange(); this.region = new Region(sourceRange.getIdStartPos(), sourceRange.getIdLength()); @@ -89,6 +89,7 @@ public abstract class CRefactoring extends Refactoring { } } else { this.file = file; + this.tu = (ITranslationUnit) CCorePlugin.getDefault().getCoreModel().create(file); this.region = SelectionHelper.getRegion(selection); } @@ -235,10 +236,10 @@ public abstract class CRefactoring extends Refactoring { protected boolean loadTranslationUnit(RefactoringStatus status, IProgressMonitor mon) { SubMonitor subMonitor = SubMonitor.convert(mon, 10); - if (file != null) { + if (tu != null) { try { subMonitor.subTask(Messages.Refactoring_PM_ParseTU); - ast = loadTranslationUnit(file); + ast = tu.getAST(fIndex, AST_STYLE); if (ast == null) { subMonitor.done(); return false; @@ -256,7 +257,7 @@ public abstract class CRefactoring extends Refactoring { return false; } } else { - status.addFatalError(Messages.NO_FILE); + status.addFatalError(NLS.bind(Messages.CRefactoring_FileNotFound, tu.getPath().toString())); subMonitor.done(); return false; } @@ -264,15 +265,6 @@ public abstract class CRefactoring extends Refactoring { return true; } - protected IASTTranslationUnit loadTranslationUnit(IFile file) throws CoreException { - ITranslationUnit tu = (ITranslationUnit) CCorePlugin.getDefault().getCoreModel().create(file); - if (tu == null) { - initStatus.addFatalError(NLS.bind(Messages.CRefactoring_FileNotFound, file.getName())); - return null; - } - return tu.getAST(fIndex, AST_STYLE); - } - protected boolean translationUnitHasProblem() { ProblemFinder pf = new ProblemFinder(initStatus); ast.accept(pf); @@ -305,6 +297,13 @@ public abstract class CRefactoring extends Refactoring { return fIndex; } + /** + * Returns the translation unit where the refactoring started. + */ + public ITranslationUnit getTranslationUnit() { + return tu; + } + public IASTTranslationUnit getUnit() { return ast; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/CRefactoring2.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/CRefactoring2.java index cb2d941aaa2..8ca463f7655 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/CRefactoring2.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/CRefactoring2.java @@ -239,6 +239,13 @@ public abstract class CRefactoring2 extends Refactoring { return name; } + /** + * Returns the translation unit where the refactoring started. + */ + public ITranslationUnit getTranslationUnit() { + return tu; + } + protected IASTTranslationUnit getAST(ITranslationUnit tu, IProgressMonitor pm) throws CoreException, OperationCanceledException { return astCache.getAST(tu, pm); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ChangeParametersControl.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ChangeParametersControl.java new file mode 100644 index 00000000000..dd3d10f796c --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ChangeParametersControl.java @@ -0,0 +1,815 @@ +/******************************************************************************* + * Copyright (c) 2000, 2011 IBM Corporation 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: + * IBM Corporation - initial API and implementation + * Sergey Prigogin (Google) + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.refactoring; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.eclipse.core.runtime.Assert; +import org.eclipse.jface.contentassist.SubjectControlContentAssistant; +import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.jface.viewers.CellEditor; +import org.eclipse.jface.viewers.ColumnWeightData; +import org.eclipse.jface.viewers.ComboBoxCellEditor; +import org.eclipse.jface.viewers.ICellModifier; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionChangedListener; +import org.eclipse.jface.viewers.IStructuredContentProvider; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.ITableFontProvider; +import org.eclipse.jface.viewers.ITableLabelProvider; +import org.eclipse.jface.viewers.LabelProvider; +import org.eclipse.jface.viewers.SelectionChangedEvent; +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.CCombo; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.TraverseEvent; +import org.eclipse.swt.events.TraverseListener; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableColumn; +import org.eclipse.swt.widgets.TableItem; +import org.eclipse.swt.widgets.Text; +import org.eclipse.ui.contentassist.ContentAssistHandler; + +import org.eclipse.cdt.internal.corext.codemanipulation.StubUtility; + +import org.eclipse.cdt.internal.ui.dialogs.TableTextCellEditor; +import org.eclipse.cdt.internal.ui.dialogs.TextFieldNavigationHandler; +import org.eclipse.cdt.internal.ui.util.SWTUtil; +import org.eclipse.cdt.internal.ui.util.TableLayoutComposite; + +/** + * A special control to edit and reorder method parameters. + */ +public class ChangeParametersControl extends Composite { + + public static enum Mode { + EXTRACT_METHOD, EXTRACT_METHOD_FIXED_RETURN, CHANGE_METHOD_SIGNATURE, INTRODUCE_PARAMETER; + + public boolean canChangeTypes() { + return this == CHANGE_METHOD_SIGNATURE; + } + + public boolean canAddParameters() { + return this == Mode.CHANGE_METHOD_SIGNATURE; + } + + public boolean canChangeDefault() { + return this == Mode.CHANGE_METHOD_SIGNATURE; + } + + public boolean shouldShowDirection() { + return this == Mode.EXTRACT_METHOD || this == Mode.EXTRACT_METHOD_FIXED_RETURN; + } + + public boolean canChangeReturn() { + return this == Mode.EXTRACT_METHOD; + } + } + + private static class NameInformationContentProvider implements IStructuredContentProvider { + @Override + @SuppressWarnings("unchecked") + public Object[] getElements(Object inputElement) { + return removeMarkedAsDeleted((List) inputElement); + } + + private NameInformation[] removeMarkedAsDeleted(List params) { + List result= new ArrayList(params.size()); + for (Iterator iter= params.iterator(); iter.hasNext();) { + NameInformation info= iter.next(); + if (!info.isDeleted()) + result.add(info); + } + return result.toArray(new NameInformation[result.size()]); + } + + @Override + public void dispose() { + // do nothing + } + + @Override + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { + // do nothing + } + } + + private class NameInformationLabelProvider extends LabelProvider + implements ITableLabelProvider, ITableFontProvider { + @Override + public Image getColumnImage(Object element, int columnIndex) { + return null; + } + + @Override + public String getColumnText(Object element, int columnIndex) { + NameInformation info= (NameInformation) element; + if (columnIndex == indexType) { + return info.getTypeName(); + } else if (columnIndex == indexDirection) { + return getDirectionLabel(info); + } else if (columnIndex == indexName) { + return info.getNewName(); + } else if (columnIndex == indexDefault) { + if (info.isAdded()) { + return info.getDefaultValue(); + } else { + return "-"; //$NON-NLS-1$ + } + } else { + throw new IllegalArgumentException(columnIndex + ": " + element); //$NON-NLS-1$ + } + } + + @Override + public Font getFont(Object element, int columnIndex) { + NameInformation info= (NameInformation) element; + if (info.isAdded()) { + return JFaceResources.getFontRegistry().getBold(JFaceResources.DIALOG_FONT); + } + return null; + } + } + + private class ParametersCellModifier implements ICellModifier { + @Override + public boolean canModify(Object element, String property) { + Assert.isTrue(element instanceof NameInformation); + if (property.equals(columnProperties[indexType])) { + return fMode.canChangeTypes(); + } else if (property.equals(columnProperties[indexDirection])) { + return fMode.canChangeReturn() && ((NameInformation) element).isOutput(); + } else if (property.equals(columnProperties[indexName])) { + return true; + } else if (property.equals(columnProperties[indexDefault])) { + return ((NameInformation) element).isAdded(); + } + Assert.isTrue(false); + return false; + } + + @Override + public Object getValue(Object element, String property) { + Assert.isTrue(element instanceof NameInformation); + if (property.equals(columnProperties[indexType])) { + return ((NameInformation) element).getTypeName(); + } else if (property.equals(columnProperties[indexDirection])) { + return ((NameInformation) element).isReturnValue() ? INDEX_RETURN : INDEX_OUTPUT; + } else if (property.equals(columnProperties[indexName])) { + return ((NameInformation) element).getNewName(); + } else if (property.equals(columnProperties[indexDefault])) { + return ((NameInformation) element).getDefaultValue(); + } + Assert.isTrue(false); + return null; + } + + @Override + public void modify(Object element, String property, Object value) { + if (element instanceof TableItem) + element= ((TableItem) element).getData(); + if (!(element instanceof NameInformation)) + return; + String[] columnsToUpdate = new String[] { property }; + boolean unchanged; + NameInformation parameterInfo= (NameInformation) element; + if (property.equals(columnProperties[indexType])) { + unchanged= parameterInfo.getTypeName().equals(value); + parameterInfo.setTypeName((String) value); + } else if (property.equals(columnProperties[indexDirection])) { + columnsToUpdate = new String[] { property, columnProperties[indexType] }; + boolean isReturn = value.equals(INDEX_RETURN); + unchanged= isReturn == parameterInfo.isReturnValue(); + if (!unchanged && isReturn) { + for (NameInformation param : fParameters) { + if (param != parameterInfo && param.isOutput()) { + param.setReturnValue(false); + ChangeParametersControl.this.fListener.parameterChanged(param); + ChangeParametersControl.this.fTableViewer.update(param, columnsToUpdate); + break; + } + } + } + parameterInfo.setReturnValue(isReturn); + } else if (property.equals(columnProperties[indexName])) { + unchanged= parameterInfo.getNewName().equals(value); + parameterInfo.setNewName((String) value); + } else if (property.equals(columnProperties[indexDefault])) { + unchanged= parameterInfo.getDefaultValue().equals(value); + parameterInfo.setDefaultValue((String) value); + } else { + throw new IllegalStateException(); + } + if (!unchanged) { + ChangeParametersControl.this.fListener.parameterChanged(parameterInfo); + ChangeParametersControl.this.fTableViewer.update(parameterInfo, columnsToUpdate); + } + } + } + + private static class DirectionCellEditor extends ComboBoxCellEditor { + DirectionCellEditor(Table table) { + super(table, + new String[] { + /* INDEX_OUTPUT */ Messages.ChangeParametersControl_output, + /* INDEX_RETURN */ Messages.ChangeParametersControl_return}, + SWT.READ_ONLY); + } + + @Override + protected Control createControl(Composite parent) { + final CCombo comboBox = (CCombo) super.createControl(parent); + comboBox.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent event) { + fireApplyEditorValue(); + } + }); + return comboBox; + } + } + + private final String[] columnProperties; + private final int indexType; + private final int indexDirection; + private final int indexName; + private final int indexDefault; + + private static final int ROW_COUNT= 7; + + static final Integer INDEX_OUTPUT = 0; + static final Integer INDEX_RETURN = 1; + + private final Mode fMode; + private final IParameterListChangeListener fListener; + private List fParameters; + private final StubTypeContext fTypeContext; + private final String[] fParamNameProposals; + private ContentAssistHandler fNameContentAssistHandler; + + private TableViewer fTableViewer; + private Button fUpButton; + private Button fDownButton; + private Button fEditButton; + private Button fAddButton; + private Button fRemoveButton; + + public ChangeParametersControl(Composite parent, int style, String label, + IParameterListChangeListener listener, Mode mode, StubTypeContext typeContext) { + this(parent, style, label, listener, mode, typeContext, new String[0]); + } + + public ChangeParametersControl(Composite parent, int style, String label, + IParameterListChangeListener listener, Mode mode) { + this(parent, style, label, listener, mode, null, new String[0]); + } + + public ChangeParametersControl(Composite parent, int style, String label, + IParameterListChangeListener listener, Mode mode, String[] paramNameProposals) { + this(parent, style, label, listener, mode, null, paramNameProposals); + } + + /** + * @param label the label before the table or null + * @param typeContext the package in which to complete types + */ + private ChangeParametersControl(Composite parent, int style, String label, + IParameterListChangeListener listener, Mode mode, StubTypeContext typeContext, + String[] paramNameProposals) { + super(parent, style); + Assert.isNotNull(listener); + fListener= listener; + fMode= mode; + fTypeContext= typeContext; + fParamNameProposals= paramNameProposals; + + ArrayList properties = new ArrayList(); + indexType = properties.size(); + properties.add("type"); //$NON-NLS-1$ + + if (fMode.shouldShowDirection()) { + indexDirection = properties.size(); + properties.add("direction"); //$NON-NLS-1$ + } else { + indexDirection = -1; + } + + indexName = properties.size(); + properties.add("name"); //$NON-NLS-1$ + + if (fMode.canChangeDefault()) { + indexDefault = properties.size(); + properties.add("default"); //$NON-NLS-1$ + } else { + indexDefault = -1; + } + columnProperties = properties.toArray(new String[properties.size()]); + + GridLayout layout= new GridLayout(); + layout.numColumns= 2; + layout.marginWidth= 0; + layout.marginHeight= 0; + setLayout(layout); + + if (label != null) { + Label tableLabel= new Label(this, SWT.NONE); + GridData labelGd= new GridData(); + labelGd.horizontalSpan= 2; + tableLabel.setLayoutData(labelGd); + tableLabel.setText(label); + } + + createParameterList(this); + createButtonComposite(this); + } + + public void setInput(List parameterInfos) { + Assert.isNotNull(parameterInfos); + fParameters= parameterInfos; + fTableViewer.setInput(fParameters); + if (fParameters.size() > 0) + fTableViewer.setSelection(new StructuredSelection(fParameters.get(0))); + } + + public void editParameter(NameInformation info) { + fTableViewer.getControl().setFocus(); + if (!info.isDeleted()) { + fTableViewer.setSelection(new StructuredSelection(info), true); + updateButtonsEnabledState(); + editColumnOrNextPossible(indexName); + return; + } + } + + // ---- Parameter table ----------------------------------------------------------------------------------- + + private void createParameterList(Composite parent) { + TableLayoutComposite layouter= new TableLayoutComposite(parent, SWT.NONE); + addColumnLayoutData(layouter); + + final Table table= new Table(layouter, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION); + table.setHeaderVisible(true); + table.setLinesVisible(true); + TableColumn tc; + tc= new TableColumn(table, SWT.NONE, indexType); + tc.setResizable(true); + tc.setText(Messages.ChangeParametersControl_table_type); + + if (indexDirection >= 0) { + tc= new TableColumn(table, SWT.NONE, indexDirection); + tc.setResizable(true); + tc.setText(Messages.ChangeParametersControl_table_direction); + } + + tc= new TableColumn(table, SWT.NONE, indexName); + tc.setResizable(true); + tc.setText(Messages.ChangeParametersControl_table_name); + + if (indexDefault >= 0) { + tc= new TableColumn(table, SWT.NONE, indexDefault); + tc.setResizable(true); + tc.setText(Messages.ChangeParametersControl_table_default_value); + } + + GridData gd= new GridData(GridData.FILL_BOTH); + gd.heightHint= SWTUtil.getTableHeightHint(table, ROW_COUNT); + gd.widthHint= 40; + layouter.setLayoutData(gd); + + fTableViewer= new TableViewer(table); + fTableViewer.setUseHashlookup(true); + fTableViewer.setContentProvider(new NameInformationContentProvider()); + fTableViewer.setLabelProvider(new NameInformationLabelProvider()); + fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() { + @Override + public void selectionChanged(SelectionChangedEvent event) { + updateButtonsEnabledState(); + } + }); + + table.addTraverseListener(new TraverseListener() { + @Override + public void keyTraversed(TraverseEvent e) { + if (e.detail == SWT.TRAVERSE_RETURN && e.stateMask == SWT.NONE) { + editColumnOrNextPossible(0); + e.detail= SWT.TRAVERSE_NONE; + } + } + }); + table.addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + if (e.keyCode == SWT.F2 && e.stateMask == SWT.NONE) { + editColumnOrNextPossible(0); + e.doit= false; + } + } + }); + + addCellEditors(); + } + + private static String getDirectionLabel(NameInformation parameter) { + return parameter.isReturnValue() ? + Messages.ChangeParametersControl_return : + parameter.isOutput() ? + Messages.ChangeParametersControl_output : + Messages.ChangeParametersControl_input; + } + + private void editColumnOrNextPossible(int column) { + NameInformation[] selected= getSelectedElements(); + if (selected.length != 1) + return; + int nextColumn= column; + do { + fTableViewer.editElement(selected[0], nextColumn); + if (fTableViewer.isCellEditorActive()) + return; + nextColumn= nextColumn(nextColumn); + } while (nextColumn != column); + } + + private void editColumnOrPrevPossible(int column) { + NameInformation[] selected= getSelectedElements(); + if (selected.length != 1) + return; + int prevColumn= column; + do { + fTableViewer.editElement(selected[0], prevColumn); + if (fTableViewer.isCellEditorActive()) + return; + prevColumn= prevColumn(prevColumn); + } while (prevColumn != column); + } + + private int nextColumn(int column) { + return column >= getTable().getColumnCount() - 1 ? 0 : column + 1; + } + + private int prevColumn(int column) { + return column <= 0 ? getTable().getColumnCount() - 1 : column - 1; + } + + private void addColumnLayoutData(TableLayoutComposite layouter) { + for (int i = 0; i < columnProperties.length; i++) { + layouter.addColumnData(new ColumnWeightData(10, true)); + } + } + + private NameInformation[] getSelectedElements() { + ISelection selection= fTableViewer.getSelection(); + if (selection == null) + return new NameInformation[0]; + + if (!(selection instanceof IStructuredSelection)) + return new NameInformation[0]; + + List selected= ((IStructuredSelection) selection).toList(); + return selected.toArray(new NameInformation[selected.size()]); + } + + // ---- Button bar -------------------------------------------------------------------------------------- + + private void createButtonComposite(Composite parent) { + Composite buttonComposite= new Composite(parent, SWT.NONE); + buttonComposite.setLayoutData(new GridData(GridData.FILL_VERTICAL)); + GridLayout gl= new GridLayout(); + gl.marginHeight= 0; + gl.marginWidth= 0; + buttonComposite.setLayout(gl); + + if (fMode.canAddParameters()) + fAddButton= createAddButton(buttonComposite); + + fEditButton= createEditButton(buttonComposite); + + if (fMode.canAddParameters()) + fRemoveButton= createRemoveButton(buttonComposite); + + if (buttonComposite.getChildren().length != 0) + addSpacer(buttonComposite); + + fUpButton= createButton(buttonComposite, Messages.ChangeParametersControl_buttons_move_up, true); + fDownButton= createButton(buttonComposite, Messages.ChangeParametersControl_buttons_move_down, false); + + updateButtonsEnabledState(); + } + + private void addSpacer(Composite parent) { + Label label= new Label(parent, SWT.NONE); + GridData gd= new GridData(GridData.FILL_HORIZONTAL); + gd.heightHint= 5; + label.setLayoutData(gd); + } + + private void updateButtonsEnabledState() { + fUpButton.setEnabled(canMove(true)); + fDownButton.setEnabled(canMove(false)); + if (fEditButton != null) + fEditButton.setEnabled(getTableSelectionCount() == 1); + if (fAddButton != null) + fAddButton.setEnabled(true); + if (fRemoveButton != null) + fRemoveButton.setEnabled(getTableSelectionCount() != 0); + } + + private int getTableSelectionCount() { + return getTable().getSelectionCount(); + } + + private int getTableItemCount() { + return getTable().getItemCount(); + } + + private Table getTable() { + return fTableViewer.getTable(); + } + + private Button createEditButton(Composite buttonComposite) { + Button button= new Button(buttonComposite, SWT.PUSH); + button.setText(Messages.ChangeParametersControl_buttons_edit); + button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + SWTUtil.setButtonDimensionHint(button); + button.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + try { + NameInformation[] selected= getSelectedElements(); + Assert.isTrue(selected.length == 1); + NameInformation parameterInfo= selected[0]; + ParameterEditDialog dialog= new ParameterEditDialog(getShell(), parameterInfo, + fMode.canChangeTypes(), fMode.canChangeDefault(), + fMode.canChangeReturn() && parameterInfo.isOutput()); + dialog.open(); + fListener.parameterChanged(parameterInfo); + fTableViewer.update(parameterInfo, columnProperties); + } finally { + fTableViewer.getControl().setFocus(); + } + } + }); + return button; + } + + private Button createAddButton(Composite buttonComposite) { + Button button= new Button(buttonComposite, SWT.PUSH); + button.setText(Messages.ChangeParametersControl_buttons_add); + button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + SWTUtil.setButtonDimensionHint(button); + button.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + Set excludedParamNames= new HashSet(fParameters.size()); + for (int i= 0; i < fParameters.size(); i++) { + NameInformation info= fParameters.get(i); + excludedParamNames.add(info.getNewName()); + } + String newParamName= StubUtility.suggestParameterName("newParam", excludedParamNames, //$NON-NLS-1$ + fTypeContext != null ? fTypeContext.getTranslationUnit() : null); + NameInformation newInfo= NameInformation.createInfoForAddedParameter("int", newParamName, "0"); //$NON-NLS-1$ //$NON-NLS-2$ + int insertIndex= fParameters.size(); + fParameters.add(insertIndex, newInfo); + fListener.parameterAdded(newInfo); + fTableViewer.refresh(); + fTableViewer.getControl().setFocus(); + fTableViewer.setSelection(new StructuredSelection(newInfo), true); + updateButtonsEnabledState(); + editColumnOrNextPossible(0); + } + }); + return button; + } + + private Button createRemoveButton(Composite buttonComposite) { + final Button button= new Button(buttonComposite, SWT.PUSH); + button.setText(Messages.ChangeParametersControl_buttons_remove); + button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + SWTUtil.setButtonDimensionHint(button); + button.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + int index= getTable().getSelectionIndices()[0]; + NameInformation[] selected= getSelectedElements(); + for (int i= 0; i < selected.length; i++) { + if (selected[i].isAdded()) { + fParameters.remove(selected[i]); + } else { + selected[i].markAsDeleted(); + } + } + restoreSelection(index); + } + + private void restoreSelection(int index) { + fTableViewer.refresh(); + fTableViewer.getControl().setFocus(); + int itemCount= getTableItemCount(); + if (itemCount != 0) { + if (index >= itemCount) + index= itemCount - 1; + getTable().setSelection(index); + } + fListener.parameterListChanged(); + updateButtonsEnabledState(); + } + }); + return button; + } + + private Button createButton(Composite buttonComposite, String text, final boolean up) { + Button button= new Button(buttonComposite, SWT.PUSH); + button.setText(text); + button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + SWTUtil.setButtonDimensionHint(button); + button.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + ISelection savedSelection= fTableViewer.getSelection(); + if (savedSelection == null) + return; + NameInformation[] selection= getSelectedElements(); + if (selection.length == 0) + return; + + if (up) { + moveUp(selection); + } else { + moveDown(selection); + } + fTableViewer.refresh(); + fTableViewer.setSelection(savedSelection); + fListener.parameterListChanged(); + fTableViewer.getControl().setFocus(); + } + }); + return button; + } + + //---- editing ----------------------------------------------------------------------------------------------- + + private void addCellEditors() { + fTableViewer.setColumnProperties(columnProperties); + + ArrayList editors = new ArrayList(); + TableTextCellEditor cellEditorType= new TableTextCellEditor(fTableViewer, indexType); + editors.add(cellEditorType); + if (indexDirection >= 0) { + ComboBoxCellEditor cellEditorDirection= new DirectionCellEditor(fTableViewer.getTable()); + editors.add(cellEditorDirection); + } + TableTextCellEditor cellEditorName= new TableTextCellEditor(fTableViewer, indexName); + editors.add(cellEditorName); + if (indexDefault >= 0) { + TableTextCellEditor cellEditorDefault= new TableTextCellEditor(fTableViewer, indexDefault); + editors.add(cellEditorDefault); + } + + if (fParamNameProposals.length > 0) { + SubjectControlContentAssistant assistant= installParameterNameContentAssist(cellEditorName.getText()); + cellEditorName.setContentAssistant(assistant); + } + + for (int i = 0; i < editors.size(); i++) { + final int editorColumn= i; + final CellEditor editor = editors.get(i); + // Support tabbing between columns while editing + Control control = editor.getControl(); + control.addTraverseListener(new TraverseListener() { + @Override + public void keyTraversed(TraverseEvent e) { + switch (e.detail) { + case SWT.TRAVERSE_TAB_NEXT: + editColumnOrNextPossible(nextColumn(editorColumn)); + e.detail= SWT.TRAVERSE_NONE; + break; + + case SWT.TRAVERSE_TAB_PREVIOUS: + editColumnOrPrevPossible(prevColumn(editorColumn)); + e.detail= SWT.TRAVERSE_NONE; + break; + } + } + }); + if (control instanceof Text) { + TextFieldNavigationHandler.install((Text) control); + } + } + + cellEditorName.setActivationListener(new TableTextCellEditor.IActivationListener() { + @Override + public void activate() { + NameInformation[] selected= getSelectedElements(); + if (selected.length == 1 && fNameContentAssistHandler != null) { + fNameContentAssistHandler.setEnabled(selected[0].isAdded()); + } + } + }); + + fTableViewer.setCellEditors(editors.toArray(new CellEditor[editors.size()])); + fTableViewer.setCellModifier(new ParametersCellModifier()); + } + + //---- change order ---------------------------------------------------------------------------------------- + + private void moveUp(NameInformation[] selection) { + moveUp(fParameters, Arrays.asList(selection)); + } + + private void moveDown(NameInformation[] selection) { + Collections.reverse(fParameters); + moveUp(fParameters, Arrays.asList(selection)); + Collections.reverse(fParameters); + } + + private static void moveUp(List elements, List move) { + List res= new ArrayList(elements.size()); + List deleted= new ArrayList(); + NameInformation floating= null; + for (Iterator iter= elements.iterator(); iter.hasNext();) { + NameInformation curr= iter.next(); + if (move.contains(curr)) { + res.add(curr); + } else if (curr.isDeleted()) { + deleted.add(curr); + } else { + if (floating != null) + res.add(floating); + floating= curr; + } + } + if (floating != null) { + res.add(floating); + } + res.addAll(deleted); + elements.clear(); + for (Iterator iter= res.iterator(); iter.hasNext();) { + elements.add(iter.next()); + } + } + + private boolean canMove(boolean up) { + int notDeletedInfosCount= getNotDeletedInfosCount(); + if (notDeletedInfosCount == 0) + return false; + int[] indc= getTable().getSelectionIndices(); + if (indc.length == 0) + return false; + int invalid= up ? 0 : notDeletedInfosCount - 1; + for (int i= 0; i < indc.length; i++) { + if (indc[i] == invalid) + return false; + } + return true; + } + + private int getNotDeletedInfosCount() { + if (fParameters == null) // during initialization + return 0; + int result= 0; + for (Iterator iter= fParameters.iterator(); iter.hasNext();) { + NameInformation info= iter.next(); + if (!info.isDeleted()) + result++; + } + return result; + } + + private SubjectControlContentAssistant installParameterNameContentAssist(Text text) { + return null; + // TODO(sprigogin): Implement to support parameter name content assist. +// VariableNamesProcessor processor= new VariableNamesProcessor(fParamNameProposals); +// SubjectControlContentAssistant contentAssistant= ControlContentAssistHelper.createCContentAssistant(processor); +// fNameContentAssistHandler= ContentAssistHandler.createHandlerForText(text, contentAssistant); +// return contentAssistant; + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/IParameterListChangeListener.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/IParameterListChangeListener.java new file mode 100644 index 00000000000..32ead0149a9 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/IParameterListChangeListener.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2000, 2012 IBM Corporation 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: + * IBM Corporation - initial API and implementation + * Sergey Prigogin (Google) + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.refactoring; + +/** + * @see ChangeParametersControl + */ +public interface IParameterListChangeListener { + /** + * Gets fired when the given parameter has changed + * @param parameter the parameter that has changed. + */ + public void parameterChanged(NameInformation parameter); + + /** + * Gets fired when the given parameter has been added + * @param parameter the parameter that has been added. + */ + public void parameterAdded(NameInformation parameter); + + /** + * Gets fired if the parameter list got modified by reordering or removing + * parameters (note that adding is handled by parameterAdded)) + */ + public void parameterListChanged(); +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/Messages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/Messages.java index 93d5ca243ff..22176b2c2fc 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/Messages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/Messages.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2011 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008, 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 @@ -35,7 +35,6 @@ public final class Messages extends NLS { public static String Refactoring_CantLoadTU; public static String Refactoring_Ambiguity; public static String Refactoring_ParsingError; - public static String NO_FILE; public static String RefactoringSaveHelper_unexpected_exception; public static String RefactoringSaveHelper_saving; public static String RefactoringSaveHelper_always_save; @@ -49,6 +48,28 @@ public final class Messages extends NLS { public static String ChangeExceptionHandler_undo_button; public static String ChangeExceptionHandler_undo_dialog_message; public static String ChangeExceptionHandler_undo_dialog_title; + public static String ChangeParametersControl_table_type; + public static String ChangeParametersControl_table_direction; + public static String ChangeParametersControl_table_name; + public static String ChangeParametersControl_table_default_value; + public static String ChangeParametersControl_input; + public static String ChangeParametersControl_output; + public static String ChangeParametersControl_return; + public static String ChangeParametersControl_buttons_move_up; + public static String ChangeParametersControl_buttons_move_down; + public static String ChangeParametersControl_buttons_edit; + public static String ChangeParametersControl_buttons_add; + public static String ChangeParametersControl_buttons_remove; + public static String ParameterEditDialog_title; + public static String ParameterEditDialog_message_new; + public static String ParameterEditDialog_message; + public static String ParameterEditDialog_type; + public static String ParameterEditDialog_name; + public static String ParameterEditDialog_name_error; + public static String ParameterEditDialog_default_value; + public static String ParameterEditDialog_default_value_error; + public static String ParameterEditDialog_default_value_invalid; + public static String ParameterEditDialog_use_as_return; public static String RefactoringExecutionHelper_cannot_execute; static { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/Messages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/Messages.properties index 04adb314e15..c42894c574b 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/Messages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/Messages.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2008, 2009 Institute for Software, HSR Hochschule fuer Technik +# Copyright (c) 2008, 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 @@ -22,7 +22,7 @@ Refactoring_CanceledByUser=Refactoring canceled by user. Refactoring_CompileErrorInTU=The translation unit contains one or several problems. This can be caused by a syntax error in the code or a parser flaw. The refactoring will possibly fail. AddDeclarationNodeToClassChange_AddDeclaration=Add Declaration to Class {0}. CreateFileChange_CreateFile=Create file: {0} -CreateFileChange_UnknownLoc=Unknown Location: {0} +CreateFileChange_UnknownLoc=Unknown location: {0} CreateFileChange_FileExists=File already exists: {0} CRefactoring_FileNotFound=The file {0} is not on the build path of a C/C++ project. CRefactoring_checking_final_conditions=Checking preconditions... @@ -30,7 +30,6 @@ Refactoring_SelectionNotValid=Selection is not valid. Refactoring_CantLoadTU=Can not load translation unit. Refactoring_Ambiguity=Translation unit is ambiguous. Refactoring_ParsingError=Unable to parse {0}. -NO_FILE=File not found. RefactoringSaveHelper_unexpected_exception=An unexpected exception occurred. See the error log for more details. RefactoringSaveHelper_saving=Saving Resources RefactoringSaveHelper_always_save=&Always save all modified resources automatically prior to refactoring @@ -44,4 +43,26 @@ ChangeExceptionHandler_status_without_detail=Exception does not provide a detail ChangeExceptionHandler_undo_dialog_title=Undo Refactoring ChangeExceptionHandler_undo_dialog_message=An unexpected exception occurred while undoing the refactoring ''{0}'' ChangeExceptionHandler_dialog_message=An exception has been caught while processing the refactoring ''{0}''. +ChangeParametersControl_table_type=Type +ChangeParametersControl_table_direction=Input/Output +ChangeParametersControl_table_name=Name +ChangeParametersControl_table_default_value=Default value +ChangeParametersControl_input=input +ChangeParametersControl_output=output +ChangeParametersControl_return=return +ChangeParametersControl_buttons_move_up=&Up +ChangeParametersControl_buttons_move_down=D&own +ChangeParametersControl_buttons_edit=&Edit... +ChangeParametersControl_buttons_add=&Add +ChangeParametersControl_buttons_remove=Re&move +ParameterEditDialog_title=Function Parameter +ParameterEditDialog_message_new=Declaration of parameter: +ParameterEditDialog_message=Declaration of parameter ''{0}'': +ParameterEditDialog_type=&Type: +ParameterEditDialog_name=&Name: +ParameterEditDialog_name_error= Parameter name must not be empty. +ParameterEditDialog_default_value=&Default value: +ParameterEditDialog_default_value_error= Default value must not be empty. +ParameterEditDialog_default_value_invalid=''{0}'' is not a valid expression. +ParameterEditDialog_use_as_return=Use as &return value RefactoringExecutionHelper_cannot_execute=The operation cannot be performed due to the following problem:\n\n{0} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NameInformation.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NameInformation.java new file mode 100644 index 00000000000..aba38c6b706 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NameInformation.java @@ -0,0 +1,315 @@ +/******************************************************************************* + * Copyright (c) 2008, 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 + * 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) + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.refactoring; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.runtime.Assert; + +import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTArrayModifier; +import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTFileLocation; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IASTNode.CopyStyle; +import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; +import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; +import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; +import org.eclipse.cdt.core.dom.ast.INodeFactory; +import org.eclipse.cdt.core.dom.ast.IType; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPNodeFactory; +import org.eclipse.cdt.core.dom.rewrite.TypeHelper; +import org.eclipse.cdt.core.model.ITranslationUnit; + +import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriterVisitor; + +/** + * Additional information about an IASTName in code being refactored. + */ +public class NameInformation { + public static final int INDEX_FOR_ADDED = -1; + + private final IASTName name; + private IASTName declarationName; + private final List references; + private List referencesAfterCached; + private int lastCachedReferencesHash; + private boolean isOutput; + private boolean mustBeReturnValue; + private boolean isWriteAccess; + private boolean passOutputByPointer; + private boolean isReturnValue; + private String newName; + private int newOrder; + private boolean isDeleted; + private String defaultValue; + private String newTypeName; + + public NameInformation(IASTName name) { + this.name = name; + this.newName = String.valueOf(name.getSimpleID()); + references = new ArrayList(); + } + + public static NameInformation createInfoForAddedParameter(String type, String name, + String defaultValue) { + NameInformation info= new NameInformation(null); + info.setTypeName(type); + info.setNewName(name); + info.setDefaultValue(defaultValue); + info.setNewOrder(INDEX_FOR_ADDED); + return info; + } + + /** + * For debugging only. + */ + @Override + public String toString() { + return name.toString(); + } + + public int getNewOrder() { + return newOrder; + } + + public void setNewOrder(int newOrder) { + this.newOrder = newOrder; + } + + /** + * Returns true if the value of the variable has to propagate to the outside world. + */ + public boolean isOutput() { + return isOutput; + } + + void setOutput(boolean isOutput) { + this.isOutput = isOutput; + } + + public boolean isOutputParameter() { + return isOutput() && !isReturnValue(); + } + + public boolean mustBeReturnValue() { + return mustBeReturnValue; + } + + public void setMustBeReturnValue(boolean mustBeReturnValue) { + this.mustBeReturnValue = mustBeReturnValue; + } + + public boolean isReturnValue() { + return mustBeReturnValue || isReturnValue; + } + + public void setReturnValue(boolean isReturnValue) { + Assert.isTrue(isReturnValue || !mustBeReturnValue); + this.isReturnValue = isReturnValue; + } + + public String getNewName() { + return newName; + } + + public void setNewName(String newName) { + this.newName = newName; + } + + public boolean isWriteAccess() { + return isWriteAccess; + } + + void setWriteAccess(boolean isWriteAceess) { + this.isWriteAccess = isWriteAceess; + } + + public boolean isDeleted() { + return isDeleted; + } + + public void markAsDeleted() { + Assert.isTrue(!isAdded()); // Added parameters should be simply removed from the list + isDeleted= true; + } + + public boolean isAdded() { + // TODO(sprigogin): Adding parameters is not supported yet. + return false; + } + + public String getDefaultValue() { + return defaultValue; + } + + public void setDefaultValue(String value) { + Assert.isNotNull(value); + defaultValue= value; + } + + public IASTName getDeclarationName() { + return declarationName; + } + + public IASTDeclarator getDeclarator() { + return (IASTDeclarator) declarationName.getParent(); + } + + public IASTDeclSpecifier getDeclSpecifier() { + IASTNode parent = getDeclarator().getParent(); + if (parent instanceof IASTSimpleDeclaration) { + return ((IASTSimpleDeclaration) parent).getDeclSpecifier(); + } else if (parent instanceof IASTParameterDeclaration) { + return ((IASTParameterDeclaration) parent).getDeclSpecifier(); + } + return null; + } + + void setDeclarationName(IASTName declarationName) { + Assert.isTrue(declarationName.getParent() instanceof IASTDeclarator); + this.declarationName = declarationName; + } + + public IASTName getName() { + return name; + } + + public boolean isRenamed() { + return name == null ? newName != null : String.valueOf(name.getSimpleID()).equals(name); + } + + void addReference(IASTName name) { + references.add(name); + } + + public String getTypeName() { + if (newTypeName != null) + return newTypeName; + INodeFactory nodeFactory = name.getTranslationUnit().getASTNodeFactory(); + IASTParameterDeclaration declaration = getParameterDeclaration(nodeFactory, null); + ASTWriterVisitor writer = new ASTWriterVisitor(); + declaration.accept(writer); + return writer.toString(); + } + + public void setTypeName(String type) { + Assert.isNotNull(type); + newTypeName= type; + } + + public String getReturnType() { + if (!isReturnValue()) + return null; + INodeFactory nodeFactory = name.getTranslationUnit().getASTNodeFactory(); + IASTDeclarator sourceDeclarator = getDeclarator(); + IASTDeclSpecifier declSpec = safeCopy(getDeclSpecifier()); + IASTDeclarator declarator = createDeclarator(nodeFactory, sourceDeclarator, null); + IASTParameterDeclaration declaration = nodeFactory.newParameterDeclaration(declSpec, declarator); + ASTWriterVisitor writer = new ASTWriterVisitor(); + declaration.accept(writer); + return writer.toString(); + } + + public List getReferencesAfterSelection(int endOffset) { + if (referencesAfterCached == null || lastCachedReferencesHash != references.hashCode()) { + lastCachedReferencesHash = references.hashCode(); + referencesAfterCached = new ArrayList(); + for (IASTName ref : references) { + IASTFileLocation loc = ref.getFileLocation(); + if (loc.getNodeOffset() >= endOffset) { + referencesAfterCached.add(ref); + } + } + } + return referencesAfterCached; + } + + public boolean isReferencedAfterSelection(int endOffset) { + return !getReferencesAfterSelection(endOffset).isEmpty(); + } + + public IASTParameterDeclaration getParameterDeclaration(INodeFactory nodeFactory) { + return getParameterDeclaration(nodeFactory, newName); + } + + private IASTParameterDeclaration getParameterDeclaration(INodeFactory nodeFactory, String paramName) { + IASTDeclarator sourceDeclarator = getDeclarator(); + IASTDeclSpecifier declSpec = safeCopy(getDeclSpecifier()); + IASTDeclarator declarator = createDeclarator(nodeFactory, sourceDeclarator, paramName); + + if (isOutputParameter()) { + if (nodeFactory instanceof ICPPNodeFactory && !passOutputByPointer) { + declarator.addPointerOperator(((ICPPNodeFactory) nodeFactory).newReferenceOperator(false)); + } else { + declarator.addPointerOperator(nodeFactory.newPointer()); + } + } else if (declSpec != null && !isWriteAccess) { + IType type = TypeHelper.createType(sourceDeclarator); + if (TypeHelper.shouldBePassedByReference(type, declarationName.getTranslationUnit())) { + if (nodeFactory instanceof ICPPNodeFactory) { + declarator.addPointerOperator(((ICPPNodeFactory) nodeFactory).newReferenceOperator(false)); + } else { + declarator.addPointerOperator(nodeFactory.newPointer()); + } + declSpec.setConst(true); + } + } + + declarator.setNestedDeclarator(sourceDeclarator.getNestedDeclarator()); + + return nodeFactory.newParameterDeclaration(declSpec, declarator); + } + + private IASTDeclarator createDeclarator(INodeFactory nodeFactory, IASTDeclarator sourceDeclarator, + String name) { + IASTName astName = name != null ? + nodeFactory.newName(name.toCharArray()) : nodeFactory.newName(); + IASTDeclarator declarator; + if (sourceDeclarator instanceof IASTArrayDeclarator) { + IASTArrayDeclarator arrDeclarator = (IASTArrayDeclarator) sourceDeclarator; + IASTArrayDeclarator arrayDeclarator = nodeFactory.newArrayDeclarator(astName); + IASTArrayModifier[] arrayModifiers = arrDeclarator.getArrayModifiers(); + for (IASTArrayModifier arrayModifier : arrayModifiers) { + arrayDeclarator.addArrayModifier(arrayModifier.copy(CopyStyle.withLocations)); + } + declarator= arrayDeclarator; + } else { + declarator = nodeFactory.newDeclarator(astName); + } + for (IASTPointerOperator pointerOp : sourceDeclarator.getPointerOperators()) { + declarator.addPointerOperator(pointerOp.copy(CopyStyle.withLocations)); + } + return declarator; + } + + @SuppressWarnings("unchecked") + private static T safeCopy(T node) { + return node == null ? null : (T) node.copy(CopyStyle.withLocations); + } + + public ITranslationUnit getTranslationUnit() { + return name != null ? name.getTranslationUnit().getOriginatingTranslationUnit() : null; + } + + public boolean isPassOutputByPointer() { + return passOutputByPointer; + } + + public void setPassOutputByPointer(boolean passOutputByPointer) { + this.passOutputByPointer = passOutputByPointer; + } +} \ No newline at end of file diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NodeContainer.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NodeContainer.java index 353000961ef..48131146709 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NodeContainer.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NodeContainer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2009 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008, 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 @@ -18,263 +18,39 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import org.eclipse.core.runtime.Assert; +import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.ILog; import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.preferences.IPreferencesService; import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.DOMException; -import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator; -import org.eclipse.cdt.core.dom.ast.IASTArrayModifier; -import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTDeclarator; -import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNode; -import org.eclipse.cdt.core.dom.ast.IASTNode.CopyStyle; import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; -import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; -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.dom.ast.INodeFactory; import org.eclipse.cdt.core.dom.ast.IVariable; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator; -import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeTemplateParameter; import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding; -import org.eclipse.cdt.core.dom.ast.cpp.ICPPNodeFactory; import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateTypeParameter; +import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.cdt.ui.PreferenceConstants; -import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVariableReadWriteFlags; -import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriter; import org.eclipse.cdt.internal.core.pdom.dom.PDOMName; public class NodeContainer { - public final NameInformation NULL_NAME_INFORMATION = new NameInformation(new CPPASTName()); - private final List nodes; private List names; private List interfaceNames; - public class NameInformation { - private IASTName name; - private IASTName declaration; - private final List references; - private List referencesAfterCached; - private int lastCachedReferencesHash; - private boolean mustBeOutput; - private boolean mustBeReturnValue; - private boolean isConst; - private boolean isWriteAccess; - - private boolean isOutput; - private boolean isReturnValue; - private String userSetName; - private int userOrder; - - public int getUserOrder() { - return userOrder; - } - - public void setUserOrder(int userOrder) { - this.userOrder = userOrder; - } - - public NameInformation(IASTName name) { - super(); - this.name = name; - references = new ArrayList(); - } - - public IASTName getDeclaration() { - return declaration; - } - - public void setDeclaration(IASTName declaration) { - this.declaration = declaration; - } - - public IASTName getName() { - return name; - } - - public void setName(IASTName name) { - this.name = name; - } - - public void addReference(IASTName name) { - references.add(name); - } - - public List getReferencesAfterSelection() { - if (referencesAfterCached == null || lastCachedReferencesHash != references.hashCode()) { - lastCachedReferencesHash = references.hashCode(); - referencesAfterCached = new ArrayList(); - for (IASTName ref : references) { - IASTFileLocation loc = ref.getFileLocation(); - if (loc.getNodeOffset() >= getEndOffset()) { - referencesAfterCached.add(ref); - } - } - } - return referencesAfterCached; - } - - public boolean isReferencedAfterSelection() { - return !getReferencesAfterSelection().isEmpty(); - } - - public IASTParameterDeclaration getParameterDeclaration(INodeFactory nodeFactory) { - IASTDeclarator sourceDeclarator = (IASTDeclarator) getDeclaration().getParent(); - - IASTDeclSpecifier declSpec= null; - IASTDeclarator declarator= null; - - if (sourceDeclarator.getParent() instanceof IASTSimpleDeclaration) { - IASTSimpleDeclaration decl = (IASTSimpleDeclaration) sourceDeclarator.getParent(); - declSpec = decl.getDeclSpecifier().copy(CopyStyle.withLocations); - } else if (sourceDeclarator.getParent() instanceof IASTParameterDeclaration) { - IASTParameterDeclaration decl = (IASTParameterDeclaration) sourceDeclarator.getParent(); - declSpec = decl.getDeclSpecifier().copy(CopyStyle.withLocations); - } - - IASTName name= nodeFactory.newName(getDeclaration().toCharArray()); - if (sourceDeclarator instanceof IASTArrayDeclarator) { - IASTArrayDeclarator arrDeclarator = (IASTArrayDeclarator) sourceDeclarator; - IASTArrayDeclarator arrayDtor = nodeFactory.newArrayDeclarator(name); - IASTArrayModifier[] arrayModifiers = arrDeclarator.getArrayModifiers(); - for (IASTArrayModifier arrayModifier : arrayModifiers) { - arrayDtor.addArrayModifier(arrayModifier.copy(CopyStyle.withLocations)); - } - declarator= arrayDtor; - } else { - declarator = nodeFactory.newDeclarator(name); - } - for (IASTPointerOperator pointerOp : sourceDeclarator.getPointerOperators()) { - declarator.addPointerOperator(pointerOp.copy(CopyStyle.withLocations)); - } - - boolean output = isOutput() && !isReturnValue(); - if (output && !hasReferenceOperator(declarator)) { - if (nodeFactory instanceof ICPPNodeFactory) { - declarator.addPointerOperator(((ICPPNodeFactory) nodeFactory).newReferenceOperator(false)); - } else { - declarator.addPointerOperator(nodeFactory.newPointer()); - } - } - - declarator.setNestedDeclarator(sourceDeclarator.getNestedDeclarator()); - - return nodeFactory.newParameterDeclaration(declSpec, declarator); - } - - public boolean hasReferenceOperator(IASTDeclarator declarator) { - for (IASTPointerOperator pOp : declarator.getPointerOperators()) { - if (pOp instanceof ICPPASTReferenceOperator) { - return true; - } - } - return false; - } - - public String getType() { - IASTDeclSpecifier declSpec = null; - - IASTNode node = getDeclaration().getParent(); - if (node instanceof ICPPASTSimpleTypeTemplateParameter) { - ICPPASTSimpleTypeTemplateParameter parameter = (ICPPASTSimpleTypeTemplateParameter) node; - return parameter.getName().toString(); - } - IASTDeclarator sourceDeclarator = (IASTDeclarator) node; - if (sourceDeclarator.getParent() instanceof IASTSimpleDeclaration) { - IASTSimpleDeclaration decl = (IASTSimpleDeclaration) sourceDeclarator.getParent(); - declSpec = decl.getDeclSpecifier(); - } else if (sourceDeclarator.getParent() instanceof IASTParameterDeclaration) { - IASTParameterDeclaration decl = (IASTParameterDeclaration) sourceDeclarator.getParent(); - declSpec = decl.getDeclSpecifier(); - } - - ASTWriter writer = new ASTWriter(); - return writer.write(declSpec); - } - - public boolean isDeclaredInSelection() { - if (declaration != null && declaration.toCharArray().length > 0) { - int declOffset = declaration.getFileLocation().getNodeOffset(); - return declOffset >= getStartOffset() && declOffset <= getEndOffset(); - } - return true; - } - - @Override - public String toString() { - return name.toString() + (isDeclaredInSelection() ? " (declared inside)" : ""); //$NON-NLS-1$//$NON-NLS-2$ - } - - public boolean mustBeOutput() { - return mustBeOutput; - } - - public void setMustBeOutput(boolean mustBeOutput) { - this.mustBeOutput = mustBeOutput; - } - - public boolean isOutput() { - return mustBeOutput || isOutput; - } - - public void setIsOutput(boolean isOutput) { - Assert.isTrue(isOutput || !mustBeOutput); - this.isOutput = isOutput; - } - - public boolean mustBeReturnValue() { - return mustBeReturnValue; - } - - public void setMustBeReturnValue(boolean mustBeReturnValue) { - this.mustBeReturnValue = mustBeReturnValue; - } - - public boolean isReturnValue() { - return mustBeReturnValue || isReturnValue; - } - - public void setReturnValue(boolean isReturnValue) { - Assert.isTrue(isReturnValue || !mustBeReturnValue); - this.isReturnValue = isReturnValue; - } - - public String getUserSetName() { - return userSetName; - } - - public void setUserSetName(String userSetName) { - this.userSetName = userSetName; - } - - public boolean isConst() { - return isConst; - } - - public void setConst(boolean isConst) { - this.isConst = isConst; - } - - public boolean isWriteAccess() { - return isWriteAccess; - } - - public void setWriteAccess(boolean isWriteAceess) { - this.isWriteAccess = isWriteAceess; - } - } - public NodeContainer() { super(); nodes = new ArrayList(); @@ -297,6 +73,12 @@ public class NodeContainer { return; } names = new ArrayList(); + + IPreferencesService preferences = Platform.getPreferencesService(); + final boolean passOutputByPointer = preferences.getBoolean(CUIPlugin.PLUGIN_ID, + PreferenceConstants.FUNCTION_PASS_OUTPUT_PARAMETERS_BY_POINTER, false, + PreferenceConstants.getPreferenceScopes(getProject())); + for (IASTNode node : nodes) { node.accept(new ASTVisitor() { { @@ -305,18 +87,19 @@ public class NodeContainer { @Override public int visit(IASTName name) { - IBinding bind = name.resolveBinding(); + IBinding binding = name.resolveBinding(); - if (bind instanceof ICPPBinding && !(bind instanceof ICPPTemplateTypeParameter)) { - ICPPBinding cppBind = (ICPPBinding) bind; + if (binding instanceof ICPPBinding && !(binding instanceof ICPPTemplateTypeParameter)) { + ICPPBinding cppBinding = (ICPPBinding) binding; try { - if (!cppBind.isGloballyQualified()) { - NameInformation nameInformation = new NameInformation(name); - IASTName[] refs = name.getTranslationUnit().getReferences(bind); + if (!cppBinding.isGloballyQualified()) { + NameInformation nameInfo = new NameInformation(name); + nameInfo.setPassOutputByPointer(passOutputByPointer); + IASTName[] refs = name.getTranslationUnit().getReferences(binding); for (IASTName ref : refs) { - nameInformation.addReference(ref); + nameInfo.addReference(ref); } - names.add(nameInformation); + names.add(nameInfo); } } catch (DOMException e) { ILog logger = CUIPlugin.getDefault().getLog(); @@ -324,10 +107,10 @@ public class NodeContainer { CUIPlugin.PLUGIN_ID, IStatus.OK, e.getMessage(), e); logger.log(status); } - } else if (bind instanceof IVariable) { + } else if (binding instanceof IVariable) { NameInformation nameInformation = new NameInformation(name); - IASTName[] refs = name.getTranslationUnit().getReferences(bind); + IASTName[] refs = name.getTranslationUnit().getReferences(binding); for (IASTName ref : refs) { nameInformation.addReference(ref); } @@ -344,11 +127,21 @@ public class NodeContainer { IASTTranslationUnit unit = name.getTranslationUnit(); IASTName[] nameDeclarations = unit.getDeclarationsInAST(name.resolveBinding()); if (nameDeclarations.length != 0) { - nameInfo.setDeclaration(nameDeclarations[nameDeclarations.length - 1]); + nameInfo.setDeclarationName(nameDeclarations[nameDeclarations.length - 1]); } } } + private IProject getProject() { + IProject project = null; + if (nodes.isEmpty()) { + ITranslationUnit tu = nodes.get(0).getTranslationUnit().getOriginatingTranslationUnit(); + if (tu != null) + project = tu.getCProject().getProject(); + } + return project; + } + /** * Returns names that are either parameter or return value candidates. */ @@ -359,25 +152,30 @@ public class NodeContainer { Set declarations = new HashSet(); interfaceNames = new ArrayList(); + int endOffset = getEndOffset(); for (NameInformation nameInfo : names) { - if (declarations.add(nameInfo.getDeclaration())) { - if (nameInfo.isDeclaredInSelection()) { - if (nameInfo.isReferencedAfterSelection()) { + IASTName declarationName = nameInfo.getDeclarationName(); + if (declarations.add(declarationName)) { + if (isDeclaredInSelection(nameInfo)) { + if (nameInfo.isReferencedAfterSelection(endOffset)) { nameInfo.setMustBeReturnValue(true); interfaceNames.add(nameInfo); } } else { - for (NameInformation n2 : names) { - if (n2.getDeclaration() == nameInfo.getDeclaration()) { - int flag = CPPVariableReadWriteFlags.getReadWriteFlags(n2.getName()); - if ((flag & PDOMName.WRITE_ACCESS) != 0) { - nameInfo.setWriteAccess(true); - break; + IASTDeclarator declarator = (IASTDeclarator) declarationName.getParent(); + if (!hasReferenceOperator(declarator)) { + for (NameInformation n2 : names) { + if (n2.getDeclarationName() == declarationName) { + int flag = CPPVariableReadWriteFlags.getReadWriteFlags(n2.getName()); + if ((flag & PDOMName.WRITE_ACCESS) != 0) { + nameInfo.setWriteAccess(true); + break; + } } } - } - if (nameInfo.isWriteAccess() && nameInfo.isReferencedAfterSelection()) { - nameInfo.setMustBeOutput(true); + if (nameInfo.isWriteAccess() && nameInfo.isReferencedAfterSelection(endOffset)) { + nameInfo.setOutput(true); + } } interfaceNames.add(nameInfo); } @@ -388,6 +186,20 @@ public class NodeContainer { return interfaceNames; } + public static boolean hasReferenceOperator(IASTDeclarator declarator) { + IASTPointerOperator[] operators = declarator.getPointerOperators(); + return operators.length != 0 && operators[operators.length - 1] instanceof ICPPASTReferenceOperator; + } + + public boolean isDeclaredInSelection(NameInformation nameInfo) { + IASTName declaration = nameInfo.getDeclarationName(); + if (declaration != null && declaration.toCharArray().length > 0) { + int declOffset = declaration.getFileLocation().getNodeOffset(); + return declOffset >= getStartOffset() && declOffset <= getEndOffset(); + } + return true; + } + private List getInterfaceNames(boolean isReturnValue) { List selectedNames = null; @@ -412,7 +224,6 @@ public class NodeContainer { return getInterfaceNames(false); } - /** * Returns names that are candidates for being used as the function return value. Multiple * return value candidates mean that the function cannot be extracted. diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ParameterEditDialog.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ParameterEditDialog.java new file mode 100644 index 00000000000..13b9ae916c3 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ParameterEditDialog.java @@ -0,0 +1,219 @@ +/******************************************************************************* + * Copyright (c) 2000, 2012 IBM Corporation 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: + * IBM Corporation - initial API and implementation + * Sergey Prigogin (Google) + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.refactoring; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jface.dialogs.StatusDialog; +import org.eclipse.osgi.util.NLS; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; + +import org.eclipse.cdt.core.CConventions; +import org.eclipse.cdt.ui.CUIPlugin; + +import org.eclipse.cdt.internal.ui.dialogs.TextFieldNavigationHandler; +import org.eclipse.cdt.internal.ui.viewsupport.BasicElementLabels; + +public class ParameterEditDialog extends StatusDialog { + private final NameInformation fParameter; + private final boolean fEditType; + private final boolean fEditDefault; + private final boolean fEditReturn; + private Text fType; + private Text fName; + private Text fDefaultValue; + private Button fReturn; + + /** + * @param parentShell + * @param parameter + * @param canEditType + * @param canEditDefault + * @param canChangeReturn + * Can be null if canEditType is false. + */ + public ParameterEditDialog(Shell parentShell, NameInformation parameter, boolean canEditType, + boolean canEditDefault, boolean canChangeReturn) { + super(parentShell); + fParameter= parameter; + fEditType= canEditType; + fEditDefault= canEditDefault; + fEditReturn = canChangeReturn; + } + + @Override + protected void configureShell(Shell newShell) { + super.configureShell(newShell); + newShell.setText(Messages.ParameterEditDialog_title); + } + + @Override + protected Control createDialogArea(Composite parent) { + Composite result= (Composite) super.createDialogArea(parent); + GridLayout layout= (GridLayout) result.getLayout(); + layout.numColumns= 2; + Label label; + GridData gd; + + label= new Label(result, SWT.NONE); + String newName = fParameter.getNewName(); + if (newName.isEmpty()) { + label.setText(Messages.ParameterEditDialog_message_new); + } else { + label.setText(NLS.bind(Messages.ParameterEditDialog_message, + BasicElementLabels.getCElementName(newName))); + } + gd= new GridData(GridData.FILL_HORIZONTAL); + gd.horizontalSpan= 2; + label.setLayoutData(gd); + + if (fEditType) { + label= new Label(result, SWT.NONE); + label.setText(Messages.ParameterEditDialog_type); + fType= new Text(result, SWT.BORDER); + gd= new GridData(GridData.FILL_HORIZONTAL); + fType.setLayoutData(gd); + fType.setText(fParameter.getTypeName()); + fType.addModifyListener( + new ModifyListener() { + @Override + public void modifyText(ModifyEvent e) { + validate((Text) e.widget); + } + }); + TextFieldNavigationHandler.install(fType); + } + + label= new Label(result, SWT.NONE); + fName= new Text(result, SWT.BORDER); + initializeDialogUnits(fName); + label.setText(Messages.ParameterEditDialog_name); + gd= new GridData(GridData.FILL_HORIZONTAL); + gd.widthHint= convertWidthInCharsToPixels(45); + fName.setLayoutData(gd); + fName.setText(newName); + fName.addModifyListener( + new ModifyListener() { + @Override + public void modifyText(ModifyEvent e) { + validate((Text) e.widget); + } + }); + TextFieldNavigationHandler.install(fName); + + if (fEditDefault && fParameter.isAdded()) { + label= new Label(result, SWT.NONE); + label.setText(Messages.ParameterEditDialog_default_value); + fDefaultValue= new Text(result, SWT.BORDER); + gd= new GridData(GridData.FILL_HORIZONTAL); + fDefaultValue.setLayoutData(gd); + fDefaultValue.setText(fParameter.getDefaultValue()); + fDefaultValue.addModifyListener( + new ModifyListener() { + @Override + public void modifyText(ModifyEvent e) { + validate((Text) e.widget); + } + }); + TextFieldNavigationHandler.install(fDefaultValue); + } + if (fEditReturn) { + fReturn = new Button(result, SWT.CHECK); + fReturn.setText(Messages.ParameterEditDialog_use_as_return); + fReturn.setSelection(fParameter.isReturnValue()); + fReturn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + } + applyDialogFont(result); + return result; + } + + @Override + protected void okPressed() { + if (fType != null) { + fParameter.setTypeName(fType.getText()); + } + fParameter.setNewName(fName.getText()); + if (fDefaultValue != null) { + fParameter.setDefaultValue(fDefaultValue.getText()); + } + if (fReturn != null) { + fParameter.setReturnValue(fReturn.getSelection()); + } + super.okPressed(); + } + + private void validate(Text first) { + IStatus[] result= new IStatus[3]; + if (first == fType) { + result[0]= validateType(); + result[1]= validateName(); + result[2]= validateDefaultValue(); + } else if (first == fName) { + result[0]= validateName(); + result[1]= validateType(); + result[2]= validateDefaultValue(); + } else { + result[0]= validateDefaultValue(); + result[1]= validateName(); + result[2]= validateType(); + } + for (int i= 0; i < result.length; i++) { + IStatus status= result[i]; + if (status != null && !status.isOK()) { + updateStatus(status); + return; + } + } + updateStatus(Status.OK_STATUS); + } + + private IStatus validateType() { + // TODO(sprigogin): Implement type validation. + return Status.OK_STATUS; + } + + private IStatus validateName() { + if (fName == null) + return null; + String name= fName.getText(); + if (name.isEmpty()) + return createErrorStatus(Messages.ParameterEditDialog_name_error); + IStatus status= CConventions.validateFieldName(name); + if (status.matches(IStatus.ERROR)) + return status; + return Status.OK_STATUS; + } + + private IStatus validateDefaultValue() { + if (fDefaultValue == null) + return null; + String defaultValue= fDefaultValue.getText(); + if (defaultValue.isEmpty()) + return createErrorStatus(Messages.ParameterEditDialog_default_value_error); + // TODO(sprigogin): Implement real default value validation. + return Status.OK_STATUS; + } + + private Status createErrorStatus(String message) { + return new Status(IStatus.ERROR, CUIPlugin.getPluginId(), IStatus.ERROR, message, null); + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/StubTypeContext.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/StubTypeContext.java new file mode 100644 index 00000000000..4223a0eee31 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/StubTypeContext.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (c) 2000, 2012 IBM Corporation 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: + * IBM Corporation - initial API and implementation + * Sergey Prigogin (Googel) + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.refactoring; + +import org.eclipse.cdt.core.model.ITranslationUnit; + +public class StubTypeContext { + private final ITranslationUnit tu; + + public StubTypeContext(ITranslationUnit tu) { + this.tu = tu; + } + + public ITranslationUnit getTranslationUnit() { + return tu; + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ChooserComposite.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ChooserComposite.java deleted file mode 100644 index 69d9d694a38..00000000000 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ChooserComposite.java +++ /dev/null @@ -1,204 +0,0 @@ -/******************************************************************************* - * 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 - *******************************************************************************/ -package org.eclipse.cdt.internal.ui.refactoring.extractfunction; - -import java.util.ArrayList; - -import org.eclipse.swt.SWT; -import org.eclipse.swt.custom.TableEditor; -import org.eclipse.swt.events.SelectionEvent; -import org.eclipse.swt.events.SelectionListener; -import org.eclipse.swt.layout.GridData; -import org.eclipse.swt.layout.GridLayout; -import org.eclipse.swt.widgets.Button; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Event; -import org.eclipse.swt.widgets.Table; -import org.eclipse.swt.widgets.TableColumn; -import org.eclipse.swt.widgets.TableItem; - -import org.eclipse.cdt.core.dom.ast.IASTDeclarator; - -import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation; - -public class ChooserComposite extends Composite { - private static final String COLUMN_RETURN = Messages.ChooserComposite_Return; - private static final String COLUMN_REFERENCE = Messages.ChooserComposite_CallByRef; - private static final String COLUMN_NAME = Messages.ChooserComposite_Name; - private static final String COLUMN_TYPE = Messages.ChooserComposite_Type; - - private Button checkboxVoidReturn; - - public ChooserComposite(Composite parent, final ExtractFunctionInformation info, - ExtractFunctionInputPage page) { - super(parent, SWT.NONE); - - GridLayout layout = new GridLayout(); - setLayout(layout); - - final ArrayList