Java Code Examples for com.intellij.psi.util.PsiTreeUtil#treeWalkUp()

The following examples show how to use com.intellij.psi.util.PsiTreeUtil#treeWalkUp() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommandNameCompletionProvider.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@NotNull
private Collection<LookupElement> collectFunctions(PsiElement lookupElement, int groupId) {
    BashFunctionVariantsProcessor processor = new BashFunctionVariantsProcessor(lookupElement);
    PsiTreeUtil.treeWalkUp(processor, lookupElement, BashPsiUtils.findFileContext(lookupElement), ResolveState.initial());

    return CompletionProviderUtils.createFromPsiItems(processor.getFunctionDefs(), BashIcons.FUNCTION_ICON, groupId);
}
 
Example 2
Source File: VariableNameCompletionProvider.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
private int addCollectedVariables(PsiElement element, CompletionResultSet result, BashVarCollectorProcessor processor) {
    PsiTreeUtil.treeWalkUp(processor, element, BashPsiUtils.findFileContext(element), ResolveState.initial());

    Collection<LookupElement> items = CompletionProviderUtils.createFromPsiItems(processor.getVariables(), BashIcons.VAR_ICON, CompletionGrouping.NormalVar.ordinal());
    result.addAllElements(items);

    return items.size();
}
 
Example 3
Source File: GLSLReferenceBase.java    From glsl4idea with GNU Lesser General Public License v3.0 5 votes vote down vote up
@NotNull
public Object[] getVariants() {
    List<PsiNamedElement> elements = new ArrayList<>();
    NamedElementCollector collector = new NamedElementCollector(elements);
    PsiTreeUtil.treeWalkUp(collector, source, null, ResolveState.initial());
    return elements.toArray();
}
 
Example 4
Source File: HaxeResolver.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
private List<? extends PsiElement> checkByTreeWalk(HaxeReference reference) {
  final List<PsiElement> result = new ArrayList<>();
  PsiTreeUtil.treeWalkUp(new ResolveScopeProcessor(result, reference.getText()), reference, null, new ResolveState());
  if (result.isEmpty()) return null;
  LogResolution(reference, "via tree walk.");
  return result;
}
 
Example 5
Source File: HaxeMacroUtil.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
public static Set<HaxeComponentName> findVariables(@Nullable PsiElement at) {
  if (at == null) {
    return Collections.emptySet();
  }
  final Set<HaxeComponentName> result = new THashSet<HaxeComponentName>();
  PsiTreeUtil.treeWalkUp(new PsiScopeProcessor() {
    @Override
    public boolean execute(@NotNull PsiElement element, ResolveState state) {
      if (element instanceof HaxeNamedComponent) {
        final HaxeNamedComponent haxeNamedComponent = (HaxeNamedComponent)element;
        if (haxeNamedComponent.getComponentName() != null && HaxeComponentType.isVariable(HaxeComponentType.typeOf(haxeNamedComponent))) {
          result.add(haxeNamedComponent.getComponentName());
        }
      }
      return true;
    }

    @Override
    public <T> T getHint(@NotNull Key<T> hintKey) {
      return null;
    }

    @Override
    public void handleEvent(Event event, @Nullable Object associated) {
    }
  }, at, null, ResolveState.initial());
  return result;
}
 
Example 6
Source File: HaxeRefactoringUtil.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
public static Set<String> collectUsedNames(HaxePsiCompositeElement context) {
  final Set<HaxeComponentName> usedComponentNames = new THashSet<HaxeComponentName>();
  PsiTreeUtil.treeWalkUp(new ComponentNameScopeProcessor(usedComponentNames), context, null, new ResolveState());
  return new THashSet<String>(ContainerUtil.map(usedComponentNames, new Function<HaxeComponentName, String>() {
    @Nullable
    @Override
    public String fun(HaxeComponentName componentName) {
      return componentName.getName();
    }
  }));
}
 
Example 7
Source File: VariableCollector.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
private void addProposedReferencesFromLocalScopes() {
    VariableVariantsScopeProcessor processor = new VariableVariantsScopeProcessor();
    PsiTreeUtil.treeWalkUp(processor, sourceOfReference, null, ResolveState.initial());
    List<XQueryQName<XQueryVarName>> references = processor.getProposedReferences();
    proposedReferences.addAll(convertToLookupElements(references, VARIABLES_PRIORITY));
    collectedNames.addAll(references);
}
 
Example 8
Source File: XQueryXmlNamespaceReference.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
private XQueryAttrLocalName getReferenceFromXmlUpInTheHierarchy() {
    XmlTagNamespaceReferenceScopeProcessor processor = new XmlTagNamespaceReferenceScopeProcessor<T>(myElement);
    PsiTreeUtil.treeWalkUp(processor, myElement, null, ResolveState.initial());
    if (processor.getResult() != null) {
        return processor.getResult();
    } else {
        return null;
    }
}
 
Example 9
Source File: DuplicateFunctionDefInspection.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new BashVisitor() {
        @Override
        public void visitFunctionDef(BashFunctionDef functionDef) {
            BashFunctionProcessor p = new BashFunctionProcessor(functionDef.getName(), true);

            boolean isOnGlobalLevel = BashPsiUtils.findNextVarDefFunctionDefScope(functionDef) == null;
            PsiElement start = functionDef.getContext() != null && !isOnGlobalLevel
                    ? functionDef.getContext()
                    : functionDef.getPrevSibling();

            if (start != null) {
                PsiTreeUtil.treeWalkUp(p, start, functionDef.getContainingFile(), ResolveState.initial());

                if (p.hasResults()) {
                    List<PsiElement> results = p.getResults() != null ? Lists.newArrayList(p.getResults()) : Lists.<PsiElement>newArrayList();
                    results.remove(functionDef);

                    if (!results.isEmpty()) {
                        //find the result which has the lowest textOffset in the file
                        PsiElement firstFunctionDef = results.get(0);
                        for (PsiElement e : results) {
                            if (e.getTextOffset() < firstFunctionDef.getTextOffset()) {
                                firstFunctionDef = e;
                            }
                        }

                        if (firstFunctionDef.getTextOffset() < functionDef.getTextOffset()) {
                            BashFunctionDefName nameSymbol = functionDef.getNameSymbol();

                            if (nameSymbol != null) {
                                String message = String.format("The function '%s' is already defined at line %d.",
                                        functionDef.getName(),
                                        BashPsiUtils.getElementLineNumber(firstFunctionDef));

                                holder.registerProblem(
                                        nameSymbol,
                                        message,
                                        ProblemHighlightType.GENERIC_ERROR_OR_WARNING
                                );
                            }
                        }
                    }
                }
            }
        }
    };
}
 
Example 10
Source File: CSharpRefactoringUtil.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
public static Set<PsiNamedElement> collectUsedComponents(@Nonnull PsiElement context, @Nullable PsiElement toSkip)
{
	final Set<PsiNamedElement> usedComponentNames = new THashSet<>();
	PsiTreeUtil.treeWalkUp(new ComponentNameScopeProcessor(usedComponentNames, toSkip), context, null, new ResolveState());
	return usedComponentNames;
}
 
Example 11
Source File: HaxeReferenceImpl.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public Object[] getVariants() {
  final Set<HaxeComponentName> suggestedVariants = new HashSet<>();
  final Set<HaxeComponentName> suggestedVariantsExtensions = new HashSet<>();

  // if not first in chain
  // foo.bar.baz
  final HaxeReference leftReference = HaxeResolveUtil.getLeftReference(this);
  HaxeClassResolveResult result = null;
  HaxeClass haxeClass = null;
  String name = null;
  HaxeGenericResolver resolver = null;
  if (leftReference != null) {
    result = leftReference.resolveHaxeClass();
    if (result != HaxeClassResolveResult.EMPTY) {
      haxeClass = result.getHaxeClass();
      if (haxeClass != null) {
        name = haxeClass.getName();
      }
      resolver = result.getSpecialization().toGenericResolver(haxeClass);
    }
  }

  boolean isThis = leftReference instanceof HaxeThisExpression;
  if (leftReference != null && name != null &&
      HaxeResolveUtil.splitQName(leftReference.getText()).getSecond().equals(name)) {

    if (!isInUsingStatement() && !(isInImportStatement() && (haxeClass.isEnum() || haxeClass instanceof HaxeAbstractClassDeclaration))) {
      addClassStaticMembersVariants(suggestedVariants, haxeClass, !(isThis));
    }

    addChildClassVariants(suggestedVariants, haxeClass);
  } else if (leftReference != null && !result.isFunctionType()) {
    if (null == haxeClass) {
      // TODO: fix haxeClass by type inference. Use compiler code assist?!
    }
    if (haxeClass != null) {
      boolean isSuper = leftReference instanceof HaxeSuperExpression;
      PsiElement resolvedValue = leftReference.resolve();
      if (!isSuper && (resolvedValue instanceof HaxeClassDeclaration ||
                       resolvedValue instanceof HaxeAbstractClassDeclaration ||
                       resolvedValue instanceof HaxeInterfaceDeclaration ||
                       resolvedValue instanceof HaxeExternClassDeclaration)) {
        List<HaxeModel> models = HaxeProjectModel.fromElement(this).resolve(new FullyQualifiedInfo("", "Class", null, null));
        if (models != null && !models.isEmpty() && models.get(0) instanceof HaxeClassModel) {
          haxeClass = ((HaxeClassModel)models.get(0)).haxeClass;
        } else {
          haxeClass = null;
        }
      }

      addClassNonStaticMembersVariants(suggestedVariants, haxeClass, resolver,
                                       !(isThis || isSuper));
      addUsingVariants(suggestedVariants, suggestedVariantsExtensions, haxeClass, this);
    }
  } else {
    if (leftReference == null) {
      final boolean isElementInForwardMeta = HaxeAbstractForwardUtil.isElementInForwardMeta(this);
      if (isElementInForwardMeta) {
        addAbstractUnderlyingClassVariants(suggestedVariants, PsiTreeUtil.getParentOfType(this, HaxeClass.class), resolver);
      } else {
        PsiTreeUtil.treeWalkUp(new ComponentNameScopeProcessor(suggestedVariants), this, null, new ResolveState());
        addClassVariants(suggestedVariants, PsiTreeUtil.getParentOfType(this, HaxeClass.class), false);
      }
    }
  }

  Object[] variants = HaxeLookupElement.convert(result, suggestedVariants, suggestedVariantsExtensions).toArray();
  PsiElement leftTarget = leftReference != null ? leftReference.resolve() : null;

  if (leftTarget instanceof PsiPackage) {
    return ArrayUtil.mergeArrays(variants, ((PsiPackage)leftTarget).getSubPackages());
  } else if (leftTarget instanceof HaxeFile) {
    return ArrayUtil.mergeArrays(variants, ((HaxeFile)leftTarget).getClasses());
  } else if (leftReference == null) {
    PsiPackage rootPackage = JavaPsiFacade.getInstance(getElement().getProject()).findPackage("");
    return rootPackage == null ? variants : ArrayUtil.mergeArrays(variants, rootPackage.getSubPackages());
  }
  return variants;
}
 
Example 12
Source File: XQueryVariableReferenceResolver.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
private void addReferencesFromLocalScopes() {
    VariableReferenceScopeProcessor processor = new VariableReferenceScopeProcessor(myElement);
    PsiTreeUtil.treeWalkUp(processor, myElement, null, ResolveState.initial());
    if (processor.getResult() != null)
        matchingVariableNames.add(processor.getResult());
}