Search in sources :

Example 1 with FunctionStatement

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

the class LocalReferenceTest method testReferenceToFunctionArg.

@Test
public void testReferenceToFunctionArg() {
    BuildFile file = createBuildFile(new WorkspacePath("java/com/google/defs.bzl"), "def function(arg1, arg2):", "  arg1(arg2)");
    FunctionStatement def = file.findFunctionInScope("function");
    FuncallExpression call = PsiUtils.findFirstChildOfClassRecursive(file, FuncallExpression.class);
    Parameter fnParam = def.getParameterList().findParameterByName("arg1");
    assertThat(fnParam).isNotNull();
    assertThat(call.getReference().resolve()).isEqualTo(fnParam);
}
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) Parameter(com.google.idea.blaze.base.lang.buildfile.psi.Parameter) FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression) Test(org.junit.Test)

Example 2 with FunctionStatement

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

the class RenameRefactoringTest method testRenameFunctionParameter.

@Test
public void testRenameFunctionParameter() {
    BuildFile extFile = createBuildFile(new WorkspacePath("java/com/google/tools/build_defs.bzl"), "def function(name, deps)");
    BuildFile buildFile = createBuildFile(new WorkspacePath("java/com/google/BUILD"), "load(", "\"//java/com/google/tools:build_defs.bzl\",", "\"function\"", ")", "function(name = \"name\", deps = []");
    FunctionStatement fn = extFile.findChildByClass(FunctionStatement.class);
    Parameter param = fn.getParameterList().findParameterByName("deps");
    testFixture.renameElement(param, "exports");
    assertFileContents(extFile, "def function(name, exports)");
    assertFileContents(buildFile, "load(", "\"//java/com/google/tools:build_defs.bzl\",", "\"function\"", ")", "function(name = \"name\", exports = []");
}
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) Parameter(com.google.idea.blaze.base.lang.buildfile.psi.Parameter) Test(org.junit.Test)

Example 3 with FunctionStatement

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

the class FindParameterUsagesTest method testLocalReferences.

@Test
public void testLocalReferences() {
    BuildFile buildFile = createBuildFile(new WorkspacePath("java/com/google/build_defs.bzl"), "def function(arg1, arg2)", "function(arg1 = 1, arg2 = \"name\")");
    FunctionStatement fn = buildFile.findChildByClass(FunctionStatement.class);
    ParameterList params = fn.getParameterList();
    PsiReference[] references = FindUsages.findAllReferences(params.findParameterByName("arg1"));
    assertThat(references).hasLength(1);
    references = FindUsages.findAllReferences(params.findParameterByName("arg2"));
    assertThat(references).hasLength(1);
}
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) ParameterList(com.google.idea.blaze.base.lang.buildfile.psi.ParameterList) PsiReference(com.intellij.psi.PsiReference) Test(org.junit.Test)

Example 4 with FunctionStatement

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

the class FunctionStatementUsagesTest method testLocalReferences.

@Test
public void testLocalReferences() {
    BuildFile buildFile = createBuildFile(new WorkspacePath("java/com/google/build_defs.bzl"), "def function(name, srcs, deps):", "    # function body", "function(name = \"foo\")");
    FunctionStatement funcDef = buildFile.findChildByClass(FunctionStatement.class);
    PsiReference[] references = FindUsages.findAllReferences(funcDef);
    assertThat(references).hasLength(1);
    PsiElement ref = references[0].getElement();
    assertThat(ref).isInstanceOf(FuncallExpression.class);
}
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) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) Test(org.junit.Test)

Example 5 with FunctionStatement

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

the class ArgumentReference method resolveFunction.

@Nullable
protected FunctionStatement resolveFunction() {
    FuncallExpression call = PsiTreeUtil.getParentOfType(myElement, FuncallExpression.class);
    if (call == null) {
        return null;
    }
    PsiElement callee = call.getReferencedElement();
    return callee instanceof FunctionStatement ? (FunctionStatement) callee : null;
}
Also used : FunctionStatement(com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement) FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression) PsiElement(com.intellij.psi.PsiElement) Nullable(javax.annotation.Nullable)

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