Search in sources :

Example 1 with TargetExpression

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

the class LoadedSkylarkExtensionTest method testOverridenBuiltInSymbolReference.

@Test
public void testOverridenBuiltInSymbolReference() {
    setBuiltInRuleNames("java_library");
    BuildFile extFile = createBuildFile(new WorkspacePath("java/com/google/tools/build_defs.bzl"), "java_library = rule()");
    BuildFile buildFile = createBuildFile(new WorkspacePath("java/com/google/BUILD"), "load(", "\"//java/com/google/tools:build_defs.bzl\",", "\"java_library\"", ")", "java_library(name = 'name')");
    TargetExpression target = PsiUtils.findFirstChildOfClassRecursive(extFile, TargetExpression.class);
    FuncallExpression funcall = buildFile.firstChildOfClass(FuncallExpression.class);
    LoadedSymbol loadElement = PsiUtils.findFirstChildOfClassRecursive(buildFile, LoadedSymbol.class);
    assertThat(target).isNotNull();
    assertThat(funcall.getReferencedElement()).isEqualTo(target);
    assertThat(loadElement.getImport().getReferencedElement()).isEqualTo(target);
    assertThat(Arrays.stream(FindUsages.findAllReferences(target)).map(PsiReference::getElement).collect(Collectors.toList())).containsExactly(funcall, loadElement.getImport());
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) LoadedSymbol(com.google.idea.blaze.base.lang.buildfile.psi.LoadedSymbol) TargetExpression(com.google.idea.blaze.base.lang.buildfile.psi.TargetExpression) FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression) Test(org.junit.Test)

Example 2 with TargetExpression

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

the class LoadedSkylarkExtensionTest method testLoadedSymbolReference.

@Test
public void testLoadedSymbolReference() {
    BuildFile extFile = createBuildFile(new WorkspacePath("java/com/google/tools/build_defs.bzl"), "CONSTANT = 1");
    BuildFile buildFile = createBuildFile(new WorkspacePath("java/com/google/BUILD"), "load(", "\"//java/com/google/tools:build_defs.bzl\",", "\"CONSTANT\"", ")", "NEW_CONSTANT = CONSTANT");
    TargetExpression target = PsiUtils.findFirstChildOfClassRecursive(extFile, TargetExpression.class);
    ReferenceExpression ref = PsiUtils.findFirstChildOfClassRecursive(buildFile, ReferenceExpression.class);
    LoadedSymbol loadElement = PsiUtils.findFirstChildOfClassRecursive(buildFile, LoadedSymbol.class);
    assertThat(target).isNotNull();
    assertThat(ref.getReferencedElement()).isEqualTo(target);
    assertThat(loadElement.getImport().getReferencedElement()).isEqualTo(target);
    assertThat(Arrays.stream(FindUsages.findAllReferences(target)).map(PsiReference::getElement).collect(Collectors.toList())).containsExactly(ref, loadElement.getImport());
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) LoadedSymbol(com.google.idea.blaze.base.lang.buildfile.psi.LoadedSymbol) TargetExpression(com.google.idea.blaze.base.lang.buildfile.psi.TargetExpression) ReferenceExpression(com.google.idea.blaze.base.lang.buildfile.psi.ReferenceExpression) Test(org.junit.Test)

Example 3 with TargetExpression

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

the class RenameRefactoringTest method testRenameLocalVariable.

@Test
public void testRenameLocalVariable() {
    BuildFile file = createBuildFile(new WorkspacePath("java/com/google/BUILD"), "a = 1", "c = a");
    TargetExpression target = PsiUtils.findFirstChildOfClassRecursive(file, TargetExpression.class);
    assertThat(target.getText()).isEqualTo("a");
    testFixture.renameElement(target, "b");
    assertFileContents(file, "b = 1", "c = b");
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) TargetExpression(com.google.idea.blaze.base.lang.buildfile.psi.TargetExpression) Test(org.junit.Test)

Example 4 with TargetExpression

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

the class LocalVariableUsagesTest method testMultipleAssignments.

// the case where a symbol is the target of multiple assignment statements
@Test
public void testMultipleAssignments() {
    BuildFile buildFile = createBuildFile(new WorkspacePath("java/com/google/BUILD"), "var = 5", "var += 1", "var = 0");
    TargetExpression target = buildFile.findChildByClass(AssignmentStatement.class).getLeftHandSideExpression();
    PsiReference[] references = FindUsages.findAllReferences(target);
    assertThat(references).hasLength(2);
    assertThat(references[0]).isInstanceOf(LocalReference.class);
    assertThat(references[1]).isInstanceOf(TargetReference.class);
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) 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) Test(org.junit.Test)

Example 5 with TargetExpression

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

Aggregations

BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)8 TargetExpression (com.google.idea.blaze.base.lang.buildfile.psi.TargetExpression)8 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)7 Test (org.junit.Test)7 AssignmentStatement (com.google.idea.blaze.base.lang.buildfile.psi.AssignmentStatement)4 FuncallExpression (com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression)4 ReferenceExpression (com.google.idea.blaze.base.lang.buildfile.psi.ReferenceExpression)3 FunctionStatement (com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement)2 LoadedSymbol (com.google.idea.blaze.base.lang.buildfile.psi.LoadedSymbol)2 PsiElement (com.intellij.psi.PsiElement)2 PsiReference (com.intellij.psi.PsiReference)2 Expression (com.google.idea.blaze.base.lang.buildfile.psi.Expression)1 ForStatement (com.google.idea.blaze.base.lang.buildfile.psi.ForStatement)1 Parameter (com.google.idea.blaze.base.lang.buildfile.psi.Parameter)1 StatementList (com.google.idea.blaze.base.lang.buildfile.psi.StatementList)1 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)1