Search in sources :

Example 26 with PsiElement

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

the class GoMoveToStructInitializationIntention method addFieldDefinition.

private static void addFieldDefinition(@NotNull GoLiteralValue literalValue, @NotNull String name, @NotNull String value) {
    Project project = literalValue.getProject();
    PsiElement newField = GoElementFactory.createLiteralValueElement(project, name, value);
    GoElement lastElement = getLastItem(literalValue.getElementList());
    if (lastElement == null) {
        literalValue.addAfter(newField, literalValue.getLbrace());
    } else {
        lastElement.add(GoElementFactory.createComma(project));
        lastElement.add(newField);
    }
}
Also used : Project(com.intellij.openapi.project.Project) PsiElement(com.intellij.psi.PsiElement)

Example 27 with PsiElement

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

the class GoMoveToStructInitializationIntention method isAssignedInPreviousStatement.

private static boolean isAssignedInPreviousStatement(@NotNull GoExpression referenceExpression, @NotNull GoAssignmentStatement assignment) {
    GoReferenceExpression rightExpression = unwrapParensAndCast(GoPsiImplUtil.getRightExpression(assignment, getTopmostExpression(referenceExpression)));
    PsiElement resolve = rightExpression != null ? rightExpression.resolve() : null;
    GoStatement previousElement = resolve != null ? PsiTreeUtil.getPrevSiblingOfType(assignment, GoStatement.class) : null;
    return previousElement != null && exists(getLeftHandElements(previousElement), e -> isResolvedTo(e, resolve));
}
Also used : BaseElementAtCaretIntentionAction(com.intellij.codeInsight.intention.BaseElementAtCaretIntentionAction) IncorrectOperationException(com.intellij.util.IncorrectOperationException) com.goide.psi(com.goide.psi) GoPsiImplUtil(com.goide.psi.impl.GoPsiImplUtil) Set(java.util.Set) ContainerUtil(com.intellij.util.containers.ContainerUtil) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Contract(org.jetbrains.annotations.Contract) List(java.util.List) Nls(org.jetbrains.annotations.Nls) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) ObjectUtils(com.intellij.util.ObjectUtils) GoElementFactory(com.goide.psi.impl.GoElementFactory) NotNull(org.jetbrains.annotations.NotNull) MultiMap(com.intellij.util.containers.MultiMap) PsiElement(com.intellij.psi.PsiElement)

Example 28 with PsiElement

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

the class GoRecursiveCallMarkerProvider method collectSlowLineMarkers.

@Override
public void collectSlowLineMarkers(@NotNull List<PsiElement> elements, @NotNull Collection<LineMarkerInfo> result) {
    Set<Integer> lines = ContainerUtil.newHashSet();
    for (PsiElement element : elements) {
        if (element instanceof GoCallExpr) {
            PsiElement resolve = GoPsiImplUtil.resolveCall((GoCallExpr) element);
            if (resolve instanceof GoFunctionOrMethodDeclaration) {
                if (isRecursiveCall(element, (GoFunctionOrMethodDeclaration) resolve)) {
                    PsiDocumentManager instance = PsiDocumentManager.getInstance(element.getProject());
                    Document document = instance.getDocument(element.getContainingFile());
                    int textOffset = element.getTextOffset();
                    if (document == null)
                        continue;
                    int lineNumber = document.getLineNumber(textOffset);
                    if (!lines.contains(lineNumber)) {
                        result.add(new RecursiveMethodCallMarkerInfo(element));
                    }
                    lines.add(lineNumber);
                }
            }
        }
    }
}
Also used : GoFunctionOrMethodDeclaration(com.goide.psi.GoFunctionOrMethodDeclaration) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) GoCallExpr(com.goide.psi.GoCallExpr) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 29 with PsiElement

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

the class GoRecursiveVisitor method visitCompositeElement.

@Override
public void visitCompositeElement(@NotNull GoCompositeElement o) {
    super.visitCompositeElement(o);
    for (PsiElement psiElement : o.getChildren()) {
        psiElement.accept(this);
        ProgressIndicatorProvider.checkCanceled();
    }
}
Also used : PsiElement(com.intellij.psi.PsiElement)

Example 30 with PsiElement

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

the class GoExpressionUtil method identical.

public static boolean identical(@Nullable GoExpression left, @Nullable GoExpression right) {
    if (left == right)
        return true;
    if (left == null || right == null)
        return false;
    GoExpression l = unwrap(left);
    GoExpression r = unwrap(right);
    if (l == null || r == null)
        return false;
    if (!l.getClass().equals(r.getClass()))
        return false;
    if (l instanceof GoBinaryExpr) {
        GoBinaryExpr lBin = (GoBinaryExpr) l;
        GoBinaryExpr rBin = (GoBinaryExpr) r;
        return isOperatorEquals(lBin.getOperator(), rBin.getOperator()) && isChildrenExprEquals(lBin, rBin);
    }
    if (l instanceof GoUnaryExpr) {
        GoUnaryExpr lUnary = (GoUnaryExpr) l;
        GoUnaryExpr rUnary = (GoUnaryExpr) r;
        return isOperatorEquals(lUnary.getOperator(), rUnary.getOperator()) && identical(lUnary.getExpression(), rUnary.getExpression());
    }
    if (l instanceof GoReferenceExpression) {
        PsiElement resolve = ((GoReferenceExpression) l).resolve();
        return resolve != null && resolve.isEquivalentTo(((GoReferenceExpression) r).resolve());
    }
    if (l instanceof GoIndexOrSliceExpr) {
        GoIndexOrSliceExpr lSlice = (GoIndexOrSliceExpr) l;
        GoIndexOrSliceExpr rSlice = (GoIndexOrSliceExpr) r;
        return identical(lSlice.getExpression(), rSlice.getExpression()) && isIndicesIdentical(lSlice.getIndices(), rSlice.getIndices());
    }
    if (l instanceof GoStringLiteral) {
        return Comparing.equal(((GoStringLiteral) l).getDecodedText(), ((GoStringLiteral) r).getDecodedText());
    }
    if (l instanceof GoLiteralTypeExpr) {
        GoLiteralTypeExpr lLit = (GoLiteralTypeExpr) l;
        GoLiteralTypeExpr rLit = (GoLiteralTypeExpr) r;
        GoTypeReferenceExpression lExpr = lLit.getTypeReferenceExpression();
        GoTypeReferenceExpression rExpr = rLit.getTypeReferenceExpression();
        PsiElement lResolve = lExpr != null ? lExpr.resolve() : null;
        return lResolve != null && rExpr != null && lResolve.equals(rExpr.resolve());
    //todo: add || GoTypeUtil.identical(lLit.getType(), rLit.getType)
    }
    if (l instanceof GoLiteral) {
        return l.textMatches(r);
    }
    if (l instanceof GoBuiltinCallExpr || l instanceof GoCallExpr) {
        return false;
    }
    if (l instanceof GoCompositeLit) {
        GoCompositeLit lLit = (GoCompositeLit) l;
        GoCompositeLit rLit = (GoCompositeLit) r;
        return identical(lLit.getLiteralValue(), rLit.getLiteralValue());
    // todo: add && GoTypeUtil.identical
    }
    if (l instanceof GoTypeAssertionExpr) {
        GoTypeAssertionExpr lAssertion = (GoTypeAssertionExpr) l;
        GoTypeAssertionExpr rAssertion = (GoTypeAssertionExpr) r;
        return identical(lAssertion.getExpression(), rAssertion.getExpression());
    //todo: add && GoTypeUtil.identical(lAssertion.getType(), rAssertion.getType())
    }
    String lText = l.getText();
    return lText != null && lText.equals(r.getText());
}
Also used : PsiElement(com.intellij.psi.PsiElement)

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