mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java: convert "Create field from parameter" to use in-place mode instead of a dialog (IDEA-312355)
GitOrigin-RevId: 2e3e0fbfe1f1438cefc6c65888f5bec31d1cf71f
This commit is contained in:
committed by
intellij-monorepo-bot
parent
9eae75c034
commit
c66a099689
+3
-17
@@ -1,18 +1,4 @@
|
||||
/*
|
||||
* Copyright 2000-2012 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-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.codeInsight.intention.impl;
|
||||
|
||||
import com.intellij.codeInsight.daemon.HighlightDisplayKey;
|
||||
@@ -80,7 +66,7 @@ public class CreateFieldFromParameterAction extends CreateFieldFromParameterActi
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void performRefactoring(@NotNull Project project,
|
||||
protected PsiVariable createField(@NotNull Project project,
|
||||
@NotNull PsiClass targetClass,
|
||||
@NotNull PsiMethod method,
|
||||
@NotNull PsiParameter myParameter,
|
||||
@@ -88,6 +74,6 @@ public class CreateFieldFromParameterAction extends CreateFieldFromParameterActi
|
||||
@NotNull String fieldName,
|
||||
boolean methodStatic,
|
||||
boolean isFinal) {
|
||||
FieldFromParameterUtils.createFieldAndAddAssignment(project, targetClass, method, myParameter, type, fieldName, methodStatic, isFinal);
|
||||
return FieldFromParameterUtils.createFieldAndAddAssignment(project, targetClass, method, myParameter, type, fieldName, methodStatic, isFinal);
|
||||
}
|
||||
}
|
||||
|
||||
+31
-67
@@ -6,7 +6,6 @@ import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo;
|
||||
import com.intellij.codeInsight.intention.preview.IntentionPreviewUtils;
|
||||
import com.intellij.java.JavaBundle;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.fileEditor.ex.IdeDocumentHistory;
|
||||
import com.intellij.openapi.project.Project;
|
||||
@@ -14,18 +13,10 @@ import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.SuggestedNameInfo;
|
||||
import com.intellij.psi.codeStyle.VariableKind;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.ArrayUtilRt;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.refactoring.rename.inplace.MemberInplaceRenamer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class CreateFieldFromParameterActionBase extends BaseIntentionAction {
|
||||
private static final Logger LOG = Logger.getInstance(CreateFieldFromParameterActionBase.class);
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
@@ -49,93 +40,66 @@ public abstract class CreateFieldFromParameterActionBase extends BaseIntentionAc
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
PsiParameter myParameter = FieldFromParameterUtils.findParameterAtCursor(file, editor);
|
||||
if (myParameter == null || !FileModificationService.getInstance().prepareFileForWrite(file)) return;
|
||||
PsiParameter parameter = FieldFromParameterUtils.findParameterAtCursor(file, editor);
|
||||
if (parameter == null || !FileModificationService.getInstance().prepareFileForWrite(file)) return;
|
||||
|
||||
IdeDocumentHistory.getInstance(project).includeCurrentPlaceAsChangePlace();
|
||||
try {
|
||||
processParameter(project, myParameter, !ApplicationManager.getApplication().isHeadlessEnvironment());
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
processParameter(editor, parameter, !ApplicationManager.getApplication().isHeadlessEnvironment());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull IntentionPreviewInfo generatePreview(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
|
||||
PsiParameter parameter = FieldFromParameterUtils.findParameterAtCursor(file, editor);
|
||||
if (parameter == null) return IntentionPreviewInfo.EMPTY;
|
||||
processParameter(project, parameter, false);
|
||||
processParameter(editor, parameter, false);
|
||||
return IntentionPreviewInfo.DIFF;
|
||||
}
|
||||
|
||||
private void processParameter(@NotNull Project project,
|
||||
@NotNull PsiParameter myParameter,
|
||||
boolean isInteractive) {
|
||||
PsiType type = getSubstitutedType(myParameter);
|
||||
private void processParameter(@NotNull Editor editor, @NotNull PsiParameter parameter, boolean isInteractive) {
|
||||
Project project = parameter.getProject();
|
||||
PsiType type = getSubstitutedType(parameter);
|
||||
JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project);
|
||||
String parameterName = myParameter.getName();
|
||||
String parameterName = parameter.getName();
|
||||
String propertyName = styleManager.variableNameToPropertyName(parameterName, VariableKind.PARAMETER);
|
||||
|
||||
PsiClass targetClass = PsiTreeUtil.getParentOfType(myParameter, PsiClass.class);
|
||||
PsiMethod method = (PsiMethod)parameter.getDeclarationScope();
|
||||
PsiClass targetClass = method.getContainingClass();
|
||||
if (targetClass == null) return;
|
||||
PsiMethod method = (PsiMethod)myParameter.getDeclarationScope();
|
||||
|
||||
boolean isMethodStatic = method.hasModifierProperty(PsiModifier.STATIC);
|
||||
|
||||
VariableKind kind = isMethodStatic ? VariableKind.STATIC_FIELD : VariableKind.FIELD;
|
||||
SuggestedNameInfo suggestedNameInfo = styleManager.suggestVariableName(kind, propertyName, null, type);
|
||||
String[] names = suggestedNameInfo.names;
|
||||
SuggestedNameInfo uniqueNameInfo = styleManager.suggestUniqueVariableName(suggestedNameInfo, targetClass, true);
|
||||
|
||||
String fieldNameToCalc;
|
||||
boolean isFinalToCalc;
|
||||
if (isInteractive) {
|
||||
List<String> namesList = new ArrayList<>();
|
||||
ContainerUtil.addAll(namesList, names);
|
||||
String defaultName = styleManager.propertyNameToVariableName(propertyName, kind);
|
||||
if (namesList.contains(defaultName)) {
|
||||
Collections.swap(namesList, 0, namesList.indexOf(defaultName));
|
||||
}
|
||||
else {
|
||||
namesList.add(0, defaultName);
|
||||
}
|
||||
names = ArrayUtilRt.toStringArray(namesList);
|
||||
boolean isFinal = !isMethodStatic && method.isConstructor();
|
||||
|
||||
CreateFieldFromParameterDialog dialog = new CreateFieldFromParameterDialog(
|
||||
project,
|
||||
names,
|
||||
targetClass,
|
||||
method.isConstructor(),
|
||||
type
|
||||
);
|
||||
if (!dialog.showAndGet()) {
|
||||
return;
|
||||
}
|
||||
fieldNameToCalc = dialog.getEnteredName();
|
||||
isFinalToCalc = dialog.isDeclareFinal();
|
||||
PsiVariable variable = IntentionPreviewUtils.writeAndCompute(
|
||||
() -> createField(project, targetClass, method, parameter, type, uniqueNameInfo.names[0], isMethodStatic, isFinal));
|
||||
|
||||
suggestedNameInfo.nameChosen(fieldNameToCalc);
|
||||
}
|
||||
else {
|
||||
isFinalToCalc = !isMethodStatic && method.isConstructor();
|
||||
fieldNameToCalc = names[0];
|
||||
}
|
||||
if (!isInteractive) return;
|
||||
|
||||
boolean isFinal = isFinalToCalc;
|
||||
String fieldName = fieldNameToCalc;
|
||||
IntentionPreviewUtils.write(() -> {
|
||||
try {
|
||||
performRefactoring(project, targetClass, method, myParameter, type, fieldName, isMethodStatic, isFinal);
|
||||
PsiIdentifier identifier = variable.getNameIdentifier();
|
||||
if (identifier == null) return;
|
||||
final int textOffset = identifier.getTextOffset();
|
||||
editor.getCaretModel().moveToOffset(textOffset);
|
||||
new MemberInplaceRenamer(variable, null, editor) {
|
||||
@Override
|
||||
protected boolean shouldSelectAll() {
|
||||
return true;
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
LOG.error(e);
|
||||
|
||||
@Override
|
||||
protected void moveOffsetAfter(boolean success) {
|
||||
super.moveOffsetAfter(success);
|
||||
editor.getCaretModel().moveToOffset(parameter.getTextRange().getEndOffset());
|
||||
}
|
||||
});
|
||||
}.performInplaceRename();
|
||||
}
|
||||
|
||||
protected abstract PsiType getSubstitutedType(@NotNull PsiParameter parameter);
|
||||
|
||||
protected abstract void performRefactoring(@NotNull Project project,
|
||||
protected abstract PsiVariable createField(@NotNull Project project,
|
||||
@NotNull PsiClass targetClass,
|
||||
@NotNull PsiMethod method,
|
||||
@NotNull PsiParameter myParameter,
|
||||
|
||||
-260
@@ -1,260 +0,0 @@
|
||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.codeInsight.intention.impl;
|
||||
|
||||
import com.intellij.ide.util.PropertiesComponent;
|
||||
import com.intellij.java.JavaBundle;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.ComboBox;
|
||||
import com.intellij.openapi.ui.DialogWrapper;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.intellij.psi.PsiNameHelper;
|
||||
import com.intellij.psi.PsiType;
|
||||
import com.intellij.refactoring.ui.TypeSelector;
|
||||
import com.intellij.ui.DocumentAdapter;
|
||||
import com.intellij.util.ui.JBInsets;
|
||||
import com.intellij.util.ui.JBUI;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.DocumentEvent;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
public class CreateFieldFromParameterDialog extends DialogWrapper {
|
||||
private final Project myProject;
|
||||
private final String[] myNames;
|
||||
private final PsiType[] myTypes;
|
||||
private final PsiClass myTargetClass;
|
||||
private final boolean myFieldMayBeFinal;
|
||||
|
||||
private JComponent myNameField;
|
||||
private JCheckBox myCbFinal;
|
||||
private static final @NonNls String PROPERTY_NAME = "CREATE_FIELD_FROM_PARAMETER_DECLARE_FINAL";
|
||||
private TypeSelector myTypeSelector;
|
||||
|
||||
public CreateFieldFromParameterDialog(@NotNull Project project,
|
||||
String @NotNull [] names,
|
||||
@NotNull PsiClass targetClass,
|
||||
boolean fieldMayBeFinal,
|
||||
PsiType @NotNull ... types) {
|
||||
super(project, true);
|
||||
myProject = project;
|
||||
myNames = names;
|
||||
myTypes = types;
|
||||
myTargetClass = targetClass;
|
||||
myFieldMayBeFinal = fieldMayBeFinal;
|
||||
|
||||
setTitle(JavaBundle.message("dialog.create.field.from.parameter.title"));
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doOKAction() {
|
||||
if (myCbFinal.isEnabled()) {
|
||||
PropertiesComponent.getInstance().setValue(PROPERTY_NAME, myCbFinal.isSelected());
|
||||
}
|
||||
|
||||
final PsiField[] fields = myTargetClass.getFields();
|
||||
for (PsiField field : fields) {
|
||||
if (field.getName().equals(getEnteredName())) {
|
||||
int result = Messages.showOkCancelDialog(
|
||||
getContentPane(),
|
||||
JavaBundle.message("dialog.create.field.from.parameter.already.exists.text", getEnteredName()),
|
||||
JavaBundle.message("dialog.create.field.from.parameter.already.exists.title"),
|
||||
JavaBundle.message("dialog.create.field.from.parameter.already.exists.use.existing.button"),
|
||||
Messages.getCancelButton(),
|
||||
Messages.getQuestionIcon());
|
||||
if (result == Messages.OK) {
|
||||
close(OK_EXIT_CODE);
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
close(OK_EXIT_CODE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
super.init();
|
||||
updateOkStatus();
|
||||
}
|
||||
|
||||
public String getEnteredName() {
|
||||
if (myNameField instanceof JComboBox<?> combobox) {
|
||||
return (String)combobox.getEditor().getItem();
|
||||
}
|
||||
return ((JTextField)myNameField).getText();
|
||||
}
|
||||
|
||||
public boolean isDeclareFinal() {
|
||||
if (myCbFinal.isEnabled()) {
|
||||
return myCbFinal.isSelected();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JComponent createNorthPanel() {
|
||||
if (myNames.length > 1) {
|
||||
final ComboBox combobox = new ComboBox(myNames, 200);
|
||||
myNameField = combobox;
|
||||
combobox.setEditable(true);
|
||||
combobox.setSelectedIndex(0);
|
||||
combobox.setMaximumRowCount(8);
|
||||
|
||||
combobox.registerKeyboardAction(
|
||||
__ -> {
|
||||
if (combobox.isPopupVisible()) {
|
||||
combobox.setPopupVisible(false);
|
||||
}
|
||||
else {
|
||||
doCancelAction();
|
||||
}
|
||||
},
|
||||
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
|
||||
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
|
||||
);
|
||||
|
||||
combobox.addItemListener(__ -> updateOkStatus());
|
||||
combobox.getEditor().getEditorComponent().addKeyListener(
|
||||
new KeyAdapter() {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
updateOkStatus();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
updateOkStatus();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
updateOkStatus();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
else {
|
||||
JTextField field = new JTextField() {
|
||||
@Override
|
||||
public Dimension getPreferredSize() {
|
||||
Dimension size = super.getPreferredSize();
|
||||
return new Dimension(200, size.height);
|
||||
}
|
||||
};
|
||||
myNameField = field;
|
||||
field.setText(myNames[0]);
|
||||
|
||||
field.getDocument().addDocumentListener(new DocumentAdapter() {
|
||||
@Override
|
||||
protected void textChanged(@NotNull DocumentEvent e) {
|
||||
updateOkStatus();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
JPanel panel = new JPanel();
|
||||
panel.setLayout(new GridBagLayout());
|
||||
GridBagConstraints gbConstraints = new GridBagConstraints();
|
||||
|
||||
gbConstraints.insets = JBUI.insets(4);
|
||||
gbConstraints.anchor = GridBagConstraints.EAST;
|
||||
gbConstraints.fill = GridBagConstraints.BOTH;
|
||||
|
||||
gbConstraints.gridwidth = 1;
|
||||
gbConstraints.weightx = 1;
|
||||
gbConstraints.weighty = 1;
|
||||
gbConstraints.gridx = 0;
|
||||
gbConstraints.gridy = 0;
|
||||
final JLabel typeLabel = new JLabel(JavaBundle.message("dialog.create.field.from.parameter.field.type.label"));
|
||||
panel.add(typeLabel, gbConstraints);
|
||||
gbConstraints.gridx = 1;
|
||||
if (myTypes.length > 1) {
|
||||
myTypeSelector = new TypeSelector(myProject);
|
||||
myTypeSelector.setTypes(myTypes);
|
||||
}
|
||||
else {
|
||||
myTypeSelector = new TypeSelector(myTypes[0], myProject);
|
||||
}
|
||||
panel.add(myTypeSelector.getComponent(), gbConstraints);
|
||||
|
||||
gbConstraints.gridwidth = 1;
|
||||
gbConstraints.weightx = 0;
|
||||
gbConstraints.weighty = 1;
|
||||
gbConstraints.gridx = 0;
|
||||
gbConstraints.gridy = 1;
|
||||
JLabel namePrompt = new JLabel(JavaBundle.message("dialog.create.field.from.parameter.field.name.label"));
|
||||
panel.add(namePrompt, gbConstraints);
|
||||
|
||||
gbConstraints.gridwidth = 1;
|
||||
gbConstraints.weightx = 1;
|
||||
gbConstraints.gridx = 1;
|
||||
gbConstraints.gridy = 1;
|
||||
panel.add(myNameField, gbConstraints);
|
||||
|
||||
return panel;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JComponent createCenterPanel() {
|
||||
JPanel panel = new JPanel(new GridBagLayout());
|
||||
GridBagConstraints gbConstraints = new GridBagConstraints();
|
||||
gbConstraints.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbConstraints.weightx = 1;
|
||||
gbConstraints.weighty = 0;
|
||||
gbConstraints.gridwidth = 1;
|
||||
gbConstraints.gridx = 0;
|
||||
gbConstraints.gridy = 0;
|
||||
gbConstraints.insets = JBInsets.emptyInsets();
|
||||
|
||||
myCbFinal = new JCheckBox(JavaBundle.message("dialog.create.field.from.parameter.declare.final.checkbox"));
|
||||
if (myFieldMayBeFinal) {
|
||||
myCbFinal.setSelected(PropertiesComponent.getInstance().isTrueValue(PROPERTY_NAME));
|
||||
}
|
||||
else {
|
||||
myCbFinal.setSelected(false);
|
||||
myCbFinal.setEnabled(false);
|
||||
}
|
||||
|
||||
gbConstraints.gridy++;
|
||||
panel.add(myCbFinal, gbConstraints);
|
||||
myCbFinal.addActionListener(__ -> requestFocusInNameWindow());
|
||||
|
||||
return panel;
|
||||
}
|
||||
|
||||
private void requestFocusInNameWindow() {
|
||||
if (myNameField instanceof JTextField) {
|
||||
myNameField.requestFocusInWindow();
|
||||
}
|
||||
else {
|
||||
((JComboBox<?>)myNameField).getEditor().getEditorComponent().requestFocusInWindow();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateOkStatus() {
|
||||
String text = getEnteredName();
|
||||
setOKActionEnabled(PsiNameHelper.getInstance(myProject).isIdentifier(text));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JComponent getPreferredFocusedComponent() {
|
||||
return myNameField;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public PsiType getType() {
|
||||
return myTypeSelector.getSelectedType();
|
||||
}
|
||||
}
|
||||
+16
-16
@@ -15,7 +15,6 @@ import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.util.CommonJavaRefactoringUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -26,6 +25,8 @@ import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public final class FieldFromParameterUtils {
|
||||
private FieldFromParameterUtils() {}
|
||||
|
||||
@Nullable
|
||||
public static PsiParameter findParameterAtCursor(@NotNull PsiFile file, @NotNull Editor editor) {
|
||||
int offset = editor.getCaretModel().getOffset();
|
||||
@@ -183,14 +184,14 @@ public final class FieldFromParameterUtils {
|
||||
return i;
|
||||
}
|
||||
|
||||
public static void createFieldAndAddAssignment(@NotNull Project project,
|
||||
@NotNull PsiClass targetClass,
|
||||
@NotNull PsiMethod method,
|
||||
@NotNull PsiParameter parameter,
|
||||
@NotNull PsiType fieldType,
|
||||
@NotNull String fieldName,
|
||||
boolean isStatic,
|
||||
boolean isFinal) throws IncorrectOperationException {
|
||||
public static PsiField createFieldAndAddAssignment(@NotNull Project project,
|
||||
@NotNull PsiClass targetClass,
|
||||
@NotNull PsiMethod method,
|
||||
@NotNull PsiParameter parameter,
|
||||
@NotNull PsiType fieldType,
|
||||
@NotNull String fieldName,
|
||||
boolean isStatic,
|
||||
boolean isFinal) {
|
||||
PsiManager psiManager = PsiManager.getInstance(project);
|
||||
JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(psiManager.getProject());
|
||||
PsiElementFactory factory = psiFacade.getElementFactory();
|
||||
@@ -198,7 +199,7 @@ public final class FieldFromParameterUtils {
|
||||
PsiField field = factory.createField(fieldName, fieldType);
|
||||
|
||||
PsiModifierList modifierList = field.getModifierList();
|
||||
if (modifierList == null) return;
|
||||
if (modifierList == null) return null;
|
||||
modifierList.setModifierProperty(PsiModifier.STATIC, isStatic);
|
||||
modifierList.setModifierProperty(PsiModifier.FINAL, isFinal);
|
||||
|
||||
@@ -208,7 +209,7 @@ public final class FieldFromParameterUtils {
|
||||
}
|
||||
|
||||
PsiCodeBlock methodBody = method.getBody();
|
||||
if (methodBody == null) return;
|
||||
if (methodBody == null) return null;
|
||||
PsiStatement[] statements = methodBody.getStatements();
|
||||
|
||||
|
||||
@@ -243,16 +244,17 @@ public final class FieldFromParameterUtils {
|
||||
if (!anchor.isNull()) {
|
||||
PsiField inField = anchor.get();
|
||||
if (isBefore.get()) {
|
||||
targetClass.addBefore(field, inField);
|
||||
return (PsiField)targetClass.addBefore(field, inField);
|
||||
}
|
||||
else {
|
||||
targetClass.addAfter(field, inField);
|
||||
return (PsiField)targetClass.addAfter(field, inField);
|
||||
}
|
||||
}
|
||||
else {
|
||||
targetClass.add(field);
|
||||
return (PsiField)targetClass.add(field);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isAvailable(@NotNull PsiParameter myParameter,
|
||||
@@ -275,6 +277,4 @@ public final class FieldFromParameterUtils {
|
||||
!targetClass.isInterface() &&
|
||||
getParameterAssignedToField(myParameter, findIndirectAssignments) == null;
|
||||
}
|
||||
|
||||
private FieldFromParameterUtils() { }
|
||||
}
|
||||
|
||||
+3
-4
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.refactoring.changeSignature;
|
||||
|
||||
import com.intellij.codeInsight.completion.CompletionResultSet;
|
||||
@@ -6,7 +6,6 @@ import com.intellij.codeInsight.lookup.LookupElementBuilder;
|
||||
import com.intellij.codeInspection.dataFlow.JavaMethodContractUtil;
|
||||
import com.intellij.icons.AllIcons;
|
||||
import com.intellij.ide.highlighter.JavaFileType;
|
||||
import com.intellij.java.JavaBundle;
|
||||
import com.intellij.java.refactoring.JavaRefactoringBundle;
|
||||
import com.intellij.openapi.actionSystem.ActionUpdateThread;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
@@ -330,7 +329,7 @@ public class JavaChangeSignatureDialog extends ChangeSignatureDialogBase<Paramet
|
||||
myNameEditor = new EditorTextField(item.parameter.getName(), getProject(), getFileType());
|
||||
myNameEditor.addDocumentListener(getSignatureUpdater());
|
||||
myNameEditor.addDocumentListener(new RowEditorChangeListener(1));
|
||||
add(createLabeledPanel(JavaBundle.message("dialog.create.field.from.parameter.field.name.label"), myNameEditor), BorderLayout.CENTER);
|
||||
add(createLabeledPanel(RefactoringBundle.message("column.name.name"), myNameEditor), BorderLayout.CENTER);
|
||||
new TextFieldCompletionProvider() {
|
||||
|
||||
@Override
|
||||
@@ -512,7 +511,7 @@ public class JavaChangeSignatureDialog extends ChangeSignatureDialogBase<Paramet
|
||||
@Override
|
||||
protected void invokeRefactoring(final BaseRefactoringProcessor processor) {
|
||||
if (myMethodsToPropagateExceptions != null && !mayPropagateExceptions()) {
|
||||
Messages.showWarningDialog(myProject, JavaRefactoringBundle.message("changeSignature.exceptions.wont.propagate"),
|
||||
Messages.showWarningDialog(myProject, JavaRefactoringBundle.message("changeSignature.exceptions.wont.propagate"),
|
||||
RefactoringBundle.message("changeSignature.refactoring.name"));
|
||||
myMethodsToPropagateExceptions = null;
|
||||
}
|
||||
|
||||
+3
-2
@@ -2,10 +2,11 @@
|
||||
|
||||
class Test{
|
||||
int myP1;
|
||||
private final int myP11;
|
||||
int myP2;
|
||||
|
||||
Test(int p<caret>1, int p2){
|
||||
myP1 = p1;
|
||||
Test(int p1, int p2){
|
||||
myP11 = p1;
|
||||
myP2 = p2;
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -3,9 +3,10 @@
|
||||
class Test{
|
||||
int myP1;
|
||||
int myP2;
|
||||
|
||||
private int myP11;
|
||||
|
||||
void f(int p1, int p2){
|
||||
myP1 = p1;
|
||||
myP11 = p1;
|
||||
int myP2 = p1;
|
||||
p1 = 0;
|
||||
}
|
||||
|
||||
@@ -185,13 +185,6 @@ dialog.create.class.destination.package.label=Destination package:
|
||||
dialog.create.class.label=Create {0}:
|
||||
dialog.create.class.name=Create {0} {1}
|
||||
dialog.create.class.package.chooser.title=Choose Destination Package
|
||||
dialog.create.field.from.parameter.already.exists.text=Use existing field ''{0}''?
|
||||
dialog.create.field.from.parameter.already.exists.use.existing.button=Use Existing Field
|
||||
dialog.create.field.from.parameter.already.exists.title=Field Already Exists
|
||||
dialog.create.field.from.parameter.declare.final.checkbox=Declare &final
|
||||
dialog.create.field.from.parameter.field.name.label=Name:
|
||||
dialog.create.field.from.parameter.field.type.label=Field of type:
|
||||
dialog.create.field.from.parameter.title=Create Field
|
||||
dialog.edit.template.checkbox.smart.type.completion=Type-matching c&ompletion
|
||||
dialog.edit.template.checkbox.use.static.import=Use static &import if possible
|
||||
dialog.import.on.paste.title=Select Classes to Import
|
||||
|
||||
+12
-11
@@ -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-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package org.jetbrains.plugins.groovy.intentions.declaration;
|
||||
|
||||
import com.intellij.codeInsight.intention.impl.CreateFieldFromParameterActionBase;
|
||||
@@ -61,7 +61,7 @@ public class GrCreateFieldForParameterIntention extends CreateFieldFromParameter
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void performRefactoring(@NotNull Project project,
|
||||
protected PsiVariable createField(@NotNull Project project,
|
||||
@NotNull PsiClass targetClass,
|
||||
@NotNull PsiMethod method,
|
||||
@NotNull PsiParameter myParameter,
|
||||
@@ -69,23 +69,24 @@ public class GrCreateFieldForParameterIntention extends CreateFieldFromParameter
|
||||
@NotNull String fieldName,
|
||||
boolean methodStatic,
|
||||
boolean isFinal) {
|
||||
GrOpenBlock block = ((GrMethod)method).getBlock();
|
||||
if (block == null) return null;
|
||||
|
||||
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
|
||||
GrAssignmentExpression assignment = createAssignment(targetClass, myParameter, fieldName, methodStatic, factory);
|
||||
GrStatement anchor = getAnchor(block);
|
||||
|
||||
GrStatement statement = block.addStatementBefore(assignment, anchor);
|
||||
JavaCodeStyleManager.getInstance(project).shortenClassReferences(statement);
|
||||
|
||||
if (targetClass.findFieldByName(fieldName, false) == null) {
|
||||
String[] modifiers = getModifiers(methodStatic, isFinal);
|
||||
GrVariableDeclaration fieldDeclaration = factory.createFieldDeclaration(modifiers, fieldName, null, type);
|
||||
GrVariableDeclaration inserted = (GrVariableDeclaration)targetClass.add(fieldDeclaration);
|
||||
JavaCodeStyleManager.getInstance(project).shortenClassReferences(inserted);
|
||||
return inserted.getVariables()[0];
|
||||
}
|
||||
|
||||
GrOpenBlock block = ((GrMethod)method).getBlock();
|
||||
if (block == null) return;
|
||||
|
||||
GrAssignmentExpression assignment = createAssignment(targetClass, myParameter, fieldName, methodStatic, factory);
|
||||
GrStatement anchor = getAnchor(block);
|
||||
|
||||
GrStatement statement = block.addStatementBefore(assignment, anchor);
|
||||
JavaCodeStyleManager.getInstance(project).shortenClassReferences(statement);
|
||||
return null;
|
||||
}
|
||||
|
||||
private static GrAssignmentExpression createAssignment(PsiClass targetClass,
|
||||
|
||||
Reference in New Issue
Block a user