mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
cs: detect delete group; tests
This commit is contained in:
@@ -139,6 +139,12 @@ public class JavaChangeSignatureDetector implements LanguageChangeSignatureDetec
|
||||
if (changeInfo instanceof MyJavaChangeInfo) {
|
||||
final MyJavaChangeInfo info = (MyJavaChangeInfo)changeInfo;
|
||||
final PsiMethod method = info.getSuperMethod();
|
||||
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
temporallyRevertChanges(method, oldText);
|
||||
createChangeSignatureProcessor(info, method).run();
|
||||
return true;
|
||||
}
|
||||
final JavaChangeSignatureDialog dialog =
|
||||
new JavaChangeSignatureDialog(method.getProject(), new JavaMethodDescriptor(info.getMethod()) {
|
||||
@Override
|
||||
@@ -147,16 +153,7 @@ public class JavaChangeSignatureDetector implements LanguageChangeSignatureDetec
|
||||
}
|
||||
}, true, method) {
|
||||
protected BaseRefactoringProcessor createRefactoringProcessor() {
|
||||
return new ChangeSignatureProcessor(myProject, new MyJavaChangeInfo(info.getNewVisibility(), info.getSuperMethod(),
|
||||
info.getNewReturnType(),
|
||||
(ParameterInfoImpl[])info.getNewParameters(),
|
||||
info.getNewExceptions(), info.getOldName()) {
|
||||
@Override
|
||||
protected void fillOldParams(PsiMethod method) {
|
||||
oldParameterNames = info.getOldParameterNames();
|
||||
oldParameterTypes = info.getOldParameterTypes();
|
||||
}
|
||||
});
|
||||
return createChangeSignatureProcessor(info, method);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -164,18 +161,7 @@ public class JavaChangeSignatureDetector implements LanguageChangeSignatureDetec
|
||||
CommandProcessor.getInstance().executeCommand(myProject, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final PsiFile file = method.getContainingFile();
|
||||
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
|
||||
final Document document = documentManager.getDocument(file);
|
||||
if (document != null) {
|
||||
document.setText(oldText);
|
||||
documentManager.commitDocument(document);
|
||||
}
|
||||
}
|
||||
});
|
||||
temporallyRevertChanges(method, oldText);
|
||||
doRefactor(processor);
|
||||
}
|
||||
}, RefactoringBundle.message("changing.signature.of.0", UsageViewUtil.getDescriptiveName(info.getMethod())), null);
|
||||
@@ -192,6 +178,35 @@ public class JavaChangeSignatureDetector implements LanguageChangeSignatureDetec
|
||||
|
||||
}
|
||||
|
||||
private static void temporallyRevertChanges(final PsiMethod method, final String oldText) {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final PsiFile file = method.getContainingFile();
|
||||
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(method.getProject());
|
||||
final Document document = documentManager.getDocument(file);
|
||||
if (document != null) {
|
||||
document.setText(oldText);
|
||||
documentManager.commitDocument(document);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static ChangeSignatureProcessor createChangeSignatureProcessor(final MyJavaChangeInfo info,
|
||||
final PsiMethod method) {
|
||||
return new ChangeSignatureProcessor(method.getProject(), new MyJavaChangeInfo(info.getNewVisibility(), info.getSuperMethod(),
|
||||
info.getNewReturnType(),
|
||||
(ParameterInfoImpl[])info.getNewParameters(),
|
||||
info.getNewExceptions(), info.getOldName()) {
|
||||
@Override
|
||||
protected void fillOldParams(PsiMethod method) {
|
||||
oldParameterNames = info.getOldParameterNames();
|
||||
oldParameterTypes = info.getOldParameterTypes();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isChangeSignatureAvailable(PsiElement element, ChangeInfo currentInfo) {
|
||||
if (currentInfo instanceof JavaChangeInfo) {
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
public class Parent {
|
||||
public void foo(float j, int i, String s) {
|
||||
System.out.println(j + s + i);
|
||||
}
|
||||
}
|
||||
|
||||
class Child extends Parent {
|
||||
public void foo(float j, <selection> int i,</selection> String s ) {
|
||||
}
|
||||
|
||||
void bar() {
|
||||
foo(1.0, 1, "aaa");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
public class Parent {
|
||||
public void foo(float j, String s) {
|
||||
System.out.println(j + s + i);
|
||||
}
|
||||
}
|
||||
|
||||
class Child extends Parent {
|
||||
public void foo(float j, String s) {
|
||||
}
|
||||
|
||||
void bar() {
|
||||
foo(1.0, "aaa");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
void foo(int i<caret>) {
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
public class Parent {
|
||||
public void foo(int i) {
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
|
||||
class Child extends Parent {
|
||||
public void foo(int i<caret>) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
public class Parent {
|
||||
public void foo(int i) {
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
|
||||
class Child extends Parent {
|
||||
public void foo(int i<caret>) {
|
||||
}
|
||||
|
||||
void bar() {
|
||||
foo(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
public class Parent {
|
||||
public void foo(int i, int param) {
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
|
||||
class Child extends Parent {
|
||||
public void foo(int i, int param) {
|
||||
}
|
||||
|
||||
void bar() {
|
||||
foo(1,intellijidearulezzz);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
public class Parent {
|
||||
public void foo(int i, int param) {
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
|
||||
class Child extends Parent {
|
||||
public void foo(int i, int param) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
void foo(int i, int param) {
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
void foo(int i<caret>) {
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
void foo(int iparam) {
|
||||
System.out.println(iparam);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright 2000-2010 JetBrains s.r.o.
|
||||
*
|
||||
* 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 com.intellij.refactoring;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.actions.EditorActionUtil;
|
||||
import com.intellij.psi.PsiManager;
|
||||
import com.intellij.refactoring.changeSignature.ChangeSignatureGestureDetector;
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
|
||||
/**
|
||||
* User: anna
|
||||
* Date: Sep 9, 2010
|
||||
*/
|
||||
public class ChangeSignatureGestureTest extends LightCodeInsightFixtureTestCase {
|
||||
private void doTest(Runnable run) {
|
||||
myFixture.configureByFile("/refactoring/changeSignatureGesture/" + getTestName(false) + ".java");
|
||||
final ChangeSignatureGestureDetector detector = ChangeSignatureGestureDetector.getInstance(getProject());
|
||||
final Document document = myFixture.getEditor().getDocument();
|
||||
try {
|
||||
PsiManager.getInstance(getProject()).addPsiTreeChangeListener(detector);
|
||||
detector.addDocListener(document);
|
||||
run.run();
|
||||
myFixture.doHighlighting();
|
||||
final IntentionAction intention = myFixture.findSingleIntention("Change signature ...");
|
||||
myFixture.launchAction(intention);
|
||||
myFixture.checkResultByFile("/refactoring/changeSignatureGesture/" + getTestName(false) + "_after.java");
|
||||
}
|
||||
finally {
|
||||
detector.removeDocListener(document);
|
||||
PsiManager.getInstance(getProject()).removePsiTreeChangeListener(detector);
|
||||
}
|
||||
}
|
||||
|
||||
public void testSimple() {
|
||||
doTypingTest("param");
|
||||
}
|
||||
|
||||
public void testNewParam() {
|
||||
doTypingTest(", int param");
|
||||
}
|
||||
|
||||
public void testNewParamInSuper() {
|
||||
doTypingTest(", int param");
|
||||
}
|
||||
|
||||
public void testNewParamInSuperUsed() {
|
||||
doTypingTest(", int param");
|
||||
}
|
||||
|
||||
private void doTypingTest(final String param) {
|
||||
doTest(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
myFixture.type(param);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void testDeleteParamInSuperUsed() {
|
||||
doDeleteTest();
|
||||
}
|
||||
|
||||
private void doDeleteTest() {
|
||||
doTest(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final Editor editor = myFixture.getEditor();
|
||||
final Document document = editor.getDocument();
|
||||
final int selectionStart = editor.getSelectionModel().getSelectionStart();
|
||||
final int selectionEnd = editor.getSelectionModel().getSelectionEnd();
|
||||
CommandProcessor.getInstance().setCurrentCommandGroupId(EditorActionUtil.DELETE_COMMAND_GROUP);
|
||||
document.deleteString(selectionStart, selectionEnd);
|
||||
editor.getCaretModel().moveToOffset(selectionStart);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return JavaTestUtil.getRelativeJavaTestDataPath();
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.EditorBundle;
|
||||
import com.intellij.openapi.editor.EditorFactory;
|
||||
import com.intellij.openapi.editor.actions.EditorActionUtil;
|
||||
import com.intellij.openapi.editor.event.DocumentAdapter;
|
||||
import com.intellij.openapi.editor.event.DocumentEvent;
|
||||
import com.intellij.openapi.editor.event.EditorFactoryEvent;
|
||||
@@ -120,14 +121,25 @@ public class ChangeSignatureGestureDetector extends PsiTreeChangeAdapter impleme
|
||||
public void disposeComponent() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void childRemoved(PsiTreeChangeEvent event) {
|
||||
if (myDeaf) return;
|
||||
change(event.getParent());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void childReplaced(PsiTreeChangeEvent event) {
|
||||
if (myDeaf) return;
|
||||
final PsiFile file = event.getChild().getContainingFile();
|
||||
change(event.getChild());
|
||||
}
|
||||
|
||||
private void change(PsiElement child) {
|
||||
if (child == null) return;
|
||||
final PsiFile file = child.getContainingFile();
|
||||
if (file != null) {
|
||||
final MyDocumentChangeAdapter changeBean = myListenerMap.get(file);
|
||||
if (changeBean != null && changeBean.getInitialText() != null) {
|
||||
final ChangeInfo info = LanguageChangeSignatureDetectors.createCurrentChangeInfo(event.getChild(), changeBean.getCurrentInfo());
|
||||
final ChangeInfo info = LanguageChangeSignatureDetectors.createCurrentChangeInfo(child, changeBean.getCurrentInfo());
|
||||
changeBean.setCurrentInfo(info);
|
||||
if (info == null) {
|
||||
changeBean.setInitialText(null);
|
||||
@@ -138,7 +150,10 @@ public class ChangeSignatureGestureDetector extends PsiTreeChangeAdapter impleme
|
||||
|
||||
@Override
|
||||
public void editorCreated(EditorFactoryEvent event) {
|
||||
final Document document = event.getEditor().getDocument();
|
||||
addDocListener(event.getEditor().getDocument());
|
||||
}
|
||||
|
||||
public void addDocListener(Document document) {
|
||||
final PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
|
||||
if (file != null) {
|
||||
final MyDocumentChangeAdapter adapter = new MyDocumentChangeAdapter();
|
||||
@@ -149,7 +164,10 @@ public class ChangeSignatureGestureDetector extends PsiTreeChangeAdapter impleme
|
||||
|
||||
@Override
|
||||
public void editorReleased(EditorFactoryEvent event) {
|
||||
final Document document = event.getEditor().getDocument();
|
||||
removeDocListener(event.getEditor().getDocument());
|
||||
}
|
||||
|
||||
public void removeDocListener(Document document) {
|
||||
final PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
|
||||
final MyDocumentChangeAdapter adapter = myListenerMap.remove(file);
|
||||
if (adapter != null) {
|
||||
@@ -194,9 +212,11 @@ public class ChangeSignatureGestureDetector extends PsiTreeChangeAdapter impleme
|
||||
final Document document = e.getDocument();
|
||||
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject);
|
||||
if (!documentManager.isUncommited(document)) {
|
||||
final String currentCommandName = CommandProcessor.getInstance().getCurrentCommandName();
|
||||
final CommandProcessor processor = CommandProcessor.getInstance();
|
||||
final String currentCommandName = processor.getCurrentCommandName();
|
||||
if (!Comparing.strEqual(EditorBundle.message("typing.in.editor.command.name"), currentCommandName) &&
|
||||
!Comparing.strEqual(EditorBundle.message("paste.command.name"), currentCommandName)) {
|
||||
!Comparing.strEqual(EditorBundle.message("paste.command.name"), currentCommandName) &&
|
||||
!Comparing.equal(EditorActionUtil.DELETE_COMMAND_GROUP, processor.getCurrentCommandGroupId())) {
|
||||
return;
|
||||
}
|
||||
final PsiFile file = documentManager.getPsiFile(document);
|
||||
|
||||
@@ -45,7 +45,7 @@ import java.util.List;
|
||||
|
||||
public class EditorActionUtil {
|
||||
protected static final Object EDIT_COMMAND_GROUP = Key.create("EditGroup");
|
||||
protected static final Object DELETE_COMMAND_GROUP = Key.create("DeleteGroup");
|
||||
public static final Object DELETE_COMMAND_GROUP = Key.create("DeleteGroup");
|
||||
|
||||
private EditorActionUtil() {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user