Search in sources :

Example 21 with DummyHolder

use of com.intellij.psi.impl.source.DummyHolder 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 22 with DummyHolder

use of com.intellij.psi.impl.source.DummyHolder in project intellij-community by JetBrains.

the class PsiFileFactoryImpl method createElementFromText.

@Nullable
public PsiElement createElementFromText(@Nullable final String text, @NotNull final Language language, @NotNull final IElementType type, @Nullable final PsiElement context) {
    if (text == null)
        return null;
    final DummyHolder result = DummyHolderFactory.createHolder(myManager, language, context);
    final FileElement holder = result.getTreeElement();
    final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(language);
    if (parserDefinition == null) {
        throw new AssertionError("No parser definition for " + language);
    }
    final Project project = myManager.getProject();
    final Lexer lexer = parserDefinition.createLexer(project);
    final PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder(project, holder, lexer, language, text);
    final ASTNode node = parserDefinition.createParser(project).parse(type, builder);
    holder.rawAddChildren((TreeElement) node);
    markGenerated(result);
    return node.getPsi();
}
Also used : Project(com.intellij.openapi.project.Project) Lexer(com.intellij.lexer.Lexer) DummyHolder(com.intellij.psi.impl.source.DummyHolder) FileElement(com.intellij.psi.impl.source.tree.FileElement) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with DummyHolder

use of com.intellij.psi.impl.source.DummyHolder in project intellij-community by JetBrains.

the class ChangeUtil method copyToElement.

public static TreeElement copyToElement(PsiElement original) {
    final DummyHolder holder = DummyHolderFactory.createHolder(original.getManager(), null, original.getLanguage());
    final FileElement holderElement = holder.getTreeElement();
    final TreeElement treeElement = generateTreeElement(original, holderElement.getCharTable(), original.getManager());
    //  TreeElement treePrev = treeElement.getTreePrev(); // This is hack to support bug used in formater
    holderElement.rawAddChildren(treeElement);
    TreeUtil.clearCaches(holderElement);
    //  treeElement.setTreePrev(treePrev);
    saveIndentationToCopy((TreeElement) original.getNode(), treeElement);
    return treeElement;
}
Also used : DummyHolder(com.intellij.psi.impl.source.DummyHolder)

Aggregations

DummyHolder (com.intellij.psi.impl.source.DummyHolder)23 IncorrectOperationException (com.intellij.util.IncorrectOperationException)15 NotNull (org.jetbrains.annotations.NotNull)15 JavaDummyElement (com.intellij.psi.impl.source.JavaDummyElement)14 FileElement (com.intellij.psi.impl.source.tree.FileElement)3 ASTNode (com.intellij.lang.ASTNode)2 Project (com.intellij.openapi.project.Project)2 LanguageLevel (com.intellij.pom.java.LanguageLevel)2 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)2 CharTable (com.intellij.util.CharTable)2 Nullable (org.jetbrains.annotations.Nullable)2 FileASTNode (com.intellij.lang.FileASTNode)1 Language (com.intellij.lang.Language)1 PsiBuilder (com.intellij.lang.PsiBuilder)1 Lexer (com.intellij.lexer.Lexer)1 CommandProcessor (com.intellij.openapi.command.CommandProcessor)1 Document (com.intellij.openapi.editor.Document)1 FileType (com.intellij.openapi.fileTypes.FileType)1 PlainTextLanguage (com.intellij.openapi.fileTypes.PlainTextLanguage)1 EmptyProgressIndicator (com.intellij.openapi.progress.EmptyProgressIndicator)1