Search in sources :

Example 6 with RunResult

use of com.intellij.openapi.application.RunResult in project intellij-community by JetBrains.

the class AddWithParamFix method invoke.

public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) throws IncorrectOperationException {
    final RunResult<SmartPsiElementPointer<XmlTag>> result = new WriteAction<SmartPsiElementPointer<XmlTag>>() {

        protected void run(@NotNull Result<SmartPsiElementPointer<XmlTag>> result) throws Throwable {
            final XmlTag withParamTag = RefactoringUtil.addWithParam(myTag);
            withParamTag.setAttribute("name", myName != null ? myName : "dummy");
            withParamTag.setAttribute("select", "dummy");
            result.setResult(SmartPointerManager.getInstance(project).createSmartPsiElementPointer(withParamTag));
        }
    }.execute();
    final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
    final Document doc = psiDocumentManager.getDocument(file);
    assert doc != null;
    psiDocumentManager.doPostponedOperationsAndUnblockDocument(doc);
    final XmlTag withParamTag = result.getResultObject().getElement();
    assert withParamTag != null;
    final TemplateBuilderImpl builder = new TemplateBuilderImpl(withParamTag);
    final XmlAttribute selectAttr = withParamTag.getAttribute("select", null);
    assert selectAttr != null;
    PsiElement dummy = XsltSupport.getAttValueToken(selectAttr);
    builder.replaceElement(dummy, new MacroCallNode(new CompleteMacro()));
    if (myName == null) {
        final XmlAttribute nameAttr = withParamTag.getAttribute("name", null);
        assert nameAttr != null;
        dummy = XsltSupport.getAttValueToken(nameAttr);
        builder.replaceElement(dummy, new MacroCallNode(new CompleteMacro()));
    }
    moveTo(editor, withParamTag);
    new WriteAction() {

        @SuppressWarnings({ "RawUseOfParameterizedType" })
        protected void run(@NotNull Result result) throws Throwable {
            PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
            final TemplateManager mgr = TemplateManager.getInstance(myTag.getProject());
            mgr.startTemplate(editor, builder.buildInlineTemplate());
        }
    }.execute();
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) WriteAction(com.intellij.openapi.application.WriteAction) Document(com.intellij.openapi.editor.Document) CompleteMacro(com.intellij.codeInsight.template.macro.CompleteMacro) Result(com.intellij.openapi.application.Result) RunResult(com.intellij.openapi.application.RunResult) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) TemplateManager(com.intellij.codeInsight.template.TemplateManager) MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

RunResult (com.intellij.openapi.application.RunResult)6 Result (com.intellij.openapi.application.Result)5 WriteAction (com.intellij.openapi.application.WriteAction)4 IOException (java.io.IOException)2 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1 MacroCallNode (com.intellij.codeInsight.template.impl.MacroCallNode)1 CompleteMacro (com.intellij.codeInsight.template.macro.CompleteMacro)1 GenericCompilerCache (com.intellij.compiler.impl.generic.GenericCompilerCache)1 ReadAction (com.intellij.openapi.application.ReadAction)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Document (com.intellij.openapi.editor.Document)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlTag (com.intellij.psi.xml.XmlTag)1 THashSet (gnu.trove.THashSet)1 File (java.io.File)1