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

The following examples show how to use com.intellij.lang.ASTNode#getElementType() . 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: ElmManualPsiElementFactory.java    From elm-plugin with MIT License 6 votes vote down vote up
public static PsiElement createElement(ASTNode node) {
    IElementType type = node.getElementType();
    if (type == ElmTypes.CASE_OF) {
        return new ElmCaseOfImpl(node);
    }
    if (type == ElmTypes.LET_IN) {
        return new ElmLetInImpl(node);
    }
    if (type == ElmTypes.UPPER_CASE_PATH) {
        return new ElmUpperCasePathImpl(node);
    }
    if (type == ElmTypes.LOWER_CASE_PATH) {
        return new ElmLowerCasePathImpl(node);
    }
    if (type == ElmTypes.MIXED_CASE_PATH) {
        return new ElmMixedCasePathImpl(node);
    }
    if (type == ElmTypes.FIELD_ACCESS) {
        return new ElmFieldAccessImpl(node);
    }
    if (type == ElmTypes.EFFECT) {
        return new ElmEffectImpl(node);
    }
    return null;
}
 
Example 2
Source File: ASTDelegatePsiElement.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@RequiredReadAction
protected <T extends PsiElement> List<T> findChildrenByType(IElementType elementType) {
  List<T> result = EMPTY;
  ASTNode child = getNode().getFirstChildNode();
  while (child != null) {
    if (elementType == child.getElementType()) {
      if (result == EMPTY) {
        result = new ArrayList<T>();
      }
      result.add((T)child.getPsi());
    }
    child = child.getTreeNext();
  }
  return result;
}
 
Example 3
Source File: BcfgParserDefinition.java    From buck with Apache License 2.0 6 votes vote down vote up
@Override
public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) {
  IElementType lType = left.getElementType();
  IElementType rType = right.getElementType();
  if (lType == BcfgTypes.FILE_PATH || rType == BcfgTypes.FILE_PATH) {
    return SpaceRequirements.MUST_NOT; // No space around <file:...>
  }
  if (lType == BcfgTypes.COMMENT) {
    return SpaceRequirements.MUST_LINE_BREAK; // Must break *after* line comment
  }
  if (rType == BcfgTypes.PROPERTY_NAME) {
    return SpaceRequirements.MUST_LINE_BREAK; // Must line break *before* property name
  }
  if (rType == BcfgTypes.SECTION_HEADER) {
    return SpaceRequirements.MUST_LINE_BREAK; // Must line break *before* section header
  }
  if (rType == BcfgTypes.REQUIRED_FILE || rType == BcfgTypes.OPTIONAL_FILE) {
    return SpaceRequirements.MUST_LINE_BREAK; // Must line break *before* <file> import
  }
  return SpaceRequirements.MAY;
}
 
Example 4
Source File: HaxeFoldingBuilder.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
/**
 * Gather the CC and its expression (if any) as the placeholder.
 *
 * @param node The Compiler Conditional node.
 * @return a placeholder string for code folding.
 */
@NotNull
private static String getCCPlaceholder(ASTNode node) {
  StringBuilder s = new StringBuilder();
  s.append(node.getText());
  ASTNode next = node.getTreeNext();
  while (null != next && next.getElementType() == PPEXPRESSION) {
    s.append(next.getText());
    next = next.getTreeNext();
  }
  if (0 != s.length()) {
    s.append(' ');
  }
  String placeholder = s.toString();
  return placeholder.isEmpty() ? "compiler conditional" : placeholder;
}
 
Example 5
Source File: HaxeWrappingProcessor.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
private static Wrap createChildWrap(ASTNode child, int parentWrap, boolean newLineAfterLBrace, boolean newLineBeforeRBrace) {
  IElementType childType = child.getElementType();
  if (childType != PLPAREN && childType != PRPAREN) {
    if (FormatterUtil.isPrecededBy(child, PLBRACK)) {
      if (newLineAfterLBrace) {
        return Wrap.createChildWrap(Wrap.createWrap(parentWrap, true), WrapType.ALWAYS, true);
      }
      else {
        return Wrap.createWrap(WrapType.NONE, true);
      }
    }
    return Wrap.createWrap(WrappingUtil.getWrapType(parentWrap), true);
  }
  if (childType == PRBRACK && newLineBeforeRBrace) {
    return Wrap.createWrap(WrapType.ALWAYS, true);
  }
  return Wrap.createWrap(WrapType.NONE, true);
}
 
Example 6
Source File: HaxeFoldingBuilder.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
private boolean isUnusedCCRegion(ASTNode node) {
  // This works because unused regions only contain PPEXPRESSION and PPBODY element types.
  // There is always at least one PPBODY element in any unused section, even if it is whitespace.

  if (null == node) {
    return false;
  }
  // Sections start with a PsiComment for the marker (#if/#else/#elseif/#end),
  // followed by a set of PsiComment(PPEXPRESSION), and finally, a PsiComment(PPBODY)
  // when the section is unused.
  if (!ONLY_CC_DIRECTIVES.contains(node.getElementType())) {
    return false;
  }

  node = node.getTreeNext();
  while (PPEXPRESSION == node.getElementType()) {
    node = node.getTreeNext();
  }
  return node.getElementType() == PPBODY;

}
 
Example 7
Source File: TreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ASTNode findSibling(ASTNode start, IElementType elementType) {
  ASTNode child = start;
  while (true) {
    if (child == null) return null;
    if (child.getElementType() == elementType) return child;
    child = child.getTreeNext();
  }
}
 
Example 8
Source File: BlockSupportImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void reportInconsistentLength(PsiFile file, CharSequence newFileText, ASTNode node, int start, int end) {
  String message = "Index out of bounds: type=" + node.getElementType() + "; file=" + file + "; file.class=" + file.getClass() + "; start=" + start + "; end=" + end + "; length=" + node.getTextLength();
  String newTextBefore = newFileText.subSequence(0, start).toString();
  String oldTextBefore = file.getText().subSequence(0, start).toString();
  if (oldTextBefore.equals(newTextBefore)) {
    message += "; oldTextBefore==newTextBefore";
  }
  LOG.error(message, new Attachment(file.getName() + "_oldNodeText.txt", node.getText()), new Attachment(file.getName() + "_oldFileText.txt", file.getText()),
            new Attachment(file.getName() + "_newFileText.txt", newFileText.toString()));
}
 
Example 9
Source File: WeaveFolding.java    From mule-intellij-plugins with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public String getPlaceholderText(@NotNull ASTNode node) {
    final IElementType type = node.getElementType();
    if (isObject(type)) {
        return "{...}";
    } else if (isArray((type))) {
        return "[...]";
    }
    return "...";
}
 
Example 10
Source File: PsiUtil.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * Gets the closest next sibling, where the type is not skip, relative to node
 *
 * @param node node to find sibling of
 * @param skip the token to skip
 * @return non-skip sibling, or null if none was found
 */
@Nullable
public static ASTNode getNextSiblingNotType(@NotNull ASTNode node, @NotNull IElementType skip) {
	ASTNode sibling = node.getTreeNext();
	while (sibling != null) {
		if (sibling.getElementType() == skip) {
			sibling = sibling.getTreeNext();
		} else {
			break;
		}
	}
	return sibling;
}
 
Example 11
Source File: ASTDelegatePsiElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
@RequiredReadAction
protected PsiElement findLastChildByType(IElementType type) {
  PsiElement child = getLastChild();
  while (child != null) {
    final ASTNode node = child.getNode();
    if (node != null && node.getElementType() == type) return child;
    child = child.getPrevSibling();
  }
  return null;
}
 
Example 12
Source File: BashSpacingProcessorBasic.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether a node is contained in a parameter expansion.
 *
 * @param node
 * @return True if the on of the nodes is embedded in a string
 */
private static boolean hasAncestorNodeType(@Nullable ASTNode node, int levelsUp, @NotNull IElementType parentNodeType) {
    if (node == null) {
        return false;
    }

    if (levelsUp <= 0) {
        return node.getElementType() == parentNodeType;
    }

    return hasAncestorNodeType(node.getTreeParent(), levelsUp - 1, parentNodeType);
}
 
Example 13
Source File: FormatterUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean isOneOf(@Nullable ASTNode node, @Nonnull IElementType... types) {
  if (node == null) return false;
  IElementType elementType = node.getElementType();
  for (IElementType each : types) {
    if (elementType == each) return true;
  }
  return false;
}
 
Example 14
Source File: DotEnvTypes.java    From idea-php-dotenv-plugin with MIT License 5 votes vote down vote up
public static PsiElement createElement(ASTNode node) {
  IElementType type = node.getElementType();
  if (type == KEY) {
    return new DotEnvKeyImpl(node);
  }
  else if (type == PROPERTY) {
    return new DotEnvPropertyImpl(node);
  }
  else if (type == VALUE) {
    return new DotEnvValueImpl(node);
  }
  throw new AssertionError("Unknown element type: " + type);
}
 
Example 15
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 16
Source File: CodeEditUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static ASTNode addChildren(ASTNode parent, @Nonnull ASTNode first, @Nonnull ASTNode last, ASTNode anchorBefore) {
  ASTNode lastChild = last.getTreeNext();
  ASTNode current = first;
  while (current != lastChild) {
    saveWhitespacesInfo(current);
    checkForOuters(current);
    current = current.getTreeNext();
  }

  if (anchorBefore != null && CommentUtilCore.isComment(anchorBefore)) {
    final ASTNode anchorPrev = anchorBefore.getTreePrev();
    if (anchorPrev != null && anchorPrev.getElementType() == TokenType.WHITE_SPACE) {
      anchorBefore = anchorPrev;
    }
  }

  parent.addChildren(first, lastChild, anchorBefore);
  ASTNode firstAddedLeaf = findFirstLeaf(first, last);
  ASTNode prevLeaf = TreeUtil.prevLeaf(first);
  ASTNode result = first;
  if (firstAddedLeaf != null) {
    ASTNode placeHolderEnd = makePlaceHolderBetweenTokens(prevLeaf, firstAddedLeaf, isFormattingRequired(prevLeaf, first), false);
    if (placeHolderEnd != prevLeaf && first == firstAddedLeaf) {
      result = placeHolderEnd;
    }
    ASTNode lastAddedLeaf = findLastLeaf(first, last);
    placeHolderEnd = makePlaceHolderBetweenTokens(lastAddedLeaf, TreeUtil.nextLeaf(last), true, false);
    if (placeHolderEnd != lastAddedLeaf && lastAddedLeaf == first) {
      result = placeHolderEnd;
    }
  }
  else {
    makePlaceHolderBetweenTokens(prevLeaf, TreeUtil.nextLeaf(last), isFormattingRequired(prevLeaf, first), false);
  }
  return result;
}
 
Example 17
Source File: DebugUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static void treeToBuffer(@Nonnull final Appendable buffer,
                                @Nonnull final ASTNode root,
                                final int indent,
                                final boolean skipWhiteSpaces,
                                final boolean showRanges,
                                final boolean showChildrenRanges,
                                final boolean usePsi,
                                PairConsumer<PsiElement, Consumer<PsiElement>> extra) {
  if (skipWhiteSpaces && root.getElementType() == TokenType.WHITE_SPACE) return;

  StringUtil.repeatSymbol(buffer, ' ', indent);
  try {
    PsiElement psiElement = null;
    if (root instanceof CompositeElement) {
      if (usePsi) {
        psiElement = root.getPsi();
        if (psiElement != null) {
          buffer.append(psiElement.toString());
        }
        else {
          buffer.append(root.getElementType().toString());
        }
      }
      else {
        buffer.append(root.toString());
      }
    }
    else {
      final String text = fixWhiteSpaces(root.getText());
      buffer.append(root.toString()).append("('").append(text).append("')");
    }
    if (showRanges) buffer.append(root.getTextRange().toString());
    buffer.append("\n");
    if (root instanceof CompositeElement) {
      ASTNode child = root.getFirstChildNode();

      if (child == null) {
        StringUtil.repeatSymbol(buffer, ' ', indent + 2);
        buffer.append("<empty list>\n");
      }
      else {
        while (child != null) {
          treeToBuffer(buffer, child, indent + 2, skipWhiteSpaces, showChildrenRanges, showChildrenRanges, usePsi, extra);
          child = child.getTreeNext();
        }
      }
    }
    if (psiElement != null && extra != null ) {
      extra.consume(psiElement, new Consumer<PsiElement>() {
        @Override
        public void consume(PsiElement element) {
          treeToBuffer(buffer, element.getNode(), indent + 2, skipWhiteSpaces, showChildrenRanges, showChildrenRanges, usePsi, null);
        }
      });
    }
  }
  catch (IOException e) {
    LOG.error(e);
  }
}
 
Example 18
Source File: FunctionCaseConverter.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 4 votes vote down vote up
private boolean isNonStarCount(@NotNull ASTNode node) {
    IElementType type = node.getElementType();
    return type == CypherTypes.K_COUNT && !testParentType(node, CypherTypes.COUNT_STAR);
}
 
Example 19
Source File: ImportResolver.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static boolean isImportStatement(PsiElement el) {
	ASTNode node = el.getNode();
	return node != null && node.getElementType() == RULE_ELEMENT_TYPES.get(ANTLRv4Parser.RULE_delegateGrammar);
}
 
Example 20
Source File: PsiUtil.java    From arma-intellij-plugin with MIT License 2 votes vote down vote up
/**
 * Checks if the given ASTNode is of IElementType et
 *
 * @param node ASTNode (if null, returns false)
 * @param et   IElement type
 * @return true if node is of type et, false otherwise
 */
public static boolean isOfElementType(@Nullable ASTNode node, @NotNull IElementType et) {
	return node != null && node.getElementType() == et;
}