Java Code Examples for com.intellij.psi.PsiElement#getFirstChild()

The following examples show how to use com.intellij.psi.PsiElement#getFirstChild() . 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: Trees.java    From antlr4-intellij-adaptor with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Get all non-WS, non-Comment children of t */
@NotNull
public static PsiElement[] getChildren(PsiElement t) {
	if ( t==null ) return PsiElement.EMPTY_ARRAY;

	PsiElement psiChild = t.getFirstChild();
	if (psiChild == null) return PsiElement.EMPTY_ARRAY;

	List<PsiElement> result = new ArrayList<>();
	while (psiChild != null) {
		if ( !(psiChild instanceof PsiComment) &&
			 !(psiChild instanceof PsiWhiteSpace) )
		{
			result.add(psiChild);
		}
		psiChild = psiChild.getNextSibling();
	}
	return PsiUtilCore.toPsiElementArray(result);
}
 
Example 2
Source File: PsiPhpHelper.java    From yiistorm with MIT License 6 votes vote down vote up
/**
 * The IDE is not returning the full list of children sometimes when using "PsiElemetn.getChildren()" I don'w know why, this method fixes that
 *
 * @return
 */
@NotNull
public static List<PsiElement> getFullListOfChildren(PsiElement psiElement) {
    List<PsiElement> children = new ArrayList<PsiElement>();
    if (psiElement != null) {
        // this doesn't work as expected for example for a Method Reference it returns only Class reference and Parameter List
        // it doesn't return the scope resolution (or arrow), psiwhitespaces (if exists), identifier (method name), etc...
        // PsiElement[] children = psiElement.getChildren();
        PsiElement child = psiElement.getFirstChild();
        while (child != null) {
            children.add(child);
            child = child.getNextSibling();
        }
    }
    return children;
}
 
Example 3
Source File: CsvValidationInspection.java    From intellij-csv-validator with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new PsiElementVisitor() {
        @Override
        public void visitElement(PsiElement element) {
            if (element == null || !holder.getFile().getLanguage().isKindOf(CsvLanguage.INSTANCE)) {
                return;
            }

            IElementType elementType = CsvHelper.getElementType(element);
            PsiElement firstChild = element.getFirstChild();
            PsiElement nextSibling = element.getNextSibling();
            if (elementType == TokenType.ERROR_ELEMENT && firstChild != null && element.getText().equals(firstChild.getText())) {
                CsvValidationInspection.this.registerError(holder, element, UNESCAPED_SEQUENCE, fixUnescapedSequence);
                if (!"\"".equals(firstChild.getText())) {
                    CsvValidationInspection.this.registerError(holder, element, SEPARATOR_MISSING, fixSeparatorMissing);
                }
            } else if ((elementType == CsvTypes.TEXT || elementType == CsvTypes.ESCAPED_TEXT) &&
                    CsvHelper.getElementType(nextSibling) == TokenType.ERROR_ELEMENT &&
                    nextSibling.getFirstChild() == null) {
                CsvValidationInspection.this.registerError(holder, element, CLOSING_QUOTE_MISSING, fixClosingQuoteMissing);
            }
        }
    };
}
 
Example 4
Source File: BuckCopyPasteProcessor.java    From Buck-IntelliJ-Plugin with Apache License 2.0 6 votes vote down vote up
protected boolean checkPropertyName(PsiElement property) {
  if (property == null) {
    return false;
  }
  PsiElement leftValue = property.getFirstChild();
  if (leftValue == null || leftValue.getNode().getElementType() != BuckTypes.PROPERTY_LVALUE) {
    return false;
  }
  leftValue = leftValue.getFirstChild();
  if (leftValue == null || leftValue.getNode().getElementType() != BuckTypes.IDENTIFIER) {
    return false;
  }
  if (leftValue.getText().equals("deps") ||
      leftValue.getText().equals("visibility")) {
    return true;
  }
  return false;
}
 
Example 5
Source File: AbstractMatchingVisitor.java    From consulo with Apache License 2.0 6 votes vote down vote up
public final boolean matchSonsInAnyOrder(PsiElement element1, PsiElement element2) {
  if (element1 == null && isLeftLooseMatching()) {
    return true;
  }
  if (element2 == null && isRightLooseMatching()) {
    return true;
  }
  if (element1 == null || element2 == null) {
    return element1 == element2;
  }
  PsiElement e = element1.getFirstChild();
  PsiElement e2 = element2.getFirstChild();
  return (e == null && isLeftLooseMatching()) ||
         (e2 == null && isRightLooseMatching()) ||
         matchInAnyOrder(new FilteringNodeIterator(new SiblingNodeIterator(e), getNodeFilter()),
                         new FilteringNodeIterator(new SiblingNodeIterator(e2), getNodeFilter()));
}
 
Example 6
Source File: BashAnnotator.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
private void annotateWord(PsiElement bashWord, AnnotationHolder annotationHolder) {
    //we have to mark the remapped tokens (which are words now) to have the default word formatting.
    PsiElement child = bashWord.getFirstChild();

    while (child != null && false) {
        if (!noWordHighlightErase.contains(child.getNode().getElementType())) {
            Annotation annotation = annotationHolder.createInfoAnnotation(child, null);
            annotation.setEnforcedTextAttributes(TextAttributes.ERASE_MARKER);

            annotation = annotationHolder.createInfoAnnotation(child, null);
            annotation.setEnforcedTextAttributes(EditorColorsManager.getInstance().getGlobalScheme().getAttributes(HighlighterColors.TEXT));
        }

        child = child.getNextSibling();
    }
}
 
Example 7
Source File: PsiUtil.java    From arma-intellij-plugin with MIT License 6 votes vote down vote up
private static <E extends PsiElement> void findDescendantElementsOfInstance(@NotNull PsiElement rootElement,
																			@NotNull Class<?> type,
																			@Nullable PsiElement cursor,
																			@Nullable String textContent,
																			@NotNull List<E> list) {
	PsiElement child = rootElement.getFirstChild();
	while (child != null) {
		if (cursor != null && child == cursor) {
			continue;
		}
		if (type.isAssignableFrom(child.getClass()) && (textContent == null || child.getText().equals(textContent))) {
			list.add((E) child);
		}
		findDescendantElementsOfInstance(child, type, cursor, textContent, list);
		child = child.getNextSibling();
	}
}
 
Example 8
Source File: BuckBuildUtil.java    From Buck-IntelliJ-Plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Get the value of a property in a specific buck rule body.
 */
public static String getPropertyValue(BuckRuleBody body, String name) {
  if (body == null) {
    return null;
  }
  PsiElement[] children = body.getChildren();
  for (PsiElement child : children) {
    if (BuckPsiUtils.testType(child, BuckTypes.PROPERTY)) {
      PsiElement lvalue = child.getFirstChild();
      PsiElement propertyName = lvalue.getFirstChild();
      if (propertyName != null && propertyName.getText().equals(name)) {
        BuckExpression expression =
            (BuckExpression) BuckPsiUtils.findChildWithType(child, BuckTypes.EXPRESSION);
        return expression != null ? BuckPsiUtils.getStringValueFromExpression(expression) : null;
      }
    }
  }
  return null;
}
 
Example 9
Source File: PsiElementUtils.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public static <T extends PsiElement> T getChildrenOfType(@Nullable PsiElement element, ElementPattern<T> pattern) {
    if (element == null) return null;

    for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (pattern.accepts(child)) {
            //noinspection unchecked
            return (T)child;
        }
    }

    return null;
}
 
Example 10
Source File: ORUtil.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
@Nullable
public static <T extends PsiElement> T findImmediateFirstChildOfClass(@NotNull PsiElement element, @NotNull Class<T> clazz) {
    PsiElement child = element.getFirstChild();

    while (child != null) {
        if (clazz.isInstance(child)) {
            return (T) child;
        }
        child = child.getNextSibling();
    }

    return null;
}
 
Example 11
Source File: BashFileReferenceManipulator.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public TextRange getRangeInElement(@NotNull PsiElement element) {
    PsiElement firstChild = element.getFirstChild();
    if (firstChild instanceof BashCharSequence) {
        return ((BashCharSequence) firstChild).getTextContentRange().shiftRight(firstChild.getStartOffsetInParent());
    }

    return TextRange.from(0, element.getTextLength());
}
 
Example 12
Source File: ModeSpecNode.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public GrammarElementRefNode getId() {
    PsiElement idNode = findFirstChildOfType(this, ANTLRv4TokenTypes.getRuleElementType(ANTLRv4Parser.RULE_identifier));

    if (idNode != null) {
        PsiElement firstChild = idNode.getFirstChild();

        if (firstChild instanceof GrammarElementRefNode) {
            return (GrammarElementRefNode) firstChild;
        }
    }

    return null;
}
 
Example 13
Source File: TreeHasherBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private TreeHashResult[] computeHashesForChildren(PsiElement element,
                                                  PsiFragment parentFragment,
                                                  NodeSpecificHasher nodeSpecificHasher) {
  final List<TreeHashResult> result = new ArrayList<>();

  for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
    final TreeHashResult childResult = hash(element, parentFragment, nodeSpecificHasher);
    result.add(childResult);
  }
  return result.toArray(new TreeHashResult[0]);
}
 
Example 14
Source File: BuckPsiUtils.java    From Buck-IntelliJ-Plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Return the text content if the given BuckExpression has only one string value.
 * Return null if this expression has multiple values, for example: "a" + "b"
 */
public static String getStringValueFromExpression(BuckExpression expression) {
  List<BuckValue> values = expression.getValueList();
  if (values.size() != 1) {
    return null;
  }
  PsiElement value = values.get(0); // This should be a "Value" type.
  if (value.getFirstChild() != null &&
      BuckPsiUtils.hasElementType(value.getFirstChild(), BuckPsiUtils.STRING_LITERALS)) {
    String text = value.getFirstChild().getText();
    return text.length() > 2 ? text.substring(1, text.length() - 1) : null;
  } else {
    return null;
  }
}
 
Example 15
Source File: PhpElementsUtil.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Nullable
static public AnnotationTarget findAnnotationTarget(@Nullable PhpDocComment phpDocComment) {
    if(phpDocComment == null) {
        return null;
    }

    if(phpDocComment.getNextPsiSibling() instanceof Method) {
        return AnnotationTarget.METHOD;
    }

    if(PlatformPatterns.psiElement(PhpElementTypes.CLASS_FIELDS).accepts(phpDocComment.getNextPsiSibling())) {
        return AnnotationTarget.PROPERTY;
    }

    if(phpDocComment.getNextPsiSibling() instanceof PhpClass) {
        return AnnotationTarget.CLASS;
    }

    // workaround: if file has no use statements all is wrapped inside a group
    if(phpDocComment.getNextPsiSibling() instanceof GroupStatement) {
        PsiElement groupStatement =  phpDocComment.getNextPsiSibling();
        if(groupStatement != null && groupStatement.getFirstChild() instanceof PhpClass) {
            return AnnotationTarget.CLASS;
        }
    }

    return null;
}
 
Example 16
Source File: BashFileImpl.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
private static void collectNestedFunctionDefinitions(PsiElement parent, List<BashFunctionDef> target) {
    for (PsiElement e = parent.getFirstChild(); e != null; e = e.getNextSibling()) {
        if (e instanceof BashFunctionDef) {
            target.add((BashFunctionDef) e);
        }

        collectNestedFunctionDefinitions(e, target);
    }
}
 
Example 17
Source File: PsiUtil.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
@Nullable
public static <T extends PsiElement> T findFirstDescendantElement(@NotNull PsiElement element, @NotNull Class<T> type) {
	PsiElement child = element.getFirstChild();
	while (child != null) {
		if (type.isInstance(child)) {
			return (T) child;
		}
		T e = findFirstDescendantElement(child, type);
		if (e != null) {
			return e;
		}
		child = child.getNextSibling();
	}
	return null;
}
 
Example 18
Source File: SneakyThrowsTest.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static PsiMethodCallExpression findMethodCall(PsiElement element) {
  if (element instanceof PsiMethodCallExpression) {
    return (PsiMethodCallExpression) element;
  }

  for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
    final PsiMethodCallExpression call = findMethodCall(child);
    if (call != null) {
      return call;
    }
  }

  return null;
}
 
Example 19
Source File: IgnoreDirectoryMarkerProvider.java    From idea-gitignore with MIT License 5 votes vote down vote up
/**
 * Returns {@link LineMarkerInfo} with set {@link PlatformIcons#FOLDER_ICON} if entry points to the directory.
 *
 * @param element current element
 * @return <code>null</code> if entry is not a directory
 */
@Nullable
@Override
public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement element) {
    if (!(element instanceof IgnoreEntryFile)) {
        return null;
    }

    boolean isDirectory = element instanceof IgnoreEntryDirectory;

    if (!isDirectory) {
        final String key = element.getText();
        if (cache.containsKey(key)) {
            isDirectory = cache.get(key);
        } else {
            final IgnoreEntryFile entry = (IgnoreEntryFile) element;
            final VirtualFile parent = element.getContainingFile().getVirtualFile().getParent();
            if (parent == null) {
                return null;
            }

            final Project project = element.getProject();
            final Module module = Utils.getModuleForFile(parent, project);
            if (module == null) {
                return null;
            }

            final MatcherUtil matcher = IgnoreManager.getInstance(project).getMatcher();
            final VirtualFile file = Glob.findOne(parent, entry, matcher);
            cache.put(key, isDirectory = file != null && file.isDirectory());
        }
    }

    if (isDirectory) {
        return new LineMarkerInfo<>(element.getFirstChild(), element.getTextRange(),
                PlatformIcons.FOLDER_ICON, null, null, GutterIconRenderer.Alignment.CENTER);
    }
    return null;
}
 
Example 20
Source File: GoogleTestLocation.java    From intellij with Apache License 2.0 5 votes vote down vote up
private void visitRecursively(PsiElement element) {
  PsiElement child = element.getFirstChild();
  while (child != null && !foundGtestMacroCall) {
    child.accept(this);
    child = child.getNextSibling();
  }
}