generate default method and improved text & description for "delegated method with default parameter value" intentions

This commit is contained in:
Bas Leijdekkers
2015-08-08 21:54:24 +02:00
parent 49354727e9
commit 59f25f9789
27 changed files with 85 additions and 44 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2015 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.
@@ -17,10 +17,12 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.intention.impl.ParameterClassMember;
import com.intellij.ide.util.MemberChooser;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiParameter;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -32,24 +34,42 @@ import java.util.List;
*/
public class DefineParamsDefaultValueAction extends DelegateWithDefaultParamValueIntentionAction {
@NotNull
@Override
public String getText() {
return "Define params default value";
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
if (!JavaLanguage.INSTANCE.equals(element.getLanguage())) {
return false;
}
final PsiElement parent = PsiTreeUtil.getParentOfType(element, PsiMethod.class, PsiCodeBlock.class);
if (!(parent instanceof PsiMethod)) {
return false;
}
final PsiMethod method = (PsiMethod)parent;
final PsiParameterList parameterList = method.getParameterList();
if (parameterList.getParametersCount() == 0) {
return false;
}
final PsiClass containingClass = method.getContainingClass();
if (containingClass == null || (containingClass.isInterface() && !PsiUtil.isLanguageLevel8OrHigher(method))) {
return false;
}
setText("Generate overloaded " + (method.isConstructor() ? "constructor" : "method") + " with default parameter values");
return true;
}
@Nullable
@Override
protected PsiParameter[] getParams(PsiElement element) {
final PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
assert method != null;
final PsiParameter[] parameters = method.getParameterList().getParameters();
final ParameterClassMember[] members = new ParameterClassMember[parameters.length];
for (int i = 0; i < members.length; i++) {
members[i] = new ParameterClassMember(parameters[i]);
}
final MemberChooser<ParameterClassMember> chooser = new MemberChooser<ParameterClassMember>(members, false, true, element.getProject());
final MemberChooser<ParameterClassMember> chooser =
new MemberChooser<ParameterClassMember>(members, false, true, element.getProject());
chooser.selectElements(members);
chooser.setTitle("Choose " + (method.isConstructor() ? "Constructor" : "Method") + " Parameters");
chooser.setTitle("Choose Default Value Parameters");
if (chooser.showAndGet()) {
final List<ParameterClassMember> elements = chooser.getSelectedElements();
if (elements != null) {
@@ -67,4 +87,10 @@ public class DefineParamsDefaultValueAction extends DelegateWithDefaultParamValu
public boolean startInWriteAction() {
return false;
}
@NotNull
@Override
public String getFamilyName() {
return "Generate overloaded method with default parameter values";
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2015 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.
@@ -33,6 +33,7 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Function;
@@ -57,8 +58,12 @@ public class DelegateWithDefaultParamValueIntentionAction extends PsiElementBase
if (declarationScope instanceof PsiMethod) {
final PsiMethod method = (PsiMethod)declarationScope;
final PsiClass containingClass = method.getContainingClass();
if (containingClass != null && !containingClass.isInterface()) {
return containingClass.findMethodBySignature(generateMethodPrototype(method, parameter), false) == null;
if (containingClass != null && (!containingClass.isInterface() || PsiUtil.isLanguageLevel7OrHigher(method))) {
if (containingClass.findMethodBySignature(generateMethodPrototype(method, parameter), false) != null) {
return false;
}
setText("Generate overloaded " + (method.isConstructor() ? "constructor" : "method") + " with default parameter value");
return true;
}
}
}
@@ -81,6 +86,11 @@ public class DelegateWithDefaultParamValueIntentionAction extends PsiElementBase
prototype.getModifierList().setModifierProperty(PsiModifier.ABSTRACT, false);
prototype.addBefore(emptyBody, null);
}
final PsiClass aClass = method.getContainingClass();
if (aClass != null && aClass.isInterface()) {
prototype.getModifierList().setModifierProperty(PsiModifier.DEFAULT, true);
}
for (int i = params.length - 1; i >= 0; i--) {
PsiParameter param = params[i];
final int parameterIndex = method.getParameterList().getParameterIndex(param);
@@ -98,7 +108,8 @@ public class DelegateWithDefaultParamValueIntentionAction extends PsiElementBase
final PsiMethod existingMethod = method.getContainingClass().findMethodBySignature(methodPrototype, false);
if (existingMethod != null) {
editor.getCaretModel().moveToOffset(existingMethod.getTextOffset());
HintManager.getInstance().showErrorHint(editor, "Method with the chosen signature already exist");
HintManager.getInstance().showErrorHint(editor, (existingMethod.isConstructor() ? "Constructor" : "Method") +
" with the chosen signature already exists");
return;
}
@@ -170,15 +181,9 @@ public class DelegateWithDefaultParamValueIntentionAction extends PsiElementBase
return new PsiParameter[]{PsiTreeUtil.getParentOfType(element, PsiParameter.class)};
}
@NotNull
@Override
public String getText() {
return "Generate delegated method with default parameter value";
}
@NotNull
@Override
public String getFamilyName() {
return getText();
return "Generate overloaded method with default parameter value";
}
}
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "true"
// "Generate overloaded method with default parameter value" "true"
class Test {
void foo() {
foo(<caret>);
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "true"
// "Generate overloaded method with default parameter value" "true"
abstract class Test {
int foo(boolean... args) {
return foo(<caret>, args);
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "true"
// "Generate overloaded method with default parameter value" "true"
class Test {
int foo() {
return foo();
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "true"
// "Generate overloaded constructor with default parameter value" "true"
class Test {
Test() {
this(<caret>);
@@ -0,0 +1,8 @@
// "Generate overloaded method with default parameter value" "true"
interface Test {
default void foo() {
foo();
}
void foo(int ii);
}
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "true"
// "Generate overloaded method with default parameter value" "true"
class Test {
/**
*/
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "true"
// "Generate overloaded method with default parameter value" "true"
class Test {
int foo() {
return foo(<caret>);
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "true"
// "Generate overloaded method with default parameter value" "true"
class Test {
<T> int foo(boolean... args) {
return foo(<caret>, args);
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "true"
// "Generate overloaded method with default parameter value" "true"
class Test {
int foo(boolean... args) {
return foo(<caret>, args);
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "true"
// "Generate overloaded method with default parameter value" "true"
class Test {
void foo(int i<caret>i){
}
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "true"
// "Generate overloaded method with default parameter value" "true"
abstract class Test {
abstract int foo(int i<caret>i, boolean... args);
}
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "true"
// "Generate overloaded method with default parameter value" "true"
class Test {
int foo(int i<caret>i){
//comment1
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "true"
// "Generate overloaded constructor with default parameter value" "true"
class Test {
Test(int i<caret>i){}
}
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "false"
// "Generate overloaded method with default parameter value" "false"
class Test {
void foo(){}
void foo(int i<caret>i){
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "false"
// "Generate overloaded method with default parameter value" "true"
interface Test {
void foo(int i<caret>i);
}
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "true"
// "Generate overloaded method with default parameter value" "true"
class Test {
/**
* @param i
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "true"
// "Generate overloaded method with default parameter value" "true"
class Test {
int foo(int i<caret>i){
return 1;
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "true"
// "Generate overloaded method with default parameter value" "true"
class Test {
<T> int foo(T i<caret>i, boolean... args){
return 1;
@@ -1,4 +1,4 @@
// "Generate delegated method with default parameter value" "true"
// "Generate overloaded method with default parameter value" "true"
class Test {
int foo(int i<caret>i, boolean... args){
return 1;
@@ -2,7 +2,8 @@ class Test {
void foo() {
foo(<spot>|</spot>,);
}
void foo(int a, int b){
void foo(int a, int b) {
//do smth
}
}
@@ -1,5 +1,5 @@
class Test {
void foo(int a, int <spot>b</spot>){
<spot>void foo(int a, int b)</spot> {
//do smth
}
}
@@ -1,5 +1,5 @@
<html>
<body>
This intention generates method which delegates to the current one setting the selected parameters with the default value.
Generates an overloaded method which delegates to the current one setting the selected parameters to the specified default values.
</body>
</html>
@@ -2,7 +2,8 @@ class Test {
void foo(int a) {
foo(a, <spot>|</spot>);
}
void foo(int a, int b){
void foo(int a, int b) {
//do smth
}
}
@@ -1,5 +1,5 @@
class Test {
void foo(int a, int <spot>b</spot>){
void foo(int a, <spot>int b</spot>) {
//do smth
}
}
@@ -1,5 +1,5 @@
<html>
<body>
This intention generates method which delegates to the current one setting the selected parameter the default value.
Generates an overloaded method which delegates to the current one setting the selected parameter to the specified default value.
</body>
</html>