Add migration action and marker fix interface (KT-23401)

This commit is contained in:
Nikolay Krasko
2018-07-30 18:08:38 +03:00
parent feb456b0e1
commit 30ec73f322
12 changed files with 209 additions and 1 deletions
+5
View File
@@ -115,6 +115,11 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<add-to-group group-id="KotlinToolsGroup"/>
</action>
<action id="KotlinCodeMigration" class="org.jetbrains.kotlin.idea.migration.CodeMigrationAction"
text="Run Code Migrations">
<add-to-group group-id="KotlinToolsGroup"/>
</action>
<action id="ExtractFunction" class="org.jetbrains.kotlin.idea.refactoring.introduce.extractFunction.ExtractFunctionAction"
text="_Function..." use-shortcut-of="ExtractMethod">
<add-to-group group-id="IntroduceActionsGroup" anchor="after" relative-to-action="ExtractMethod"/>
+5
View File
@@ -115,6 +115,11 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<add-to-group group-id="KotlinToolsGroup"/>
</action>
<action id="KotlinCodeMigration" class="org.jetbrains.kotlin.idea.migration.CodeMigrationAction"
text="Run Code Migrations">
<add-to-group group-id="KotlinToolsGroup"/>
</action>
<action id="ExtractFunction" class="org.jetbrains.kotlin.idea.refactoring.introduce.extractFunction.ExtractFunctionAction"
text="_Function..." use-shortcut-of="ExtractMethod">
<add-to-group group-id="IntroduceActionsGroup" anchor="after" relative-to-action="ExtractMethod"/>
+5
View File
@@ -115,6 +115,11 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<add-to-group group-id="KotlinToolsGroup"/>
</action>
<action id="KotlinCodeMigration" class="org.jetbrains.kotlin.idea.migration.CodeMigrationAction"
text="Run Code Migrations">
<add-to-group group-id="KotlinToolsGroup"/>
</action>
<action id="ExtractFunction" class="org.jetbrains.kotlin.idea.refactoring.introduce.extractFunction.ExtractFunctionAction"
text="_Function..." use-shortcut-of="ExtractMethod">
<add-to-group group-id="IntroduceActionsGroup" anchor="after" relative-to-action="ExtractMethod"/>
+5
View File
@@ -116,6 +116,11 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<add-to-group group-id="KotlinToolsGroup"/>
</action>
<action id="KotlinCodeMigration" class="org.jetbrains.kotlin.idea.migration.CodeMigrationAction"
text="Run Code Migrations">
<add-to-group group-id="KotlinToolsGroup"/>
</action>
<action id="ExtractFunction" class="org.jetbrains.kotlin.idea.refactoring.introduce.extractFunction.ExtractFunctionAction"
text="_Function..." use-shortcut-of="ExtractMethod">
<add-to-group group-id="IntroduceActionsGroup" anchor="after" relative-to-action="ExtractMethod"/>
+5
View File
@@ -115,6 +115,11 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<add-to-group group-id="KotlinToolsGroup"/>
</action>
<action id="KotlinCodeMigration" class="org.jetbrains.kotlin.idea.migration.CodeMigrationAction"
text="Run Code Migrations">
<add-to-group group-id="KotlinToolsGroup"/>
</action>
<action id="ExtractFunction" class="org.jetbrains.kotlin.idea.refactoring.introduce.extractFunction.ExtractFunctionAction"
text="_Function..." use-shortcut-of="ExtractMethod">
<add-to-group group-id="IntroduceActionsGroup" anchor="after" relative-to-action="ExtractMethod"/>
+5
View File
@@ -115,6 +115,11 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<add-to-group group-id="KotlinToolsGroup"/>
</action>
<action id="KotlinCodeMigration" class="org.jetbrains.kotlin.idea.migration.CodeMigrationAction"
text="Run Code Migrations">
<add-to-group group-id="KotlinToolsGroup"/>
</action>
<action id="ExtractFunction" class="org.jetbrains.kotlin.idea.refactoring.introduce.extractFunction.ExtractFunctionAction"
text="_Function..." use-shortcut-of="ExtractMethod">
<add-to-group group-id="IntroduceActionsGroup" anchor="after" relative-to-action="ExtractMethod"/>
+5
View File
@@ -114,6 +114,11 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<add-to-group group-id="KotlinToolsGroup"/>
</action>
<action id="KotlinCodeMigration" class="org.jetbrains.kotlin.idea.migration.CodeMigrationAction"
text="Run Code Migrations">
<add-to-group group-id="KotlinToolsGroup"/>
</action>
<action id="ExtractFunction" class="org.jetbrains.kotlin.idea.refactoring.introduce.extractFunction.ExtractFunctionAction"
text="_Function..." use-shortcut-of="ExtractMethod">
<add-to-group group-id="IntroduceActionsGroup" anchor="after" relative-to-action="ExtractMethod"/>
@@ -5,6 +5,10 @@
package org.jetbrains.kotlin.idea.configuration
import com.intellij.ide.DataManager
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.ActionPlaces
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.externalSystem.service.project.manage.ProjectDataImportListener
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
@@ -21,6 +25,7 @@ import org.jetbrains.kotlin.config.LanguageVersionSettingsImpl
import org.jetbrains.kotlin.idea.configuration.ui.MigrationNotificationDialog
import org.jetbrains.kotlin.idea.framework.GRADLE_SYSTEM_ID
import org.jetbrains.kotlin.idea.framework.MAVEN_SYSTEM_ID
import org.jetbrains.kotlin.idea.migration.CodeMigrationAction
import org.jetbrains.kotlin.idea.project.languageVersionSettings
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.idea.versions.LibInfo
@@ -54,6 +59,15 @@ class KotlinMigrationProjectComponent(val project: Project) {
ApplicationManager.getApplication().invokeLater {
val migrationNotificationDialog = MigrationNotificationDialog(project, migrationInfo)
migrationNotificationDialog.show()
if (migrationNotificationDialog.isOK) {
val action = ActionManager.getInstance().getAction(CodeMigrationAction.ACTION_ID)
val dataContext = DataManager.getInstance().dataContextFromFocus.result
val actionEvent = AnActionEvent.createFromAnAction(action, null, ActionPlaces.ACTION_SEARCH, dataContext)
action.actionPerformed(actionEvent)
}
}
}
@@ -102,7 +116,7 @@ private class MigrationState(
}
}
internal data class MigrationInfo(
data class MigrationInfo(
val oldStdlibVersion: String,
val newStdlibVersion: String,
val oldApiVersion: ApiVersion,
@@ -0,0 +1,75 @@
/*
* Copyright 2010-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/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.migration;
import com.intellij.analysis.AnalysisScope;
import com.intellij.analysis.BaseAnalysisAction;
import com.intellij.codeInspection.InspectionManager;
import com.intellij.codeInspection.ex.GlobalInspectionContextImpl;
import com.intellij.codeInspection.ex.InspectionManagerEx;
import com.intellij.codeInspection.ex.InspectionProfileImpl;
import com.intellij.featureStatistics.FeatureUsageTracker;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import static org.jetbrains.kotlin.idea.migration.KotlinMigrationProfileKt.createMigrationProfile;
public class CodeInspectionAction extends BaseAnalysisAction {
private GlobalInspectionContextImpl myGlobalInspectionContext;
private InspectionProfileImpl myExternalProfile;
public CodeInspectionAction(String title, String analysisNoon) {
super(title, analysisNoon);
}
@Override
protected void analyze(@NotNull Project project, @NotNull AnalysisScope scope) {
FeatureUsageTracker.getInstance().triggerFeatureUsed("codeassist.inspect.batch");
try {
runInspections(project, scope);
}
finally {
myGlobalInspectionContext = null;
myExternalProfile = null;
}
}
private void runInspections(Project project, AnalysisScope scope) {
scope.setSearchInLibraries(false);
FileDocumentManager.getInstance().saveAllDocuments();
GlobalInspectionContextImpl inspectionContext = getGlobalInspectionContext(project);
InspectionManagerEx managerEx = (InspectionManagerEx) InspectionManager.getInstance(project);
myExternalProfile = createMigrationProfile(managerEx, null);
inspectionContext.setExternalProfile(myExternalProfile);
inspectionContext.setCurrentScope(scope);
inspectionContext.doInspections(scope);
}
private GlobalInspectionContextImpl getGlobalInspectionContext(Project project) {
if (myGlobalInspectionContext == null) {
myGlobalInspectionContext = ((InspectionManagerEx) InspectionManager.getInstance(project)).createNewGlobalContext(false);
}
return myGlobalInspectionContext;
}
@Override
@NonNls
protected String getHelpTopic() {
return "reference.dialogs.inspection.scope";
}
@Override
protected void canceled() {
super.canceled();
myGlobalInspectionContext = null;
}
}
@@ -0,0 +1,16 @@
/*
* Copyright 2010-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/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.migration
class CodeMigrationAction : CodeInspectionAction("Code Migration", "Code migration") {
override fun getHelpTopic(): String {
return "reference.dialogs.cleanup.scope"
}
companion object {
const val ACTION_ID = "KotlinCodeMigration"
}
}
@@ -0,0 +1,53 @@
/*
* Copyright 2010-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/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.migration
import com.intellij.codeInspection.ex.InspectionManagerEx
import com.intellij.codeInspection.ex.InspectionProfileImpl
import com.intellij.codeInspection.ex.InspectionToolWrapper
import com.intellij.codeInspection.ex.createSimple
import com.intellij.openapi.util.InvalidDataException
import com.intellij.openapi.util.WriteExternalException
import com.intellij.profile.codeInspection.InspectionProfileManager
import com.intellij.psi.PsiElement
import org.jdom.Element
import org.jetbrains.kotlin.idea.quickfix.migration.MigrationFix
import java.util.LinkedHashSet
fun createMigrationProfile(managerEx: InspectionManagerEx, psiElement: PsiElement?): InspectionProfileImpl {
val rootProfile = InspectionProfileManager.getInstance().currentProfile
val migrationFixWrappers = rootProfile.allTools.asSequence()
.map { it.tool }
.filter { toolWrapper: InspectionToolWrapper<*, *> ->
toolWrapper.tool is MigrationFix
}
.toList()
val allWrappers = LinkedHashSet<InspectionToolWrapper<*, *>>()
for (toolWrapper in migrationFixWrappers) {
allWrappers.add(toolWrapper)
rootProfile.collectDependentInspections(toolWrapper, allWrappers, managerEx.project)
}
val model = createSimple("Migration", managerEx.project, migrationFixWrappers)
try {
val element = Element("toCopy")
for (wrapper in migrationFixWrappers) {
wrapper.tool.writeSettings(element)
val tw = (if (psiElement == null)
model.getInspectionTool(wrapper.shortName, managerEx.project)
else
model.getInspectionTool(wrapper.shortName, psiElement))!!
tw.tool.readSettings(element)
}
} catch (ignored: WriteExternalException) {
} catch (ignored: InvalidDataException) {
}
return model
}
@@ -0,0 +1,15 @@
/*
* Copyright 2010-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/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.quickfix.migration
import org.jetbrains.kotlin.idea.configuration.MigrationInfo
/**
* Marker interface for inspections that can be used during kotlin migrations
*/
interface MigrationFix {
fun isApplicable(migrationInfo: MigrationInfo): Boolean
}