Search in sources :

Example 16 with FileASTNode

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

the class JavaStubBuilderTest method doTest.

private void doTest(String source, String expected) {
    PsiJavaFile file = (PsiJavaFile) createLightFile("test.java", source);
    FileASTNode fileNode = file.getNode();
    assertNotNull(fileNode);
    assertFalse(fileNode.isParsed());
    StubElement lightTree = myBuilder.buildStubTree(file);
    assertFalse(fileNode.isParsed());
    // force switch to AST
    file.getNode().getChildren(null);
    StubElement astBasedTree = myBuilder.buildStubTree(file);
    assertTrue(fileNode.isParsed());
    assertEquals("light tree differs", expected, DebugUtil.stubTreeToString(lightTree));
    assertEquals("AST-based tree differs", expected, DebugUtil.stubTreeToString(astBasedTree));
}
Also used : FileASTNode(com.intellij.lang.FileASTNode) StubElement(com.intellij.psi.stubs.StubElement)

Example 17 with FileASTNode

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

the class SubstrateRef method createAstStrongRef.

public static SubstrateRef createAstStrongRef(@NotNull final ASTNode node) {
    return new SubstrateRef() {

        @NotNull
        @Override
        public ASTNode getNode() {
            return node;
        }

        @Override
        public boolean isValid() {
            FileASTNode fileElement = SharedImplUtil.findFileElement(node);
            PsiElement file = fileElement == null ? null : fileElement.getPsi();
            return file != null && file.isValid();
        }

        @NotNull
        @Override
        public PsiFile getContainingFile() {
            PsiFile file = SharedImplUtil.getContainingFile(node);
            if (file == null)
                throw PsiInvalidElementAccessException.createByNode(node, null);
            return file;
        }
    };
}
Also used : FileASTNode(com.intellij.lang.FileASTNode) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 18 with FileASTNode

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

the class BlockSupportImpl method reparseRange.

@Override
@NotNull
public DiffLog reparseRange(@NotNull final PsiFile file, @NotNull FileASTNode oldFileNode, @NotNull TextRange changedPsiRange, @NotNull final CharSequence newFileText, @NotNull final ProgressIndicator indicator, @NotNull CharSequence lastCommittedText) {
    final PsiFileImpl fileImpl = (PsiFileImpl) file;
    final Couple<ASTNode> reparseableRoots = findReparseableRoots(fileImpl, oldFileNode, changedPsiRange, newFileText);
    return reparseableRoots != null ? mergeTrees(fileImpl, reparseableRoots.first, reparseableRoots.second, indicator, lastCommittedText) : makeFullParse(fileImpl, oldFileNode, newFileText, indicator, lastCommittedText);
}
Also used : PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) FileASTNode(com.intellij.lang.FileASTNode) ASTNode(com.intellij.lang.ASTNode) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with FileASTNode

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

the class BlockSupportImpl method findReparseableRoots.

/**
   * This method searches ast node that could be reparsed incrementally and returns pair of target reparseable node and new replacement node.
   * Returns null if there is no any chance to make incremental parsing.
   */
@Nullable
public Couple<ASTNode> findReparseableRoots(@NotNull PsiFileImpl file, @NotNull FileASTNode oldFileNode, @NotNull TextRange changedPsiRange, @NotNull CharSequence newFileText) {
    Project project = file.getProject();
    final FileElement fileElement = (FileElement) oldFileNode;
    final CharTable charTable = fileElement.getCharTable();
    int lengthShift = newFileText.length() - fileElement.getTextLength();
    if (fileElement.getElementType() instanceof ITemplateDataElementType || isTooDeep(file)) {
        // unable to perform incremental reparse for template data in JSP, or in exceptionally deep trees
        return null;
    }
    final ASTNode leafAtStart = fileElement.findLeafElementAt(Math.max(0, changedPsiRange.getStartOffset() - 1));
    final ASTNode leafAtEnd = fileElement.findLeafElementAt(Math.min(changedPsiRange.getEndOffset(), fileElement.getTextLength() - 1));
    ASTNode node = leafAtStart != null && leafAtEnd != null ? TreeUtil.findCommonParent(leafAtStart, leafAtEnd) : fileElement;
    Language baseLanguage = file.getViewProvider().getBaseLanguage();
    while (node != null && !(node instanceof FileElement)) {
        IElementType elementType = node.getElementType();
        if (elementType instanceof IReparseableElementType) {
            final TextRange textRange = node.getTextRange();
            final IReparseableElementType reparseable = (IReparseableElementType) elementType;
            if (baseLanguage.isKindOf(reparseable.getLanguage()) && textRange.getLength() + lengthShift > 0) {
                final int start = textRange.getStartOffset();
                final int end = start + textRange.getLength() + lengthShift;
                if (end > newFileText.length()) {
                    reportInconsistentLength(file, newFileText, node, start, end);
                    break;
                }
                CharSequence newTextStr = newFileText.subSequence(start, end);
                if (reparseable.isParsable(node.getTreeParent(), newTextStr, baseLanguage, project)) {
                    ASTNode chameleon = reparseable.createNode(newTextStr);
                    if (chameleon != null) {
                        DummyHolder holder = DummyHolderFactory.createHolder(file.getManager(), null, node.getPsi(), charTable);
                        holder.getTreeElement().rawAddChildren((TreeElement) chameleon);
                        if (holder.getTextLength() != newTextStr.length()) {
                            String details = ApplicationManager.getApplication().isInternal() ? "text=" + newTextStr + "; treeText=" + holder.getText() + ";" : "";
                            LOG.error("Inconsistent reparse: " + details + " type=" + elementType);
                        }
                        return Couple.of(node, chameleon);
                    }
                }
            }
        }
        node = node.getTreeParent();
    }
    return null;
}
Also used : ITemplateDataElementType(com.intellij.psi.templateLanguages.ITemplateDataElementType) DummyHolder(com.intellij.psi.impl.source.DummyHolder) TextRange(com.intellij.openapi.util.TextRange) CharTable(com.intellij.util.CharTable) IElementType(com.intellij.psi.tree.IElementType) Project(com.intellij.openapi.project.Project) Language(com.intellij.lang.Language) PlainTextLanguage(com.intellij.openapi.fileTypes.PlainTextLanguage) FileASTNode(com.intellij.lang.FileASTNode) ASTNode(com.intellij.lang.ASTNode) IReparseableElementType(com.intellij.psi.tree.IReparseableElementType) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with FileASTNode

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

the class PyStubsTest method doTestUnsupportedNamedTuple.

private void doTestUnsupportedNamedTuple() {
    final PyFile file = getTestFile();
    final PyTargetExpression attribute = file.findTopLevelAttribute("nt");
    assertNotNull(attribute);
    final PyType typeFromStub = TypeEvalContext.codeInsightFallback(myFixture.getProject()).getType(attribute);
    assertNull(typeFromStub);
    assertNotParsed(file);
    final FileASTNode astNode = file.getNode();
    assertNotNull(astNode);
    final PyType typeFromAst = TypeEvalContext.userInitiated(myFixture.getProject(), file).getType(attribute);
    assertNull(typeFromAst);
}
Also used : FileASTNode(com.intellij.lang.FileASTNode) PyType(com.jetbrains.python.psi.types.PyType)

Aggregations

FileASTNode (com.intellij.lang.FileASTNode)20 ASTNode (com.intellij.lang.ASTNode)6 Project (com.intellij.openapi.project.Project)5 NotNull (org.jetbrains.annotations.NotNull)5 Document (com.intellij.openapi.editor.Document)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiElement (com.intellij.psi.PsiElement)3 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)3 SmartList (com.intellij.util.SmartList)3 Nullable (org.jetbrains.annotations.Nullable)3 LighterAST (com.intellij.lang.LighterAST)2 TreeBackedLighterAST (com.intellij.lang.TreeBackedLighterAST)2 TextRange (com.intellij.openapi.util.TextRange)2 StubElement (com.intellij.psi.stubs.StubElement)2 Processor (com.intellij.util.Processor)2 Lock (java.util.concurrent.locks.Lock)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 LogEventException (com.intellij.diagnostic.LogEventException)1 DuplicatesProfile (com.intellij.dupLocator.DuplicatesProfile)1 DuplocatorState (com.intellij.dupLocator.DuplocatorState)1