2016-09-08 4 views
0

플러그인에서 XML 파일의 의도에 대해 다음 작업을 수행하려고합니다.Intellij 플러그인 : 다른 접두사가 동일한 사용자 정의 속성으로 속성을 대체하는 XML 파일에 대한 의도가 동일한 값을 갖는

당신이 (텍스트에 커서) XML 속성에있을 때 의도가 나타 의도가 나는 아래와 같은 라벨 태그에 두 개의 속성을 추가 할 호출

:

텍스트를 "hello"로 대체하는 코드는 무엇입니까? : 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(); 
    } 
} 

감사합니다.

/** 
* Adds a child to this PSI element, after the specified anchor element. 
* 
* @param element the child element to add. 
* @param anchor the anchor after which the child element is inserted (must be a child of this PSI element) 
* @return the element which was actually added (either <code>element</code> or its copy). 
* @throws IncorrectOperationException if the modification is not supported or not possible for some reason. 
*/ 
PsiElement addAfter(@NotNull PsiElement element, @Nullable PsiElement anchor) throws IncorrectOperationException; 

올바른 코드가 있어야한다 : 작동

tag.addAfter(iAttribute, xmlAttribute); 
tag.addBefore(aAttribute, xmlAttribute); 
xmlAttribute.delete(); 

답변

1

당신은 addAfter()addBefore()의 두 개의 인수를 교환.
+0

감사 –