下面列出了com.intellij.psi.PsiElement#getNextSibling ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static PsiElement findQuotePositionsUntilSeparator(final PsiElement element, List<Integer> quotePositions, boolean stopAtEscapedTexts) {
PsiElement currentElement = element;
PsiElement separatorElement = null;
while (separatorElement == null && currentElement != null) {
if (CsvHelper.getElementType(currentElement) == CsvTypes.COMMA || CsvHelper.getElementType(currentElement) == CsvTypes.CRLF ||
(stopAtEscapedTexts && CsvHelper.getElementType(currentElement) == CsvTypes.ESCAPED_TEXT)) {
separatorElement = currentElement;
continue;
}
if (currentElement.getFirstChild() != null) {
separatorElement = findQuotePositionsUntilSeparator(currentElement.getFirstChild(), quotePositions, stopAtEscapedTexts);
} else if (currentElement.getText().equals("\"")) {
quotePositions.add(currentElement.getTextOffset());
}
currentElement = currentElement.getNextSibling();
}
return separatorElement;
}
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();
}
}
private static void removeWithNextSibling(PsiElement element, IElementType token, List<PsiElement> collectForDelete)
{
PsiElement nextSibling = element.getNextSibling();
if(nextSibling instanceof PsiWhiteSpace)
{
collectForDelete.add(nextSibling);
nextSibling = nextSibling.getNextSibling();
}
if(nextSibling != null && nextSibling.getNode().getElementType() == token)
{
collectForDelete.add(nextSibling);
nextSibling = nextSibling.getNextSibling();
}
if(nextSibling instanceof PsiWhiteSpace)
{
collectForDelete.add(nextSibling);
}
}
/**
* Fetches the initialization, the condition and the counter elements and places them in an array.
* The array will always have length 3 and the elements are always placed in their respective places.
* They will be null if missing.
*
* @return an array containing the loop elements.
*/
@NotNull
private GLSLElement[] getForElements() {
GLSLElement[] result = new GLSLElement[3];
int numberOfSemicolonsPassed = 0;
PsiElement current = getFirstChild();
while (current != null) {
ASTNode node = current.getNode();
if (current instanceof GLSLExpression || current instanceof GLSLDeclaration) {
result[numberOfSemicolonsPassed] = (GLSLElement) current;
} else if (node != null) {
if (node.getElementType() == GLSLTokenTypes.SEMICOLON) {
numberOfSemicolonsPassed++;
}
if (node.getElementType() == GLSLTokenTypes.RIGHT_PAREN) {
break;
}
}
current = current.getNextSibling();
}
return result;
}
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();
}
}
@Nullable
public GLSLExpression getFalseBranch() {
PsiElement condition = findChildByType(COLON);
while (condition != null && !(condition instanceof GLSLExpression)) {
condition = condition.getNextSibling();
}
return (GLSLExpression) condition;
}
public static PsiElement findNextSiblingOfType(PsiElement psiElement, String[] types) {
PsiElement siblingElement = psiElement.getNextSibling();
while (siblingElement != null && isNotElementType(siblingElement, types)) {
siblingElement = siblingElement.getNextSibling();
}
return siblingElement;
}
public static PsiElement getParentFieldElement(final PsiElement element) {
PsiElement currentElement = element;
IElementType elementType = CsvHelper.getElementType(currentElement);
if (elementType == CsvTypes.COMMA || elementType == CsvTypes.CRLF) {
currentElement = currentElement.getPrevSibling();
elementType = CsvHelper.getElementType(currentElement);
}
if (elementType == CsvTypes.RECORD) {
currentElement = currentElement.getLastChild();
elementType = CsvHelper.getElementType(currentElement);
}
if (elementType == TokenType.WHITE_SPACE) {
if (CsvHelper.getElementType(currentElement.getParent()) == CsvTypes.FIELD) {
currentElement = currentElement.getParent();
} else if (CsvHelper.getElementType(currentElement.getPrevSibling()) == CsvTypes.FIELD) {
currentElement = currentElement.getPrevSibling();
} else if (CsvHelper.getElementType(currentElement.getNextSibling()) == CsvTypes.FIELD) {
currentElement = currentElement.getNextSibling();
} else {
currentElement = null;
}
} else {
while (currentElement != null && elementType != CsvTypes.FIELD) {
currentElement = currentElement.getParent();
elementType = CsvHelper.getElementType(currentElement);
}
}
return currentElement;
}
@Nullable
public static <T> T nextSiblingOfClass(@Nullable PsiElement element, @NotNull Class<T> clazz) {
if (element == null) {
return null;
}
PsiElement nextSibling = element.getNextSibling();
while (nextSibling != null && !(nextSibling.getClass().isAssignableFrom(clazz))) {
nextSibling = nextSibling.getNextSibling();
}
return nextSibling != null && nextSibling.getClass().isAssignableFrom(clazz) ? (T) nextSibling : null;
}
/**
* Extract parameter: @foobar('my_value')
*/
@Nullable
private Pair<BladePsiDirectiveParameter, String> extractSectionParameter(@NotNull PsiElement psiElement) {
PsiElement nextSibling = psiElement.getNextSibling();
if(nextSibling instanceof BladePsiDirectiveParameter) {
String sectionName = BladePsiUtil.getSection(nextSibling);
if (sectionName != null && StringUtils.isNotBlank(sectionName)) {
return Pair.create((BladePsiDirectiveParameter) nextSibling, sectionName);
}
}
return null;
}
@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;
}
/**
* When appropriate, auto-inserts closing tags. i.e. When "{#tagName}" or "{^tagName} is typed,
* {/tagName} is automatically inserted
*/
private void autoInsertCloseTag(Project project, int offset, Editor editor, FileViewProvider provider) {
PsiDocumentManager.getInstance(project).commitAllDocuments();
PsiElement elementAtCaret = provider.findElementAt(offset - 1, DustLanguage.class);
PsiElement openTag = DustPsiUtil.findParentOpenTagElement(elementAtCaret);
String tagName = getTagName(openTag);
if (!tagName.trim().equals("")) {
boolean addCloseTag = true;
PsiElement sibling = openTag.getNextSibling();
DustCloseTag closeTag;
while (sibling != null && addCloseTag) {
if (sibling instanceof DustCloseTag) {
closeTag = (DustCloseTag) sibling;
if (getTagName(closeTag).equals(tagName)) {
addCloseTag = false;
}
}
sibling = sibling.getNextSibling();
}
if (addCloseTag) {
// insert the corresponding close tag
editor.getDocument().insertString(offset, "{/" + tagName + "}");
}
}
}
public static PsiElement getNextCRLF(final PsiElement element) {
PsiElement currentElement = element;
while (currentElement != null) {
if (CsvHelper.getElementType(currentElement) == CsvTypes.CRLF) {
break;
}
currentElement = currentElement.getNextSibling();
}
return currentElement;
}
@Nullable
@Override
public PsiElement getNameIdentifier() {
PsiElement child = getFirstChild();
while (child != null) { // we can't iterate over getChildren(), as that ignores leaf elements
if (child.getNode().getElementType() == GLSLTokenTypes.IDENTIFIER) return child;
child = child.getNextSibling();
}
return null;
}
@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;
}
/**
* Like this @section('sidebar')
*/
@NotNull
private Collection<LineMarkerInfo> collectOverwrittenSection(@NotNull LeafPsiElement psiElement, @NotNull String sectionName, @NotNull LazyVirtualFileTemplateResolver resolver) {
List<GotoRelatedItem> gotoRelatedItems = new ArrayList<>();
for(PsiElement psiElement1 : psiElement.getContainingFile().getChildren()) {
PsiElement extendDirective = psiElement1.getFirstChild();
if(extendDirective != null && extendDirective.getNode().getElementType() == BladeTokenTypes.EXTENDS_DIRECTIVE) {
PsiElement bladeParameter = extendDirective.getNextSibling();
if(bladeParameter instanceof BladePsiDirectiveParameter) {
String extendTemplate = BladePsiUtil.getSection(bladeParameter);
if(extendTemplate != null) {
for(VirtualFile virtualFile: resolver.resolveTemplateName(psiElement.getProject(), extendTemplate)) {
PsiFile psiFile = PsiManager.getInstance(psiElement.getProject()).findFile(virtualFile);
if(psiFile != null) {
visitOverwrittenTemplateFile(psiFile, gotoRelatedItems, sectionName, resolver);
}
}
}
}
}
}
if(gotoRelatedItems.size() == 0) {
return Collections.emptyList();
}
return Collections.singletonList(
getRelatedPopover("Parent Section", "Blade Section", psiElement, gotoRelatedItems, PhpIcons.OVERRIDES)
);
}
public static PsiElement getNextElementOfType(PsiElement start, Class type, Set<String> terminators, Set<String> exclusions)
{
PsiElement sibling = start.getNextSibling();
while(sibling != null && !(sibling instanceof JSLiteralExpression) && !(sibling instanceof JSParameter) && !(sibling.getText().equals("]")) && !terminators.contains(sibling.getText()))
{
if(type.isInstance(sibling) && !exclusions.contains(sibling.getText()))
{
return sibling;
}
sibling = sibling.getNextSibling();
}
return null;
}
private boolean isRecursive() {
// Find first element after the LET
PsiElement firstChild = getFirstChild();
PsiElement sibling = firstChild.getNextSibling();
if (sibling instanceof PsiWhiteSpace) {
sibling = sibling.getNextSibling();
}
return sibling != null && "rec".equals(sibling.getText());
}
private static PsiElement skipOverDocComments(PsiElement startElement) {
PsiElement elt = startElement.getNextSibling();
if (elt == null) {
return startElement;
} else if (elt instanceof PsiComment) {
IElementType commentType = ((PsiComment) elt).getTokenType();
if (commentType == START_DOC_COMMENT) {
return PsiTreeUtil.skipSiblingsForward(elt, PsiComment.class);
}
}
return elt;
}
/**
* Adds end variable after the specified element
*/
public void setEndVariableAfter(PsiElement element) {
element = element.getNextSibling();
setEndVariableBefore(element);
}