add ParameterInfo#isNew, replace usages of obscure -1 constant

GitOrigin-RevId: 54dc2b09e8024bba632d32b41610db15e80d70ee
This commit is contained in:
Artem Khvastunov
2020-01-21 14:05:07 +01:00
committed by intellij-monorepo-bot
parent 376782793e
commit a35e9fb6a3
11 changed files with 33 additions and 77 deletions

View File

@@ -356,7 +356,7 @@ public class CreateConstructorParameterFromFieldFix implements IntentionAction {
}
}
for (int i = 0; i < newParameters.length; i++) {
if (parameterInfos[i].getOldIndex() == -1) {
if (parameterInfos[i].isNew()) {
final PsiParameter parameter = newParameters[i];
final PsiType paramType = parameterInfos[i].getTypeWrapper().getType(parameter);
if (type.isAssignableFrom(paramType)){

View File

@@ -119,7 +119,7 @@ public class CreateParameterFromUsageFix extends CreateVarFromUsageFix {
dialog.setParameterInfos(parameterInfos);
if (dialog.showAndGet()) {
for (ParameterInfoImpl info : parameterInfos) {
if (info.getOldIndex() == -1) {
if (info.isNew()) {
final String newParamName = info.getName();
if (!Comparing.strEqual(varName, newParamName)) {
final PsiExpression newExpr =

View File

@@ -266,7 +266,7 @@ class DetectedJavaChangeInfo extends JavaChangeInfoImpl {
PsiMethod prototype;
if (isGenerateDelegate()) {
for (JavaParameterInfo info : getNewParameters()) {
if (info.getOldIndex() == -1) {
if (info.isNew()) {
((ParameterInfoImpl)info).setDefaultValue("null"); //to be replaced with template expr
}
}
@@ -287,7 +287,7 @@ class DetectedJavaChangeInfo extends JavaChangeInfoImpl {
JavaParameterInfo[] parameters = getNewParameters();
PsiExpression[] toBeDefault =
Arrays.stream(parameters)
.filter(param -> param.getOldIndex() == -1)
.filter(ParameterInfo::isNew)
.map(info -> {
int i = ArrayUtil.find(parameters, info);
return expressions[i];

View File

@@ -1,18 +1,4 @@
/*
* Copyright 2000-2016 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.
*/
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.refactoring.changeSignature;
import com.intellij.ide.highlighter.JavaFileType;
@@ -120,7 +106,7 @@ public class JavaChangeSignatureDetector implements LanguageChangeSignatureDetec
}
for (JavaParameterInfo param : newParameters) {
if (param.getOldIndex() == -1) {
if (param.isNew()) {
if (first) {
first = false;
}

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.refactoring.changeSignature;
import com.intellij.codeInsight.completion.CompletionResultSet;
@@ -351,7 +351,7 @@ public class JavaChangeSignatureDialog extends ChangeSignatureDialogBase<Paramet
}
}.apply(myNameEditor, item.parameter.getName());
if (!item.isEllipsisType() && item.parameter.getOldIndex() == -1) {
if (!item.isEllipsisType() && item.parameter.isNew()) {
final JPanel additionalPanel = new JPanel(new BorderLayout());
final Document doc = PsiDocumentManager.getInstance(getProject()).getDocument(item.defaultValueCodeFragment);
myDefaultValueEditor = new EditorTextField(doc, getProject(), getFileType());

View File

@@ -1,18 +1,4 @@
/*
* Copyright 2000-2017 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.
*/
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.refactoring.changeSignature;
import com.intellij.codeInsight.AnnotationUtil;
@@ -663,7 +649,7 @@ public class JavaChangeSignatureUsageProcessor implements ChangeSignatureUsagePr
final ParameterInfo[] parameters = changeInfo.getNewParameters();
for (ParameterInfo parameter : parameters) {
final String defaultValue = parameter.getDefaultValue();
if (defaultValue == null && parameter.getOldIndex() == -1) {
if (defaultValue == null && parameter.isNew()) {
((ParameterInfoImpl)parameter).setDefaultValue("");
if (!ApplicationManager.getApplication().isUnitTestMode()) {
final PsiType type = ((ParameterInfoImpl)parameter).getTypeWrapper().getType(element);

View File

@@ -1,18 +1,4 @@
/*
* Copyright 2000-2009 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.
*/
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.refactoring.changeSignature;
@@ -25,6 +11,9 @@ import org.jetbrains.annotations.Nullable;
* @author yole
*/
public interface ParameterInfo {
int NEW_PARAMETER = -1;
/**
* Returns the name of the parameter after the refactoring.
*
@@ -33,13 +22,22 @@ public interface ParameterInfo {
String getName();
/**
* Returns the index of the parameter in the old parameter list, or -1 if the parameter
* Returns the index of the parameter in the old parameter list, or {@link #NEW_PARAMETER} if the parameter
* was added by the refactoring.
*
* @return old parameter index, or -1.
* @return old parameter index, or {@link #NEW_PARAMETER}.
*/
int getOldIndex();
/**
* Returns {@code true} if the parameter was added by the refactoring.
*
* @return {@code true} if the parameter was added by the refactoring
*/
default boolean isNew() {
return getOldIndex() == NEW_PARAMETER;
}
/**
* For added parameters, returns the string representation of the default parameter value.
*

View File

@@ -1,18 +1,4 @@
/*
* Copyright 2000-2016 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.
*/
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.refactoring.changeSignature;
import com.intellij.openapi.fileTypes.FileType;
@@ -215,7 +201,7 @@ public abstract class ParameterTableModelBase<P extends ParameterInfo, TableItem
@Override
public boolean isCellEditable(TableItem item) {
return !item.isEllipsisType() && item.parameter.getOldIndex() == -1;
return !item.isEllipsisType() && item.parameter.isNew();
}
@Override
@@ -242,7 +228,7 @@ public abstract class ParameterTableModelBase<P extends ParameterInfo, TableItem
@Override
public boolean isCellEditable(TableItem item) {
return !item.isEllipsisType() && item.parameter.getOldIndex() == -1;
return !item.isEllipsisType() && item.parameter.isNew();
}
@Override

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.groovy.refactoring.changeSignature;
import com.intellij.openapi.application.ApplicationManager;
@@ -137,7 +137,7 @@ public class GrChangeSignatureUsageProcessor implements ChangeSignatureUsageProc
final ParameterInfo[] parameters = changeInfo.getNewParameters();
for (ParameterInfo parameter : parameters) {
final String defaultValue = parameter.getDefaultValue();
if (defaultValue == null && parameter.getOldIndex() == -1) {
if (defaultValue == null && parameter.isNew()) {
((ParameterInfoImpl)parameter).setDefaultValue("");
if (!ApplicationManager.getApplication().isUnitTestMode()) {
final PsiType type = ((ParameterInfoImpl)parameter).getTypeWrapper().getType(element);

View File

@@ -124,7 +124,7 @@ public class PyChangeSignatureQuickFix extends LocalQuickFixOnPsiElement {
if (function == null) {
return getFamilyName();
}
final String params = StringUtil.join(createMethodDescriptor(function).getParameters(), info -> info.getOldIndex() == -1 ? "<b>" + info.getName() + "</b>" : info.getName(), ", ");
final String params = StringUtil.join(createMethodDescriptor(function).getParameters(), info -> info.isNew() ? "<b>" + info.getName() + "</b>" : info.getName(), ", ");
final String message = PyBundle.message("QFIX.change.signature.of", StringUtil.notNullize(function.getName()) + "(" + params + ")");
return XmlStringUtil.wrapInHtml(message);

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.jetbrains.python.refactoring.changeSignature;
import com.intellij.lang.LanguageNamesValidation;
@@ -316,7 +316,7 @@ public class PyChangeSignatureDialog extends
}
});
myDefaultInSignature.addChangeListener(mySignatureUpdater);
myDefaultInSignature.setEnabled(item.parameter.getOldIndex() == -1);
myDefaultInSignature.setEnabled(item.parameter.isNew());
defaultValuePanel.add(myDefaultInSignature, BorderLayout.EAST);
return defaultValuePanel;
}