0
プラグインでは、XMLファイルの意図に対して次の操作を行います。Intellijプラグイン:異なるプレフィックスと同じ値を持つ2つのカスタム属性を持つ属性を置き換えるxmlファイルの意図
:あなたは、XML属性(テキスト上のカーソル)
上にあるときの意図は、私は以下のようなラベルタグに2つの属性を追加する呼び出されると
意向が表示されますtext = "hello" a:text = "hello"
私は以下のコードを試しました:
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() {
setText(NsBundle.message("intention.declare.platform.specific.property.value"));
}
@Override
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())) {
return;
}
new WriteCommandAction(project, tag.getContainingFile()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
tag.addAfter(xmlAttribute, iAttribute);
tag.addBefore(xmlAttribute, aAttribute);
xmlAttribute.delete();
}
}.execute();
}
}
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement psiElement) {
return psiElement instanceof XmlToken;
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return getText();
}
}
ありがとうございます。あなたがaddAfter()
とaddBefore()
の二つの引数スワップ
感謝。 –