diff options
author | Markus Schorn | 2008-04-16 12:31:09 +0000 |
---|---|---|
committer | Markus Schorn | 2008-04-16 12:31:09 +0000 |
commit | d353a33638e7eb62427ace558f53301653b235bc (patch) | |
tree | 91c833e2b56479a8f2d0983fb9f529f01db2b8df | |
parent | ca61147d16a313e1ea8a8376f97626d9475d7b95 (diff) | |
download | org.eclipse.cdt-d353a33638e7eb62427ace558f53301653b235bc.tar.gz org.eclipse.cdt-d353a33638e7eb62427ace558f53301653b235bc.tar.xz org.eclipse.cdt-d353a33638e7eb62427ace558f53301653b235bc.zip |
Hide Method (work in progress) by Emanuel Graf, bug 226533.
24 files changed, 1431 insertions, 68 deletions
diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/HideMethod.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/HideMethod.rts new file mode 100644 index 00000000000..686eb645339 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/HideMethod.rts @@ -0,0 +1,845 @@ +//!HideMethodChangeToDefaultVisibility +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + //$void method2();$// +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: +private: + void method2(); +}; + +#endif /*A_H_*/ + +//!HideMethodSimple +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + //$void method2();$// + std::string toString(); +private: + int i; +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + std::string toString(); +private: + int i; + void method2(); +}; + +#endif /*A_H_*/ + +//!HideMethodLineComment +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + //Kommentar + //$void method2();$// + std::string toString(); +private: + int i; +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + std::string toString(); +private: + int i; + //Kommentar + void method2(); +}; + +#endif /*A_H_*/ + +//!HideMethodBlockComment +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + /*Kommentar*/ + //$void method2();$// + std::string toString(); + +private: + int i; +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + std::string toString(); +private: + int i; + /*Kommentar*/ + void method2(); +}; + +#endif /*A_H_*/ + +//!HideMethodLineCommentBehind +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + //$void method2();$////Kommentar + std::string toString(); +private: + int i; +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + std::string toString(); +private: + int i; + void method2(); //Kommentar +}; + +#endif /*A_H_*/ + +//!HideMethodBlockCommentBehind +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + //$void method2();$///*Kommentar*/ + std::string toString(); +private: + int i; +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + std::string toString(); +private: + int i; + void method2(); /*Kommentar*/ +}; + +#endif /*A_H_*/ + +//!HideMethodLineCommentWithSpace +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + + //Kommentar + //$void method2();$// + std::string toString(); +private: + int i; +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + std::string toString(); +private: + int i; + //Kommentar + void method2(); +}; + +#endif /*A_H_*/ + +//!HideMethodBlockCommentWithSpace +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + + /*Kommentar*/ + //$void method2();$// + std::string toString(); +private: + int i; +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + std::string toString(); +private: + int i; + /*Kommentar*/ + void method2(); +}; + +#endif /*A_H_*/ + +//!HideMethodLineCommentWithSpaceBehind +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + //$void method2();$////Kommentar + std::string toString(); +private: + int i; +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + std::string toString(); +private: + int i; + void method2(); //Kommentar +}; + +#endif /*A_H_*/ + +//!HideMethodBlockCommentWithSpaceBehind +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + //$void method2();$///*Kommentar*/ + + std::string toString(); +private: + int i; +}; +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + std::string toString(); +private: + int i; + void method2(); /*Kommentar*/ +}; +#endif /*A_H_*/ + +//!HideMethodBigBlockComment +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + /* + * Kommentar + */ + //$void method2();$// + std::string toString(); +private: + int i; +}; +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + std::string toString(); +private: + int i; + /* + * Kommentar + */ + void method2(); +}; +#endif /*A_H_*/ + +//!HideMethodBigBlockCommentBehind +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + //$void method2();$// /* + * Kommentar + */ + std::string toString(); +private: + int i; +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + std::string toString(); +private: + int i; + void method2(); /* + * Kommentar + */ +}; + +#endif /*A_H_*/ + +//!HideMethodBigBlockCommentBeforeAndBehind +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + /* + * Davor + */ + //$void method2();$// /* + * Kommentar + */ + std::string toString(); +private: + int i; +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + std::string toString(); +private: + int i; + /* + * Davor + */ + void method2(); /* + * Kommentar + */ +}; + +#endif /*A_H_*/ + +//!HideMethodMixedCommentBeforeAndAfter +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + /*123*/ + //$void method2();$////TEST + std::string toString(); +private: + int i; +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + std::string toString(); +private: + int i; + /*123*/ + void method2(); //TEST +}; + +#endif /*A_H_*/ + +//!HideMethodBlockCommentBeforeAndBehind +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + /*123*/ + //$void method2();$///*TEST*/ + std::string toString(); +private: + int i; +}; +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + std::string toString(); +private: + int i; + /*123*/ + void method2(); /*TEST*/ +}; +#endif /*A_H_*/ + +//!HideMethodNoChange +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +private: + //$void method2();$// +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +private: + void method2(); +}; + +#endif /*A_H_*/ + +//!HideMethod2MethodsDifferentLine +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + //$void method1();$// + void method2(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + void method2(); +private: + void method1(); +}; + +#endif /*A_H_*/ + +//!HideMethod2MethodsSameLine +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + //$void method1();$//void method2(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + void method2(); +private: + void method1(); +}; + +#endif /*A_H_*/ + +//!HideMethod2MethodsDifferentLineWithComment +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + //Kommentar + //$void method1();$// + void method2(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + void method2(); +private: + //Kommentar + void method1(); +}; + +#endif /*A_H_*/ + +//!HideMethod2MethodsSameLineWithComment +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + //Kommentar + //$void method1();$//void method2(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + void method2(); +private: + //Kommentar + void method1(); +}; + +#endif /*A_H_*/ + +//!HideMethodSimple ImplementationFile +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + void method2(); + std::string toString(); +private: + int i; +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + std::string toString(); +private: + int i; + void method2(); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +void A:://$method2$//() +{ +} + +//!HideMethodSimple ImplementationFile with Comments BUG #60 +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@A.h +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + //TEST 1 + void method2(); //TEST 2 + std::string toString(); +private: + int i; +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include <iostream> + +class A{ +public: + A(); + std::string toString(); +private: + int i; + //TEST 1 + void method2(); //TEST 2 +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +void A:://$method2$//() +{ +} + diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/RefactoringTestSuite.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/RefactoringTestSuite.java index 1a0f3e7c193..e7cb4b67ece 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/RefactoringTestSuite.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/RefactoringTestSuite.java @@ -29,6 +29,7 @@ public class RefactoringTestSuite extends TestSuite { suite.addTest(RenameRegressionTests.suite()); suite.addTest(ExtractFunctionTestSuite.suite()); suite.addTest(RefactoringTester.suite("ExtractConstantRefactoringTests", "resources/refactoring/ExtractConstant.rts")); + suite.addTest(RefactoringTester.suite("HideMethodRefactoringTests", "resources/refactoring/HideMethod.rts")); suite.addTest(UtilTestSuite.suite()); return suite; } diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/hidemethod/HideMethodRefactoringTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/hidemethod/HideMethodRefactoringTest.java new file mode 100644 index 00000000000..6cd48da8d25 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/hidemethod/HideMethodRefactoringTest.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2008 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.ui.tests.refactoring.hidemethod; + +import java.util.Properties; +import java.util.Vector; + +import org.eclipse.core.resources.IFile; +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; + +import org.eclipse.cdt.ui.tests.refactoring.RefactoringTest; +import org.eclipse.cdt.ui.tests.refactoring.TestSourceFile; + +import org.eclipse.cdt.internal.ui.refactoring.hidemethod.HideMethodRefactoring; + +/** + * @author Guido Zgraggen IFS + */ +public class HideMethodRefactoringTest extends RefactoringTest { + + public HideMethodRefactoringTest(String name, Vector<TestSourceFile> files) { + super(name, files); + } + + @Override + protected void runTest() throws Throwable { + + IFile refFile = project.getFile(fileWithSelection); + + HideMethodRefactoring refactoring = new HideMethodRefactoring(refFile,selection, null); + RefactoringStatus checkInitialConditions = refactoring.checkInitialConditions(NULL_PROGRESS_MONITOR); + assertConditionsOk(checkInitialConditions); + + Change createChange = refactoring.createChange(NULL_PROGRESS_MONITOR); + RefactoringStatus finalConditions = refactoring.checkFinalConditions(NULL_PROGRESS_MONITOR); + assertConditionsOk(finalConditions); + createChange.perform(NULL_PROGRESS_MONITOR); + + compareFiles(fileMap); + + } + + @Override + protected void configureRefactoring(Properties refactoringProperties) { + } +} diff --git a/core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF b/core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF index a0b98067114..5abd0be4a26 100644 --- a/core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF +++ b/core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF @@ -7,10 +7,10 @@ Bundle-Activator: org.eclipse.cdt.ui.CUIPlugin Bundle-Vendor: %providerName Bundle-Localization: plugin Export-Package: org.eclipse.cdt.internal.corext;x-internal:=true, - org.eclipse.cdt.internal.corext.codemanipulation, + org.eclipse.cdt.internal.corext.codemanipulation;x-internal:=true, org.eclipse.cdt.internal.corext.template.c;x-internal:=true, org.eclipse.cdt.internal.corext.util;x-internal:=true, - org.eclipse.cdt.internal.ui, + org.eclipse.cdt.internal.ui;x-internal:=true, org.eclipse.cdt.internal.ui.actions;x-internal:=true, org.eclipse.cdt.internal.ui.browser.opentype;x-internal:=true, org.eclipse.cdt.internal.ui.buildconsole;x-internal:=true, @@ -25,13 +25,14 @@ Export-Package: org.eclipse.cdt.internal.corext;x-internal:=true, org.eclipse.cdt.internal.ui.filters;x-internal:=true, org.eclipse.cdt.internal.ui.includebrowser;x-internal:=true, org.eclipse.cdt.internal.ui.indexview;x-internal:=true, - org.eclipse.cdt.internal.ui.navigator, + org.eclipse.cdt.internal.ui.navigator;x-internal:=true, org.eclipse.cdt.internal.ui.preferences;x-internal:=true, - org.eclipse.cdt.internal.ui.preferences.formatter, + org.eclipse.cdt.internal.ui.preferences.formatter;x-internal:=true, org.eclipse.cdt.internal.ui.refactoring;x-friends:="org.eclipse.cdt.ui.tests", - org.eclipse.cdt.internal.ui.refactoring.dialogs, + org.eclipse.cdt.internal.ui.refactoring.dialogs;x-internal:=true, org.eclipse.cdt.internal.ui.refactoring.extractconstant;x-friends:="org.eclipse.cdt.ui.tests", - org.eclipse.cdt.internal.ui.refactoring.extractfunction, + org.eclipse.cdt.internal.ui.refactoring.extractfunction;x-friends:="org.eclipse.cdt.ui.tests", + org.eclipse.cdt.internal.ui.refactoring.hidemethod;x-friends:="org.eclipse.cdt.ui.tests", org.eclipse.cdt.internal.ui.refactoring.rename;x-friends:="org.eclipse.cdt.ui.tests", org.eclipse.cdt.internal.ui.refactoring.utils;x-friends:="org.eclipse.cdt.ui.tests", org.eclipse.cdt.internal.ui.search;x-internal:=true, diff --git a/core/org.eclipse.cdt.ui/plugin.properties b/core/org.eclipse.cdt.ui/plugin.properties index fa7e0163118..5bd80b700b7 100644 --- a/core/org.eclipse.cdt.ui/plugin.properties +++ b/core/org.eclipse.cdt.ui/plugin.properties @@ -137,6 +137,7 @@ ActionDefinition.showMacroExplorer.description= Opens a quick view for macro exp category.refactoring.description= C/C++ Refactorings category.refactoring.name = Refactor - C++ refactoringExtractConstant.label = Extract Constant... +refactoringHideMethod.label = Hide Memeber Function... ActionDefinition.renameElement.name= Rename - Refactoring @@ -154,6 +155,7 @@ Refactoring.menu.label= Refac&tor Refactoring.renameAction.label=Re&name... Refactoring.extractConstant.label=Extr&act Constant... Refactoring.extractFunction.label=Extract &Function... (work in progress) +Refactoring.hideMethod.label=Hide Member Function... (work in progress) CEditor.name=C/C++ Editor diff --git a/core/org.eclipse.cdt.ui/plugin.xml b/core/org.eclipse.cdt.ui/plugin.xml index 3dc49bc5ebb..9e7a19655c9 100644 --- a/core/org.eclipse.cdt.ui/plugin.xml +++ b/core/org.eclipse.cdt.ui/plugin.xml @@ -1151,6 +1151,13 @@ retarget="true"> </action> <action + definitionId="org.eclipse.cdt.ui.refactor.hide.method" + label="%Refactoring.hideMethod.label" + menubarPath="org.eclipse.jdt.ui.refactoring.menu/codingGroup" + id="org.eclipse.cdt.ui.actions.HideMethod" + retarget="true"> + </action> + <action definitionId="org.eclipse.cdt.ui.refactor.extract.function" label="%Refactoring.extractFunction.label" menubarPath="org.eclipse.jdt.ui.refactoring.menu/codingGroup" @@ -1967,6 +1974,10 @@ categoryId="org.eclipse.cdt.ui.category.refactoring" id="org.eclipse.cdt.ui.refactoring.command.ExtractConstant" name="%refactoringExtractConstant.label"/> + <command + categoryId="org.eclipse.cdt.ui.category.refactoring" + id="org.eclipse.cdt.ui.refactor.hide.method" + name="%refactoringHideMethod.label"/> <command name="%ActionDefinition.renameElement.name" description="%ActionDefinition.renameElement.description" diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ICEditorActionDefinitionIds.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ICEditorActionDefinitionIds.java index 436c2c988bc..4429608093b 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ICEditorActionDefinitionIds.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ICEditorActionDefinitionIds.java @@ -105,6 +105,12 @@ public interface ICEditorActionDefinitionIds extends ITextEditorActionDefinition public static final String EXTRACT_FUNCTION = "org.eclipse.cdt.ui.refactor.extract.function"; //$NON-NLS-1$ /** + * Action definition ID of the refactor -> hide method action + * (value <code>"org.eclipse.cdt.ui.refactor.hide.method"</code>). + */ + public static final String HIDE_METHOD= "org.eclipse.cdt.ui.refactor.hide.method"; //$NON-NLS-1$ + + /** * Action definition ID of the refactor -> undo action * (value <code>"org.eclipse.cdt.ui.edit.text.undo.action"</code>). */ 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 61fd238d171..a11985198b8 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 @@ -12,6 +12,7 @@ package org.eclipse.cdt.internal.ui.refactoring; import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; @@ -20,6 +21,7 @@ import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.SubMonitor; import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.text.Region; import org.eclipse.jface.viewers.ISelection; import org.eclipse.ltk.core.refactoring.Change; import org.eclipse.ltk.core.refactoring.Refactoring; @@ -48,8 +50,12 @@ import org.eclipse.cdt.core.dom.ast.IASTTypeId; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName; import org.eclipse.cdt.core.index.IIndex; +import org.eclipse.cdt.core.model.CModelException; import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.model.ISourceRange; +import org.eclipse.cdt.core.model.ISourceReference; import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousDeclaration; @@ -65,25 +71,41 @@ import org.eclipse.cdt.internal.core.dom.parser.IASTDeclarationAmbiguity; public abstract class CRefactoring extends Refactoring { protected static final String EMPTY_STRING = ""; //$NON-NLS-1$ private static final int AST_STYLE = ITranslationUnit.AST_CONFIGURE_USING_SOURCE_CONTEXT | ITranslationUnit.AST_SKIP_INDEXED_HEADERS; - public static final String NEWLINE = "\n"; // mstodo //$NON-NLS-1$ + public static final String NEWLINE = "\n"; //$NON-NLS-1$ protected String name = Messages.HSRRefactoring_name; protected IFile file; - protected ISelection selection; + protected Region region; protected RefactoringStatus initStatus; protected IASTTranslationUnit unit; private IIndex fIndex; - public CRefactoring(IFile file, ISelection selection) { - this.file = file; - this.selection = selection; - this.initStatus=new RefactoringStatus(); + public CRefactoring(IFile file, ISelection selection, ICElement element) { + if (element instanceof ISourceReference) { + ISourceReference sourceRef= (ISourceReference) element; + ITranslationUnit 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()); + } catch (CModelException e) { + CCorePlugin.log(e); + } + } + else { + this.file = file; + this.region = getRegion(selection); + } - if(selection == null){ - initStatus.addError(Messages.HSRRefactoring_SelectionNotValid); + this.initStatus=new RefactoringStatus(); + if (this.file == null || region == null) { + initStatus.addFatalError(Messages.HSRRefactoring_SelectionNotValid); } } - + private class ProblemFinder extends ASTVisitor{ private boolean problemFound = false; @@ -248,10 +270,14 @@ public abstract class CRefactoring extends Refactoring { return name; } - protected ITextSelection getTextSelection() { - return (ITextSelection) selection; + private Region getRegion(ISelection selection) { + if (selection instanceof ITextSelection) { + final ITextSelection txtSelection= (ITextSelection) selection; + return new Region(txtSelection.getOffset(), txtSelection.getLength()); + } + return null; } - + private boolean loadTranslationUnit(RefactoringStatus status, IProgressMonitor mon) { SubMonitor subMonitor = SubMonitor.convert(mon, 10); @@ -325,7 +351,7 @@ public abstract class CRefactoring extends Refactoring { return selection; } - protected boolean isExpressionWhollyInSelection(ITextSelection textSelection, IASTNode expression) { + protected boolean isExpressionWhollyInSelection(Region textSelection, IASTNode expression) { ExpressionPosition exprPos = createExpressionPosition(expression); int selStart = textSelection.getOffset(); @@ -334,7 +360,7 @@ public abstract class CRefactoring extends Refactoring { return exprPos.start >= selStart && exprPos.end <= selEnd; } - public static boolean isSelectionOnExpression(ITextSelection textSelection, IASTNode expression) { + public static boolean isSelectionOnExpression(Region textSelection, IASTNode expression) { ExpressionPosition exprPos = createExpressionPosition(expression); int selStart = textSelection.getOffset(); int selEnd = textSelection.getLength() + selStart; @@ -348,14 +374,14 @@ public abstract class CRefactoring extends Refactoring { return locFile.equals(tmpFile); } - protected boolean isInSameFileSelection(ITextSelection textSelection, IASTNode node) { + protected boolean isInSameFileSelection(Region textSelection, IASTNode node) { if( isInSameFile(node) ) { return isSelectionOnExpression(textSelection, node); } return false; } - protected boolean isSelectedFile(ITextSelection textSelection, IASTNode node) { + protected boolean isSelectedFile(Region textSelection, IASTNode node) { if( isInSameFile(node) ) { return isExpressionWhollyInSelection(textSelection, node); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/RefactoringRunner.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/RefactoringRunner.java index d5b1805f593..08e93c87011 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/RefactoringRunner.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/RefactoringRunner.java @@ -15,6 +15,8 @@ import org.eclipse.core.resources.IFile; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.window.IShellProvider; +import org.eclipse.cdt.core.model.ICElement; + /** * Base class for all refactoring runners. * @@ -25,14 +27,16 @@ public abstract class RefactoringRunner { protected IFile file; protected ISelection selection; + protected ICElement celement; protected IShellProvider shellProvider; - public RefactoringRunner(IFile file, ISelection selection, IShellProvider shellProvider) { + public RefactoringRunner(IFile file, ISelection selection, ICElement element, IShellProvider shellProvider) { this.file = file; this.selection = selection; + this.celement= element; this.shellProvider= shellProvider; } - + public abstract void run(); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoring.java index 0b7c13ad109..146e39b804b 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoring.java @@ -23,7 +23,7 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.SubMonitor; -import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.text.Region; import org.eclipse.jface.viewers.ISelection; import org.eclipse.ltk.core.refactoring.RefactoringStatus; import org.eclipse.text.edits.TextEditGroup; @@ -80,14 +80,14 @@ public class ExtractConstantRefactoring extends CRefactoring { private final NameNVisibilityInformation info; public ExtractConstantRefactoring(IFile file, ISelection selection, NameNVisibilityInformation info){ - super(file,selection); + super(file,selection, null); this.info = info; name = Messages.ExtractConstantRefactoring_ExtractConst; } @Override public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException { - SubMonitor sm = SubMonitor.convert(pm, 10); + SubMonitor sm = SubMonitor.convert(pm, 9); super.checkInitialConditions(sm.newChild(6)); Collection<IASTLiteralExpression> literalExpressionVector = findAllLiterals(); @@ -95,16 +95,11 @@ public class ExtractConstantRefactoring extends CRefactoring { initStatus.addFatalError(Messages.ExtractConstantRefactoring_LiteralMustBeSelected); return initStatus; } - sm.worked(1); - - if(isProgressMonitorCanceld(sm, initStatus)) return initStatus; - ITextSelection textSelection = getTextSelection(); sm.worked(1); - if(isProgressMonitorCanceld(sm, initStatus)) return initStatus; - boolean oneMarked = isOneMarked(literalExpressionVector, textSelection); + boolean oneMarked = region != null && isOneMarked(literalExpressionVector, region); if(!oneMarked){ //No or more than one marked if(target == null){ @@ -132,7 +127,7 @@ public class ExtractConstantRefactoring extends CRefactoring { String nameString = literal.toString(); switch (literal.getKind()) { case IASTLiteralExpression.lk_char_constant: - case ICPPASTLiteralExpression.lk_string_literal: + case IASTLiteralExpression.lk_string_literal: int beginIndex = 1; if(nameString.startsWith("L")) { //$NON-NLS-1$ beginIndex = 2; @@ -220,7 +215,7 @@ public class ExtractConstantRefactoring extends CRefactoring { } } - private boolean isOneMarked(Collection<IASTLiteralExpression> literalExpressionVector, ITextSelection textSelection) { + private boolean isOneMarked(Collection<IASTLiteralExpression> literalExpressionVector, Region textSelection) { boolean oneMarked = false; for (IASTLiteralExpression expression : literalExpressionVector) { boolean isInSameFileSelection = isInSameFileSelection(textSelection, expression); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoringRunner.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoringRunner.java index 2a6f285f0e8..db541d16d4a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoringRunner.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoringRunner.java @@ -30,7 +30,7 @@ import org.eclipse.cdt.internal.ui.refactoring.RefactoringRunner; public class ExtractConstantRefactoringRunner extends RefactoringRunner { public ExtractConstantRefactoringRunner(IFile file, ISelection selection, IShellProvider shellProvider) { - super(file, selection, shellProvider); + super(file, selection, null, shellProvider); } @Override diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java index c3b21f44a2e..c45ce101a43 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java @@ -28,7 +28,6 @@ import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.SubMonitor; -import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.viewers.ISelection; import org.eclipse.ltk.core.refactoring.RefactoringStatus; import org.eclipse.text.edits.TextEditGroup; @@ -123,7 +122,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { public ExtractFunctionRefactoring(IFile file, ISelection selection, ExtractFunctionInformation info) { - super(file, selection); + super(file, selection, null); this.info = info; name = Messages.ExtractFunctionRefactoring_ExtractFunction; names = new HashMap<String, Integer>(); @@ -902,39 +901,31 @@ public class ExtractFunctionRefactoring extends CRefactoring { private NodeContainer findExtractableNodes() { final NodeContainer container = new NodeContainer(); - if (selection instanceof ITextSelection) { - final ITextSelection textSelection = (ITextSelection) selection; - - unit.accept(new CPPASTVisitor() { - - { - shouldVisitStatements = true; - shouldVisitExpressions = true; - } + unit.accept(new CPPASTVisitor() { + { + shouldVisitStatements = true; + shouldVisitExpressions = true; + } - @Override - public int visit(IASTStatement stmt) { - if (!(stmt instanceof IASTCompoundStatement) - && isSelectedFile(textSelection, stmt)) { - container.add(stmt); - return PROCESS_SKIP; - } - return super.visit(stmt); + @Override + public int visit(IASTStatement stmt) { + if (!(stmt instanceof IASTCompoundStatement) + && isSelectedFile(region, stmt)) { + container.add(stmt); + return PROCESS_SKIP; } + return super.visit(stmt); + } - @Override - public int visit(IASTExpression expression) { - if (isSelectedFile(textSelection, expression)) { - container.add(expression); - return PROCESS_SKIP; - } - return super.visit(expression); + @Override + public int visit(IASTExpression expression) { + if (isSelectedFile(region, expression)) { + container.add(expression); + return PROCESS_SKIP; } - - }); - - } - + return super.visit(expression); + } + }); return container; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoringRunner.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoringRunner.java index 653e54f6bf6..5e453713bf7 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoringRunner.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoringRunner.java @@ -26,7 +26,7 @@ import org.eclipse.cdt.internal.ui.refactoring.RefactoringRunner; public class ExtractFunctionRefactoringRunner extends RefactoringRunner { public ExtractFunctionRefactoringRunner(IFile file, ISelection selection, IShellProvider shellProvider) { - super(file, selection, shellProvider); + super(file, selection, null, shellProvider); } @Override diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/HideMethodRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/HideMethodRefactoring.java new file mode 100644 index 00000000000..006500a8845 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/HideMethodRefactoring.java @@ -0,0 +1,192 @@ +/******************************************************************************* + * Copyright (c) 2008 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.hidemethod; + +import java.util.Vector; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; +import org.eclipse.core.runtime.SubMonitor; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import org.eclipse.text.edits.TextEditGroup; + +import org.eclipse.cdt.core.dom.ast.ASTVisitor; +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName; +import org.eclipse.cdt.core.dom.rewrite.ASTRewrite; +import org.eclipse.cdt.core.model.ICElement; + +import org.eclipse.cdt.internal.ui.refactoring.AddDeclarationNodeToClassChange; +import org.eclipse.cdt.internal.ui.refactoring.CRefactoring; +import org.eclipse.cdt.internal.ui.refactoring.ModificationCollector; +import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum; + +/** + * @author Guido Zgraggen IFS + * + */ +public class HideMethodRefactoring extends CRefactoring { + + private IASTName methodToMove; + private IASTSimpleDeclaration fieldToMoveDecl; + + public HideMethodRefactoring(IFile file, ISelection selection, ICElement element) { + super(file, selection, element); + name = Messages.HideMethodRefactoring_HIDE_METHOD; + } + + @Override + public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException { + SubMonitor sm = SubMonitor.convert(pm, 10); + super.checkInitialConditions(sm.newChild(6)); + + if(initStatus.hasFatalError()){ + return initStatus; + } + + if(isProgressMonitorCanceld(sm, initStatus)) return initStatus; + + IASTName name; + Vector<IASTName> names = findAllMarkedNames(); + if(names.size() > 1) { + name = names.lastElement(); + } else if (names.size() < 1) { + initStatus.addFatalError(Messages.HideMethodRefactoring_NoNameSelected); + return initStatus; + }else { + name = names.get(0); + } + sm.worked(1); + if(isProgressMonitorCanceld(sm, initStatus)) return initStatus; + + this.methodToMove = getDeclaration(name); + + if(this.methodToMove == null) { + initStatus.addFatalError(Messages.HideMethodRefactoring_NoMethodNameSeleceted); + return initStatus; + } + sm.worked(1); + fieldToMoveDecl = findSimpleDeclaration(this.methodToMove); + + if(isProgressMonitorCanceld(sm, initStatus)) return initStatus; + sm.worked(1); + for(IASTDeclarator declarator : fieldToMoveDecl.getDeclarators()) { + if(declarator.getName().getRawSignature().equals(name.getRawSignature())) { + if (!(declarator instanceof IASTFunctionDeclarator)) { + initStatus.addFatalError(Messages.HideMethodRefactoring_CanOnlyHideMethods); + return initStatus; + } + } + } + sm.done(); + return initStatus; + } + + private Vector<IASTName> findAllMarkedNames() { + final Vector<IASTName> namesVector = new Vector<IASTName>(); + + unit.accept(new CPPASTVisitor(){ + + { + shouldVisitNames = true; + } + + @Override + public int visit(IASTName name) { + if( isInSameFileSelection(region, name) ) { + if (!(name instanceof ICPPASTQualifiedName)) { + namesVector.add(name); + } + } + return super.visit(name); + } + + }); + return namesVector; + } + + protected IASTSimpleDeclaration findSimpleDeclaration(IASTNode fieldToFoundDecl) { + while(fieldToFoundDecl != null){ + if (fieldToFoundDecl instanceof IASTSimpleDeclaration) { + return (IASTSimpleDeclaration) fieldToFoundDecl; + } + fieldToFoundDecl = fieldToFoundDecl.getParent(); + } + return null; + } + + private IASTName getDeclaration(IASTName name) { + DeclarationFinder df = new DeclarationFinder(name); + unit.accept(df); + return df.getDeclaration(); + } + + private class DeclarationFinder extends CPPASTVisitor{ + + IASTName astName; + IASTName declaration = null; + + { + shouldVisitDeclarators = true; + } + + public DeclarationFinder(IASTName name) { + this.astName = name; + } + + @Override + public int visit(IASTDeclarator declarator) { + if(declarator instanceof ICPPASTFunctionDeclarator) { + ICPPASTFunctionDeclarator funcDec = (ICPPASTFunctionDeclarator)declarator; + if(funcDec.getName().getRawSignature().equals(astName.getRawSignature())){ + declaration = funcDec.getName(); + return PROCESS_ABORT; + } + } + return ASTVisitor.PROCESS_CONTINUE; + } + + public IASTName getDeclaration() { + return declaration; + } + } + + @Override + public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException { + return super.checkFinalConditions(pm); + } + + @Override + protected void collectModifications(IProgressMonitor pm, ModificationCollector collector) throws CoreException, OperationCanceledException { + + ASTRewrite rewriter = collector.rewriterForTranslationUnit(unit); + TextEditGroup editGroup = new TextEditGroup(Messages.HideMethodRefactoring_FILE_CHANGE_TEXT+ methodToMove.getRawSignature()); + + // egtodo +// IASTNode parent = fieldToMoveDecl.getParent(); + + ICPPASTCompositeTypeSpecifier classDefinition = (ICPPASTCompositeTypeSpecifier) fieldToMoveDecl.getParent(); + AddDeclarationNodeToClassChange.createChange(classDefinition, VisibilityEnum.v_private, fieldToMoveDecl, false, collector); + + rewriter.remove(fieldToMoveDecl, editGroup); + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/HideMethodRefactoringRunner.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/HideMethodRefactoringRunner.java new file mode 100644 index 00000000000..87ee6269072 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/HideMethodRefactoringRunner.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2008 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.hidemethod; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.window.IShellProvider; +import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation; + +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.ui.CUIPlugin; + +import org.eclipse.cdt.internal.ui.refactoring.CRefactoring; +import org.eclipse.cdt.internal.ui.refactoring.RefactoringRunner; + +/** + * @author Guido Zgraggen IFS + * + */ +public class HideMethodRefactoringRunner extends RefactoringRunner { + + public HideMethodRefactoringRunner(IFile file, ISelection selection, ICElement element, IShellProvider shellProvider) { + super(file, selection, element, shellProvider); + } + + + @Override + public void run() { + CRefactoring refactoring= new HideMethodRefactoring(file, selection, celement); + HideMethodRefactoringWizard wizard = new HideMethodRefactoringWizard(refactoring); + RefactoringWizardOpenOperation operator = new RefactoringWizardOpenOperation(wizard); + + try { + refactoring.lockIndex(); + try { + operator.run(shellProvider.getShell(), refactoring.getName()); + } + finally { + refactoring.unlockIndex(); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (CoreException e) { + CUIPlugin.log(e); + } + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/HideMethodRefactoringWizard.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/HideMethodRefactoringWizard.java new file mode 100644 index 00000000000..0e73249045f --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/HideMethodRefactoringWizard.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2008 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.hidemethod; + +import org.eclipse.ltk.core.refactoring.Refactoring; +import org.eclipse.ltk.ui.refactoring.RefactoringWizard; + +/** + * @author Guido Zgraggen IFS + * + */ +public class HideMethodRefactoringWizard extends RefactoringWizard { + + public HideMethodRefactoringWizard(Refactoring refactoring) { + super(refactoring, WIZARD_BASED_USER_INTERFACE); + } + + @Override + protected void addUserInputPages() { + //No spezial User Wizard to add + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/Messages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/Messages.java new file mode 100644 index 00000000000..5d248d43ee1 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/Messages.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright (c) 2008 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.hidemethod; + +import org.eclipse.osgi.util.NLS; + +public final class Messages extends NLS { + + private static final String BUNDLE_NAME = "org.eclipse.cdt.hsrrefactoring.hidemethod.messages";//$NON-NLS-1$ + + private Messages() { + // Do not instantiate + } + + public static String HideMethodRefactoring_HIDE_METHOD; + public static String HideMethodRefactoring_NoNameSelected; + public static String HideMethodRefactoring_NoMethodNameSeleceted; + public static String HideMethodRefactoring_CanOnlyHideMethods; + public static String HideMethodRefactoring_FILE_CHANGE_TEXT; + public static String HideMethodRefactoring_BAD_TYPE; + public static String HideMethodRefactoring_NO_SELECTION; + public static String HideMethodRefactoring_BAD_DIALECT; + public static String HideMethodRefactoring_NO_FILE; + public static String HideMethodRefactoring_NO_PROPER_SELECTION; + public static String HideMethodRefactoring_EDITOR_NOT_SAVE; + public static String HideMethodRefactoring_NO_NODES; + + static { + NLS.initializeMessages(BUNDLE_NAME, Messages.class); + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/messages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/messages.properties new file mode 100644 index 00000000000..440c3f39926 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/messages.properties @@ -0,0 +1,25 @@ +############################################################################### +# Copyright (c) 2008 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 +############################################################################### +HideMethodRefactoring_HIDE_METHOD=Hide Method +HideMethodRefactoring_NoNameSelected=No names selected. +HideMethodRefactoring_NoMethodNameSeleceted=No method name selected. +HideMethodRefactoring_CanOnlyHideMethods=Hide Method can only hide Methods. +HideMethodRefactoring_FILE_CHANGE_TEXT=Hide +HideMethodRefactoring_BAD_TYPE=Bad selection-type +HideMethodRefactoring_NO_SELECTION=No Selection available. + +HideMethodRefactoring_BAD_DIALECT=Not Supported Dialect +HideMethodRefactoring_NO_FILE=There is no file marked. +HideMethodRefactoring_NO_PROPER_SELECTION=No proper Selection\! +HideMethodRefactoring_EDITOR_NOT_SAVE=Editor is not saved +HideMethodRefactoring_NO_NODES=No nodes found. + diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/CdtActionConstants.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/CdtActionConstants.java index 1672e554ac3..871ce7c85ff 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/CdtActionConstants.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/CdtActionConstants.java @@ -247,6 +247,12 @@ public class CdtActionConstants { * (value <code>"org.eclipse.cdt.ui.actions.ExtractConstant"</code>). */ public static final String EXTRACT_CONSTANT= "org.eclipse.cdt.ui.actions.ExtractConstant"; //$NON-NLS-1$ + + /** + * Refactor menu: name of standard Hide Method global action + * (value <code>"org.eclipse.cdt.ui.actions.HideMethod"</code>). + */ + public static final String HIDE_METHOD= "org.eclipse.cdt.ui.actions.HideMethod"; //$NON-NLS-1$ /** * Refactor menu: name of standard Introduce Parameter global action diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/CRefactoringActionGroup.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/CRefactoringActionGroup.java index 4392ca6dcaf..4ad44b96550 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/CRefactoringActionGroup.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/CRefactoringActionGroup.java @@ -114,6 +114,7 @@ public class CRefactoringActionGroup extends ActionGroup implements ISelectionCh private CRenameAction fRenameAction; private RefactoringAction fExtractConstantAction; private RefactoringAction fExtractFunctionAction; + private RefactoringAction fHideMethodAction; private IWorkbenchSite fSite; private List<RefactoringAction> fAllActions= new ArrayList<RefactoringAction>(); @@ -154,6 +155,10 @@ public class CRefactoringActionGroup extends ActionGroup implements ISelectionCh fExtractFunctionAction.setActionDefinitionId(ICEditorActionDefinitionIds.EXTRACT_FUNCTION); fAllActions.add(fExtractFunctionAction); } + + fHideMethodAction = new HideMethodAction(); + fHideMethodAction.setActionDefinitionId(ICEditorActionDefinitionIds.HIDE_METHOD); + fAllActions.add(fHideMethodAction); } public void setWorkbenchSite(IWorkbenchSite site) { @@ -190,6 +195,7 @@ public class CRefactoringActionGroup extends ActionGroup implements ISelectionCh setActionHandler(actionBar, CdtActionConstants.RENAME, fRenameAction); setActionHandler(actionBar, CdtActionConstants.EXTRACT_CONSTANT, fExtractConstantAction); setActionHandler(actionBar, CdtActionConstants.EXTRACT_METHOD, fExtractFunctionAction); + setActionHandler(actionBar, CdtActionConstants.HIDE_METHOD, fHideMethodAction); } private void setActionHandler(IActionBars actionBar, String id, RefactoringAction action) { @@ -219,6 +225,8 @@ public class CRefactoringActionGroup extends ActionGroup implements ISelectionCh refactorSubmenu.add(new Separator(GROUP_CODING)); addAction(refactorSubmenu, fExtractConstantAction); addAction(refactorSubmenu, fExtractFunctionAction); + addAction(refactorSubmenu, fHideMethodAction); + refactorSubmenu.add(new Separator(GROUP_REORG2)); refactorSubmenu.add(new Separator(GROUP_TYPE)); refactorSubmenu.add(new Separator(GROUP_TYPE2)); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/HideMethodAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/HideMethodAction.java new file mode 100644 index 00000000000..be07c76bd20 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/HideMethodAction.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * Copyright (c) 2008 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.ui.refactoring.actions; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.window.IShellProvider; + +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.IMethodDeclaration; +import org.eclipse.cdt.core.model.ISourceReference; +import org.eclipse.cdt.core.model.IWorkingCopy; + +import org.eclipse.cdt.internal.ui.refactoring.hidemethod.HideMethodRefactoringRunner; + +/** + * Launches a HideMethod refacoring + * @author Guido Zgraggen IFS + */ +public class HideMethodAction extends RefactoringAction { + + public HideMethodAction() { + super(Messages.HideMethodAction_label); + } + + @Override + public void run(IShellProvider shellProvider, ICElement elem) { + if (elem instanceof ISourceReference) { + new HideMethodRefactoringRunner(null, null, elem, shellProvider).run(); + } + } + + @Override + public void run(IShellProvider shellProvider, IWorkingCopy wc, ITextSelection s) { + IResource res= wc.getResource(); + if (res instanceof IFile) { + new HideMethodRefactoringRunner((IFile) res, + fEditor.getSelectionProvider().getSelection(), null, + fEditor.getSite().getWorkbenchWindow()).run(); + } + } + + @Override + public void updateSelection(ICElement elem) { + super.updateSelection(elem); + if (elem instanceof IMethodDeclaration == false + || elem instanceof ISourceReference == false + || ((ISourceReference) elem).getTranslationUnit().getResource() instanceof IFile == false) { + setEnabled(false); + } + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/Messages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/Messages.java index 5e039d03ece..b6b21caa8a9 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/Messages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/Messages.java @@ -18,6 +18,8 @@ public class Messages extends NLS { public static String CRenameAction_label; public static String ExtractConstantAction_label; public static String ExtractFunctionAction_label; + public static String HideMethodAction_label; + static { // initialize resource bundle NLS.initializeMessages(BUNDLE_NAME, Messages.class); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/RefactoringAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/RefactoringAction.java index 5457fc05d38..f320148e400 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/RefactoringAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/RefactoringAction.java @@ -78,6 +78,7 @@ public abstract class RefactoringAction extends Action { public void updateSelection(ICElement elem) { fElement= elem; + setEnabled(elem != null); } public abstract void run(IShellProvider shellProvider, IWorkingCopy wc, ITextSelection s); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/messages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/messages.properties index 40b345509f4..a21aa9ad87c 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/messages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/refactoring/actions/messages.properties @@ -11,4 +11,5 @@ CRefactoringActionGroup_menu=Refactor CRenameAction_label=Rename... ExtractConstantAction_label=Extract Constant... +HideMethodAction_label=Hide Member Function... (work in progress) ExtractFunctionAction_label=Extract Function... (work in progress) |