@SuppressWarnings({"HardCodedStringLiteral"}) private static TreeElement createCharEntity(char ch, CharTable charTable) { switch (ch) { case '<': return ASTFactory.leaf(XmlTokenType.XML_CHAR_ENTITY_REF, "<"); case '\'': return ASTFactory.leaf(XmlTokenType.XML_CHAR_ENTITY_REF, "'"); case '"': return ASTFactory.leaf(XmlTokenType.XML_CHAR_ENTITY_REF, """); case '>': return ASTFactory.leaf(XmlTokenType.XML_CHAR_ENTITY_REF, ">"); case '&': return ASTFactory.leaf(XmlTokenType.XML_CHAR_ENTITY_REF, "&"); case '\u00a0': return ASTFactory.leaf(XmlTokenType.XML_CHAR_ENTITY_REF, " "); default: final String charEncoding = "&#" + (int)ch + ";"; return ASTFactory.leaf(XmlTokenType.XML_CHAR_ENTITY_REF, charTable.intern(charEncoding)); } }
public static TreeElement addParenthToReplacedChild(@NotNull IElementType parenthType, @NotNull TreeElement newChild, @NotNull PsiManager manager) { CompositeElement parenthExpr = ASTFactory.composite(parenthType); TreeElement dummyExpr = (TreeElement)newChild.clone(); final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(newChild); new DummyHolder(manager, parenthExpr, null, charTableByTree); parenthExpr.putUserData(CharTable.CHAR_TABLE_KEY, charTableByTree); parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.LPARENTH, "(")); parenthExpr.rawAddChildren(dummyExpr); parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.RPARENTH, ")")); try { CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject()); PsiElement formatted = codeStyleManager.reformat(SourceTreeToPsiMap.treeToPsiNotNull(parenthExpr)); parenthExpr = (CompositeElement)SourceTreeToPsiMap.psiToTreeNotNull(formatted); } catch (IncorrectOperationException e) { LOG.error(e); // should not happen } newChild.putUserData(CharTable.CHAR_TABLE_KEY, SharedImplUtil.findCharTableByTree(newChild)); dummyExpr.getTreeParent().replaceChild(dummyExpr, newChild); // TODO remove explicit caches drop since this should be ok if we will use ChangeUtil for the modification TreeUtil.clearCaches(TreeUtil.getFileElement(parenthExpr)); return parenthExpr; }
@Override @NotNull public PsiElement createWhiteSpaceFromText(@NotNull @NonNls String text) throws IncorrectOperationException { final FileElement holderElement = DummyHolderFactory.createHolder(myManager, null).getTreeElement(); final LeafElement newElement = ASTFactory.leaf(TokenType.WHITE_SPACE, holderElement.getCharTable().intern(text)); holderElement.rawAddChildren(newElement); GeneratedMarkerVisitor.markGenerated(newElement.getPsi()); return newElement.getPsi(); }
@NotNull public static LeafElement createSingleLeafElement(@NotNull IElementType type, CharSequence buffer, int startOffset, int endOffset, CharTable table, PsiManager manager, PsiFile originalFile) { DummyHolder dummyHolder = DummyHolderFactory.createHolder(manager, table, type.getLanguage()); dummyHolder.setOriginalFile(originalFile); FileElement holderElement = dummyHolder.getTreeElement(); LeafElement newElement = ASTFactory.leaf(type, holderElement.getCharTable().intern( buffer, startOffset, endOffset)); holderElement.rawAddChildren(newElement); CodeEditUtil.setNodeGenerated(newElement, true); return newElement; }
@NotNull public static LeafElement createSingleLeafElement(@NotNull IElementType type, CharSequence buffer, int startOffset, int endOffset, CharTable table, PsiManager manager, boolean generatedFlag) { final FileElement holderElement = DummyHolderFactory.createHolder(manager, table, type.getLanguage()).getTreeElement(); final LeafElement newElement = ASTFactory.leaf(type, holderElement.getCharTable().intern( buffer, startOffset, endOffset)); holderElement.rawAddChildren(newElement); if(generatedFlag) CodeEditUtil.setNodeGenerated(newElement, true); return newElement; }
@NotNull public static CompositeElement createCompositeElement(@NotNull IElementType type, final CharTable charTableByTree, final PsiManager manager) { final FileElement treeElement = DummyHolderFactory.createHolder(manager, null, charTableByTree).getTreeElement(); final CompositeElement composite = ASTFactory.composite(type); treeElement.rawAddChildren(composite); return composite; }
@NotNull public LeafElement rawReplaceWithText(@NotNull String newText) { LeafElement newLeaf = ASTFactory.leaf(getElementType(), newText); copyUserDataTo(newLeaf); rawReplaceWithList(newLeaf); newLeaf.clearCaches(); return newLeaf; }
@NotNull public static LeafElement copyLeafWithText(@NotNull LeafElement original, @NotNull String text) { LeafElement element = ASTFactory.leaf(original.getElementType(), text); original.copyCopyableDataTo(element); encodeInformation(element, original); TreeUtil.clearCaches(element); saveIndentationToCopy(original, element); return element; }
@Override public LeafElement split(LeafElement leaf, int offset, final CharTable table) { final CharSequence chars = leaf.getChars(); final LeafElement leftPart = ASTFactory.leaf(leaf.getElementType(), table.intern(chars, 0, offset)); final LeafElement rightPart = ASTFactory.leaf(leaf.getElementType(), table.intern(chars, offset, chars.length())); leaf.rawInsertAfterMe(leftPart); leftPart.rawInsertAfterMe(rightPart); leaf.rawRemove(); return leftPart; }
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); } }
@Nullable public static TreeElement createTokenElement(Lexer lexer, CharTable table) { IElementType tokenType = lexer.getTokenType(); if (tokenType == null) { return null; } else if (tokenType instanceof ILazyParseableElementType) { return ASTFactory.lazy((ILazyParseableElementType)tokenType, LexerUtil.internToken(lexer, table)); } else { return ASTFactory.leaf(tokenType, LexerUtil.internToken(lexer, table)); } }
@Override public ASTNode addInternal(ASTNode first, ASTNode last, ASTNode anchor, Boolean before) { if (first.getPsi() instanceof PyStatement && getStatements().length == 1) { ASTNode treePrev = getNode().getTreePrev(); if (treePrev != null && treePrev.getElementType() == TokenType.WHITE_SPACE && !treePrev.textContains('\n')) { ASTNode lineBreak = ASTFactory.whitespace("\n"); treePrev.getTreeParent().replaceChild(treePrev, lineBreak); } } return super.addInternal(first, last, anchor, before); }
public void addArgumentFirst(PyExpression arg) { ASTNode node = getNode(); ASTNode[] pars = node.getChildren(TokenSet.create(PyTokenTypes.LPAR)); if (pars.length == 0) { // there's no starting paren try { add(arg); } catch (IncorrectOperationException e1) { throw new IllegalStateException(e1); } } else { ASTNode before = PyPsiUtils.getNextNonWhitespaceSibling(pars[0]); ASTNode anchorBefore; if (before != null && elementPrecedesElementsOfType(before, PythonDialectsTokenSetProvider.INSTANCE.getExpressionTokens())) { ASTNode comma = createComma(); node.addChild(comma, before); node.addChild(ASTFactory.whitespace(" "), before); anchorBefore = comma; } else { anchorBefore = before; } ASTNode argNode = arg.getNode(); if (anchorBefore == null) { node.addChild(argNode); } else { node.addChild(argNode, anchorBefore); } } }
private void addArgumentNode(PyExpression arg, ASTNode beforeThis, boolean commaFirst) { ASTNode comma = PyElementGenerator.getInstance(getProject()).createComma(); ASTNode node = getNode(); ASTNode argNode = arg.getNode(); if (commaFirst) { node.addChild(comma, beforeThis); node.addChild(ASTFactory.whitespace(" "), beforeThis); node.addChild(argNode, beforeThis); } else { node.addChild(argNode, beforeThis); node.addChild(comma, beforeThis); node.addChild(ASTFactory.whitespace(" "), beforeThis); } }
@Override public XmlToken handleContentChange(@NotNull XmlToken xmlToken, @NotNull TextRange range, String newContent) throws IncorrectOperationException { String oldText = xmlToken.getText(); String newText = oldText.substring(0, range.getStartOffset()) + newContent + oldText.substring(range.getEndOffset()); IElementType tokenType = xmlToken.getTokenType(); FileElement holder = DummyHolderFactory.createHolder(xmlToken.getManager(), null).getTreeElement(); LeafElement leaf = ASTFactory.leaf(tokenType, holder.getCharTable().intern(newText)); holder.rawAddChildren(leaf); return (XmlToken)xmlToken.replace(leaf.getPsi()); }
@SuppressWarnings({"HardCodedStringLiteral"}) public static FileElement createCDATAElement(final PsiManager manager, final CharTable charTableByTree, final String displayText) { final FileElement dummyParent = DummyHolderFactory.createHolder(manager, null, charTableByTree).getTreeElement(); final CompositeElement cdata = ASTFactory.composite(XmlElementType.XML_CDATA); dummyParent.rawAddChildren(cdata); cdata.rawAddChildren(ASTFactory.leaf(XmlTokenType.XML_CDATA_START, "<![CDATA[")); cdata.rawAddChildren(ASTFactory.leaf(XmlTokenType.XML_DATA_CHARACTERS, dummyParent.getCharTable().intern(displayText))); cdata.rawAddChildren(ASTFactory.leaf(XmlTokenType.XML_CDATA_END, "]]>")); dummyParent.acceptTree(new GeneratedMarkerVisitor()); return dummyParent; }
@Override @NotNull public XmlText createDisplayText(@NotNull String s) throws IncorrectOperationException { final XmlTag tagFromText = createTagFromText("<a>" + XmlTagUtil.getCDATAQuote(s) + "</a>"); final XmlText[] textElements = tagFromText.getValue().getTextElements(); if (textElements.length == 0) return (XmlText)ASTFactory.composite(XmlElementType.XML_TEXT); return textElements[0]; }
public static TreeElement addParenthToReplacedChild(@NotNull IElementType parenthType, @NotNull TreeElement newChild, @NotNull PsiManager manager) { CompositeElement parenthExpr = ASTFactory.composite(parenthType); TreeElement dummyExpr = (TreeElement)newChild.clone(); final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(newChild); new DummyHolder(manager, parenthExpr, null, charTableByTree); parenthExpr.putUserData(CharTable.CHAR_TABLE_KEY, charTableByTree); parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.LPARENTH, "(")); parenthExpr.rawAddChildren(dummyExpr); parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.RPARENTH, ")")); try { CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject()); PsiElement formatted = codeStyleManager.reformat(SourceTreeToPsiMap.treeToPsiNotNull(parenthExpr)); parenthExpr = (CompositeElement)SourceTreeToPsiMap.psiToTreeNotNull(formatted); } catch (IncorrectOperationException e) { LOG.error(e); // should not happen } newChild.putUserData(CharTable.CHAR_TABLE_KEY, SharedImplUtil.findCharTableByTree(newChild)); dummyExpr.rawReplaceWithList(newChild); newChild = parenthExpr; // TODO remove explicit caches drop since this should be ok if we will use ChangeUtil for the modification TreeUtil.clearCaches(TreeUtil.getFileElement(newChild)); return newChild; }
public static LeafElement createSingleLeafElement(IElementType type, CharSequence buffer, int startOffset, int endOffset, CharTable table, PsiManager manager, PsiFile originalFile) { DummyHolder dummyHolder = DummyHolderFactory.createHolder(manager, table, type.getLanguage()); dummyHolder.setOriginalFile(originalFile); FileElement holderElement = dummyHolder.getTreeElement(); LeafElement newElement = ASTFactory.leaf(type, holderElement.getCharTable().intern( buffer, startOffset, endOffset)); holderElement.rawAddChildren(newElement); CodeEditUtil.setNodeGenerated(newElement, true); return newElement; }
public static LeafElement createSingleLeafElement(IElementType type, CharSequence buffer, int startOffset, int endOffset, CharTable table, PsiManager manager, boolean generatedFlag) { final FileElement holderElement = DummyHolderFactory.createHolder(manager, table, type.getLanguage()).getTreeElement(); final LeafElement newElement = ASTFactory.leaf(type, holderElement.getCharTable().intern( buffer, startOffset, endOffset)); holderElement.rawAddChildren(newElement); if(generatedFlag) CodeEditUtil.setNodeGenerated(newElement, true); return newElement; }
@NotNull public static CompositeElement createCompositeElement(final IElementType type, final CharTable charTableByTree, final PsiManager manager) { final FileElement treeElement = DummyHolderFactory.createHolder(manager, null, charTableByTree).getTreeElement(); final CompositeElement composite = ASTFactory.composite(type); treeElement.rawAddChildren(composite); return composite; }
public LeafElement rawReplaceWithText(String newText) { LeafElement newLeaf = ASTFactory.leaf(getElementType(), newText); copyUserDataTo(newLeaf); rawReplaceWithList(newLeaf); newLeaf.clearCaches(); return newLeaf; }
public static LeafElement copyLeafWithText(LeafElement original, String text) { LeafElement element = ASTFactory.leaf(original.getElementType(), text); original.copyCopyableDataTo(element); encodeInformation(element, original); TreeUtil.clearCaches(element); saveIndentationToCopy(original, element); return element; }
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.length() == 0 && lastWS == null) { return; } if (lastWS != null && whiteSpace.length() == 0) { 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); } }
public XmlToken handleContentChange(XmlToken xmlToken, TextRange range, String newContent) throws IncorrectOperationException { String oldText = xmlToken.getText(); String newText = oldText.substring(0, range.getStartOffset()) + newContent + oldText.substring(range.getEndOffset()); IElementType tokenType = xmlToken.getTokenType(); FileElement holder = DummyHolderFactory.createHolder(xmlToken.getManager(), null).getTreeElement(); LeafElement leaf = ASTFactory.leaf(tokenType, holder.getCharTable().intern(newText)); holder.rawAddChildren(leaf); return (XmlToken)xmlToken.replace(leaf.getPsi()); }
@Override @Nonnull public PsiElement createWhiteSpaceFromText(@Nonnull @NonNls String text) throws IncorrectOperationException { final FileElement holderElement = DummyHolderFactory.createHolder(myManager, null).getTreeElement(); final LeafElement newElement = ASTFactory.leaf(TokenType.WHITE_SPACE, holderElement.getCharTable().intern(text)); holderElement.rawAddChildren(newElement); GeneratedMarkerVisitor.markGenerated(newElement.getPsi()); return newElement.getPsi(); }
@Nonnull public static LeafElement createSingleLeafElement(@Nonnull IElementType type, CharSequence buffer, int startOffset, int endOffset, CharTable table, PsiManager manager, PsiFile originalFile) { DummyHolder dummyHolder = DummyHolderFactory.createHolder(manager, table, type.getLanguage()); dummyHolder.setOriginalFile(originalFile); FileElement holderElement = dummyHolder.getTreeElement(); LeafElement newElement = ASTFactory.leaf(type, holderElement.getCharTable().intern( buffer, startOffset, endOffset)); holderElement.rawAddChildren(newElement); CodeEditUtil.setNodeGenerated(newElement, true); return newElement; }
@Nonnull public static LeafElement createSingleLeafElement(@Nonnull IElementType type, CharSequence buffer, int startOffset, int endOffset, CharTable table, PsiManager manager, boolean generatedFlag) { final FileElement holderElement = DummyHolderFactory.createHolder(manager, table, type.getLanguage()).getTreeElement(); final LeafElement newElement = ASTFactory.leaf(type, holderElement.getCharTable().intern( buffer, startOffset, endOffset)); holderElement.rawAddChildren(newElement); if(generatedFlag) CodeEditUtil.setNodeGenerated(newElement, true); return newElement; }
@Nonnull public static CompositeElement createCompositeElement(@Nonnull IElementType type, final CharTable charTableByTree, final PsiManager manager) { final FileElement treeElement = DummyHolderFactory.createHolder(manager, null, charTableByTree).getTreeElement(); final CompositeElement composite = ASTFactory.composite(type); treeElement.rawAddChildren(composite); return composite; }
@Override public void addLeaf(@Nonnull final IElementType leafType, final CharSequence leafText, final ASTNode anchorBefore) { FileElement holder = new DummyHolder(getManager(), null).getTreeElement(); final LeafElement leaf = ASTFactory.leaf(leafType, holder.getCharTable().intern(leafText)); CodeEditUtil.setNodeGenerated(leaf, true); holder.rawAddChildren(leaf); addChild(leaf, anchorBefore); }
@Nonnull public static LeafElement copyLeafWithText(@Nonnull LeafElement original, @Nonnull String text) { LeafElement element = ASTFactory.leaf(original.getElementType(), text); original.copyCopyableDataTo(element); encodeInformation(element, original); TreeUtil.clearCaches(element); saveIndentationToCopy(original, element); return element; }