Search in sources :

Example 21 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class RestBlock method buildSubBlocks.

private List<RestBlock> buildSubBlocks() {
    List<RestBlock> blocks = new ArrayList<>();
    for (ASTNode child = myNode.getFirstChildNode(); child != null; child = child.getTreeNext()) {
        IElementType childType = child.getElementType();
        if (child.getTextRange().getLength() == 0)
            continue;
        if (childType == RestTokenTypes.WHITESPACE) {
            continue;
        }
        blocks.add(buildSubBlock(child));
    }
    return Collections.unmodifiableList(blocks);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ArrayList(java.util.ArrayList) ASTNode(com.intellij.lang.ASTNode)

Example 22 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class PyClassRefactoringUtil method addSuperClassExpressions.

/**
   * Adds expressions to superclass list
   *
   * @param project          project
   * @param clazz            class to add expressions to superclass list
   * @param paramExpressions param expressions. Like "object" or "MySuperClass". Will not add any param exp. if null.
   * @param keywordArguments keyword args like "metaclass=ABCMeta". key-value pairs.  Will not add any keyword arg. if null.
   */
public static void addSuperClassExpressions(@NotNull final Project project, @NotNull final PyClass clazz, @Nullable final Collection<String> paramExpressions, @Nullable final Collection<Pair<String, String>> keywordArguments) {
    final PyElementGenerator generator = PyElementGenerator.getInstance(project);
    final LanguageLevel languageLevel = LanguageLevel.forElement(clazz);
    PyArgumentList superClassExpressionList = clazz.getSuperClassExpressionList();
    boolean addExpression = false;
    if (superClassExpressionList == null) {
        superClassExpressionList = generator.createFromText(languageLevel, PyClass.class, "class foo():pass").getSuperClassExpressionList();
        assert superClassExpressionList != null : "expression not created";
        addExpression = true;
    }
    generator.createFromText(LanguageLevel.PYTHON34, PyClass.class, "class foo(object, metaclass=Foo): pass").getSuperClassExpressionList();
    if (paramExpressions != null) {
        for (final String paramExpression : paramExpressions) {
            superClassExpressionList.addArgument(generator.createParameter(paramExpression));
        }
    }
    if (keywordArguments != null) {
        for (final Pair<String, String> keywordArgument : keywordArguments) {
            superClassExpressionList.addArgument(generator.createKeywordArgument(languageLevel, keywordArgument.first, keywordArgument.second));
        }
    }
    // If class has no expression list, then we need to add it manually.
    if (addExpression) {
        // For nameless classes we simply add expression list directly to them
        final ASTNode classNameNode = clazz.getNameNode();
        final PsiElement elementToAddAfter = (classNameNode == null) ? clazz.getFirstChild() : classNameNode.getPsi();
        clazz.addAfter(superClassExpressionList, elementToAddAfter);
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode)

Example 23 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class PyReferenceImpl method handleElementRename.

@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
    ASTNode nameElement = myElement.getNameElement();
    newElementName = StringUtil.trimEnd(newElementName, PyNames.DOT_PY);
    if (nameElement != null && PyNames.isIdentifier(newElementName)) {
        final ASTNode newNameElement = PyUtil.createNewName(myElement, newElementName);
        myElement.getNode().replaceChild(nameElement, newNameElement);
    }
    return myElement;
}
Also used : ASTNode(com.intellij.lang.ASTNode)

Example 24 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class XmlAttributeImpl method getDisplayValue.

@Override
public String getDisplayValue() {
    String displayText = myDisplayText;
    if (displayText != null)
        return displayText;
    XmlAttributeValue value = getValueElement();
    if (value == null)
        return null;
    PsiElement firstChild = value.getFirstChild();
    if (firstChild == null)
        return null;
    ASTNode child = firstChild.getNode();
    TextRange valueTextRange = new TextRange(0, value.getTextLength());
    if (child != null && child.getElementType() == XmlTokenType.XML_ATTRIBUTE_VALUE_START_DELIMITER) {
        valueTextRange = new TextRange(child.getTextLength(), valueTextRange.getEndOffset());
        child = child.getTreeNext();
    }
    final TIntArrayList gapsStarts = new TIntArrayList();
    final TIntArrayList gapsShifts = new TIntArrayList();
    StringBuilder buffer = new StringBuilder(getTextLength());
    while (child != null) {
        final int start = buffer.length();
        IElementType elementType = child.getElementType();
        if (elementType == XmlTokenType.XML_ATTRIBUTE_VALUE_END_DELIMITER) {
            valueTextRange = new TextRange(valueTextRange.getStartOffset(), child.getTextRange().getStartOffset() - value.getTextRange().getStartOffset());
            break;
        }
        if (elementType == XmlTokenType.XML_CHAR_ENTITY_REF) {
            buffer.append(XmlUtil.getCharFromEntityRef(child.getText()));
        } else if (elementType == XmlElementType.XML_ENTITY_REF) {
            buffer.append(XmlUtil.getEntityValue((XmlEntityRef) child));
        } else {
            appendChildToDisplayValue(buffer, child);
        }
        int end = buffer.length();
        int originalLength = child.getTextLength();
        if (end - start != originalLength) {
            gapsStarts.add(start);
            gapsShifts.add(originalLength - (end - start));
        }
        child = child.getTreeNext();
    }
    int[] gapDisplayStarts = ArrayUtil.newIntArray(gapsShifts.size());
    int[] gapPhysicalStarts = ArrayUtil.newIntArray(gapsShifts.size());
    int currentGapsSum = 0;
    for (int i = 0; i < gapDisplayStarts.length; i++) {
        currentGapsSum += gapsShifts.get(i);
        gapDisplayStarts[i] = gapsStarts.get(i);
        gapPhysicalStarts[i] = gapDisplayStarts[i] + currentGapsSum;
    }
    myGapDisplayStarts = gapDisplayStarts;
    myGapPhysicalStarts = gapPhysicalStarts;
    myValueTextRange = valueTextRange;
    return myDisplayText = buffer.toString();
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) TIntArrayList(gnu.trove.TIntArrayList)

Example 25 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class XmlAttributeImpl method setValue.

@Override
public void setValue(String valueText) throws IncorrectOperationException {
    final ASTNode value = XmlChildRole.ATTRIBUTE_VALUE_FINDER.findChild(this);
    final PomModel model = PomManager.getModel(getProject());
    final XmlAttribute attribute = XmlElementFactory.getInstance(getProject()).createAttribute("a", valueText, this);
    final ASTNode newValue = XmlChildRole.ATTRIBUTE_VALUE_FINDER.findChild((ASTNode) attribute);
    final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
    model.runTransaction(new PomTransactionBase(this, aspect) {

        @Override
        public PomModelEvent runInner() {
            final XmlAttributeImpl att = XmlAttributeImpl.this;
            if (value != null) {
                if (newValue != null) {
                    att.replaceChild(value, newValue.copyElement());
                } else {
                    att.removeChild(value);
                }
            } else {
                if (newValue != null) {
                    att.addChild(newValue.getTreePrev().copyElement());
                    att.addChild(newValue.copyElement());
                }
            }
            return XmlAttributeSetImpl.createXmlAttributeSet(model, getParent(), getName(), newValue != null ? newValue.getText() : null);
        }
    });
}
Also used : XmlAspect(com.intellij.pom.xml.XmlAspect) ASTNode(com.intellij.lang.ASTNode) PomModel(com.intellij.pom.PomModel) PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) PomModelEvent(com.intellij.pom.event.PomModelEvent)

Aggregations

ASTNode (com.intellij.lang.ASTNode)839 NotNull (org.jetbrains.annotations.NotNull)154 PsiElement (com.intellij.psi.PsiElement)152 IElementType (com.intellij.psi.tree.IElementType)152 Nullable (org.jetbrains.annotations.Nullable)113 TextRange (com.intellij.openapi.util.TextRange)97 ArrayList (java.util.ArrayList)60 PsiFile (com.intellij.psi.PsiFile)36 IncorrectOperationException (com.intellij.util.IncorrectOperationException)35 Annotation (com.intellij.lang.annotation.Annotation)25 AbstractBlock (com.intellij.psi.formatter.common.AbstractBlock)22 CharTable (com.intellij.util.CharTable)22 FileASTNode (com.intellij.lang.FileASTNode)20 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)20 Document (com.intellij.openapi.editor.Document)20 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)18 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)18 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)18 Project (com.intellij.openapi.project.Project)17 XmlTag (com.intellij.psi.xml.XmlTag)17