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

The following examples show how to use com.intellij.lang.ASTNode#replaceChild() . 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: HaxeManyStatementsSurrounder.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
@Override
public TextRange surroundElements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements)
  throws IncorrectOperationException {
  if (elements.length == 0) return null;

  PsiElement element1 = elements[0];
  final PsiElement newStmt = doSurroundElements(elements, element1.getParent());

  ASTNode parentNode = element1.getParent().getNode();
  if (elements.length > 1) {
    parentNode.removeRange(element1.getNode().getTreeNext(), elements[elements.length - 1].getNode().getTreeNext());
  }
  parentNode.replaceChild(element1.getNode(), newStmt.getNode());

  return getSurroundSelectionRange(newStmt);
}
 
Example 2
Source File: ExternalWorkspaceReferenceFragment.java    From intellij with Apache License 2.0 5 votes vote down vote up
private PsiElement handleRename(String newStringContents) {
  ASTNode node = myElement.getNode();
  node.replaceChild(
      node.getFirstChildNode(),
      PsiUtils.createNewLabel(myElement.getProject(), newStringContents));
  return myElement;
}
 
Example 3
Source File: LabelReference.java    From intellij with Apache License 2.0 5 votes vote down vote up
private PsiElement handleRename(String newStringContents) {
  ASTNode node = myElement.getNode();
  node.replaceChild(
      node.getFirstChildNode(),
      PsiUtils.createNewLabel(myElement.getProject(), newStringContents));
  return myElement;
}
 
Example 4
Source File: PackageReferenceFragment.java    From intellij with Apache License 2.0 5 votes vote down vote up
private PsiElement handleRename(String newStringContents) {
  ASTNode node = myElement.getNode();
  node.replaceChild(
      node.getFirstChildNode(),
      PsiUtils.createNewLabel(myElement.getProject(), newStringContents));
  return myElement;
}
 
Example 5
Source File: StringLiteralElementManipulator.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
public StringLiteral handleContentChange(
    StringLiteral element, TextRange range, String newContent)
    throws IncorrectOperationException {
  ASTNode node = element.getNode();
  node.replaceChild(
      node.getFirstChildNode(), PsiUtils.createNewLabel(element.getProject(), newContent));
  return element;
}
 
Example 6
Source File: DeprecatedLoadQuickFix.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static void fixLoadString(Project project, StringLiteral importString) {
  String contents = importString.getStringContents();
  if (!contents.startsWith("/") || LabelUtils.isAbsolute(contents)) {
    return;
  }
  WorkspaceRoot root = WorkspaceRoot.fromProjectSafe(project);
  if (root == null) {
    return;
  }
  WorkspacePath workspacePath = WorkspacePath.createIfValid(contents.substring(1));
  if (workspacePath == null) {
    return;
  }
  File file = root.fileForPath(workspacePath);
  File parentPackageFile = findContainingPackage(project, file);
  if (parentPackageFile == null) {
    return;
  }
  WorkspacePath packagePath = root.workspacePathForSafe(parentPackageFile);
  if (packagePath == null) {
    return;
  }
  String relativePath =
      Paths.relativeIfUnder(workspacePath.relativePath(), packagePath.relativePath());
  if (relativePath == null) {
    return;
  }
  String newString = "//" + packagePath + ":" + relativePath + ".bzl";

  ASTNode node = importString.getNode();
  node.replaceChild(
      node.getFirstChildNode(), PsiUtils.createNewLabel(importString.getProject(), newString));
}
 
Example 7
Source File: EditorTestHelper.java    From intellij with Apache License 2.0 5 votes vote down vote up
public void replaceStringContents(StringLiteral string, String newStringContents) {
  Runnable renameOp =
      () -> {
        ASTNode node = string.getNode();
        node.replaceChild(
            node.getFirstChildNode(),
            PsiUtils.createNewLabel(string.getProject(), newStringContents));
      };
  ApplicationManager.getApplication()
      .runWriteAction(() -> CommandProcessor.getInstance().runUndoTransparentAction(renameOp));
}
 
Example 8
Source File: Intentions.java    From glsl4idea with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void replaceExpression(GLSLElement from, GLSLElement to) {
    final ASTNode newExpressionNode = to.getNode();
    final ASTNode oldExpressionNode = from.getNode();
    final PsiElement parentNode = from.getParent();
    final ASTNode grandParentNode = parentNode.getNode();

    if (!(grandParentNode == null || oldExpressionNode == null || newExpressionNode == null)) {
        grandParentNode.replaceChild(oldExpressionNode, newExpressionNode);
        reformat(parentNode);
    }
}
 
Example 9
Source File: CodeEditUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void replaceChild(ASTNode parent, @Nonnull ASTNode oldChild, @Nonnull ASTNode newChild) {
  saveWhitespacesInfo(oldChild);
  saveWhitespacesInfo(newChild);
  checkForOuters(oldChild);
  checkForOuters(newChild);

  LeafElement oldFirst = TreeUtil.findFirstLeaf(oldChild);

  parent.replaceChild(oldChild, newChild);
  final LeafElement firstLeaf = TreeUtil.findFirstLeaf(newChild);
  final ASTNode prevToken = TreeUtil.prevLeaf(newChild);
  if (firstLeaf != null) {
    final ASTNode nextLeaf = TreeUtil.nextLeaf(newChild);
    makePlaceHolderBetweenTokens(prevToken, firstLeaf, isFormattingRequired(prevToken, newChild), false);
    if (nextLeaf != null && !CharArrayUtil.containLineBreaks(nextLeaf.getText())) {
      makePlaceHolderBetweenTokens(TreeUtil.prevLeaf(nextLeaf), nextLeaf, false, false);
    }
  }
  else {
    if (oldFirst != null && prevToken == null) {
      ASTNode whitespaceNode = newChild.getTreeNext();
      if (whitespaceNode != null && whitespaceNode.getElementType() == TokenType.WHITE_SPACE) {
        // Replacing non-empty prefix to empty shall remove whitespace
        parent.removeChild(whitespaceNode);
      }
    }

    makePlaceHolderBetweenTokens(prevToken, TreeUtil.nextLeaf(newChild), isFormattingRequired(prevToken, newChild), false);
  }
}
 
Example 10
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);
  }
}