Search in sources :

Example 1 with Parameter

use of com.google.idea.blaze.base.lang.buildfile.psi.Parameter 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 Parameter

use of com.google.idea.blaze.base.lang.buildfile.psi.Parameter 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 Parameter

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

the class KeywordArgumentReference method resolve.

/**
 * Find the referenced function. If it has a keyword parameter with matching name, return that.
 * Otherwise if it has a **kwargs param, return that. Else return the function itself.
 */
@Nullable
@Override
public PsiElement resolve() {
    String keyword = myElement.getName();
    if (keyword == null) {
        return null;
    }
    FunctionStatement function = resolveFunction();
    if (function == null) {
        return null;
    }
    Parameter.StarStar kwargsParameter = null;
    for (Parameter param : function.getParameters()) {
        if (param instanceof Parameter.StarStar) {
            kwargsParameter = (Parameter.StarStar) param;
            continue;
        }
        if (keyword.equals(param.getName())) {
            return param;
        }
    }
    if (kwargsParameter != null) {
        return kwargsParameter;
    }
    return null;
}
Also used : FunctionStatement(com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement) Parameter(com.google.idea.blaze.base.lang.buildfile.psi.Parameter) Nullable(javax.annotation.Nullable)

Example 4 with Parameter

use of com.google.idea.blaze.base.lang.buildfile.psi.Parameter 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 5 with Parameter

use of com.google.idea.blaze.base.lang.buildfile.psi.Parameter 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)

Aggregations

Parameter (com.google.idea.blaze.base.lang.buildfile.psi.Parameter)6 FunctionStatement (com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement)5 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)3 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)2 PsiElement (com.intellij.psi.PsiElement)2 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)2 Test (org.junit.Test)2 NamedBuildLookupElement (com.google.idea.blaze.base.lang.buildfile.completion.NamedBuildLookupElement)1 Argument (com.google.idea.blaze.base.lang.buildfile.psi.Argument)1 Expression (com.google.idea.blaze.base.lang.buildfile.psi.Expression)1 ForStatement (com.google.idea.blaze.base.lang.buildfile.psi.ForStatement)1 FuncallExpression (com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression)1 StatementList (com.google.idea.blaze.base.lang.buildfile.psi.StatementList)1 TargetExpression (com.google.idea.blaze.base.lang.buildfile.psi.TargetExpression)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 TextRange (com.intellij.openapi.util.TextRange)1 Nullable (javax.annotation.Nullable)1