private PsiElement getNextElement(PsiElement element) { PsiElement next = element.getNextSibling(); while (next != null && next instanceof PsiWhiteSpace) { next = next.getNextSibling(); } return next; } }
public static boolean processChildrenFromTop(@NotNull PsiElement element, @NotNull PsiScopeProcessor processor, @NotNull ResolveState substitutor, @Nullable PsiElement lastParent, @NotNull PsiElement place) { PsiElement run = element.getFirstChild(); while (run != null) { if (run instanceof GoCompositeElement) { if (run.isEquivalentTo(lastParent)) return true; if (!run.processDeclarations(processor, substitutor, null, place)) return false; } run = run.getNextSibling(); } return true; } }
@NotNull public static PsiElement[] getTopLevelElementsInRange(@NotNull GoFile file, int startOffset, int endOffset, @NotNull Class<? extends PsiElement> clazz) { Couple<PsiElement> elementRange = getElementRange(file, startOffset, endOffset); if (elementRange == null) return PsiElement.EMPTY_ARRAY; Couple<PsiElement> topmostElementRange = getTopmostElementRange(elementRange); if (topmostElementRange == null) return PsiElement.EMPTY_ARRAY; if (!clazz.isInstance(topmostElementRange.first) || !clazz.isInstance(topmostElementRange.second)) { return PsiElement.EMPTY_ARRAY; } List<PsiElement> result = ContainerUtil.newSmartList(); PsiElement start = topmostElementRange.first; while (start != null && !start.isEquivalentTo(topmostElementRange.second)) { if (clazz.isInstance(start)) result.add(start); start = start.getNextSibling(); } result.add(topmostElementRange.second); return PsiUtilCore.toPsiElementArray(result); }
private static void addCommentFolds(@NotNull PsiElement comment, @NotNull Set<PsiElement> processedComments, @NotNull List<FoldingDescriptor> result) { if (processedComments.contains(comment)) return; PsiElement end = null; for (PsiElement current = comment.getNextSibling(); current != null; current = current.getNextSibling()) { ASTNode node = current.getNode(); if (node == null) break; IElementType elementType = node.getElementType(); if (elementType == GoParserDefinition.LINE_COMMENT) { end = current; processedComments.add(current); continue; } if (elementType == TokenType.WHITE_SPACE) continue; break; } if (end != null) { int startOffset = comment.getTextRange().getStartOffset(); int endOffset = end.getTextRange().getEndOffset(); result.add(new NamedFoldingDescriptor(comment, startOffset, endOffset, null, "/.../")); } }
private static void deleteImportSpec(@Nullable GoImportSpec importSpec) { GoImportDeclaration importDeclaration = PsiTreeUtil.getParentOfType(importSpec, GoImportDeclaration.class); if (importSpec != null && importDeclaration != null) { PsiElement startElementToDelete = importSpec; PsiElement endElementToDelete = importSpec; if (importDeclaration.getImportSpecList().size() == 1) { startElementToDelete = importDeclaration; endElementToDelete = importDeclaration; PsiElement nextSibling = endElementToDelete.getNextSibling(); if (nextSibling != null && nextSibling.getNode().getElementType() == GoTypes.SEMICOLON) { endElementToDelete = nextSibling; } } // todo: delete after proper formatter implementation PsiElement nextSibling = endElementToDelete.getNextSibling(); if (nextSibling instanceof PsiWhiteSpace && nextSibling.textContains('\n')) { endElementToDelete = nextSibling; } startElementToDelete.getParent().deleteChildRange(startElementToDelete, endElementToDelete); } }
@NotNull private static Collection<PsiComment> getCommentsToConsider(@NotNull GoFile file) { Collection<PsiComment> commentsToConsider = ContainerUtil.newArrayList(); PsiElement child = file.getFirstChild(); int lastEmptyLineOffset = 0; while (child != null) { if (child instanceof PsiComment) { commentsToConsider.add((PsiComment)child); } else if (child instanceof PsiWhiteSpace) { if (StringUtil.countChars(child.getText(), '\n') > 1) { lastEmptyLineOffset = child.getTextRange().getStartOffset(); } } else { break; } child = child.getNextSibling(); } int finalLastEmptyLineOffset = lastEmptyLineOffset; return ContainerUtil.filter(commentsToConsider, comment -> comment.getTextRange().getStartOffset() < finalLastEmptyLineOffset); } }
PsiElement importKeyword = firstImport.getImport(); int offset = importKeyword.getTextRange().getEndOffset(); int startOffset = importKeyword.getNextSibling() instanceof PsiWhiteSpace ? offset + 1 : offset; int endOffset = importList.getTextRange().getEndOffset(); if (endOffset - startOffset > 3) { PsiElement colon = caseClause.getColon(); if (colon != null && !caseClause.getStatementList().isEmpty()) { fold(caseClause, colon.getNextSibling(), caseClause, "...", result); PsiElement colon = commClause.getColon(); if (colon != null && !commClause.getStatementList().isEmpty()) { fold(commClause, colon.getNextSibling(), commClause, "...", result);
@Override public boolean accepts(@NotNull PsiElement psiElement, ProcessingContext processingContext) { return psiElement.getNextSibling() instanceof YAMLMapping; } }
@Nullable public static <T extends PsiElement> T getNextSiblingOfType(@Nullable PsiElement sibling, ElementPattern<PsiElement> pattern) { if (sibling == null) return null; for (PsiElement child = sibling.getNextSibling(); child != null; child = child.getNextSibling()) { if (pattern.accepts(child)) { //noinspection unchecked return (T)child; } } return null; }
@NotNull public static <T extends PsiElement> Collection<T> getNextSiblingOfTypes(@Nullable PsiElement sibling, ElementPattern<PsiElement> pattern) { if (sibling == null) return Collections.emptySet(); Collection<T> foo = new ArrayList<>(); for (PsiElement child = sibling.getNextSibling(); child != null; child = child.getNextSibling()) { if (pattern.accepts(child)) { //noinspection unchecked foo.add((T)child); } } return foo; }
/** * getChildren eg on YamlArray is empty, provide workaround */ static public PsiElement[] getChildrenFix(PsiElement psiElement) { List<PsiElement> psiElements = new ArrayList<>(); PsiElement startElement = psiElement.getFirstChild(); if(startElement == null) { return psiElements.toArray(new PsiElement[psiElements.size()]); } psiElements.add(startElement); for (PsiElement child = psiElement.getFirstChild().getNextSibling(); child != null; child = child.getNextSibling()) { psiElements.add(child); } return psiElements.toArray(new PsiElement[psiElements.size()]); }
@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; }
@NotNull public static <T extends PsiElement> Collection<T> getChildrenOfTypeAsList(@Nullable PsiElement element, ElementPattern<T> pattern) { Collection<T> collection = new ArrayList<>(); if (element == null) { return collection; } for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (pattern.accepts(child)) { //noinspection unchecked collection.add((T)child); } } return collection; }
@Nullable public static PsiElement getNextSiblingAndSkip(@NotNull PsiElement psiElement, @NotNull IElementType find, @NotNull IElementType... skip) { for (PsiElement child = psiElement.getNextSibling(); child != null; child = child.getNextSibling()) { if(child.getNode().getElementType() == find) { return child; } if(child instanceof PsiWhiteSpace) { continue; } if(!Arrays.asList(skip).contains(child.getNode().getElementType())) { return null; } } return null; }
annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.GLOBAL_VARIABLE); } else if (parent instanceof BallerinaTableColumn) { if (element.getText().equals("key") && element.getNextSibling() instanceof PsiWhiteSpace) { PsiElement nextElement = element.getNextSibling().getNextSibling(); if (nextElement instanceof LeafPsiElement && ((LeafPsiElement) element).getElementType() == BallerinaTypes.IDENTIFIER) {
@Nullable public static String getTwigMethodString(@Nullable PsiElement transPsiElement) { if (transPsiElement == null) return null; ElementPattern<PsiElement> pattern = PlatformPatterns.psiElement(TwigTokenTypes.RBRACE); String currentText = transPsiElement.getText(); for (PsiElement child = transPsiElement.getNextSibling(); child != null; child = child.getNextSibling()) { currentText = currentText + child.getText(); if (pattern.accepts(child)) { //noinspection unchecked return currentText; } } return null; }
public boolean process(@NotNull final PsiFile file, @NotNull final String pEventSourceName) { if (!CHECK_PACKAGE_HTML.equals(pEventSourceName)) { return true; } PsiElement currentSibling = findFirstSibling(file); while (currentSibling != null) { if (currentSibling.isPhysical() && currentSibling.isValid() && currentSibling instanceof PsiFile && PACKAGE_HTML_FILE.equals(((PsiFile) currentSibling).getName())) { return false; } currentSibling = currentSibling.getNextSibling(); } return true; }
public boolean process(@NotNull final PsiFile file, @NotNull final String pEventSourceName) { if (!CHECK_PACKAGE_INFO.equals(pEventSourceName)) { return true; } PsiElement currentSibling = findFirstSibling(file); while (currentSibling != null) { if (currentSibling.isPhysical() && currentSibling.isValid() && currentSibling instanceof PsiFile) { final String siblingName = ((PsiFile) currentSibling).getName(); if (PACKAGE_INFO_FILE.equals(siblingName) || (usingLegacyPackage && PACKAGE_HTML_FILE.equals(siblingName))) { return false; } } currentSibling = currentSibling.getNextSibling(); } return true; }
PsiElement whitespace = psiElement.getNextSibling(); if(whitespace instanceof PsiWhiteSpace) { PsiElement nextSibling = whitespace.getNextSibling(); if(nextSibling != null && nextSibling.getNode().getElementType() == TwigTokenTypes.IDENTIFIER) { String identifier = nextSibling.getText();
public static void visitRecursively(PsiElement element, BashVisitor visitor) { element.accept(visitor); // calling element.getChildren() is expensive, // better iterate over the chilren PsiElement child = element.getFirstChild(); while (child != null) { if (child.getNode() instanceof CompositeElement) { visitRecursively(child, visitor); } child = child.getNextSibling(); } }