Package ro.redeul.google.go.lang.psi.utils

Source Code of ro.redeul.google.go.lang.psi.utils.GoIdentifierUtils

package ro.redeul.google.go.lang.psi.utils;

import com.intellij.patterns.ElementPattern;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.Nullable;
import ro.redeul.google.go.lang.psi.GoPsiElement;
import ro.redeul.google.go.lang.psi.toplevel.GoFunctionDeclaration;

import static com.intellij.patterns.PlatformPatterns.psiElement;
import static ro.redeul.google.go.lang.psi.utils.GoPsiUtils.getAs;

public class GoIdentifierUtils {
    private static final ElementPattern FUNCTION_DECLARATION_PATTERN = psiElement(GoFunctionDeclaration.class);

    private static boolean isFunctionDeclarationIdentifier(@Nullable PsiElement identifier) {
        return FUNCTION_DECLARATION_PATTERN.accepts(identifier);
    }

    @Nullable
    public static GoFunctionDeclaration getFunctionDeclaration(@Nullable PsiElement identifier) {

        if (!(identifier instanceof GoPsiElement))
            return null;

        GoPsiElement goPsiElement =  (GoPsiElement) identifier;

        // If the identifier is not definition identifier of function, try to resolve it.
        if (!isFunctionDeclarationIdentifier(goPsiElement)) {
            goPsiElement = GoPsiUtils.resolveSafely(goPsiElement, GoPsiElement.class);
        }

        return getAs(GoFunctionDeclaration.class, goPsiElement);
    }
}
TOP

Related Classes of ro.redeul.google.go.lang.psi.utils.GoIdentifierUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.