From 0f1e169bd76f984ece8ad63bc69f05bd709cd3f0 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Wed, 22 Nov 2017 14:23:02 +0300 Subject: [PATCH] Introduce a quick fix for deprecated jre artifact in gradle (KT-20947) #KT-20947 In Progress --- .../gradle/GradleDependencyInspection.kt | 3 +- .../inspections/ReplaceStringInDocumentFix.kt | 48 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceStringInDocumentFix.kt diff --git a/idea/idea-gradle/src/org/jetbrains/kotlin/idea/inspections/gradle/GradleDependencyInspection.kt b/idea/idea-gradle/src/org/jetbrains/kotlin/idea/inspections/gradle/GradleDependencyInspection.kt index e7942db72be..844efa8851b 100644 --- a/idea/idea-gradle/src/org/jetbrains/kotlin/idea/inspections/gradle/GradleDependencyInspection.kt +++ b/idea/idea-gradle/src/org/jetbrains/kotlin/idea/inspections/gradle/GradleDependencyInspection.kt @@ -25,6 +25,7 @@ import com.intellij.util.text.VersionComparatorUtil import org.jetbrains.kotlin.idea.configuration.KotlinWithGradleConfigurator import org.jetbrains.kotlin.idea.configuration.allModules import org.jetbrains.kotlin.idea.configuration.getWholeModuleGroup +import org.jetbrains.kotlin.idea.inspections.ReplaceStringInDocumentFix import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType import org.jetbrains.kotlin.utils.PathUtil import org.jetbrains.plugins.gradle.codeInspection.GradleBaseInspection @@ -110,7 +111,7 @@ class GradleDependencyInspection : GradleBaseInspection() { registerError( reportOnElement, outdatedInfo.message, - arrayOf(), + arrayOf(ReplaceStringInDocumentFix(reportOnElement, outdatedInfo.old.name, outdatedInfo.new.name)), ProblemHighlightType.LIKE_DEPRECATED) break diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceStringInDocumentFix.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceStringInDocumentFix.kt new file mode 100644 index 00000000000..3e971b4769f --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceStringInDocumentFix.kt @@ -0,0 +1,48 @@ +/* + * Copyright 2010-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. + */ + +package org.jetbrains.kotlin.idea.inspections + +import com.intellij.codeInspection.LocalQuickFix +import com.intellij.codeInspection.ProblemDescriptor +import com.intellij.openapi.fileEditor.FileDocumentManager +import com.intellij.openapi.project.Project +import com.intellij.psi.PsiElement +import com.intellij.psi.SmartPointerManager + +class ReplaceStringInDocumentFix(element: PsiElement, private val oldString: String, private val newString: String) : LocalQuickFix { + private val elementRef = SmartPointerManager.getInstance(element.project).createSmartPsiElementPointer(element) + + override fun applyFix(project: Project, problemDescriptor: ProblemDescriptor) { + val element = elementRef.element ?: return + val virtualFile = element.containingFile?.virtualFile ?: return + val document = FileDocumentManager.getInstance().getDocument(virtualFile) ?: return + + val text = element.text + val index = text.indexOf(oldString) + if (index < 0) return + + val start = element.textOffset + index + val end = start + oldString.length + val documentText = document.text + if (end > documentText.length) return + + if (documentText.substring(start, end) != oldString) return + document.replaceString(start, end, newString) + } + + override fun getFamilyName() = "Replace '$oldString' with $newString" +} \ No newline at end of file