Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preliminary test case for refactoring pull members up functionality #214

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ private void doMoveField(PsiSubstitutor substitutor, MemberInfo info) {
}
final PsiMember movedElement = (PsiMember)myTargetSuperClass.addBefore(convertFieldToLanguage(field, myTargetSuperClass.getLanguage()), myTargetSuperClass.getRBrace());
myMembersAfterMove.add(movedElement);
reformat(movedElement);
field.delete();
}

Expand Down Expand Up @@ -270,11 +271,11 @@ private void doMoveMethod(PsiSubstitutor substitutor, MemberInfo info) {
reformat(movedElement);
}
CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(method.getProject());
if (styleSettings.INSERT_OVERRIDE_ANNOTATION) {
/*if (styleSettings.INSERT_OVERRIDE_ANNOTATION) {
if (PsiUtil.isLanguageLevel5OrHigher(mySourceClass) && !myIsTargetInterface || PsiUtil.isLanguageLevel6OrHigher(mySourceClass)) {
new AddAnnotationFix(Override.class.getName(), method).invoke(method.getProject(), null, mySourceClass.getContainingFile());
}
}
}*/
if (!PsiUtil.isLanguageLevel6OrHigher(mySourceClass) && myIsTargetInterface) {
if (isOriginalMethodAbstract) {
for (PsiMethod oMethod : OverridingMethodsSearch.search(method)) {
Expand Down Expand Up @@ -317,6 +318,10 @@ private void reformat(final PsiMember movedElement) {
public void run() {
final TextRange range = movedElement.getTextRange();
final PsiFile file = movedElement.getContainingFile();

PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(myProject);
psiDocumentManager.doPostponedOperationsAndUnblockDocument(psiDocumentManager.getDocument(file));

final PsiFile baseFile = file.getViewProvider().getPsi(file.getViewProvider().getBaseLanguage());
CodeStyleManager.getInstance(myProject).reformatText(baseFile, range.getStartOffset(), range.getEndOffset());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ private PullUpHelper<MemberInfo> getProcessor(@NotNull MemberInfo info) {
return getProcessor(info.getMember());
}

@Override
protected void doRun() {
super.doRun();
}

private PsiSubstitutor upDownSuperClassSubstitutor() {
PsiSubstitutor substitutor = PsiSubstitutor.EMPTY;
for (PsiTypeParameter parameter : PsiUtil.typeParametersIterable(mySourceClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,14 @@ public void visitMethodCallExpression(PsiMethodCallExpression expression) {

public void checkMemberPlacementInTargetClassConflict(final PsiClass targetClass, final PsiMember movedMember) {
if (movedMember instanceof PsiField) {
/*
String name = movedMember.getName();
final PsiField field = targetClass.findFieldByName(name, false);
if (field != null) {
String message = RefactoringBundle.message("0.already.contains.field.1", RefactoringUIUtil.getDescription(targetClass, false), CommonRefactoringUtil.htmlEmphasize(name));
myConflicts.putValue(field, CommonRefactoringUtil.capitalize(message));
}
*/
}
else if (movedMember instanceof PsiMethod) {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,15 @@
import com.intellij.codeInsight.intention.impl.CreateSubclassAction;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.TextRange;
import com.intellij.plugins.haxe.lang.psi.HaxeFunctionDeclarationWithAttributes;
import com.intellij.plugins.haxe.lang.psi.HaxeInterfaceDeclaration;
import com.intellij.plugins.haxe.lang.psi.HaxeVarDeclarationPart;
import com.intellij.plugins.haxe.util.HaxeElementGenerator;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.search.searches.ReferencesSearch;
Expand All @@ -54,6 +49,7 @@
import com.intellij.refactoring.util.classMembers.MemberInfo;
import com.intellij.usageView.UsageInfo;
import com.intellij.usageView.UsageViewDescriptor;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Function;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.HashSet;
Expand Down Expand Up @@ -410,9 +406,13 @@ private void decodeRef(final PsiJavaCodeReferenceElement ref,

private void removeFromTargetClass() throws IncorrectOperationException {
for (MemberInfo memberInfo : myMemberInfos) {
final PsiElement member = memberInfo.getMember();
PsiElement member = memberInfo.getMember();

if (member instanceof PsiField) {
if (member instanceof HaxeVarDeclarationPart) {
member = member.getParent();
}

member.delete();
}
else if (member instanceof PsiMethod) {
Expand Down Expand Up @@ -445,6 +445,7 @@ protected void pushDownToClass(PsiClass targetClass) throws IncorrectOperationEx
final PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(myClass, targetClass, PsiSubstitutor.EMPTY);
for (MemberInfo memberInfo : myMemberInfos) {
PsiMember member = memberInfo.getMember();

final List<PsiReference> refsToRebind = new ArrayList<PsiReference>();
final PsiModifierList list = member.getModifierList();
LOG.assertTrue(list != null);
Expand All @@ -460,6 +461,11 @@ protected void pushDownToClass(PsiClass targetClass) throws IncorrectOperationEx
refsToRebind.add(reference);
}
}

if (member instanceof HaxeVarDeclarationPart) {
member = (PsiMember)member.getParent();
}

member = (PsiMember)member.copy();
RefactoringUtil.replaceMovedMemberTypeParameters(member, PsiUtil.typeParametersIterable(myClass), substitutor, factory);
PsiMember newMember = null;
Expand All @@ -478,7 +484,7 @@ else if (member instanceof PsiMethod) {
if (methodBySignature == null) {
newMember = null;
if (myClass.isInterface()) {
if (!(targetClass instanceof HaxeInterfaceDeclaration)) {
if (!(targetClass.isInterface())) {
String text = member.getText();

if (text.endsWith(";")) {
Expand Down Expand Up @@ -589,15 +595,21 @@ public int compare(PsiReference o1, PsiReference o2) {
}

private void reformat(final PsiMember movedElement) {
/*
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
final TextRange range = movedElement.getTextRange();
final PsiFile file = movedElement.getContainingFile();

PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(myProject);
psiDocumentManager.doPostponedOperationsAndUnblockDocument(psiDocumentManager.getDocument(file));

final PsiFile baseFile = file.getViewProvider().getPsi(file.getViewProvider().getBaseLanguage());
CodeStyleManager.getInstance(myProject).reformatText(baseFile, range.getStartOffset(), range.getEndOffset());
}
});
*/
}

private boolean leaveOverrideAnnotation(PsiSubstitutor substitutor, PsiMethod method) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ public PsiField[] getAllFields() {
@Override
@Nullable
public PsiField findFieldByName(@NonNls String name, boolean checkBases) {
//TODO: check if checkBases work, not sure it was able to find field from superclass even when checkBases is false
return PsiClassImplUtil.findFieldByName(this, name, checkBases);
}

Expand Down
11 changes: 11 additions & 0 deletions testData/refactoring/pullUp/FromClassToInterface/after/Test.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ;

import bar.ArrayUtils;

class Test {
static function main() {
function is_c(val) { return val == "c"; }

trace(ArrayUtils.delete_if([ "a", "b", "c" ], is_c));
}
}
22 changes: 22 additions & 0 deletions testData/refactoring/pullUp/FromClassToInterface/after/a/A.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2014-2015 AS3Boyan
* Copyright 2014-2014 Elias Ku
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package a;
import b.B;
class A implements B {
private function foo():Void {}
}
22 changes: 22 additions & 0 deletions testData/refactoring/pullUp/FromClassToInterface/after/b/B.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2014-2015 AS3Boyan
* Copyright 2014-2014 Elias Ku
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package b;
import c.C;
interface B extends C {
function method2Move():Void;
}
21 changes: 21 additions & 0 deletions testData/refactoring/pullUp/FromClassToInterface/after/c/C.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2014-2015 AS3Boyan
* Copyright 2014-2014 Elias Ku
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package c;
interface C {
private function foo():Void;
}
11 changes: 11 additions & 0 deletions testData/refactoring/pullUp/FromClassToInterface/before/Test.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ;

import bar.ArrayUtils;

class Test {
static function main() {
function is_c(val) { return val == "c"; }

trace(ArrayUtils.delete_if([ "a", "b", "c" ], is_c));
}
}
25 changes: 25 additions & 0 deletions testData/refactoring/pullUp/FromClassToInterface/before/a/A.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2014-2015 AS3Boyan
* Copyright 2014-2014 Elias Ku
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package a;
import b.B;
class A implements B {
function method2Move():Void {
foo();
}
private function foo():Void {}
}
21 changes: 21 additions & 0 deletions testData/refactoring/pullUp/FromClassToInterface/before/b/B.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2014-2015 AS3Boyan
* Copyright 2014-2014 Elias Ku
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package b;
import c.C;
interface B extends C {
}
21 changes: 21 additions & 0 deletions testData/refactoring/pullUp/FromClassToInterface/before/c/C.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2014-2015 AS3Boyan
* Copyright 2014-2014 Elias Ku
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package c;
interface C {
private function foo():Void;
}
11 changes: 11 additions & 0 deletions testData/refactoring/pullUp/FromClassToSuperClass/after/Test.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ;

import bar.ArrayUtils;

class Test {
static function main() {
function is_c(val) { return val == "c"; }

trace(ArrayUtils.delete_if([ "a", "b", "c" ], is_c));
}
}
22 changes: 22 additions & 0 deletions testData/refactoring/pullUp/FromClassToSuperClass/after/a/A.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2014-2015 AS3Boyan
* Copyright 2014-2014 Elias Ku
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package a;
import b.B;
class A extends B {
private function foo():Void {}
}
24 changes: 24 additions & 0 deletions testData/refactoring/pullUp/FromClassToSuperClass/after/b/B.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2014-2015 AS3Boyan
* Copyright 2014-2014 Elias Ku
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package b;
import c.C;
class B extends C {
function method2Move():Void {
foo();
}
}
Loading