Search in sources :

Example 1 with GoType

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

the class GoUnderlyingTypeTest method doTest.

private void doTest(@NotNull String text, String expected) {
    myFixture.configureByText("a.go", "package a\n" + text);
    PsiElement element = myFixture.getFile().findElementAt(myFixture.getEditor().getCaretModel().getOffset());
    GoType type = PsiTreeUtil.getParentOfType(element, GoType.class);
    assertNotNull(type);
    assertEquals(expected, type.getUnderlyingType().getText());
}
Also used : PsiElement(com.intellij.psi.PsiElement) GoType(com.goide.psi.GoType)

Example 2 with GoType

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

the class GoTypeResolveTest method doTopLevelTest.

private void doTopLevelTest(@NotNull String text, @NotNull String expectedTypeText) {
    myFixture.configureByText("a.go", "package a;" + text);
    PsiElement elementAt = findElementAtCaretOrInSelection();
    GoTypeOwner typeOwner = PsiTreeUtil.getNonStrictParentOfType(elementAt, GoTypeOwner.class);
    assertNotNull("Cannot find type owner. Context element: " + elementAt.getText(), typeOwner);
    GoType type = typeOwner.getGoType(null);
    assertEquals(expectedTypeText, type == null ? "<unknown>" : type.getText());
}
Also used : GoTypeOwner(com.goide.psi.GoTypeOwner) PsiElement(com.intellij.psi.PsiElement) GoType(com.goide.psi.GoType)

Example 3 with GoType

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

the class GoTypeDeclarationProvider method getSymbolTypeDeclarations.

@Nullable
@Override
public PsiElement[] getSymbolTypeDeclarations(@NotNull PsiElement element) {
    if (!(element instanceof GoNamedElement))
        return PsiElement.EMPTY_ARRAY;
    GoType type = ((GoNamedElement) element).getGoType(null);
    GoTypeReferenceExpression ref = type != null ? type.getTypeReferenceExpression() : null;
    // todo: think about better fallback instead of `type`
    PsiElement resolve = ref != null ? ref.resolve() : type;
    return resolve != null ? new PsiElement[] { resolve } : PsiElement.EMPTY_ARRAY;
}
Also used : GoNamedElement(com.goide.psi.GoNamedElement) GoTypeReferenceExpression(com.goide.psi.GoTypeReferenceExpression) GoType(com.goide.psi.GoType) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with GoType

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

the class GoRangeIterationOnIllegalTypeInspection method buildGoVisitor.

@NotNull
@Override
protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
    return new GoVisitor() {

        @Override
        public void visitRangeClause(@NotNull GoRangeClause o) {
            super.visitRangeClause(o);
            GoExpression expression = o.getRangeExpression();
            GoType type = expression != null ? expression.getGoType(null) : null;
            if (type != null && !GoTypeUtil.isIterable(type)) {
                holder.registerProblem(expression, "Cannot range over data (type " + GoPsiImplUtil.getText(type) + ")", ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
            }
        }
    };
}
Also used : GoRangeClause(com.goide.psi.GoRangeClause) GoExpression(com.goide.psi.GoExpression) NotNull(org.jetbrains.annotations.NotNull) GoVisitor(com.goide.psi.GoVisitor) GoType(com.goide.psi.GoType) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with GoType

use of com.goide.psi.GoType 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)5 PsiElement (com.intellij.psi.PsiElement)4 GoExpression (com.goide.psi.GoExpression)1 GoNamedElement (com.goide.psi.GoNamedElement)1 GoRangeClause (com.goide.psi.GoRangeClause)1 GoTypeDeclaration (com.goide.psi.GoTypeDeclaration)1 GoTypeOwner (com.goide.psi.GoTypeOwner)1 GoTypeReferenceExpression (com.goide.psi.GoTypeReferenceExpression)1 GoTypeSpec (com.goide.psi.GoTypeSpec)1 GoVisitor (com.goide.psi.GoVisitor)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 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1