Search in sources :

Example 96 with ASTNode

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

the class PyArgumentListImpl method addArgumentFirst.

public void addArgumentFirst(PyExpression arg) {
    ASTNode node = getNode();
    ASTNode[] pars = node.getChildren(TokenSet.create(PyTokenTypes.LPAR));
    if (pars.length == 0) {
        // there's no starting paren
        try {
            add(arg);
        } catch (IncorrectOperationException e1) {
            throw new IllegalStateException(e1);
        }
    } else {
        ASTNode before = PyPsiUtils.getNextNonWhitespaceSibling(pars[0]);
        ASTNode anchorBefore;
        if (before != null && elementPrecedesElementsOfType(before, PythonDialectsTokenSetProvider.INSTANCE.getExpressionTokens())) {
            ASTNode comma = createComma();
            node.addChild(comma, before);
            node.addChild(ASTFactory.whitespace(" "), before);
            anchorBefore = comma;
        } else {
            anchorBefore = before;
        }
        ASTNode argNode = arg.getNode();
        if (anchorBefore == null) {
            node.addChild(argNode);
        } else {
            node.addChild(argNode, anchorBefore);
        }
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 97 with ASTNode

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

the class PyArgumentListImpl method getClosingParen.

@Nullable
public ASTNode getClosingParen() {
    ASTNode node = getNode();
    final ASTNode[] children = node.getChildren(TokenSet.create(PyTokenTypes.RPAR));
    return children.length == 0 ? null : children[children.length - 1];
}
Also used : ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable)

Example 98 with ASTNode

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

the class PyBinaryExpressionImpl method getPsiOperator.

@Nullable
public PsiElement getPsiOperator() {
    ASTNode node = getNode();
    final ASTNode child = node.findChildByType(PyElementTypes.BINARY_OPS);
    if (child != null)
        return child.getPsi();
    return null;
}
Also used : ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable)

Example 99 with ASTNode

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

the class GroovyCodeStyleManagerImpl method addLineFeedBefore.

protected void addLineFeedBefore(@NotNull PsiElement psiFile, @NotNull GrImportStatement result) {
    final CodeStyleSettings commonSettings = CodeStyleSettingsManager.getInstance(psiFile.getProject()).getCurrentSettings();
    final GroovyCodeStyleSettings settings = commonSettings.getCustomSettings(GroovyCodeStyleSettings.class);
    final PackageEntryTable layoutTable = settings.IMPORT_LAYOUT_TABLE;
    final PackageEntry[] entries = layoutTable.getEntries();
    PsiElement prev = result.getPrevSibling();
    while (PsiImplUtil.isWhiteSpaceOrNls(prev)) {
        prev = prev.getPrevSibling();
    }
    if (PsiImplUtil.hasElementType(prev, GroovyTokenTypes.mSEMI))
        prev = prev.getPrevSibling();
    if (PsiImplUtil.isWhiteSpaceOrNls(prev))
        prev = prev.getPrevSibling();
    ASTNode node = psiFile.getNode();
    if (prev instanceof GrImportStatement) {
        final int idx_before = getPackageEntryIdx(entries, (GrImportStatement) prev);
        final int idx = getPackageEntryIdx(entries, result);
        final int spaceCount = getMaxSpaceCount(entries, idx_before, idx);
        //skip space and semicolon after import
        if (PsiImplUtil.isWhiteSpaceOrNls(prev.getNextSibling()) && PsiImplUtil.hasElementType(prev.getNextSibling().getNextSibling(), GroovyTokenTypes.mSEMI))
            prev = prev.getNextSibling().getNextSibling();
        while (PsiImplUtil.isWhiteSpaceOrNls(prev.getNextSibling())) {
            node.removeChild(prev.getNextSibling().getNode());
        }
        node.addLeaf(GroovyTokenTypes.mNLS, StringUtil.repeat("\n", spaceCount + 1), result.getNode());
    } else if (prev instanceof GrPackageDefinition) {
        node.addLeaf(GroovyTokenTypes.mNLS, StringUtil.repeat("\n", commonSettings.BLANK_LINES_AFTER_PACKAGE), result.getNode());
    }
}
Also used : CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) PackageEntry(com.intellij.psi.codeStyle.PackageEntry) PackageEntryTable(com.intellij.psi.codeStyle.PackageEntryTable) ASTNode(com.intellij.lang.ASTNode) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) PsiElement(com.intellij.psi.PsiElement)

Example 100 with ASTNode

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

the class StubsTest method doTest.

public void doTest() throws Exception {
    final List<String> data = TestUtils.readInput(getTestDataPath() + "/" + getTestName(true) + ".test");
    String fileText = data.get(0);
    PsiFile psiFile = TestUtils.createPseudoPhysicalGroovyFile(getProject(), fileText);
    ASTNode node = psiFile.getNode();
    Assert.assertNotNull(node);
    IElementType type = node.getElementType();
    Assert.assertTrue(type instanceof IStubFileElementType);
    IStubFileElementType stubFileType = (IStubFileElementType) type;
    StubBuilder builder = stubFileType.getBuilder();
    StubElement element = builder.buildStubTree(psiFile);
    StringBuffer buffer = new StringBuffer();
    getStubsTreeImpl(element, buffer, "");
    String stubTree = buffer.toString().trim();
    assertEquals(data.get(1), stubTree);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) ASTNode(com.intellij.lang.ASTNode) PsiFile(com.intellij.psi.PsiFile) StubElement(com.intellij.psi.stubs.StubElement) StubBuilder(com.intellij.psi.StubBuilder)

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