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

The following examples show how to use com.intellij.psi.util.PsiTreeUtil#findElementOfClassAtOffset() . These examples are extracted from open source projects. 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 Project: litho   File:    License: Apache License 2.0 6 votes vote down vote up
private static Map<String, List<PsiElement>> collectRedSymbols(
    PsiFile psiFile, Document document, Project project, ProgressIndicator daemonIndicator) {
  Map<String, List<PsiElement>> redSymbolToElements = new HashMap<>();
  List<HighlightInfo> infos =
      ((DaemonCodeAnalyzerImpl) DaemonCodeAnalyzer.getInstance(project))
          .runMainPasses(psiFile, document, daemonIndicator);
  for (HighlightInfo info : infos) {
    if (!info.getSeverity().equals(HighlightSeverity.ERROR)) continue;

    final String redSymbol = document.getText(new TextRange(info.startOffset, info.endOffset));
    if (!StringUtil.isJavaIdentifier(redSymbol) || !StringUtil.isCapitalized(redSymbol)) continue;

    final PsiJavaCodeReferenceElement ref =
            psiFile, info.startOffset, PsiJavaCodeReferenceElement.class, false);
    if (ref == null) continue;

    redSymbolToElements.putIfAbsent(redSymbol, new ArrayList<>());
  return redSymbolToElements;
Example 2
Source Project: phpstorm-plugin   File:    License: MIT License 6 votes vote down vote up
protected Method getCurrentTestMethod(AnActionEvent e) {
    PhpFile file = getPhpFile(e);
    Editor editor = getEditor(e);

    if (file == null || editor == null) {
        return null;

    Method method = PsiTreeUtil.findElementOfClassAtOffset(file, editor.getCaretModel().getOffset(), Method.class, false);

    if (method != null && method.getName().startsWith("test")) {
        return method;

    return null;
Example 3
Source Project: intellij-haskforce   File:    License: Apache License 2.0 5 votes vote down vote up
/** The text range of our annotation should be based on the element at that offset. */
private TextRange getTextRange(@NotNull PsiFile psiFile) {
    final String text = psiFile.getText();
    final int offsetStart = getOffsetStart(text);
    if (offsetStart == -1) return null;
    PsiElement el = PsiTreeUtil.findElementOfClassAtOffset(psiFile, offsetStart, PsiElement.class, false);
    if (el == null) return null;
    // It's prettier to show the entire import line as unused instead of just the `import` keyword.
    if (isUnusedImport && el.getParent() instanceof HaskellImpdecl) return el.getParent().getTextRange();
    return el.getTextRange();