Search in sources :

Example 71 with ASTNode

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

the class PyElementGeneratorImpl method createComma.

public ASTNode createComma() {
    final PsiFile dummyFile = createDummyFile(LanguageLevel.getDefault(), "[0,]");
    final PyExpressionStatement expressionStatement = (PyExpressionStatement) dummyFile.getFirstChild();
    ASTNode zero = expressionStatement.getFirstChild().getNode().getFirstChildNode().getTreeNext();
    return zero.getTreeNext().copyElement();
}
Also used : ASTNode(com.intellij.lang.ASTNode) PsiFile(com.intellij.psi.PsiFile)

Example 72 with ASTNode

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

the class PyElementGeneratorImpl method insertItemIntoList.

// TODO: Adds comma to empty list: adding "foo" to () will create (foo,). That is why "insertItemIntoListRemoveRedundantCommas" was created.
// We probably need to fix this method and delete insertItemIntoListRemoveRedundantCommas
public PsiElement insertItemIntoList(PyElement list, @Nullable PyExpression afterThis, PyExpression toInsert) throws IncorrectOperationException {
    ASTNode add = toInsert.getNode().copyElement();
    if (afterThis == null) {
        ASTNode exprNode = list.getNode();
        ASTNode[] closingTokens = exprNode.getChildren(TokenSet.create(PyTokenTypes.LBRACKET, PyTokenTypes.LPAR));
        if (closingTokens.length == 0) {
            // we tried our best. let's just insert it at the end
            exprNode.addChild(add);
        } else {
            ASTNode next = PyPsiUtils.getNextNonWhitespaceSibling(closingTokens[closingTokens.length - 1]);
            if (next != null) {
                ASTNode comma = createComma();
                exprNode.addChild(comma, next);
                exprNode.addChild(add, comma);
            } else {
                exprNode.addChild(add);
            }
        }
    } else {
        ASTNode lastArgNode = afterThis.getNode();
        ASTNode comma = createComma();
        ASTNode parent = lastArgNode.getTreeParent();
        ASTNode afterLast = lastArgNode.getTreeNext();
        if (afterLast == null) {
            parent.addChild(add);
        } else {
            parent.addChild(add, afterLast);
        }
        parent.addChild(comma, add);
    }
    return add.getPsi();
}
Also used : ASTNode(com.intellij.lang.ASTNode)

Example 73 with ASTNode

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

the class PyFunctionImpl method setName.

public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
    final ASTNode nameElement = PyUtil.createNewName(this, name);
    final ASTNode nameNode = getNameNode();
    if (nameNode != null) {
        getNode().replaceChild(nameNode, nameElement);
    }
    return this;
}
Also used : ASTNode(com.intellij.lang.ASTNode)

Example 74 with ASTNode

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

the class PyLineMarkerProviderTest method testOverriding.

/**
   * Checks method has "up" arrow when overrides, and this arrow works
   */
public void testOverriding() throws Exception {
    myFixture.copyDirectoryToProject("lineMarkerTest", "");
    myFixture.configureByFile("spam.py");
    final ASTNode functionNode = myFixture.getElementAtCaret().getNode();
    // We need IDENTIFIER node
    final ASTNode[] functionChildren = functionNode.getChildren(TokenSet.create(PyTokenTypes.IDENTIFIER));
    assert functionChildren.length == 1 : "Wrong number of identifiers: " + functionChildren.length;
    final PsiElement element = functionChildren[0].getPsi();
    @SuppressWarnings("unchecked") final LineMarkerInfo<PsiElement> lineMarkerInfo = new PyLineMarkerProvider().getLineMarkerInfo(element);
    Assert.assertNotNull("No gutter displayed", lineMarkerInfo);
    final GutterIconNavigationHandler<PsiElement> handler = lineMarkerInfo.getNavigationHandler();
    Assert.assertNotNull("Gutter has no navigation handle", handler);
    handler.navigate(new MouseEvent(new JLabel(), 0, 0, 0, 0, 0, 0, false), element);
    final NavigatablePsiElement[] targets = PyLineMarkerNavigator.getNavigationTargets(element);
    Assert.assertNotNull("No navigation targets found", targets);
    Assert.assertThat("Wrong number of targets found", targets, Matchers.arrayWithSize(1));
    final NavigatablePsiElement parentMethod = targets[0];
    Assert.assertThat("Navigation target has wrong type", parentMethod, Matchers.instanceOf(PyPossibleClassMember.class));
    final PyClass parentClass = ((PyPossibleClassMember) parentMethod).getContainingClass();
    Assert.assertNotNull("Function overrides other function, but no parent displayed", parentClass);
    Assert.assertEquals("Wrong parent class name", "Eggs", parentClass.getName());
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) MouseEvent(java.awt.event.MouseEvent) ASTNode(com.intellij.lang.ASTNode) PyPossibleClassMember(com.jetbrains.python.psi.PyPossibleClassMember) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) PsiElement(com.intellij.psi.PsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement)

Example 75 with ASTNode

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

the class PropertiesRootBlock method buildChildren.

@Override
protected List<Block> buildChildren() {
    final List<Block> result = new ArrayList<>();
    ASTNode child = myNode.getFirstChildNode();
    while (child != null) {
        if (!(child instanceof PsiWhiteSpace)) {
            if (child.getElementType() instanceof PropertyListStubElementType) {
                ASTNode propertyNode = child.getFirstChildNode();
                while (propertyNode != null) {
                    if (propertyNode.getElementType() instanceof PropertyStubElementType) {
                        collectPropertyBlock(propertyNode, result);
                    } else if (PropertiesTokenTypes.END_OF_LINE_COMMENT.equals(propertyNode.getElementType()) || PropertiesTokenTypes.BAD_CHARACTER.equals(propertyNode.getElementType())) {
                        result.add(new PropertyBlock(propertyNode, null));
                    }
                    propertyNode = propertyNode.getTreeNext();
                }
            } else if (PropertiesTokenTypes.BAD_CHARACTER.equals(child.getElementType())) {
                result.add(new PropertyBlock(child, null));
            }
        }
        if (PropertiesTokenTypes.END_OF_LINE_COMMENT.equals(child.getElementType())) {
            result.add(new PropertyBlock(child, null));
        }
        child = child.getTreeNext();
    }
    return result;
}
Also used : PropertyListStubElementType(com.intellij.lang.properties.parsing.PropertyListStubElementType) PropertyStubElementType(com.intellij.lang.properties.parsing.PropertyStubElementType) ArrayList(java.util.ArrayList) ASTNode(com.intellij.lang.ASTNode) AbstractBlock(com.intellij.psi.formatter.common.AbstractBlock) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

ASTNode (com.intellij.lang.ASTNode)748 PsiElement (com.intellij.psi.PsiElement)142 NotNull (org.jetbrains.annotations.NotNull)132 IElementType (com.intellij.psi.tree.IElementType)127 Nullable (org.jetbrains.annotations.Nullable)99 TextRange (com.intellij.openapi.util.TextRange)91 ArrayList (java.util.ArrayList)53 IncorrectOperationException (com.intellij.util.IncorrectOperationException)35 PsiFile (com.intellij.psi.PsiFile)31 Annotation (com.intellij.lang.annotation.Annotation)22 CharTable (com.intellij.util.CharTable)22 Document (com.intellij.openapi.editor.Document)18 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)17 XmlTag (com.intellij.psi.xml.XmlTag)17 Project (com.intellij.openapi.project.Project)16 AbstractBlock (com.intellij.psi.formatter.common.AbstractBlock)16 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)16 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)15 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)15 PsiReference (com.intellij.psi.PsiReference)14