Search in sources :

Example 21 with FuncallExpression

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

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

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

the class BuildReferenceManager method resolveLabel.

/**
 * Finds the PSI element associated with the given label.
 */
@Nullable
public PsiElement resolveLabel(Label label, boolean excludeRules) {
    File packageDir = WorkspaceHelper.resolveBlazePackage(project, label);
    if (packageDir == null) {
        return null;
    }
    String targetName = label.targetName().toString();
    if (targetName.equals("__pkg__")) {
        return findBuildFile(packageDir);
    }
    if (!excludeRules) {
        FuncallExpression target = findRule(packageDir, targetName);
        if (target != null) {
            return target;
        }
    }
    // try a direct file reference (e.g. ":a.java")
    File fullFile = new File(packageDir, targetName);
    if (FileOperationProvider.getInstance().exists(fullFile)) {
        return resolveFile(fullFile);
    }
    return null;
}
Also used : FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression) VirtualFile(com.intellij.openapi.vfs.VirtualFile) BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) Nullable(javax.annotation.Nullable)

Example 24 with FuncallExpression

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

the class LabelReference method getRuleLookups.

private BuildLookupElement[] getRuleLookups(String labelString) {
    if (labelString.endsWith("/") || (labelString.startsWith("/") && !labelString.contains(":")) || skylarkExtensionReference(myElement)) {
        return BuildLookupElement.EMPTY_ARRAY;
    }
    String packagePrefix = LabelUtils.getPackagePathComponent(labelString);
    BuildFile referencedBuildFile = LabelUtils.getReferencedBuildFile(myElement.getContainingFile(), packagePrefix);
    if (referencedBuildFile == null) {
        return BuildLookupElement.EMPTY_ARRAY;
    }
    String self = null;
    if (referencedBuildFile == myElement.getContainingFile()) {
        FuncallExpression funcall = PsiUtils.getParentOfType(myElement, FuncallExpression.class, true);
        if (funcall != null) {
            self = funcall.getName();
        }
    }
    return LabelRuleLookupElement.collectAllRules(referencedBuildFile, labelString, packagePrefix, self, myElement.getQuoteType());
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression)

Example 25 with FuncallExpression

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

the class BuildReferenceSearcher method processQuery.

@Override
public void processQuery(SearchParameters params, Processor<PsiReference> consumer) {
    PsiElement element = params.getElementToSearch();
    if (element instanceof NamedBuildElement) {
        String fnName = ((NamedBuildElement) element).getName();
        if (fnName != null) {
            searchForString(params, element, fnName);
        }
        return;
    }
    PsiFile file = ResolveUtil.asFileSearch(element);
    if (file != null) {
        processFileReferences(params, file);
        return;
    }
    if (!(element instanceof FuncallExpression)) {
        return;
    }
    FuncallExpression funcall = (FuncallExpression) element;
    PsiFile localFile = element.getContainingFile();
    if (localFile == null) {
        return;
    }
    Label label = funcall.resolveBuildLabel();
    if (label == null) {
        searchForExternalWorkspace(params, localFile, funcall);
        return;
    }
    List<String> stringsToSearch = LabelUtils.getAllValidLabelStrings(label, true);
    for (String string : stringsToSearch) {
        if (LabelUtils.isAbsolute(string)) {
            searchForString(params, element, string);
        } else {
            // only a valid reference from local package -- restrict the search scope accordingly
            SearchScope scope = limitScopeToFile(params.getScopeDeterminedByUser(), localFile);
            if (scope != null) {
                searchForString(params, scope, element, string);
            }
        }
    }
}
Also used : NamedBuildElement(com.google.idea.blaze.base.lang.buildfile.psi.NamedBuildElement) Label(com.google.idea.blaze.base.model.primitives.Label) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiFile(com.intellij.psi.PsiFile) FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression) PsiElement(com.intellij.psi.PsiElement)

Aggregations

FuncallExpression (com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression)44 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)33 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)32 Test (org.junit.Test)32 PsiElement (com.intellij.psi.PsiElement)13 StringLiteral (com.google.idea.blaze.base.lang.buildfile.psi.StringLiteral)10 PsiReference (com.intellij.psi.PsiReference)10 Argument (com.google.idea.blaze.base.lang.buildfile.psi.Argument)7 FunctionStatement (com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement)6 PsiFile (com.intellij.psi.PsiFile)6 Nullable (javax.annotation.Nullable)5 TargetExpression (com.google.idea.blaze.base.lang.buildfile.psi.TargetExpression)4 AssignmentStatement (com.google.idea.blaze.base.lang.buildfile.psi.AssignmentStatement)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 TargetMap (com.google.idea.blaze.base.ideinfo.TargetMap)2 Expression (com.google.idea.blaze.base.lang.buildfile.psi.Expression)2 LoadedSymbol (com.google.idea.blaze.base.lang.buildfile.psi.LoadedSymbol)2 ReferenceExpression (com.google.idea.blaze.base.lang.buildfile.psi.ReferenceExpression)2 BlazeProjectData (com.google.idea.blaze.base.model.BlazeProjectData)2 MockBlazeProjectDataManager (com.google.idea.blaze.base.model.MockBlazeProjectDataManager)2