Search in sources :

Example 1 with GoTypeDeclaration

use of com.goide.psi.GoTypeDeclaration in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoCreateWrapperTypeQuickFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    if (editor == null) {
        LOG.error("Cannot run quick fix without editor: " + getClass().getSimpleName(), AttachmentFactory.createAttachment(file.getVirtualFile()));
        return;
    }
    if (!(startElement instanceof GoType))
        return;
    GoType type = (GoType) startElement;
    PsiElement anchor = PsiTreeUtil.findPrevParent(file, type);
    String name = "TypeName";
    GoTypeDeclaration decl = (GoTypeDeclaration) file.addBefore(GoElementFactory.createTypeDeclaration(project, name, type), anchor);
    if (decl == null)
        return;
    decl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(decl);
    if (decl == null)
        return;
    GoTypeSpec spec = ContainerUtil.getFirstItem(decl.getTypeSpecList());
    if (spec == null)
        return;
    TemplateBuilderImpl builder = new TemplateBuilderImpl(file);
    builder.replaceElement(type, OTHER_NAME, INPUT_NAME, false);
    builder.replaceElement(spec.getIdentifier(), INPUT_NAME, new ConstantNode(name), true);
    editor.getCaretModel().moveToOffset(file.getTextRange().getStartOffset());
    Template template = builder.buildInlineTemplate();
    editor.getCaretModel().moveToOffset(file.getTextRange().getStartOffset());
    TemplateManager.getInstance(project).startTemplate(editor, template);
}
Also used : GoTypeDeclaration(com.goide.psi.GoTypeDeclaration) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) ConstantNode(com.intellij.codeInsight.template.impl.ConstantNode) GoTypeSpec(com.goide.psi.GoTypeSpec) GoType(com.goide.psi.GoType) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement) PsiElement(com.intellij.psi.PsiElement) Template(com.intellij.codeInsight.template.Template)

Aggregations

GoType (com.goide.psi.GoType)1 GoTypeDeclaration (com.goide.psi.GoTypeDeclaration)1 GoTypeSpec (com.goide.psi.GoTypeSpec)1 Template (com.intellij.codeInsight.template.Template)1 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)1 ConstantNode (com.intellij.codeInsight.template.impl.ConstantNode)1 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)1 PsiElement (com.intellij.psi.PsiElement)1