Search in sources :

Example 1 with LoadedSymbol

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

the class CompletionResultsProcessor method process.

@Override
public boolean process(BuildElement buildElement) {
    if (buildElement == originalElement) {
        return true;
    }
    if (buildElement instanceof LoadedSymbol) {
        LoadedSymbol loadedSymbol = (LoadedSymbol) buildElement;
        String string = loadedSymbol.getSymbolString();
        results.put(string, new LoadedSymbolReferenceLookupElement(loadedSymbol, string, quoteType));
    } else if (buildElement instanceof PsiNamedElement) {
        PsiNamedElement namedElement = (PsiNamedElement) buildElement;
        String name = namedElement.getName();
        if (!allowPrivateSymbols && name != null && name.startsWith("_")) {
            return true;
        }
        results.put(name, new NamedBuildLookupElement((PsiNamedElement) buildElement, quoteType));
    }
    return true;
}
Also used : LoadedSymbol(com.google.idea.blaze.base.lang.buildfile.psi.LoadedSymbol) PsiNamedElement(com.intellij.psi.PsiNamedElement)

Example 2 with LoadedSymbol

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

the class ResolveUtil method findInScope.

/**
 * Walks up PSI tree of local file, checking PsiNamedElements
 */
@Nullable
public static PsiNamedElement findInScope(PsiElement element, String name) {
    PsiNamedElement[] resultHolder = new PsiNamedElement[1];
    Processor<BuildElement> processor = buildElement -> {
        if (buildElement == element) {
            return true;
        }
        if (buildElement instanceof PsiNamedElement && name.equals(buildElement.getName())) {
            resultHolder[0] = (PsiNamedElement) buildElement;
            return false;
        } else if (buildElement instanceof LoadedSymbol) {
            LoadedSymbol loadedSymbol = (LoadedSymbol) buildElement;
            if (name.equals(loadedSymbol.getSymbolString())) {
                PsiElement referencedElement = loadedSymbol.getVisibleElement();
                if (referencedElement instanceof PsiNamedElement) {
                    resultHolder[0] = (PsiNamedElement) referencedElement;
                    return false;
                }
            }
        }
        return true;
    };
    searchInScope(element, processor);
    return resultHolder[0];
}
Also used : TargetExpression(com.google.idea.blaze.base.lang.buildfile.psi.TargetExpression) AssignmentStatement(com.google.idea.blaze.base.lang.buildfile.psi.AssignmentStatement) FunctionStatement(com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement) Parameter(com.google.idea.blaze.base.lang.buildfile.psi.Parameter) ForStatement(com.google.idea.blaze.base.lang.buildfile.psi.ForStatement) StatementList(com.google.idea.blaze.base.lang.buildfile.psi.StatementList) Expression(com.google.idea.blaze.base.lang.buildfile.psi.Expression) BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) Processor(com.intellij.util.Processor) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) LoadedSymbol(com.google.idea.blaze.base.lang.buildfile.psi.LoadedSymbol) PsiNamedElement(com.intellij.psi.PsiNamedElement) BuildElement(com.google.idea.blaze.base.lang.buildfile.psi.BuildElement) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) Nullable(javax.annotation.Nullable) PsiNamedElement(com.intellij.psi.PsiNamedElement) LoadedSymbol(com.google.idea.blaze.base.lang.buildfile.psi.LoadedSymbol) PsiElement(com.intellij.psi.PsiElement) BuildElement(com.google.idea.blaze.base.lang.buildfile.psi.BuildElement) Nullable(javax.annotation.Nullable)

Example 3 with LoadedSymbol

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

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

the class LoadedSkylarkExtensionTest method testAliasedFuncallReference.

@Test
public void testAliasedFuncallReference() {
    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', newName = 'function'),", "newName(name = \"name\", deps = []");
    LoadedSymbol loadedSymbol = PsiUtils.findFirstChildOfClassRecursive(buildFile, LoadedSymbol.class);
    FuncallExpression funcall = buildFile.firstChildOfClass(FuncallExpression.class);
    assertThat(funcall.getReferencedElement()).isEqualTo(loadedSymbol.getVisibleElement());
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) LoadedSymbol(com.google.idea.blaze.base.lang.buildfile.psi.LoadedSymbol) FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression) Test(org.junit.Test)

Example 5 with LoadedSymbol

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

Aggregations

LoadedSymbol (com.google.idea.blaze.base.lang.buildfile.psi.LoadedSymbol)5 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)4 TargetExpression (com.google.idea.blaze.base.lang.buildfile.psi.TargetExpression)3 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)3 Test (org.junit.Test)3 FuncallExpression (com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression)2 PsiNamedElement (com.intellij.psi.PsiNamedElement)2 AssignmentStatement (com.google.idea.blaze.base.lang.buildfile.psi.AssignmentStatement)1 BuildElement (com.google.idea.blaze.base.lang.buildfile.psi.BuildElement)1 Expression (com.google.idea.blaze.base.lang.buildfile.psi.Expression)1 ForStatement (com.google.idea.blaze.base.lang.buildfile.psi.ForStatement)1 FunctionStatement (com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement)1 Parameter (com.google.idea.blaze.base.lang.buildfile.psi.Parameter)1 ReferenceExpression (com.google.idea.blaze.base.lang.buildfile.psi.ReferenceExpression)1 StatementList (com.google.idea.blaze.base.lang.buildfile.psi.StatementList)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)1 Processor (com.intellij.util.Processor)1 Nullable (javax.annotation.Nullable)1