use of com.intellij.util.CharTable in project intellij-community by JetBrains.
the class FieldElement method copyElement.
@Override
public ASTNode copyElement() {
final CharTable table = SharedImplUtil.findCharTableByTree(this);
final PsiClass psiClass = ((PsiField) getPsi()).getContainingClass();
return psiClass != null ? ChangeUtil.copyElement(this, psiClass.getTypeParameterList(), table) : super.copyElement();
}
use of com.intellij.util.CharTable in project intellij-community by JetBrains.
the class ShiftIndentInsideHelper method shiftIndentInside.
public ASTNode shiftIndentInside(ASTNode element, int indentShift) {
if (indentShift == 0)
return element;
final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(element);
String text = element.getText();
for (int offset = 0; offset < text.length(); offset++) {
char c = text.charAt(offset);
if (c == '\n' || c == '\r') {
int offset1;
for (offset1 = offset + 1; offset1 < text.length(); offset1++) {
c = text.charAt(offset1);
if (c != ' ' && c != '\t')
break;
}
if (c == '\n' || c == '\r')
continue;
String space = text.substring(offset + 1, offset1);
int indent = IndentHelperImpl.getIndent(myProject, myFileType, space, true);
int newIndent = indent + indentShift;
newIndent = Math.max(newIndent, 0);
String newSpace = IndentHelperImpl.fillIndent(myProject, myFileType, newIndent);
ASTNode leaf = element.findLeafElementAt(offset);
if (!mayShiftIndentInside(leaf)) {
LOG.error("Error", leaf.getElementType().toString(), "Type: " + leaf.getElementType() + " text: " + leaf.getText());
}
if (offset1 < text.length()) {
ASTNode next = element.findLeafElementAt(offset1);
if ((next.getElementType() == JavaTokenType.END_OF_LINE_COMMENT || next.getElementType() == JavaTokenType.C_STYLE_COMMENT || next.getElementType() == JspTokenType.JSP_COMMENT) && next != element) {
if (mySettings.KEEP_FIRST_COLUMN_COMMENT) {
int commentIndent = myIndentIndentHelper.getIndent(myProject, myFileType, next, true);
if (commentIndent == 0)
continue;
}
} else if (next.getElementType() == XmlTokenType.XML_DATA_CHARACTERS) {
continue;
}
}
int leafOffset = getStartOffset(element, leaf);
if (leaf.getElementType() == JavaDocTokenType.DOC_COMMENT_DATA && leafOffset + leaf.getTextLength() == offset + 1) {
ASTNode next = element.findLeafElementAt(offset + 1);
if (next.getElementType() == TokenType.WHITE_SPACE) {
leaf = next;
leafOffset = getStartOffset(element, leaf);
} else {
if (!newSpace.isEmpty()) {
LeafElement newLeaf = ASTFactory.whitespace(newSpace);
next.getTreeParent().addChild(newLeaf, next);
}
text = text.substring(0, offset + 1) + newSpace + text.substring(offset1);
continue;
}
}
int startOffset = offset + 1 - leafOffset;
int endOffset = offset1 - leafOffset;
if (!LOG.assertTrue(0 <= startOffset && startOffset <= endOffset && endOffset <= leaf.getTextLength())) {
continue;
}
String leafText = leaf.getText();
String newLeafText = leafText.substring(0, startOffset) + newSpace + leafText.substring(endOffset);
if (!newLeafText.isEmpty()) {
LeafElement newLeaf = Factory.createSingleLeafElement(leaf.getElementType(), newLeafText, charTableByTree, SharedImplUtil.getManagerByTree(leaf));
if (leaf.getTreeParent() != null) {
leaf.getTreeParent().replaceChild(leaf, newLeaf);
}
if (leaf == element) {
element = newLeaf;
}
} else {
ASTNode parent = leaf.getTreeParent();
if (parent != null) {
parent.removeChild(leaf);
}
}
text = text.substring(0, offset + 1) + newSpace + text.substring(offset1);
}
}
return element;
}
use of com.intellij.util.CharTable in project intellij-community by JetBrains.
the class ReplaceChangeInfoImpl method setReplaced.
public void setReplaced(@NotNull ASTNode replaced) {
CharTable charTableByTree = myNewNode.getTreeParent() == null ? null : SharedImplUtil.findCharTableByTree(myNewNode);
setOldLength(((TreeElement) replaced).getNotCachedLength());
myOldNode = replaced;
myOldNode.putUserData(CharTable.CHAR_TABLE_KEY, charTableByTree);
}
use of com.intellij.util.CharTable in project intellij-community by JetBrains.
the class DefaultXmlPsiPolicy method encodeXmlTextContents.
@Override
public ASTNode encodeXmlTextContents(String displayText, PsiElement text) {
final PsiFile containingFile = text.getContainingFile();
CharTable charTable = SharedImplUtil.findCharTableByTree(text.getNode());
final FileElement dummyParent = DummyHolderFactory.createHolder(text.getManager(), null, charTable).getTreeElement();
final XmlTag rootTag = ((XmlFile) PsiFileFactory.getInstance(containingFile.getProject()).createFileFromText("a.xml", "<a>" + displayText + "</a>")).getRootTag();
assert rootTag != null;
final XmlTagChild[] tagChildren = rootTag.getValue().getChildren();
final XmlTagChild child = tagChildren.length > 0 ? tagChildren[0] : null;
LOG.assertTrue(child != null, "Child is null for tag: " + rootTag.getText());
final TreeElement element = (TreeElement) child.getNode();
((TreeElement) tagChildren[tagChildren.length - 1].getNode().getTreeNext()).rawRemoveUpToLast();
dummyParent.rawAddChildren(element);
TreeUtil.clearCaches(dummyParent);
return element.getFirstChildNode();
}
use of com.intellij.util.CharTable in project intellij-community by JetBrains.
the class XmlTagImpl method setName.
@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;
}
Aggregations