refactorings: include additional information for refactoring helpers (IDEA-280046)

in case of no actual usages, primary element may be the only source of cleanup

GitOrigin-RevId: 5fe2045053a654f9675ce23ec499ed9889095d72
This commit is contained in:
Anna Kozlova
2021-10-18 17:01:09 +00:00
committed by intellij-monorepo-bot
parent 4fd3f63d0c
commit f4822d070a
19 changed files with 52 additions and 48 deletions
@@ -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-2021 JetBrains s.r.o. and contributors. 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;
import com.intellij.java.refactoring.JavaRefactoringBundle;
@@ -50,6 +36,16 @@ public class OptimizeImportsRefactoringHelper implements RefactoringHelper<Set<P
return javaFiles;
}
@Override
public Set<PsiJavaFile> prepareOperation(UsageInfo @NotNull [] usages, @NotNull PsiElement primaryElement) {
Set<PsiJavaFile> files = prepareOperation(usages);
PsiFile containingFile = primaryElement.getContainingFile();
if (containingFile instanceof PsiJavaFile) {
files.add((PsiJavaFile)containingFile);
}
return files;
}
@Override
public void performOperation(@NotNull final Project project, final Set<PsiJavaFile> javaFiles) {
CodeStyleManager.getInstance(project).performActionWithFormatterDisabled(
@@ -1,6 +1,4 @@
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Function;
class Foo {
@@ -1,7 +1,4 @@
package pack1;
import java.util.ArrayList;
import java.util.List;
public class A {
}
@@ -1,4 +1,4 @@
package a;
import static u.U.C;
public class A {
}
@@ -1,5 +1,3 @@
import java.io.*;
class Reader {
java.io.Reader reader;
// more code
@@ -1,4 +1,4 @@
// Copyright 2000-2021 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-2021 JetBrains s.r.o. and contributors. 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;
import com.intellij.codeInsight.actions.VcsFacade;
@@ -502,8 +502,11 @@ public abstract class BaseRefactoringProcessor implements Runnable {
myTransaction = listenerManager.startTransaction();
final Map<RefactoringHelper, Object> preparedData = new LinkedHashMap<>();
final Runnable prepareHelpersRunnable = () -> {
RefactoringEventData data = ReadAction.compute(() -> getBeforeData());
PsiElement primaryElement = data != null ? data.getUserData(RefactoringEventData.PSI_ELEMENT_KEY) : null;
for (final RefactoringHelper helper : RefactoringHelper.EP_NAME.getExtensionList()) {
Object operation = ReadAction.compute(() -> helper.prepareOperation(writableUsageInfos));
Object operation = ReadAction.compute(() -> primaryElement != null ? helper.prepareOperation(writableUsageInfos, primaryElement)
: helper.prepareOperation(writableUsageInfos));
preparedData.put(helper, operation);
}
};
@@ -1,16 +1,37 @@
// Copyright 2000-2021 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-2021 JetBrains s.r.o. and contributors. 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;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.usageView.UsageInfo;
import org.jetbrains.annotations.NotNull;
/**
* Allows performing cleanup operations after refactoring is done e.g., optimize imports
*/
public interface RefactoringHelper<T> {
ExtensionPointName<RefactoringHelper> EP_NAME = ExtensionPointName.create("com.intellij.refactoring.helper");
/**
* Is invoked in ReadAction under modal progress before refactoring is performed
*
* @return result of cleanup preparation so after refactoring is actually performed, {@link #performOperation(Project, Object)} could be executed
*/
T prepareOperation(UsageInfo @NotNull [] usages);
/**
* @param primaryElement represents the {@link BaseRefactoringProcessor#getBeforeData()}'s element which may be used in addition to {@code usages}
*/
default T prepareOperation(UsageInfo @NotNull [] usages, @NotNull PsiElement primaryElement) {
return prepareOperation(usages);
}
/**
* Is invoked in EDT, without WriteAction after refactoring is performed
*
* Performs actual cleanup based on prepared data
*/
void performOperation(@NotNull Project project, T operationData);
}
@@ -1,8 +1,5 @@
package javapackage.one;
import usage.KotlinClassOne;
import usage.KotlinClassTwo;
public class JavaClassOne {
public int field = MAGIC_CONST;
@@ -1,8 +1,5 @@
package javapackage.one;
import usage.KotlinClassOne;
import usage.KotlinClassTwo;
public class JavaClassOne {
public int field = MAGIC_CONST;
@@ -1,8 +1,6 @@
package javapackage.one;
import javapackage.two.JavaClassTwo;
import kotlinpackage.one.KotlinClassOneKt;
import kotlinpackage.two.KotlinClassTwoKt;
public class JavaClassOne {
@@ -1,7 +1,6 @@
package javapackage.one;
import javapackage.two.JavaClassTwo;
import kotlinpackage.one.KotlinClassOneKt;
import parentpack.JavaParent;
public class JavaClassOne extends JavaParent {
@@ -16,5 +16,5 @@
import javax.swing.*;
public class UIClass {
private JCheckBox OtherName;
private JButton OtherName;
}
@@ -8,12 +8,12 @@
<properties/>
<border type="none"/>
<children>
<component id="e18d1" class="javax.swing.JCheckBox" binding="OtherName">
<component id="e18d1" class="javax.swing.JButton" binding="OtherName">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="CheckBox"/>
<text value="JButton"/>
</properties>
</component>
<vspacer id="1da48">
@@ -16,5 +16,5 @@
import javax.swing.*;
public class UIClass {
private JCheckBox UIField;
private JButton UIField;
}
@@ -8,12 +8,12 @@
<properties/>
<border type="none"/>
<children>
<component id="e18d1" class="javax.swing.JCheckBox" binding="UIField">
<component id="e18d1" class="javax.swing.JButton" binding="UIField">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="CheckBox"/>
<text value="JButton"/>
</properties>
</component>
<vspacer id="1da48">
@@ -1,5 +1,5 @@
import javax.swing.*;
public class NewClass {
private JCheckBox UIField;
private JButton UIField;
}
@@ -8,12 +8,12 @@
<properties/>
<border type="none"/>
<children>
<component id="e18d1" class="javax.swing.JCheckBox" binding="UIField">
<component id="e18d1" class="javax.swing.JButton" binding="UIField">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="CheckBox"/>
<text value="JButton"/>
</properties>
</component>
<vspacer id="1da48">
@@ -1,5 +1,5 @@
import javax.swing.*;
public class UIClass {
private JCheckBox UIField;
private JButton UIField;
}
@@ -8,12 +8,12 @@
<properties/>
<border type="none"/>
<children>
<component id="e18d1" class="javax.swing.JCheckBox" binding="UIField">
<component id="e18d1" class="javax.swing.JButton" binding="UIField">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="CheckBox"/>
<text value="JButton"/>
</properties>
</component>
<vspacer id="1da48">