Search in sources :

Example 11 with FuncallExpression

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

the class LabelReferenceTest method testRulePreferredOverFile.

@Test
public void testRulePreferredOverFile() {
    BuildFile targetFile = createBuildFile(new WorkspacePath("java/com/foo/BUILD"), "java_library(name = 'lib')");
    workspace.createDirectory(new WorkspacePath("java/com/foo/lib"));
    BuildFile referencingFile = createBuildFile(new WorkspacePath("java/com/google/bar/BUILD"), "java_library(", "    name = 'bar',", "    src = glob(['**/*.java'])," + "    deps = ['//java/com/foo:lib'],", ")");
    FuncallExpression target = targetFile.findRule("lib");
    assertThat(target).isNotNull();
    PsiReference[] references = FindUsages.findAllReferences(target);
    assertThat(references).hasLength(1);
    PsiElement element = references[0].getElement();
    FuncallExpression rule = PsiUtils.getParentOfType(element, FuncallExpression.class, true);
    assertThat(rule.getName()).isEqualTo("bar");
    assertThat(rule.getContainingFile()).isEqualTo(referencingFile);
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) 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 FuncallExpression

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

the class FindRuleUsagesTest method testLocalReferences.

@Test
public void testLocalReferences() {
    BuildFile buildFile = createBuildFile(new WorkspacePath("java/com/google/BUILD"), "java_library(name = \"target\")", "top_level_ref = \":target\"", "java_library(name = \"other\", deps = [\":target\"]");
    FuncallExpression target = buildFile.findChildByClass(FuncallExpression.class);
    PsiReference[] references = FindUsages.findAllReferences(target);
    assertThat(references).hasLength(2);
    PsiElement firstRef = references[0].getElement();
    assertThat(firstRef).isInstanceOf(StringLiteral.class);
    assertThat(firstRef.getParent()).isInstanceOf(AssignmentStatement.class);
    PsiElement secondRef = references[1].getElement();
    assertThat(secondRef).isInstanceOf(StringLiteral.class);
    assertThat(secondRef.getParent()).isInstanceOf(ListLiteral.class);
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) 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 13 with FuncallExpression

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

the class FindRuleUsagesTest method testInvalidReferenceDoesntResolve.

@Test
public void testInvalidReferenceDoesntResolve() {
    // reference ":target" from another build file (missing package path in label)
    BuildFile targetFile = createBuildFile(new WorkspacePath("java/com/google/foo/BUILD"), "java_library(name = \"target\")");
    createBuildFile(new WorkspacePath("java/com/google/bar/BUILD"), "java_library(name = \"ref\", exports = [\":target\"])");
    FuncallExpression target = targetFile.findChildByClass(FuncallExpression.class);
    assertThat(target).isNotNull();
    PsiReference[] references = FindUsages.findAllReferences(target);
    assertThat(references).hasLength(0);
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) PsiReference(com.intellij.psi.PsiReference) FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression) Test(org.junit.Test)

Example 14 with FuncallExpression

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

the class FindRuleUsagesTest method testNonLocalReferences.

@Test
public void testNonLocalReferences() {
    BuildFile targetFile = createBuildFile(new WorkspacePath("java/com/google/foo/BUILD"), "java_library(name = \"target\")");
    BuildFile refFile = createBuildFile(new WorkspacePath("java/com/google/bar/BUILD"), "java_library(name = \"ref\", exports = [\"//java/com/google/foo:target\"])");
    FuncallExpression target = targetFile.findChildByClass(FuncallExpression.class);
    PsiReference[] references = FindUsages.findAllReferences(target);
    assertThat(references).hasLength(1);
    PsiElement ref = references[0].getElement();
    assertThat(ref).isInstanceOf(StringLiteral.class);
    assertThat(ref.getContainingFile()).isEqualTo(refFile);
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) 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 15 with FuncallExpression

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

the class CopyBlazeTargetPathAction method getTargetBuildingFile.

/**
 * Find a BUILD target building the selected file, if relevant.
 */
@Nullable
private static Label getTargetBuildingFile(Project project, AnActionEvent e) {
    VirtualFile vf = e.getData(CommonDataKeys.VIRTUAL_FILE);
    BlazePackage parentPackage = BuildFileUtils.getBuildFile(project, vf);
    if (parentPackage == null) {
        return null;
    }
    PsiElement target = BuildFileUtils.findBuildTarget(project, parentPackage, new File(vf.getPath()));
    return target instanceof FuncallExpression ? ((FuncallExpression) target).resolveBuildLabel() : null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) BlazePackage(com.google.idea.blaze.base.lang.buildfile.search.BlazePackage) FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) PsiElement(com.intellij.psi.PsiElement) Nullable(javax.annotation.Nullable)

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