Java Code Examples for com.intellij.lang.ASTNode#getPsi()

The following examples show how to use com.intellij.lang.ASTNode#getPsi() . 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: LatteBlock.java    From intellij-latte with MIT License 6 votes vote down vote up
@NotNull
@Override
protected Indent getChildIndent(@NotNull ASTNode astNode) {
	if (isBellowType(astNode, LatteTypes.MACRO_CONTENT)
		&& astNode.getTreePrev() != null
		&& astNode.getTreePrev().getElementType() == TokenType.WHITE_SPACE) {
		return Indent.getNormalIndent();
	}
	if (!isPair || isOpening(astNode) || isClosing(astNode)) {
		return Indent.getNoneIndent();
	}
	if (!(astNode.getPsi() instanceof LatteMacroClassic)) {
		return Indent.getNormalIndent();
	}
	PsiElement el = astNode.getPsi();
	LatteMacroTag openTag = ((LatteMacroClassic) el).getOpenTag();
	if (openTag.matchMacroName("else") || openTag.matchMacroName("elseif") || openTag.matchMacroName("elseifset")) {
		return Indent.getNoneIndent();
	}
	return Indent.getNormalIndent();
}
 
Example 2
Source File: SharedImplUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void acceptChildren(PsiElementVisitor visitor, ASTNode root) {
  ASTNode childNode = root.getFirstChildNode();

  while (childNode != null) {
    final PsiElement psi;
    if (childNode instanceof PsiElement) {
      psi = (PsiElement)childNode;
    }
    else {
      psi = childNode.getPsi();
    }
    psi.accept(visitor);

    childNode = childNode.getTreeNext();
  }
}
 
Example 3
Source File: ProtoFoldingBuilder.java    From protobuf-jetbrains-plugin with Apache License 2.0 6 votes vote down vote up
private static PsiElement findFurthestSiblingOfSameType(@NotNull PsiElement anchor, boolean after) {
    ASTNode node = anchor.getNode();
    final IElementType expectedType = node.getElementType();
    ASTNode lastSeen = node;
    while (node != null) {
        final IElementType elementType = node.getElementType();
        if (elementType == expectedType) {
            lastSeen = node;
        } else if (elementType == TokenType.WHITE_SPACE) {
            if (expectedType == token(LINE_COMMENT)
                    && node.getText().indexOf('\n', 1) != -1) {
                break;
            }
        } else if (!COMMENT_TOKEN_SET.contains(elementType) || COMMENT_TOKEN_SET.contains(expectedType)) {
            break;
        }
        node = after ? node.getTreeNext() : node.getTreePrev();
    }
    return lastSeen.getPsi();
}
 
Example 4
Source File: CodeEditUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void saveWhitespacesInfo(ASTNode first) {
  if (first == null || isNodeGenerated(first) || getOldIndentation(first) >= 0) {
    return;
  }

  PsiElement psiElement = first.getPsi();
  if (psiElement == null) {
    return;
  }

  PsiFile file = psiElement.getContainingFile();
  setOldIndentation((TreeElement)first, IndentHelper.getInstance().getIndent(file.getProject(), file.getFileType(), first));
}
 
Example 5
Source File: LattePsiImplUtil.java    From intellij-latte with MIT License 5 votes vote down vote up
private static PsiElement findFirstChildWithType(PsiElement element, @NotNull IElementType type) {
	ASTNode keyNode = element.getNode().findChildByType(type);
	if (keyNode != null) {
		return keyNode.getPsi();
	} else {
		return null;
	}
}
 
Example 6
Source File: BashVarDefImpl.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
/**
 * Returns either a assignment word element or a array assignment word.
 *
 * @return The element which represents the left part of the assignment.
 */
@NotNull
public PsiElement findAssignmentWord() {
    if (assignmentWord == null) {
        //no other lock is used in the callees, it's safe to synchronize around the whole calculation
        synchronized (stateLock) {
            if (assignmentWord == null) {
                PsiElement element = findChildByType(accepted);

                PsiElement newAssignmentWord;
                if (element != null) {
                    newAssignmentWord = element;
                } else {
                    //if null we probably represent a single var without assignment, i.e. the var node is nested inside of
                    //a parsed var
                    PsiElement firstChild = getFirstChild();
                    ASTNode childNode = firstChild != null ? firstChild.getNode() : null;

                    ASTNode node = childNode != null ? childNode.findChildByType(accepted) : null;
                    newAssignmentWord = (node != null) ? node.getPsi() : firstChild;
                }

                assignmentWord = newAssignmentWord;
            }
        }
    }

    return assignmentWord;
}
 
Example 7
Source File: XQueryFoldingBuilder.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public String getPlaceholderText(@NotNull ASTNode astNode) {
    final PsiElement psiElement = astNode.getPsi();
    if (psiElement instanceof XQueryFunctionDecl) {
        return "{...}";
    } else if (psiElement instanceof XQueryModuleImport) {
        return "...";
    } else if (psiElement instanceof XQueryNamespaceDecl) {
        return "...";
    } else {
        return "....";
    }
}
 
Example 8
Source File: BashPsiUtils.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Nullable
public static PsiElement findEquivalentParent(@NotNull PsiElement node, @Nullable IElementType stopAt) {
    ASTNode parent = findEquivalentParent(node.getNode(), stopAt);
    if (parent != null) {
        return parent.getPsi();
    }
    return null;
}
 
Example 9
Source File: BashBlock.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
private ChildAttributes getAttributesByParent() {
    ASTNode astNode = myNode;
    final PsiElement psiParent = astNode.getPsi();
    if (psiParent instanceof BashFile) {
        return new ChildAttributes(Indent.getNoneIndent(), null);
    }

    return new ChildAttributes(Indent.getNoneIndent(), null);
}
 
Example 10
Source File: RamlFile.java    From mule-intellij-plugins with Apache License 2.0 5 votes vote down vote up
public List<YAMLPsiElement> getYAMLElements()
{
    ArrayList<YAMLPsiElement> result = new ArrayList<>();
    ASTNode[] var2 = this.getNode().getChildren(null);
    for (ASTNode node : var2)
    {
        PsiElement psi = node.getPsi();
        if (psi instanceof YAMLPsiElement)
        {
            result.add((YAMLPsiElement) psi);
        }
    }

    return result;
}
 
Example 11
Source File: GroovyDslUtil.java    From ok-gradle with Apache License 2.0 5 votes vote down vote up
/**
 * This method is used in order to add elements to the back of a map,
 * it is derived from {@link ASTDelegatePsiElement#addInternal(ASTNode, ASTNode, ASTNode, Boolean)}.
 */
private static PsiElement realAddBefore(@NotNull GrListOrMap element, @NotNull PsiElement newElement, @NotNull PsiElement anchor) {
  CheckUtil.checkWritable(element);
  TreeElement elementCopy = ChangeUtil.copyToElement(newElement);
  ASTNode anchorNode = getAnchorNode(element, anchor.getNode(), true);
  ASTNode newNode = CodeEditUtil.addChildren(element.getNode(), elementCopy, elementCopy, anchorNode);
  if (newNode == null) {
    throw new IncorrectOperationException("Element cannot be added");
  }
  if (newNode instanceof TreeElement) {
    return ChangeUtil.decodeInformation((TreeElement)newNode).getPsi();
  }
  return newNode.getPsi();
}
 
Example 12
Source File: CSharpDocGtTypedHandler.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nullable
public static PsiElement getEndTagNameElement(@Nonnull CSharpDocTagImpl tag)
{
	final ASTNode node = tag.getNode();
	if(node == null)
	{
		return null;
	}

	ASTNode current = node.getLastChildNode();
	ASTNode prev = current;

	while(current != null)
	{
		final IElementType elementType = prev.getElementType();
		if((elementType == CSharpDocTokenType.XML_NAME || elementType == CSharpDocTokenType.XML_TAG_NAME) && current.getElementType() ==
				CSharpDocTokenType.XML_END_TAG_START)
		{
			return prev.getPsi();
		}

		prev = current;
		current = current.getTreePrev();

	}
	return null;
}
 
Example 13
Source File: SelectWordUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void processRanges(@Nullable PsiElement element,
                                 CharSequence text,
                                 int cursorOffset,
                                 Editor editor,
                                 Processor<TextRange> consumer) {
  if (element == null) return;

  PsiFile file = element.getContainingFile();

  FileViewProvider viewProvider = file.getViewProvider();

  processInFile(element, consumer, text, cursorOffset, editor);

  for (PsiFile psiFile : viewProvider.getAllFiles()) {
    if (psiFile == file) continue;

    FileASTNode fileNode = psiFile.getNode();
    if (fileNode == null) continue;

    ASTNode nodeAt = fileNode.findLeafElementAt(element.getTextOffset());
    if (nodeAt == null) continue;

    PsiElement elementAt = nodeAt.getPsi();

    while (!(elementAt instanceof PsiFile) && elementAt != null) {
      if (elementAt.getTextRange().contains(element.getTextRange())) break;

      elementAt = elementAt.getParent();
    }

    if (elementAt == null) continue;

    processInFile(elementAt, consumer, text, cursorOffset, editor);
  }
}
 
Example 14
Source File: CSharpFoldingBuilder.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
@RequiredReadAction
protected String getLanguagePlaceholderText(@Nonnull ASTNode node, @Nonnull TextRange range)
{
	PsiElement psi = node.getPsi();
	if(psi instanceof CSharpUsingListChild)
	{
		return "...";
	}
	else if(psi instanceof CSharpBlockStatementImpl || psi instanceof CSharpPropertyDeclaration || psi instanceof CSharpTypeDeclaration || psi instanceof CSharpEventDeclaration)
	{
		return "{...}";
	}
	else if(psi instanceof PsiComment)
	{
		IElementType tokenType = ((PsiComment) psi).getTokenType();
		if(tokenType == CSharpTokens.LINE_COMMENT)
		{
			return "// ...";
		}
		else if(tokenType == CSharpTokens.BLOCK_COMMENT)
		{
			return "/** ... */";
		}
	}

	IElementType elementType = PsiUtilCore.getElementType(psi);
	if(elementType == CSharpPreprocessorElements.PREPROCESSOR_DIRECTIVE)
	{
		return psi.getText().trim();
	}
	return null;
}
 
Example 15
Source File: SourceTreeToPsiMap.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public static PsiElement treeElementToPsi(@Nullable final ASTNode element) {
  return element == null ? null : element.getPsi();
}
 
Example 16
Source File: UsefulPsiTreeUtil.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
static public PsiElement getToken(PsiElement element, String token) {
  for (ASTNode node : element.getNode().getChildren(null)) {
    if (node.getText().equals(token))  return node.getPsi();
  }
  return null;
}
 
Example 17
Source File: ASTDelegatePsiElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
@RequiredReadAction
protected PsiElement findChildByType(IElementType type) {
  ASTNode node = getNode().findChildByType(type);
  return node == null ? null : node.getPsi();
}
 
Example 18
Source File: CompositeElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public PsiElement findPsiChildByType(@Nonnull IElementType type) {
  final ASTNode node = findChildByType(type);
  return node == null ? null : node.getPsi();
}
 
Example 19
Source File: BuildElementImpl.java    From intellij with Apache License 2.0 4 votes vote down vote up
public <P extends PsiElement> P getPsiChild(IElementType type, Class<P> psiClass) {
  ASTNode childNode = getNode().findChildByType(type);
  return childNode != null ? (P) childNode.getPsi() : null;
}
 
Example 20
Source File: CompositePsiElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public PsiElement getPrevSibling() {
  ASTNode node = getTreePrev();
  return node != null ? node.getPsi() : null;
}