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

The following examples show how to use com.intellij.lang.ASTNode#getTreeParent() . 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: BashIndentProcessor.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
/**
 * Calculates indent, based on code style, between parent block and child node
 *
 * @param parent        parent block
 * @param child         child node
 * @param prevChildNode previous child node
 * @return indent
 */
@NotNull
public static Indent getChildIndent(@NotNull final BashBlock parent, @Nullable final ASTNode prevChildNode, @NotNull final ASTNode child) {
    ASTNode node = parent.getNode();
    IElementType nodeType = node.getElementType();
    PsiElement psiElement = node.getPsi();

    // For Bash file
    if (psiElement instanceof BashFile) {
        return Indent.getNoneIndent();
    }

    if (BLOCKS.contains(nodeType)) {
        return indentForBlock(psiElement, child);
    }

    //subshell as function body
    ASTNode parentNode = node.getTreeParent();
    if (parentNode != null && parentNode.getElementType() == SUBSHELL_COMMAND) {
        if (parentNode.getTreeParent() != null && parentNode.getTreeParent().getElementType() == FUNCTION_DEF_COMMAND) {
            return Indent.getNormalIndent();
        }
    }

    return Indent.getNoneIndent();
}
 
Example 2
Source File: CodeFormatterFacade.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
static ASTNode findContainingNode(@Nonnull PsiFile file, @Nullable TextRange range) {
  Language language = file.getLanguage();
  if (range == null) return null;
  final FileViewProvider viewProvider = file.getViewProvider();
  final PsiElement startElement = viewProvider.findElementAt(range.getStartOffset(), language);
  final PsiElement endElement = viewProvider.findElementAt(range.getEndOffset() - 1, language);
  final PsiElement commonParent = startElement != null && endElement != null ? PsiTreeUtil.findCommonParent(startElement, endElement) : null;
  ASTNode node = null;
  if (commonParent != null) {
    node = commonParent.getNode();
    // Find the topmost parent with the same range.
    ASTNode parent = node.getTreeParent();
    while (parent != null && parent.getTextRange().equals(commonParent.getTextRange())) {
      node = parent;
      parent = parent.getTreeParent();
    }
  }
  if (node == null) {
    node = file.getNode();
  }
  return node;
}
 
Example 3
Source File: PsiUtilBase.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static int getRootIndex(PsiElement root) {
  ASTNode node = root.getNode();
  while(node != null && node.getTreeParent() != null) {
    node = node.getTreeParent();
  }
  if(node != null) root = node.getPsi();
  final PsiFile containingFile = root.getContainingFile();
  FileViewProvider provider = containingFile.getViewProvider();
  Set<Language> languages = provider.getLanguages();
  if (languages.size() == 1) {
    return 0;
  }
  List<Language> array = new ArrayList<Language>(languages);
  Collections.sort(array, LANGUAGE_COMPARATOR);
  for (int i = 0; i < array.size(); i++) {
    Language language = array.get(i);
    if (provider.getPsi(language) == containingFile) return i;
  }
  throw new RuntimeException("Cannot find root for: "+root);
}
 
Example 4
Source File: TokenSpecNode.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public PsiElement createElement(ASTNode node) {
	ASTNode idList = node.getTreeParent();
	ASTNode parent = null;

	if (idList != null) {
		parent = idList.getTreeParent();
	}
	if (parent != null) {
		if (parent.getElementType() == ANTLRv4TokenTypes.RULE_ELEMENT_TYPES.get(ANTLRv4Parser.RULE_tokensSpec)) {
			return new TokenSpecNode(node);
		} else if (parent.getElementType() == ANTLRv4TokenTypes.RULE_ELEMENT_TYPES.get(ANTLRv4Parser.RULE_channelsSpec)) {
			return new ChannelSpecNode(node);
		}
	}

	return new ASTWrapperPsiElement(node);
}
 
Example 5
Source File: FormatterUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private static ASTNode getNextOrPrevious(@Nullable ASTNode node, boolean isNext, @Nonnull IElementType... typesToIgnore) {
  if (node == null) return null;

  ASTNode each = isNext ? node.getTreeNext() : node.getTreePrev();
  ASTNode parent = node.getTreeParent();
  while (each == null && parent != null) {
    each = isNext ? parent.getTreeNext() : parent.getTreePrev();
    parent = parent.getTreeParent();
  }

  if (each == null) {
    return null;
  }

  for (IElementType type : typesToIgnore) {
    if (each.getElementType() == type) {
      return getNextOrPrevious(each, isNext, typesToIgnore);
    }
  }

  return each;
}
 
Example 6
Source File: FormatterUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private static ASTNode getWsCandidate(@Nullable ASTNode node) {
  if (node == null) return null;
  ASTNode treePrev = node.getTreePrev();
  if (treePrev != null) {
    if (treePrev.getElementType() == TokenType.WHITE_SPACE) {
      return treePrev;
    }
    else if (treePrev.getTextLength() == 0) {
      return getWsCandidate(treePrev);
    }
    else {
      return node;
    }
  }
  final ASTNode treeParent = node.getTreeParent();

  if (treeParent == null || treeParent.getTreeParent() == null) {
    return node;
  }
  else {
    return getWsCandidate(treeParent);
  }
}
 
Example 7
Source File: BashSpacingProcessor.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
private void init(final ASTNode child) {
    if (child == null) {
        return;
    }
    ASTNode treePrev = child.getTreePrev();
    while (treePrev != null && SpacingUtil.isWhiteSpace(treePrev)) {
        treePrev = treePrev.getTreePrev();
    }

    if (treePrev == null) {
        init(child.getTreeParent());
    } else {
        myChild2 = child;
        myChild1 = treePrev;
        final CompositeElement parent = (CompositeElement) treePrev.getTreeParent();
        myParent = SourceTreeToPsiMap.treeElementToPsi(parent);
    }
}
 
Example 8
Source File: BashPsiUtils.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@Nullable
public static ASTNode findEquivalentParent(@NotNull ASTNode node, @Nullable IElementType stopAt) {
    TextRange sourceRange = node.getTextRange();

    ASTNode current = node;
    while (true) {
        ASTNode parent = current.getTreeParent();
        if (parent == null || !parent.getTextRange().equals(sourceRange)) {
            return stopAt != null && current.getElementType() != stopAt ? null : current;
        }

        current = parent;
        if (stopAt == null || stopAt.equals(current.getElementType())) {
            return current;
        }
    }
}
 
Example 9
Source File: TreeUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static ASTNode skipElementsBack(@Nullable ASTNode element, TokenSet types) {
  if (element == null) return null;
  if (!types.contains(element.getElementType())) return element;

  ASTNode parent = element.getTreeParent();
  ASTNode prev = element;
  while (prev instanceof CompositeElement) {
    if (!types.contains(prev.getElementType())) return prev;
    prev = prev.getTreePrev();
  }
  if (prev == null) return null;
  ASTNode firstChildNode = parent.getFirstChildNode();
  ASTNode lastRelevant = null;
  while (firstChildNode != prev) {
    if (!types.contains(firstChildNode.getElementType())) lastRelevant = firstChildNode;
    firstChildNode = firstChildNode.getTreeNext();
  }
  return lastRelevant;
}
 
Example 10
Source File: IndentationFoldingBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
private void collectDescriptors(@Nonnull final ASTNode node, @Nonnull final List<FoldingDescriptor> descriptors) {
  final Queue<ASTNode> toProcess = new LinkedList<ASTNode>();
  toProcess.add(node);
  while (!toProcess.isEmpty()) {
    final ASTNode current = toProcess.remove();
    if (current.getTreeParent() != null
        && current.getTextLength() > 1
        && myTokenSet.contains(current.getElementType()))
    {
      descriptors.add(new FoldingDescriptor(current, current.getTextRange()));
    }
    for (ASTNode child = current.getFirstChildNode(); child != null; child = child.getTreeNext()) {
      toProcess.add(child);
    }
  }
}
 
Example 11
Source File: FusionIndentProcessor.java    From intellij-neos with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param node An ASTNode
 * @return The calculated indent for the given node
 */
public static Indent getIndent(ASTNode node) {
    IElementType type = node.getElementType();
    Indent indent = Indent.getNoneIndent();
    ASTNode parent = node.getTreeParent();
    if (parent == null) {
        return indent;
    }
    if (TYPES_WHICH_PROVOKE_AN_INDENT.contains(parent.getElementType())) {
        indent = Indent.getIndent(Indent.Type.NORMAL, false, true);
    }
    if (TYPES_WHICH_DO_NOT_NEED_AN_BEGINNING_INDENT.contains(type)) {
        indent = Indent.getNoneIndent();
    }
    if (TYPES_WHICH_CANNOT_GET_AN_INDENT.contains(type)) {
        indent = Indent.getAbsoluteNoneIndent();
    }
    return indent;
}
 
Example 12
Source File: PsiUtil.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * Checks if the given node has an ancestor of the given IElementType. If there is one, this method will return that ancestor. Otherwise, it will return null.<br>
 * If textContent is not null, this method will also check if the ancestor is of correct type and ancestor's text is equal to textContent.
 *
 * @param node        node to check if has a parent of IElementType type
 * @param type        IElementType to check
 * @param textContent null if to disregard text of ancestor, otherwise check if ancestor's text is equal to textContent
 * @return node's ancestor if ancestor is of IElementType type if node's ancestor's text matches textContent. If textContent is null, text can be anything for ancestor.
 */
@Nullable
public static ASTNode getFirstAncestorOfType(@NotNull ASTNode node, @NotNull IElementType type, @Nullable String textContent) {
	ASTNode parent = node.getTreeParent();
	boolean isChild = false;
	while (parent != null && !isChild) {
		parent = parent.getTreeParent();
		if (parent == null) {
			break;
		}
		isChild = parent.getElementType() == type && (textContent == null || parent.getText().equals(textContent));
	}
	return parent;
}
 
Example 13
Source File: PsiUtil.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * Will traverse up the AST tree and send each discovered node into the callback, including the starting node.
 * The function can return true, which will then terminate the traversal, or return false or null to continue the traversal.
 *
 * @param start    where to start
 * @param callback function invoked for each discovered node
 */
public static void traverseUp(@NotNull ASTNode start, @NotNull Function<ASTNode, Boolean> callback) {
	Boolean stop = callback.apply(start);
	if (stop != null && stop) {
		return;
	}
	ASTNode parent = start;
	while (parent != null) {
		parent = parent.getTreeParent();
		stop = callback.apply(parent);
		if (stop != null && stop) {
			return;
		}
	}
}
 
Example 14
Source File: CsvFormatHelper.java    From intellij-csv-validator with Apache License 2.0 5 votes vote down vote up
public static ASTNode getRoot(final ASTNode node) {
    ASTNode currentNode = node;
    ASTNode parent;
    while ((parent = currentNode.getTreeParent()) != null) {
        currentNode = parent;
    }
    return currentNode;
}
 
Example 15
Source File: FormatterUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void replaceLastWhiteSpace(final ASTNode astNode, final String whiteSpace, final TextRange textRange) {
  ASTNode lastWS = TreeUtil.findLastLeaf(astNode);
  if (lastWS == null) {
    return;
  }
  if (lastWS.getElementType() != TokenType.WHITE_SPACE) {
    lastWS = null;
  }
  if (lastWS != null && !lastWS.getTextRange().equals(textRange)) {
    return;
  }
  if (whiteSpace.isEmpty() && lastWS == null) {
    return;
  }
  if (lastWS != null && whiteSpace.isEmpty()) {
    lastWS.getTreeParent().removeRange(lastWS, null);
    return;
  }

  LeafElement whiteSpaceElement = ASTFactory.whitespace(whiteSpace);

  if (lastWS == null) {
    astNode.addChild(whiteSpaceElement, null);
  }
  else {
    ASTNode treeParent = lastWS.getTreeParent();
    treeParent.replaceChild(lastWS, whiteSpaceElement);
  }
}
 
Example 16
Source File: TreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ASTNode findParent(ASTNode element, TokenSet types) {
  for (ASTNode parent = element.getTreeParent(); parent != null; parent = parent.getTreeParent()) {
    if (types.contains(parent.getElementType())) return parent;
  }
  return null;
}
 
Example 17
Source File: SharedImplUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static FileASTNode findFileElement(@Nonnull ASTNode element) {
  ASTNode parent = element.getTreeParent();
  while (parent != null) {
    element = parent;
    parent = parent.getTreeParent();
  }

  if (element instanceof FileASTNode) {
    return (FileASTNode)element;
  }
  return null;
}
 
Example 18
Source File: TreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ASTNode prevLeaf(@Nullable ASTNode start, boolean expandChameleons) {
  while (start != null) {
    for (ASTNode each = start.getTreePrev(); each != null; each = each.getTreePrev()) {
      ASTNode leaf = findLastLeaf(each, expandChameleons);
      if (leaf != null) return leaf;
    }
    start = start.getTreeParent();
  }
  return null;
}
 
Example 19
Source File: FunctionCaseConverter.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 4 votes vote down vote up
private boolean testParentType(ASTNode node, IElementType type) {
    return node != null && node.getTreeParent() != null && node.getTreeParent().getElementType() == type;
}
 
Example 20
Source File: ASTStructure.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public ASTNode getParent(@Nonnull final ASTNode node) {
  return node.getTreeParent();
}