@Override public void deleteChildInternal(@NotNull ASTNode child) { if (child.getElementType() == CODE_BLOCK) { final ASTNode prevWS = TreeUtil.prevLeaf(child); if (prevWS != null && prevWS.getElementType() == TokenType.WHITE_SPACE) { removeChild(prevWS); } super.deleteChildInternal(child); final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this); LeafElement semicolon = Factory.createSingleLeafElement(SEMICOLON, ";", 0, 1, treeCharTab, getManager()); addInternal(semicolon, semicolon, null, Boolean.TRUE); } else { super.deleteChildInternal(child); } }
private static void addNewLineToTag(CompositeElement tag, Project project) { LOG.assertTrue(tag != null && tag.getElementType() == DOC_TAG); ASTNode current = tag.getLastChildNode(); while (current != null && current.getElementType() == DOC_COMMENT_DATA && isWhitespaceCommentData(current)) { current = current.getTreePrev(); } if (current != null && current.getElementType() == DOC_COMMENT_LEADING_ASTERISKS) return; final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(tag); final ASTNode newLine = Factory.createSingleLeafElement(DOC_COMMENT_DATA, "\n", 0, 1, treeCharTab, SharedImplUtil.getManagerByTree(tag)); tag.addChild(newLine, null); ASTNode leadingWhitespaceAnchor = null; if (JavaCodeStyleSettingsFacade.getInstance(project).isJavaDocLeadingAsterisksEnabled()) { final TreeElement leadingAsterisk = Factory.createSingleLeafElement(DOC_COMMENT_LEADING_ASTERISKS, "*", 0, 1, treeCharTab, SharedImplUtil.getManagerByTree(tag)); leadingWhitespaceAnchor = tag.addInternal(leadingAsterisk, leadingAsterisk, null, Boolean.TRUE); } final TreeElement commentData = Factory.createSingleLeafElement(DOC_COMMENT_DATA, " ", 0, 1, treeCharTab, SharedImplUtil.getManagerByTree(tag)); tag.addInternal(commentData, commentData, leadingWhitespaceAnchor, Boolean.TRUE); }
public DummyHolder(@NotNull PsiManager manager, @Nullable TreeElement contentElement, @Nullable PsiElement context, @Nullable CharTable table, @Nullable Boolean validity, Language language) { super(TokenType.DUMMY_HOLDER, TokenType.DUMMY_HOLDER, new DummyHolderViewProvider(manager)); myLanguage = language; ((DummyHolderViewProvider)getViewProvider()).setDummyHolder(this); myContext = context; myTable = table != null ? table : IdentityCharTable.INSTANCE; if (contentElement instanceof FileElement) { myFileElement = (FileElement)contentElement; myFileElement.setPsi(this); myFileElement.setCharTable(myTable); } else if (contentElement != null) { getTreeElement().rawAddChildren(contentElement); clearCaches(); } myExplicitlyValid = validity; }
@NotNull private static Pair<PsiElement, CharTable> doFindWhiteSpaceNode(@NotNull PsiFile file, int offset) { ASTNode astNode = SourceTreeToPsiMap.psiElementToTree(file); if (!(astNode instanceof FileElement)) { return new Pair<PsiElement, CharTable>(null, null); } PsiElement elementAt = InjectedLanguageUtil.findInjectedElementNoCommit(file, offset); final CharTable charTable = ((FileElement)astNode).getCharTable(); if (elementAt == null) { elementAt = findElementInTreeWithFormatterEnabled(file, offset); } if( elementAt == null) { return new Pair<PsiElement, CharTable>(null, charTable); } ASTNode node = elementAt.getNode(); if (node == null || node.getElementType() != TokenType.WHITE_SPACE) { return new Pair<PsiElement, CharTable>(null, charTable); } return Pair.create(elementAt, charTable); }
@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)); } }
@Override public void deleteChildInternal(@NotNull ASTNode child) { if (child.getElementType() == CODE_BLOCK){ final ASTNode prevWS = TreeUtil.prevLeaf(child); if (prevWS != null && prevWS.getElementType() == TokenType.WHITE_SPACE) { removeChild(prevWS); } super.deleteChildInternal(child); final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this); LeafElement semicolon = Factory.createSingleLeafElement(SEMICOLON, ";", 0, 1, treeCharTab, getManager()); addInternal(semicolon, semicolon, null, Boolean.TRUE); } else { super.deleteChildInternal(child); } }
public PsiBuilderImpl(Project project, PsiFile containingFile, @NotNull TokenSet whiteSpaces, @NotNull TokenSet comments, @NotNull Lexer lexer, CharTable charTable, @NotNull final CharSequence text, @Nullable ASTNode originalTree, @Nullable MyTreeStructure parentLightTree) { myProject = project; myFile = containingFile; myText = text; myTextArray = CharArrayUtil.fromSequenceWithoutCopying(text); myLexer = lexer; myWhitespaces = whiteSpaces; myComments = comments; myCharTable = charTable; myOriginalTree = originalTree; myParentLightTree = parentLightTree; cacheLexemes(); }
@NotNull private static Pair<PsiElement, CharTable> doFindWhiteSpaceNode(@NotNull PsiFile file, int offset) { ASTNode astNode = SourceTreeToPsiMap.psiElementToTree(file); if (!(astNode instanceof FileElement)) { return new Pair<PsiElement, CharTable>(null, null); } PsiElement elementAt = InjectedLanguageUtil.findInjectedElementNoCommit(file, offset); final CharTable charTable = ((FileElement)astNode).getCharTable(); if (elementAt == null) { elementAt = findElementInTreeWithFormatterEnabled(file, offset); } if( elementAt == null) { return new Pair<PsiElement, CharTable>(null, charTable); } ASTNode node = elementAt.getNode(); if (node == null || node.getElementType() != TokenType.WHITE_SPACE) { return new Pair<PsiElement, CharTable>(null, charTable); } return new Pair<PsiElement, CharTable>(elementAt, charTable); }
public DummyHolder(@Nonnull PsiManager manager, @Nullable TreeElement contentElement, @Nullable PsiElement context, @Nullable CharTable table, @Nullable Boolean validity, Language language) { super(TokenType.DUMMY_HOLDER, TokenType.DUMMY_HOLDER, new DummyHolderViewProvider(manager)); myLanguage = language; ((DummyHolderViewProvider)getViewProvider()).setDummyHolder(this); myContext = context; myTable = table != null ? table : IdentityCharTable.INSTANCE; if (contentElement instanceof FileElement) { ((FileElement)contentElement).setPsi(this); ((FileElement)contentElement).setCharTable(myTable); setTreeElementPointer((FileElement)contentElement); } else if (contentElement != null) { getTreeElement().rawAddChildren(contentElement); clearCaches(); } myExplicitlyValid = validity; }
@Nonnull private static Pair<PsiElement, CharTable> doFindWhiteSpaceNode(@Nonnull PsiFile file, int offset) { ASTNode astNode = SourceTreeToPsiMap.psiElementToTree(file); if (!(astNode instanceof FileElement)) { return new Pair<>(null, null); } PsiElement elementAt = InjectedLanguageManager.getInstance(file.getProject()).findInjectedElementAt(file, offset); final CharTable charTable = ((FileElement)astNode).getCharTable(); if (elementAt == null) { elementAt = findElementInTreeWithFormatterEnabled(file, offset); } if( elementAt == null) { return new Pair<>(null, charTable); } ASTNode node = elementAt.getNode(); if (node == null || node.getElementType() != TokenType.WHITE_SPACE) { return new Pair<>(null, charTable); } return Pair.create(elementAt, charTable); }
private static void scanChildren(CompositeElement element, ASTNode node, TokenSet listTypes, boolean forward) { ASTNode child = node; while(true) { child = (forward ? child.getTreeNext() : child.getTreePrev()); if(child == null || child.getElementType() == JavaTokenType.COMMA) { break; } if(listTypes.contains(child.getElementType())) { CharTable charTable = SharedImplUtil.findCharTableByTree(element); PsiManager manager = element.getPsi().getManager(); TreeElement comma = Factory.createSingleLeafElement(JavaTokenType.COMMA, ",", 0, 1, charTable, manager); element.addInternal(comma, comma, (forward ? node : child), Boolean.FALSE); break; } } }
@Override public void deleteChildInternal(@NotNull ASTNode child) { if(child.getElementType() == CODE_BLOCK) { final ASTNode prevWS = TreeUtil.prevLeaf(child); if(prevWS != null && prevWS.getElementType() == TokenType.WHITE_SPACE) { removeChild(prevWS); } super.deleteChildInternal(child); final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this); LeafElement semicolon = Factory.createSingleLeafElement(SEMICOLON, ";", 0, 1, treeCharTab, getManager()); addInternal(semicolon, semicolon, null, Boolean.TRUE); } else { super.deleteChildInternal(child); } }
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; }
private static void scanChildren(CompositeElement element, ASTNode node, TokenSet listTypes, boolean forward) { ASTNode child = node; while (true) { child = (forward ? child.getTreeNext() : child.getTreePrev()); if (child == null || child.getElementType() == JavaTokenType.COMMA) break; if (listTypes.contains(child.getElementType())) { CharTable charTable = SharedImplUtil.findCharTableByTree(element); PsiManager manager = element.getPsi().getManager(); TreeElement comma = Factory.createSingleLeafElement(JavaTokenType.COMMA, ",", 0, 1, charTable, manager); element.addInternal(comma, comma, (forward ? node : child), Boolean.FALSE); break; } } }
@Override public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before){ if (first == last && first.getElementType() == JavaElementType.TYPE){ if (getLastChildNode() != null && getLastChildNode().getElementType() == TokenType.ERROR_ELEMENT){ super.deleteChildInternal(getLastChildNode()); } } final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this); if (getFirstChildNode()== null || getFirstChildNode().getElementType() != JavaTokenType.LT){ TreeElement lt = Factory.createSingleLeafElement(JavaTokenType.LT, "<", 0, 1, treeCharTab, getManager()); super.addInternal(lt, lt, getFirstChildNode(), Boolean.TRUE); } if (getLastChildNode() == null || getLastChildNode().getElementType() != JavaTokenType.GT){ TreeElement gt = Factory.createSingleLeafElement(JavaTokenType.GT, ">", 0, 1, treeCharTab, getManager()); super.addInternal(gt, gt, getLastChildNode(), Boolean.FALSE); } if (anchor == null){ if (before == null || before.booleanValue()){ anchor = findChildByRole(ChildRole.GT_IN_TYPE_LIST); before = Boolean.TRUE; } else{ anchor = findChildByRole(ChildRole.LT_IN_TYPE_LIST); before = Boolean.FALSE; } } final TreeElement firstAdded = super.addInternal(first, last, anchor, before); if (first == last && first.getElementType() == JavaElementType.TYPE) { JavaSourceUtil.addSeparatingComma(this, first, TYPE_SET); } return firstAdded; }
@Override public TreeElement addInternal(final TreeElement first, final ASTNode last, ASTNode anchor, Boolean before) { TreeElement lt = (TreeElement)findChildByRole(ChildRole.LT_IN_TYPE_LIST); final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this); if (lt == null) { lt = Factory.createSingleLeafElement(JavaTokenType.LT, "<", 0, 1, treeCharTab, getManager()); super.addInternal(lt, lt, getFirstChildNode(), Boolean.TRUE); } TreeElement gt = (TreeElement)findChildByRole(ChildRole.GT_IN_TYPE_LIST); if (gt == null) { gt = Factory.createSingleLeafElement(JavaTokenType.GT, ">", 0, 1, treeCharTab, getManager()); super.addInternal(gt, gt, getLastChildNode(), Boolean.FALSE); } if (anchor == null) { if (before == null || before.booleanValue()){ anchor = gt; before = Boolean.TRUE; } else{ anchor = lt; before = Boolean.FALSE; } } TreeElement firstAdded = super.addInternal(first, last, anchor, before); if (first == last && first.getElementType() == JavaElementType.TYPE_PARAMETER) { JavaSourceUtil.addSeparatingComma(this, first, TYPE_PARAMETER_SET); } return firstAdded; }
@Override public void deleteChildInternal(@NotNull ASTNode child) { if (isEnum() && child.getElementType() == ENUM_CONSTANT) { JavaSourceUtil.deleteSeparatingComma(this, child); } if (child.getElementType() == FIELD) { final ASTNode nextField = TreeUtil.findSibling(child.getTreeNext(), FIELD); if (nextField != null && ((PsiField)nextField.getPsi()).getTypeElement().equals(((PsiField)child.getPsi()).getTypeElement())) { final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this); final ASTNode modifierList = child.findChildByType(MODIFIER_LIST); if (modifierList != null) { LeafElement whitespace = Factory.createSingleLeafElement(WHITE_SPACE, " ", 0, 1, treeCharTab, getManager()); final ASTNode first = nextField.getFirstChildNode(); nextField.addChild(whitespace, first); final ASTNode typeElement = child.findChildByType(TYPE); if (typeElement == null) { final TreeElement modifierListCopy = ChangeUtil.copyElement((TreeElement)modifierList, treeCharTab); nextField.addChild(modifierListCopy, whitespace); } else { ASTNode run = modifierList; do { final TreeElement copy = ChangeUtil.copyElement((TreeElement)run, treeCharTab); nextField.addChild(copy, whitespace); if (run == typeElement) break; else run = run.getTreeNext(); } while(true); } } } } super.deleteChildInternal(child); }
@Override public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) { final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this); final TreeElement treeElement = super.addInternal(first, last, anchor, before); if (first == last && first.getElementType() == JavaTokenType.IDENTIFIER) { LeafElement eq = Factory.createSingleLeafElement(JavaTokenType.EQ, "=", 0, 1, treeCharTab, getManager()); super.addInternal(eq, eq, first, Boolean.FALSE); } return treeElement; }
private void processElementaryChange(ASTNode parent, ASTNode element, ChangeInfo change, int depth) { TreeChange treeChange = myChangedElements.get(parent); if (treeChange == null) { treeChange = new TreeChangeImpl(parent); myChangedElements.put(parent, treeChange); final int index = depth >= 0 ? depth : getDepth(parent); addToEqualsDepthList(index, parent); } treeChange.addChange(element, change); if (change.getChangeType() == ChangeInfo.REMOVED) { element.putUserData(CharTable.CHAR_TABLE_KEY, myFileElement.getCharTable()); } if (treeChange.isEmpty()) removeAssociatedChanges(parent, depth); }
@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 static CharTable findCharTableByTree(ASTNode tree) { while (tree != null) { final CharTable userData = tree.getUserData(CharTable.CHAR_TABLE_KEY); if (userData != null) return userData; if (tree instanceof FileElement) return ((FileElement)tree).getCharTable(); tree = tree.getTreeParent(); } LOG.error("Invalid root element"); return null; }
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); }
public static TreeElement copyElement(TreeElement original, final PsiElement context, CharTable table) { final TreeElement element = (TreeElement)original.clone(); final PsiManager manager = original.getManager(); DummyHolderFactory.createHolder(manager, element, context, table).getTreeElement(); encodeInformation(element, original); TreeUtil.clearCaches(element); saveIndentationToCopy(original, element); return element; }
@Nullable public static TreeElement generateTreeElement(PsiElement original, CharTable table, final PsiManager manager) { PsiUtilCore.ensureValid(original); if (SourceTreeToPsiMap.hasTreeElement(original)) { return copyElement((TreeElement)SourceTreeToPsiMap.psiElementToTree(original), table); } else { for (TreeGenerator generator : Extensions.getExtensions(TreeGenerator.EP_NAME)) { final TreeElement element = generator.generateTreeFor(original, table, manager); if (element != null) return element; } return null; } }
@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; }
private void insertOuters(TreeElement root, Lexer lexer, final CharTable table) { TreePatcher patcher = TREE_PATCHER.forLanguage(root.getPsi().getLanguage()); int treeOffset = 0; LeafElement leaf = TreeUtil.findFirstLeaf(root); while (lexer.getTokenType() != null) { IElementType tt = lexer.getTokenType(); if (tt != myTemplateElementType) { while (leaf != null && treeOffset < lexer.getTokenStart()) { treeOffset += leaf.getTextLength(); if (treeOffset > lexer.getTokenStart()) { leaf = patcher.split(leaf, leaf.getTextLength() - (treeOffset - lexer.getTokenStart()), table); treeOffset = lexer.getTokenStart(); } leaf = (LeafElement)TreeUtil.nextLeaf(leaf); } if (leaf == null) break; final OuterLanguageElementImpl newLeaf = createOuterLanguageElement(lexer, table, myOuterElementType); patcher.insert(leaf.getTreeParent(), leaf, newLeaf); leaf.getTreeParent().subtreeChanged(); leaf = newLeaf; } lexer.advance(); } if (lexer.getTokenType() != null) { assert lexer.getTokenType() != myTemplateElementType; final OuterLanguageElementImpl newLeaf = createOuterLanguageElement(lexer, table, myOuterElementType); ((CompositeElement)root).rawAddChildren(newLeaf); ((CompositeElement)root).subtreeChanged(); } }
protected OuterLanguageElementImpl createOuterLanguageElement(final Lexer lexer, final CharTable table, @NotNull IElementType outerElementType) { final CharSequence buffer = lexer.getBufferSequence(); final int tokenStart = lexer.getTokenStart(); if (tokenStart < 0 || tokenStart > buffer.length()) { LOG.error("Invalid start: " + tokenStart + "; " + lexer); } final int tokenEnd = lexer.getTokenEnd(); if (tokenEnd < 0 || tokenEnd > buffer.length()) { LOG.error("Invalid end: " + tokenEnd + "; " + lexer); } return new OuterLanguageElementImpl(outerElementType, table.intern(buffer, tokenStart, tokenEnd)); }
@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)); } }
public static <T extends PsiElement> T handleContentChange(T element, TextRange range, String newContent, final IElementType tokenType) { CheckUtil.checkWritable(element); final CompositeElement attrNode = (CompositeElement)element.getNode(); final ASTNode valueNode = attrNode.findLeafElementAt(range.getStartOffset()); LOG.assertTrue(valueNode != null, "Leaf not found in " + attrNode + " at offset " + range.getStartOffset() + " in element " + element); final PsiElement elementToReplace = valueNode.getPsi(); String text; try { text = elementToReplace.getText(); final int offsetInParent = elementToReplace.getStartOffsetInParent(); String textBeforeRange = text.substring(0, range.getStartOffset() - offsetInParent); String textAfterRange = text.substring(range.getEndOffset()- offsetInParent, text.length()); newContent = element.getText().startsWith("'") || element.getText().endsWith("'") ? newContent.replace("'", "'") : newContent.replace("\"", """); text = textBeforeRange + newContent + textAfterRange; } catch(StringIndexOutOfBoundsException e) { LOG.error("Range: " + range + " in text: '" + element.getText() + "'", e); throw e; } final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(attrNode); final LeafElement newValueElement = Factory.createSingleLeafElement(tokenType, text, charTableByTree, element.getManager()); attrNode.replaceChildInternal(valueNode, newValueElement); return element; }
@Override public PsiElement setName(@NotNull final String name) throws IncorrectOperationException { final PomModel model = PomManager.getModel(getProject()); final XmlAspect aspect = model.getModelAspect(XmlAspect.class); model.runTransaction(new PomTransactionBase(this, aspect) { @Override public PomModelEvent runInner() throws IncorrectOperationException { final String oldName = getName(); final XmlTagImpl dummyTag = (XmlTagImpl)XmlElementFactory.getInstance(getProject()).createTagFromText(XmlTagUtil.composeTagText(name, "aa")); final XmlTagImpl tag = XmlTagImpl.this; final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(tag); ASTNode child = XmlChildRole.START_TAG_NAME_FINDER.findChild(tag); LOG.assertTrue(child != null, "It seems '" + name + "' is not a valid tag name"); TreeElement tagElement = (TreeElement)XmlChildRole.START_TAG_NAME_FINDER.findChild(dummyTag); LOG.assertTrue(tagElement != null, "What's wrong with it? '" + name + "'"); tag.replaceChild(child, ChangeUtil.copyElement(tagElement, charTableByTree)); final ASTNode childByRole = XmlChildRole.CLOSING_TAG_NAME_FINDER.findChild(tag); if (childByRole != null) { final TreeElement treeElement = (TreeElement)XmlChildRole.CLOSING_TAG_NAME_FINDER.findChild(dummyTag); if (treeElement != null) { tag.replaceChild(childByRole, ChangeUtil.copyElement(treeElement, charTableByTree)); } } return XmlTagNameChangedImpl.createXmlTagNameChanged(model, tag, oldName); } }); return this; }
@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 public TreeElement generateTreeFor(PsiElement original, CharTable table, PsiManager manager) { if (original instanceof GrModifierList) { final String text = original.getText(); assert text != null : "Text is null for " + original + "; " + original.getClass(); final GrModifierList modifierList = GroovyPsiElementFactory.getInstance(manager.getProject()).createModifierList(text); return (TreeElement)modifierList.getNode(); } return null; }
@Nullable @Override public TreeElement generateTreeFor(PsiElement original, CharTable table, PsiManager manager) { TreeElement result = null; if (original instanceof SqliteMagicLightMethodBuilder) { result = ChangeUtil.copyElement((TreeElement) SourceTreeToPsiMap.psiElementToTree(original), table); } return result; }