generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#51 Migrate java inspections to kotlin
- Loading branch information
Dan Timofte
committed
Dec 1, 2024
1 parent
c3256f0
commit bd18cb2
Showing
4 changed files
with
134 additions
and
110 deletions.
There are no files selected for viewing
107 changes: 0 additions & 107 deletions
107
src/main/java/ac/quant/quickfixspec/inspections/ReplaceWithDefinitionInspection.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/kotlin/ac/quant/quickfixspec/inspections/ReplaceWithDefinitionInspection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package ac.quant.quickfixspec.inspections | ||
|
||
import ac.quant.quickfixspec.common.spec.XmlUtils.findDefinition | ||
import ac.quant.quickfixspec.common.spec.XmlUtils.getRootTag | ||
import ac.quant.quickfixspec.common.spec.XmlUtils.isComponentReference | ||
import ac.quant.quickfixspec.common.spec.XmlUtils.getCurrentTag | ||
|
||
import ac.quant.quickfixspec.inspections.ReplaceWithDefinitionInspection.ReplaceWithDefinitionQuickFix | ||
import com.intellij.codeInsight.intention.FileModifier.SafeFieldForPreview | ||
import com.intellij.codeInspection.LocalInspectionTool | ||
import com.intellij.codeInspection.LocalQuickFix | ||
import com.intellij.codeInspection.ProblemDescriptor | ||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiElementVisitor | ||
import com.intellij.psi.SmartPointerManager | ||
import com.intellij.psi.SmartPsiElementPointer | ||
import com.intellij.psi.xml.XmlTag | ||
import com.intellij.util.IncorrectOperationException | ||
|
||
|
||
class ReplaceWithDefinitionInspection : LocalInspectionTool() { | ||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { | ||
return object : PsiElementVisitor() { | ||
override fun visitElement(element: PsiElement) { | ||
val rootTag = getRootTag(element) ?: return | ||
|
||
val currentTag: XmlTag = getCurrentTag(element) | ||
if (currentTag.name != "component") { | ||
return | ||
} | ||
|
||
|
||
// return if the tag is not a component reference, and it's actually a definition from the "components" group | ||
if (!isComponentReference(currentTag)) { | ||
return | ||
} | ||
|
||
val componentName = currentTag.getAttributeValue("name") ?: return | ||
val definition = findDefinition(componentName, "component", rootTag) ?: return | ||
|
||
// check if we didn't already register a problem for this tag | ||
if (holder.isOnTheFly && holder.results.any { it.psiElement == currentTag }) { | ||
return | ||
} | ||
|
||
holder.registerProblem(currentTag, "Replace with definition", ReplaceWithDefinitionQuickFix(definition)) | ||
} | ||
} | ||
} | ||
|
||
private class ReplaceWithDefinitionQuickFix(definition: XmlTag) : LocalQuickFix { | ||
@SafeFieldForPreview | ||
private val definitionPointer: SmartPsiElementPointer<XmlTag?> = SmartPointerManager.createPointer<XmlTag?>(definition) | ||
|
||
override fun getName(): String { | ||
return "Replace component reference with definition" | ||
} | ||
|
||
override fun getFamilyName(): String { | ||
return name | ||
} | ||
|
||
@Throws(IncorrectOperationException::class) | ||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { | ||
// Get the <component> tag that is to be replaced | ||
val tagToReplace = descriptor.psiElement as XmlTag | ||
|
||
// Retrieve the full definition of the <component> from the pointer | ||
val definition = definitionPointer.getElement() | ||
|
||
if (definition != null) { | ||
// Create a copy of the definition tag to be inserted | ||
val newTag = definition.copy() as XmlTag | ||
|
||
// Replace the original <component> tag with the full definition | ||
tagToReplace.replace(newTag) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters