Search in sources :

Example 41 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoApplicationRunConfigurationProducer method setupConfigurationFromContext.

@Override
protected boolean setupConfigurationFromContext(@NotNull GoApplicationConfiguration configuration, @NotNull ConfigurationContext context, Ref<PsiElement> sourceElement) {
    PsiElement contextElement = GoRunUtil.getContextElement(context);
    if (contextElement != null && GoTestFinder.isTestFile(contextElement.getContainingFile())) {
        return false;
    }
    String importPath = getImportPathFromContext(contextElement);
    if (StringUtil.isNotEmpty(importPath)) {
        configuration.setModule(context.getModule());
        configuration.setKind(GoApplicationConfiguration.Kind.PACKAGE);
        configuration.setPackage(importPath);
        configuration.setName("Build " + importPath + " and run");
        return true;
    }
    if (super.setupConfigurationFromContext(configuration, context, sourceElement)) {
        configuration.setKind(GoApplicationConfiguration.Kind.FILE);
        return true;
    }
    return false;
}
Also used : PsiElement(com.intellij.psi.PsiElement)

Example 42 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoExpressionSurrounder method surroundWithParenthesis.

@Nullable
protected TextRange surroundWithParenthesis(@NotNull PsiElement[] elements, boolean withNot) {
    GoExpression expression = getExpression(elements);
    if (expression == null)
        return null;
    String text = (withNot ? "!" : "") + "(" + expression.getText() + ")";
    GoExpression parenthExprNode = GoElementFactory.createExpression(expression.getProject(), text);
    PsiElement replace = expression.replace(parenthExprNode);
    int endOffset = replace.getTextRange().getEndOffset();
    return TextRange.create(endOffset, endOffset);
}
Also used : GoExpression(com.goide.psi.GoExpression) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 43 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoStatementsSurrounder method surroundStatementsWithIfElse.

@Nullable
protected TextRange surroundStatementsWithIfElse(@NotNull Project project, @NotNull PsiElement container, @NotNull PsiElement[] statements, boolean withElse) {
    PsiElement first = ArrayUtil.getFirstElement(statements);
    PsiElement last = ArrayUtil.getLastElement(statements);
    String block = StringUtil.join(statements, PsiElement::getText, "\n");
    GoIfStatement ifStatement = GoElementFactory.createIfStatement(project, "", block, withElse ? "" : null);
    ifStatement = (GoIfStatement) container.addAfter(ifStatement, last);
    container.deleteChildRange(first, last);
    int offset = getOffsetLBraceOfBlock(ifStatement.getBlock());
    return offset > -1 ? new TextRange(offset, offset) : null;
}
Also used : GoIfStatement(com.goide.psi.GoIfStatement) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 44 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoWithBlockSurrounder method surroundStatements.

@Nullable
@Override
protected TextRange surroundStatements(@NotNull Project project, @NotNull PsiElement container, @NotNull PsiElement[] statements) throws IncorrectOperationException {
    GoBlock block = GoElementFactory.createBlock(project);
    PsiElement first = ArrayUtil.getFirstElement(statements);
    PsiElement last = ArrayUtil.getLastElement(statements);
    block = (GoBlock) container.addAfter(block, last);
    block.addRangeAfter(first, last, block.getLbrace());
    CodeEditUtil.markToReformat(block.getNode(), true);
    container.deleteChildRange(first, last);
    int offset = block.getTextRange().getEndOffset();
    return new TextRange(offset, offset);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) GoBlock(com.goide.psi.GoBlock) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 45 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoAnnotator method annotate.

// todo: unify with DlvApi.Variable.Kind
@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (!(element instanceof GoCompositeElement) || !element.isValid())
        return;
    if (element instanceof GoPackageClause) {
        PsiElement identifier = ((GoPackageClause) element).getIdentifier();
        if (identifier != null && identifier.textMatches("_")) {
            holder.createErrorAnnotation(identifier, "Invalid package name");
            return;
        }
    }
    if (element instanceof GoContinueStatement) {
        if (!(PsiTreeUtil.getParentOfType(element, GoForStatement.class, GoFunctionLit.class) instanceof GoForStatement)) {
            Annotation annotation = holder.createErrorAnnotation(element, "Continue statement not inside a for loop");
            annotation.registerFix(new GoReplaceWithReturnStatementQuickFix(element));
        }
    } else if (element instanceof GoBreakStatement) {
        if (GoPsiImplUtil.getBreakStatementOwner(element) == null) {
            Annotation annotation = holder.createErrorAnnotation(element, "Break statement not inside a for loop, select or switch");
            annotation.registerFix(new GoReplaceWithReturnStatementQuickFix(element));
        }
    } else if (element instanceof GoReferenceExpression) {
        GoReferenceExpression reference = (GoReferenceExpression) element;
        PsiElement resolvedReference = reference.resolve();
        if (resolvedReference instanceof PsiDirectory || resolvedReference instanceof GoImportSpec) {
            // It's a package reference. It should either be inside a package clause or part of a larger reference expression.
            if (!(element.getParent() instanceof GoReferenceExpression) && PsiTreeUtil.getParentOfType(reference, GoPackageClause.class) == null) {
                holder.createErrorAnnotation(element, "Use of package " + element.getText() + " without selector");
            }
        }
        if (resolvedReference instanceof GoTypeSpec && isIllegalUseOfTypeAsExpression(reference)) {
            holder.createErrorAnnotation(element, "Type " + element.getText() + " is not an expression");
        }
        if (resolvedReference instanceof GoConstDefinition && resolvedReference.getParent() instanceof GoConstSpec && PsiTreeUtil.getParentOfType(element, GoConstDeclaration.class) != null) {
            checkSelfReference((GoReferenceExpression) element, resolvedReference, holder);
        }
        if (resolvedReference instanceof GoVarDefinition && resolvedReference.getParent() instanceof GoVarSpec && PsiTreeUtil.getParentOfType(element, GoVarDeclaration.class) != null) {
            checkSelfReference((GoReferenceExpression) element, resolvedReference, holder);
        }
    } else if (element instanceof GoLiteralTypeExpr) {
        if (isIllegalUseOfTypeAsExpression(element)) {
            holder.createErrorAnnotation(element, "Type " + element.getText() + " is not an expression");
        }
    } else if (element instanceof GoCompositeLit) {
        GoCompositeLit literal = (GoCompositeLit) element;
        if (literal.getType() instanceof GoMapType) {
            GoLiteralValue literalValue = literal.getLiteralValue();
            if (literalValue != null) {
                for (GoElement literalElement : literalValue.getElementList()) {
                    if (literalElement.getKey() == null) {
                        holder.createErrorAnnotation(literalElement, "Missing key in map literal");
                    }
                }
            }
        }
    } else if (element instanceof GoTypeAssertionExpr) {
        GoType type = ((GoTypeAssertionExpr) element).getExpression().getGoType(null);
        if (type != null) {
            GoType underlyingType = type.getUnderlyingType();
            if (!(underlyingType instanceof GoInterfaceType)) {
                String message = String.format("Invalid type assertion: %s, (non-interface type %s on left)", element.getText(), type.getText());
                holder.createErrorAnnotation(((GoTypeAssertionExpr) element).getExpression(), message);
            }
        }
    } else if (element instanceof GoBuiltinCallExpr) {
        GoBuiltinCallExpr call = (GoBuiltinCallExpr) element;
        if ("make".equals(call.getReferenceExpression().getText())) {
            checkMakeCall(call, holder);
        }
    } else if (element instanceof GoCallExpr) {
        GoCallExpr call = (GoCallExpr) element;
        GoExpression callExpression = call.getExpression();
        if (GoInspectionUtil.getFunctionResultCount(call) == 0) {
            PsiElement parent = call.getParent();
            boolean simpleStatement = parent instanceof GoLeftHandExprList && parent.getParent() instanceof GoSimpleStatement;
            boolean inDeferOrGo = parent instanceof GoDeferStatement || parent instanceof GoGoStatement;
            if (!simpleStatement && !inDeferOrGo) {
                holder.createErrorAnnotation(call, call.getText() + " used as value");
            }
        }
        if (callExpression instanceof GoReferenceExpression) {
            GoReferenceExpression reference = (GoReferenceExpression) callExpression;
            if (reference.textMatches("cap")) {
                if (GoPsiImplUtil.builtin(reference.resolve())) {
                    checkCapCall(call, holder);
                }
            }
        }
    } else if (element instanceof GoTopLevelDeclaration) {
        if (element.getParent() instanceof GoFile) {
            if (element instanceof GoTypeDeclaration) {
                for (GoTypeSpec spec : ((GoTypeDeclaration) element).getTypeSpecList()) {
                    if (spec.getIdentifier().textMatches(GoConstants.INIT)) {
                        holder.createErrorAnnotation(spec, "Cannot declare init, must be a function");
                    }
                }
            } else if (element instanceof GoVarDeclaration) {
                for (GoVarSpec spec : ((GoVarDeclaration) element).getVarSpecList()) {
                    for (GoVarDefinition definition : spec.getVarDefinitionList()) {
                        if (definition.getIdentifier().textMatches(GoConstants.INIT)) {
                            holder.createErrorAnnotation(spec, "Cannot declare init, must be a function");
                        }
                    }
                }
            } else if (element instanceof GoConstDeclaration) {
                for (GoConstSpec spec : ((GoConstDeclaration) element).getConstSpecList()) {
                    for (GoConstDefinition definition : spec.getConstDefinitionList()) {
                        if (definition.getIdentifier().textMatches(GoConstants.INIT)) {
                            holder.createErrorAnnotation(spec, "Cannot declare init, must be a function");
                        }
                    }
                }
            } else if (element instanceof GoFunctionDeclaration) {
                GoFunctionDeclaration declaration = (GoFunctionDeclaration) element;
                if (declaration.getIdentifier().textMatches(GoConstants.INIT) || declaration.getIdentifier().textMatches(GoConstants.MAIN) && GoConstants.MAIN.equals(declaration.getContainingFile().getPackageName())) {
                    GoSignature signature = declaration.getSignature();
                    if (signature != null) {
                        GoResult result = signature.getResult();
                        if (result != null && !result.isVoid()) {
                            Annotation annotation = holder.createErrorAnnotation(result, declaration.getName() + " function must have no arguments and no return values");
                            annotation.registerFix(new GoEmptySignatureQuickFix(declaration));
                        }
                        GoParameters parameters = signature.getParameters();
                        if (!parameters.getParameterDeclarationList().isEmpty()) {
                            Annotation annotation = holder.createErrorAnnotation(parameters, declaration.getName() + " function must have no arguments and no return values");
                            annotation.registerFix(new GoEmptySignatureQuickFix(declaration));
                        }
                    }
                }
            }
        }
    } else if (element instanceof GoIndexOrSliceExpr) {
        GoIndexOrSliceExpr slice = (GoIndexOrSliceExpr) element;
        GoExpression expr = slice.getExpression();
        GoExpression thirdIndex = slice.getIndices().third;
        if (expr == null || thirdIndex == null) {
            return;
        }
        if (GoTypeUtil.isString(expr.getGoType(null))) {
            ASTNode[] colons = slice.getNode().getChildren(TokenSet.create(GoTypes.COLON));
            if (colons.length == 2) {
                PsiElement secondColon = colons[1].getPsi();
                TextRange r = TextRange.create(secondColon.getTextRange().getStartOffset(), thirdIndex.getTextRange().getEndOffset());
                Annotation annotation = holder.createErrorAnnotation(r, "Invalid operation " + slice.getText() + " (3-index slice of string)");
                annotation.registerFix(new GoDeleteRangeQuickFix(secondColon, thirdIndex, "Delete third index"));
            }
        }
    }
}
Also used : GoReplaceWithReturnStatementQuickFix(com.goide.quickfix.GoReplaceWithReturnStatementQuickFix) PsiDirectory(com.intellij.psi.PsiDirectory) PsiElement(com.intellij.psi.PsiElement) GoDeleteRangeQuickFix(com.goide.quickfix.GoDeleteRangeQuickFix) TextRange(com.intellij.openapi.util.TextRange) Annotation(com.intellij.lang.annotation.Annotation) GoEmptySignatureQuickFix(com.goide.quickfix.GoEmptySignatureQuickFix)

Aggregations

PsiElement (com.intellij.psi.PsiElement)3198 Nullable (org.jetbrains.annotations.Nullable)493 PsiFile (com.intellij.psi.PsiFile)474 NotNull (org.jetbrains.annotations.NotNull)442 TextRange (com.intellij.openapi.util.TextRange)239 PsiReference (com.intellij.psi.PsiReference)227 Project (com.intellij.openapi.project.Project)222 VirtualFile (com.intellij.openapi.vfs.VirtualFile)210 ArrayList (java.util.ArrayList)195 ASTNode (com.intellij.lang.ASTNode)142 XmlTag (com.intellij.psi.xml.XmlTag)134 PsiClass (com.intellij.psi.PsiClass)115 Editor (com.intellij.openapi.editor.Editor)112 Document (com.intellij.openapi.editor.Document)109 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)85 PsiDirectory (com.intellij.psi.PsiDirectory)80 IElementType (com.intellij.psi.tree.IElementType)78 Module (com.intellij.openapi.module.Module)77 PsiMethod (com.intellij.psi.PsiMethod)73 UsageInfo (com.intellij.usageView.UsageInfo)70