Search in sources :

Example 6 with FunctionStatement

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

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

the class BuildDocumentationProvider method buildDocs.

private static String buildDocs(DocStringOwner element) {
    StringBuilder docs = new StringBuilder();
    describeFile(element.getContainingFile(), docs, !(element instanceof BuildFile));
    if (element instanceof FunctionStatement) {
        describeFunction((FunctionStatement) element, docs);
    }
    StringLiteral docString = element.getDocString();
    if (docString != null) {
        docs.append(DocStringFormatter.formatDocString(docString, element));
    }
    return wrapDocInHtml(docs.toString());
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) FunctionStatement(com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement) StringLiteral(com.google.idea.blaze.base.lang.buildfile.psi.StringLiteral)

Example 8 with FunctionStatement

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

the class FindParameterUsagesTest method testNonLocalReferences.

@Test
public void testNonLocalReferences() {
    BuildFile foo = createBuildFile(new WorkspacePath("java/com/google/build_defs.bzl"), "def function(arg1, arg2)");
    BuildFile bar = createBuildFile(new WorkspacePath("java/com/google/other/BUILD"), "load(\"//java/com/google:build_defs.bzl\", \"function\")", "function(arg1 = 1, arg2 = \"name\", extra = x)");
    FunctionStatement fn = foo.findChildByClass(FunctionStatement.class);
    ParameterList params = fn.getParameterList();
    PsiReference[] references = FindUsages.findAllReferences(params.findParameterByName("arg1"));
    assertThat(references).hasLength(1);
    assertThat(references[0].getElement().getContainingFile()).isEqualTo(bar);
    references = FindUsages.findAllReferences(params.findParameterByName("arg2"));
    assertThat(references).hasLength(1);
    assertThat(references[0].getElement().getContainingFile()).isEqualTo(bar);
}
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 9 with FunctionStatement

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

the class FunctionStatementUsagesTest method testFuncallReference.

@Test
public void testFuncallReference() {
    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 function = extFile.firstChildOfClass(FunctionStatement.class);
    FuncallExpression funcall = buildFile.firstChildOfClass(FuncallExpression.class);
    PsiReference[] references = FindUsages.findAllReferences(function);
    assertThat(references).hasLength(2);
    assertThat(references[1].getElement()).isEqualTo(funcall);
}
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) FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression) Test(org.junit.Test)

Example 10 with FunctionStatement

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

the class FunctionStatementUsagesTest method testLoadedFunctionReferences.

@Test
public void testLoadedFunctionReferences() {
    BuildFile extFile = createBuildFile(new WorkspacePath("java/com/google/build_defs.bzl"), "def function(name, deps)");
    BuildFile buildFile = createBuildFile(new WorkspacePath("java/com/google/BUILD"), "load(", "\"//java/com/google:build_defs.bzl\",", "\"function\"", ")");
    FunctionStatement funcDef = extFile.findChildByClass(FunctionStatement.class);
    LoadStatement load = buildFile.firstChildOfClass(LoadStatement.class);
    PsiReference[] references = FindUsages.findAllReferences(funcDef);
    assertThat(references).hasLength(1);
    PsiElement ref = references[0].getElement();
    assertThat(ref).isInstanceOf(StringLiteral.class);
    assertThat(ref.getParent().getParent()).isEqualTo(load);
}
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) LoadStatement(com.google.idea.blaze.base.lang.buildfile.psi.LoadStatement) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) 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