Search in sources :

Example 31 with CharTable

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();
}
Also used : PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) CharTable(com.intellij.util.CharTable)

Example 32 with CharTable

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;
}
Also used : ASTNode(com.intellij.lang.ASTNode) CharTable(com.intellij.util.CharTable) LeafElement(com.intellij.psi.impl.source.tree.LeafElement)

Example 33 with CharTable

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);
}
Also used : CharTable(com.intellij.util.CharTable)

Example 34 with CharTable

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();
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) FileElement(com.intellij.psi.impl.source.tree.FileElement) PsiFile(com.intellij.psi.PsiFile) CharTable(com.intellij.util.CharTable) XmlTagChild(com.intellij.psi.xml.XmlTagChild) XmlTag(com.intellij.psi.xml.XmlTag) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 35 with CharTable

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;
}
Also used : XmlAspect(com.intellij.pom.xml.XmlAspect) ASTNode(com.intellij.lang.ASTNode) PomModel(com.intellij.pom.PomModel) PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PomModelEvent(com.intellij.pom.event.PomModelEvent) CharTable(com.intellij.util.CharTable)

Aggregations

CharTable (com.intellij.util.CharTable)35 ASTNode (com.intellij.lang.ASTNode)22 IElementType (com.intellij.psi.tree.IElementType)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 PsiElement (com.intellij.psi.PsiElement)3 PsiFile (com.intellij.psi.PsiFile)3 FileElement (com.intellij.psi.impl.source.tree.FileElement)3 FileASTNode (com.intellij.lang.FileASTNode)2 Language (com.intellij.lang.Language)2 LighterASTNode (com.intellij.lang.LighterASTNode)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiClass (com.intellij.psi.PsiClass)2 DummyHolder (com.intellij.psi.impl.source.DummyHolder)2 LeafElement (com.intellij.psi.impl.source.tree.LeafElement)2 TreeElement (com.intellij.psi.impl.source.tree.TreeElement)2 NotNull (org.jetbrains.annotations.NotNull)2 PlainTextLanguage (com.intellij.openapi.fileTypes.PlainTextLanguage)1 Project (com.intellij.openapi.project.Project)1 Pair (com.intellij.openapi.util.Pair)1 PomModel (com.intellij.pom.PomModel)1