Search in sources :

Example 11 with FunctionStatement

use of com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement in project intellij by bazelbuild.

the class LocalVariableUsagesTest method testLocalReferences.

@Test
public void testLocalReferences() {
    BuildFile buildFile = createBuildFile(new WorkspacePath("java/com/google/BUILD"), "localVar = 5", "funcall(localVar)", "def function(name):", "    tempVar = localVar");
    TargetExpression target = buildFile.findChildByClass(AssignmentStatement.class).getLeftHandSideExpression();
    PsiReference[] references = FindUsages.findAllReferences(target);
    assertThat(references).hasLength(2);
    FuncallExpression funcall = buildFile.findChildByClass(FuncallExpression.class);
    assertThat(funcall).isNotNull();
    PsiElement firstRef = references[0].getElement();
    assertThat(PsiUtils.getParentOfType(firstRef, FuncallExpression.class, true)).isEqualTo(funcall);
    FunctionStatement function = buildFile.findChildByClass(FunctionStatement.class);
    assertThat(function).isNotNull();
    PsiElement secondRef = references[1].getElement();
    assertThat(secondRef.getParent()).isInstanceOf(AssignmentStatement.class);
    assertThat(PsiUtils.getParentOfType(secondRef, FunctionStatement.class, true)).isEqualTo(function);
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) FunctionStatement(com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement) AssignmentStatement(com.google.idea.blaze.base.lang.buildfile.psi.AssignmentStatement) TargetExpression(com.google.idea.blaze.base.lang.buildfile.psi.TargetExpression) PsiReference(com.intellij.psi.PsiReference) FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression) PsiElement(com.intellij.psi.PsiElement) Test(org.junit.Test)

Example 12 with FunctionStatement

use of com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement in project intellij by bazelbuild.

the class ArgumentReference method getVariants.

@Override
public Object[] getVariants() {
    FunctionStatement function = resolveFunction();
    if (function == null) {
        return EMPTY_ARRAY;
    }
    List<LookupElement> params = Lists.newArrayList();
    for (Parameter param : function.getParameters()) {
        params.add(new NamedBuildLookupElement(param, QuoteType.NoQuotes));
    }
    return params.toArray();
}
Also used : FunctionStatement(com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement) NamedBuildLookupElement(com.google.idea.blaze.base.lang.buildfile.completion.NamedBuildLookupElement) Parameter(com.google.idea.blaze.base.lang.buildfile.psi.Parameter) LookupElement(com.intellij.codeInsight.lookup.LookupElement) NamedBuildLookupElement(com.google.idea.blaze.base.lang.buildfile.completion.NamedBuildLookupElement)

Example 13 with FunctionStatement

use of com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement in project intellij by bazelbuild.

the class HighlightingAnnotator method visitParameter.

@Override
public void visitParameter(Parameter node) {
    FunctionStatement function = PsiTreeUtil.getParentOfType(node, FunctionStatement.class);
    if (function != null) {
        PsiElement anchor = node.hasDefaultValue() ? node.getFirstChild() : node;
        final Annotation annotation = getHolder().createInfoAnnotation(anchor, null);
        annotation.setTextAttributes(BuildSyntaxHighlighter.BUILD_PARAMETER);
    }
}
Also used : FunctionStatement(com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement) PsiElement(com.intellij.psi.PsiElement) Annotation(com.intellij.lang.annotation.Annotation)

Example 14 with FunctionStatement

use of com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement in project intellij by bazelbuild.

the class ResolveUtil method searchInScope.

/**
 * Walks up PSI tree of local file, checking PsiNamedElements
 */
public static void searchInScope(PsiElement originalElement, Processor<BuildElement> processor) {
    // TODO: Handle list comprehension (where variable is defined *later* in the code)
    boolean topLevelScope = true;
    PsiElement element = originalElement;
    while (!(element instanceof PsiFileSystemItem)) {
        PsiElement parent = element.getParent();
        if (parent instanceof BuildFile) {
            if (!((BuildFile) parent).searchSymbolsInScope(processor, topLevelScope ? element : null)) {
                return;
            }
        } else if (parent instanceof FunctionStatement) {
            topLevelScope = false;
            for (Parameter param : ((FunctionStatement) parent).getParameters()) {
                if (!processor.process(param)) {
                    return;
                }
            }
        } else if (parent instanceof ForStatement) {
            for (Expression expr : ((ForStatement) parent).getForLoopVariables()) {
                if (expr instanceof TargetExpression && !processor.process(expr)) {
                    return;
                }
            }
        } else if (parent instanceof StatementList) {
            if (!visitChildAssignmentStatements((BuildElement) parent, (Processor) processor)) {
                return;
            }
        }
        element = parent;
    }
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) FunctionStatement(com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement) TargetExpression(com.google.idea.blaze.base.lang.buildfile.psi.TargetExpression) Expression(com.google.idea.blaze.base.lang.buildfile.psi.Expression) StatementList(com.google.idea.blaze.base.lang.buildfile.psi.StatementList) Parameter(com.google.idea.blaze.base.lang.buildfile.psi.Parameter) TargetExpression(com.google.idea.blaze.base.lang.buildfile.psi.TargetExpression) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) ForStatement(com.google.idea.blaze.base.lang.buildfile.psi.ForStatement) PsiElement(com.intellij.psi.PsiElement)

Example 15 with FunctionStatement

use of com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement in project intellij by bazelbuild.

the class RenameRefactoringTest method testFunctionRenameValidation.

@Test
public void testFunctionRenameValidation() {
    BuildFile file = createBuildFile(new WorkspacePath("com/google/foo/BUILD"), "def fn_name():", "  return");
    FunctionStatement fn = PsiUtils.findFirstChildOfClassRecursive(file, FunctionStatement.class);
    assertThat(RenameUtil.isValidName(getProject(), fn, "name-with-dash")).isFalse();
    assertThat(RenameUtil.isValidName(getProject(), fn, "name:withColon")).isFalse();
    assertThat(RenameUtil.isValidName(getProject(), fn, "return")).isFalse();
    assertThat(RenameUtil.isValidName(getProject(), fn, "name_with_underscore")).isTrue();
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) FunctionStatement(com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement) Test(org.junit.Test)

Aggregations

FunctionStatement (com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement)20 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)16 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)14 Test (org.junit.Test)14 FuncallExpression (com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression)6 PsiElement (com.intellij.psi.PsiElement)6 PsiReference (com.intellij.psi.PsiReference)6 Parameter (com.google.idea.blaze.base.lang.buildfile.psi.Parameter)5 LoadStatement (com.google.idea.blaze.base.lang.buildfile.psi.LoadStatement)3 ParameterList (com.google.idea.blaze.base.lang.buildfile.psi.ParameterList)2 TargetExpression (com.google.idea.blaze.base.lang.buildfile.psi.TargetExpression)2 Nullable (javax.annotation.Nullable)2 NamedBuildLookupElement (com.google.idea.blaze.base.lang.buildfile.completion.NamedBuildLookupElement)1 AssignmentStatement (com.google.idea.blaze.base.lang.buildfile.psi.AssignmentStatement)1 Expression (com.google.idea.blaze.base.lang.buildfile.psi.Expression)1 ForStatement (com.google.idea.blaze.base.lang.buildfile.psi.ForStatement)1 StatementList (com.google.idea.blaze.base.lang.buildfile.psi.StatementList)1 StringLiteral (com.google.idea.blaze.base.lang.buildfile.psi.StringLiteral)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 Annotation (com.intellij.lang.annotation.Annotation)1