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.
Add inspection to replace component reference with full definition (#20)
* resolve and show documentation for field and group * remove unused import * Add Inspection to replace component reference with full definition * remove unused imports --------- Co-authored-by: Dan Timofte <dantimofte@proton.me>
- Loading branch information
1 parent
506d922
commit e3a1bde
Showing
6 changed files
with
155 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
103 changes: 103 additions & 0 deletions
103
src/main/java/ac/quant/quickfixspec/inspections/ReplaceWithDefinitionInspection.java
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,103 @@ | ||
package ac.quant.quickfixspec.inspections; | ||
|
||
import com.intellij.codeInspection.LocalInspectionTool; | ||
import com.intellij.codeInspection.ProblemsHolder; | ||
import com.intellij.codeInspection.ProblemDescriptor; | ||
import com.intellij.codeInspection.LocalQuickFix; | ||
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; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import static ac.quant.quickfixspec.common.PsiUtils.*; | ||
|
||
public class ReplaceWithDefinitionInspection extends LocalInspectionTool { | ||
|
||
@Override | ||
public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { | ||
return new PsiElementVisitor() { | ||
@Override | ||
public void visitElement(@NotNull PsiElement element) { | ||
if (isNotComponentTagReference(element)) { | ||
return; | ||
} | ||
|
||
final XmlTag rootTag = getRootTag(element); | ||
final XmlTag tag = (XmlTag) element; | ||
if (rootTag == null) { | ||
return; | ||
} | ||
|
||
final String componentName = tag.getAttributeValue("name"); | ||
|
||
|
||
XmlTag definition = findDefinition(componentName, tag.getName(), rootTag); | ||
if (definition != null) { | ||
holder.registerProblem(tag, "Replace with definition", new ReplaceWithDefinitionQuickFix(definition)); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
private boolean isNotComponentTagReference(@NotNull PsiElement element) { | ||
if (!(element instanceof XmlTag tag)) { | ||
return true; | ||
} | ||
|
||
final XmlTag parentTag = tag.getParentTag(); | ||
if (parentTag == null) { | ||
return true; | ||
} | ||
|
||
if (parentTag.getName().equals("components")) { | ||
return true; | ||
} | ||
|
||
// if the tag is not self-closing that means it is a definition | ||
if (tag.getSubTags().length > 0) { | ||
return true; | ||
} | ||
|
||
return !tag.getName().equals("component"); | ||
} | ||
|
||
private static class ReplaceWithDefinitionQuickFix implements LocalQuickFix { | ||
@SafeFieldForPreview | ||
private final SmartPsiElementPointer<XmlTag> definitionPointer; | ||
|
||
ReplaceWithDefinitionQuickFix(XmlTag definition) { | ||
this.definitionPointer = SmartPointerManager.createPointer(definition); | ||
} | ||
|
||
@Override | ||
public @NotNull String getName() { | ||
return "Replace with definition"; | ||
} | ||
|
||
@Override | ||
public @NotNull String getFamilyName() { | ||
return getName(); | ||
} | ||
|
||
@Override | ||
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException { | ||
// Get the <component> tag that is to be replaced | ||
XmlTag tagToReplace = (XmlTag) descriptor.getPsiElement(); | ||
|
||
// Retrieve the full definition of the <component> from the pointer | ||
XmlTag definition = definitionPointer.getElement(); | ||
|
||
if (definition != null) { | ||
// Create a copy of the definition tag to be inserted | ||
XmlTag newTag = (XmlTag) definition.copy(); | ||
|
||
// 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
35 changes: 35 additions & 0 deletions
35
src/main/resources/inspectionDescriptions/ReplaceWithDefinition.html
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,35 @@ | ||
<html lang="en"> | ||
<body> | ||
<p> | ||
Detects shorthand <code><component></code> tags (e.g., <code><component name="compName1" /></code>) within | ||
<code><message></code> sections and suggests replacing them with their full definition from the | ||
<code><components></code> section of the same XML file. | ||
</p> | ||
|
||
<h3>Example</h3> | ||
<pre><code class="language-xml"> | ||
<message> | ||
<component name="compName1" /> | ||
</message> | ||
|
||
<components> | ||
<component name="compName1"> | ||
<field name="aa" /> | ||
<field name="bb" /> | ||
</component> | ||
</components> | ||
</code></pre> | ||
|
||
<p> | ||
Suggestion: Expand <code><component name="compName1" /></code> to: | ||
</p> | ||
|
||
<pre><code class="language-xml"> | ||
<component name="compName1"> | ||
<field name="aa" /> | ||
<field name="bb" /> | ||
</component> | ||
</code></pre> | ||
|
||
</body> | ||
</html> |