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());
}
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());
}
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;
}
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);
}
}
};
}
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);
}
Aggregations