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

The following examples show how to use com.intellij.lang.ASTNode#addChild() . 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: ElmAddImportHelper.java    From elm-plugin with MIT License 6 votes vote down vote up
private static void insertImportClause(ElmImportClause importClause, ASTNode insertPosition) {
    Project project = importClause.getProject();
    ASTNode parent = insertPosition.getTreeParent();
    ASTNode beforeInsertPosition = insertPosition.getTreePrev();

    // ensure that a freshline exists immediately following
    // where we are going to insert the new import clause.
    ASTNode prevFreshline = null;
    if (insertPosition.getElementType() != ElmTypes.FRESH_LINE) {
        prevFreshline = ElmElementFactory.createFreshLine(project).getNode();
        parent.addChild(prevFreshline, insertPosition);
    } else {
        prevFreshline = insertPosition;
    }

    // insert the import clause before the freshline
    parent.addChild(importClause.getNode(), prevFreshline);

    // ensure that freshline exists *before* the new import clause
    if (beforeInsertPosition != null && beforeInsertPosition.getElementType() != ElmTypes.FRESH_LINE) {
        ASTNode newFreshline = ElmElementFactory.createFreshLine(project).getNode();
        parent.addChild(newFreshline, importClause.getNode());
    }
}
 
Example 2
Source File: CS0145.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
@RequiredUIAccess
public void invoke(@Nonnull Project project, Editor editor, PsiFile file) throws IncorrectOperationException
{
	DotNetVariable element = myVariablePointer.getElement();
	if(element == null)
	{
		return;
	}

	PsiDocumentManager.getInstance(project).commitAllDocuments();

	String defaultValueForType = MethodGenerateUtil.getDefaultValueForType(element.toTypeRef(false), element);

	if(defaultValueForType == null)
	{
		return;
	}

	DotNetExpression expression = CSharpFileFactory.createExpression(project, defaultValueForType);

	PsiElement nameIdentifier = element.getNameIdentifier();
	if(nameIdentifier == null)
	{
		return;
	}

	ASTNode variableNode = element.getNode();

	ASTNode semicolon = variableNode.findChildByType(CSharpTokens.SEMICOLON);

	variableNode.addLeaf(CSharpTokens.WHITE_SPACE, " ", semicolon);
	variableNode.addLeaf(CSharpTokens.EQ, "=", semicolon);
	variableNode.addLeaf(CSharpTokens.WHITE_SPACE, " ", semicolon);
	ASTNode node = expression.getNode();
	CodeEditUtil.setOldIndentation((TreeElement) node, 0);
	variableNode.addChild(node, semicolon);

	editor.getCaretModel().moveToOffset(element.getTextRange().getEndOffset());
}
 
Example 3
Source File: SharedImplUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static PsiElement addRange(PsiElement thisElement,
                                  PsiElement first,
                                  PsiElement last,
                                  ASTNode anchor,
                                  Boolean before) throws IncorrectOperationException {
  CheckUtil.checkWritable(thisElement);
  final CharTable table = findCharTableByTree(SourceTreeToPsiMap.psiElementToTree(thisElement));

  TreeElement copyFirst = null;
  ASTNode copyLast = null;
  ASTNode next = SourceTreeToPsiMap.psiElementToTree(last).getTreeNext();
  ASTNode parent = null;
  for (ASTNode element = SourceTreeToPsiMap.psiElementToTree(first); element != next; element = element.getTreeNext()) {
    TreeElement elementCopy = ChangeUtil.copyElement((TreeElement)element, table);
    if (element == first.getNode()) {
      copyFirst = elementCopy;
    }
    if (element == last.getNode()) {
      copyLast = elementCopy;
    }
    if (parent == null) {
      parent = elementCopy.getTreeParent();
    }
    else {
      if(elementCopy.getElementType() == TokenType.WHITE_SPACE)
        CodeEditUtil.setNodeGenerated(elementCopy, true);
      parent.addChild(elementCopy, null);
    }
  }
  if (copyFirst == null) return null;
  copyFirst = ((CompositeElement)SourceTreeToPsiMap.psiElementToTree(thisElement)).addInternal(copyFirst, copyLast, anchor, before);
  for (TreeElement element = copyFirst; element != null; element = element.getTreeNext()) {
    element = ChangeUtil.decodeInformation(element);
    if (element.getTreePrev() == null) {
      copyFirst = element;
    }
  }
  return SourceTreeToPsiMap.treeElementToPsi(copyFirst);
}
 
Example 4
Source File: FormatterUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void addWhiteSpace(final ASTNode treePrev, final LeafElement whiteSpaceElement) {
  for (WhiteSpaceFormattingStrategy strategy : WhiteSpaceFormattingStrategyFactory.getAllStrategies()) {
    if (strategy.addWhitespace(treePrev, whiteSpaceElement)) {
      return;
    }
  }

  final ASTNode treeParent = treePrev.getTreeParent();
  treeParent.addChild(whiteSpaceElement, treePrev);
}
 
Example 5
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 6
Source File: GroovyDslUtil.java    From ok-gradle with Apache License 2.0 4 votes vote down vote up
static void emplaceElementIntoList(@NotNull PsiElement anchorBefore, @NotNull PsiElement list, @NotNull PsiElement newElement) {
  final ASTNode node = list.getNode();
  final ASTNode anchor = anchorBefore.getNode().getTreeNext();
  node.addChild(newElement.getNode(), anchor);
  node.addLeaf(mCOMMA, ",", newElement.getNode());
}