Intention for xml file that replace an attribute with two custom attribute with different prefix with same value



On a plugin I want to do the following  action for an intention in XML file.

Intention appear when you are on an xml attribute (cursor on text)

<label text="hello" color="red"></label>

When intention is invoked I want to add two attribute to label tag like below:

 <label i:text="hello" a:text="hello" color="red"></label>

what's the code that replace  text="hello" with i:text="hello" a:text="hello"


I tried the code below

import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.XmlElementFactory;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.psi.xml.XmlTag;
import com.intellij.psi.xml.XmlToken;
import com.intellij.util.IncorrectOperationException;
import org.igu.plugins.nativescript.NsBundle;
import org.igu.plugins.nativescript.NsFacade;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

public class PlatformSpecificPropertyValue extends PsiElementBaseIntentionAction {

public PlatformSpecificPropertyValue() {

public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement psiElement) throws IncorrectOperationException {
if (psiElement instanceof XmlToken) {
XmlToken xmlToken = (XmlToken) psiElement;
final PsiElement parent = xmlToken.getParent();
if (parent instanceof XmlAttribute) {
XmlAttribute xmlAttribute = (XmlAttribute) parent;
String localName = xmlAttribute.getLocalName();
String value = xmlAttribute.getValue();
final XmlTag tag = xmlAttribute.getParent();
final XmlAttribute iAttribute = XmlElementFactory.getInstance(project).createAttribute("i:"+localName, value, tag);
final XmlAttribute aAttribute = XmlElementFactory.getInstance(project).createAttribute("a:"+localName, value, tag);
if (!FileModificationService.getInstance().prepareFileForWrite(xmlAttribute.getContainingFile())) {

new WriteCommandAction(project, tag.getContainingFile()) {
protected void run(@NotNull Result result) throws Throwable {
tag.addAfter(xmlAttribute, iAttribute);
tag.addBefore(xmlAttribute, aAttribute);


public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement psiElement) {
return psiElement instanceof XmlToken;

public String getFamilyName() {
return getText();

Thanks in advance

1 comment
Comment actions Permalink

I just swapped the two arguments of addAfter an addBefore.


Please sign in to leave a comment.