Search in sources :

Example 11 with CharTable

use of com.intellij.util.CharTable in project intellij-community by JetBrains.

the class TemplateDataElementType method parseContents.

@Override
public ASTNode parseContents(ASTNode chameleon) {
    final CharTable charTable = SharedImplUtil.findCharTableByTree(chameleon);
    final FileElement fileElement = TreeUtil.getFileElement((TreeElement) chameleon);
    final PsiFile psiFile = (PsiFile) fileElement.getPsi();
    PsiFile originalPsiFile = psiFile.getOriginalFile();
    final TemplateLanguageFileViewProvider viewProvider = (TemplateLanguageFileViewProvider) originalPsiFile.getViewProvider();
    final Language templateLanguage = getTemplateFileLanguage(viewProvider);
    final CharSequence sourceCode = chameleon.getChars();
    RangesCollector collector = new RangesCollector();
    final PsiFile templatePsiFile = createTemplateFile(psiFile, templateLanguage, sourceCode, viewProvider, collector);
    final FileElement templateFileElement = ((PsiFileImpl) templatePsiFile).calcTreeElement();
    DebugUtil.startPsiModification("template language parsing");
    try {
        prepareParsedTemplateFile(templateFileElement);
        insertOuters(templateFileElement, sourceCode, collector.myRanges, charTable);
        TreeElement childNode = templateFileElement.getFirstChildNode();
        DebugUtil.checkTreeStructure(templateFileElement);
        DebugUtil.checkTreeStructure(chameleon);
        if (fileElement != chameleon) {
            DebugUtil.checkTreeStructure(psiFile.getNode());
            DebugUtil.checkTreeStructure(originalPsiFile.getNode());
        }
        return childNode;
    } finally {
        DebugUtil.finishPsiModification();
    }
}
Also used : Language(com.intellij.lang.Language) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) PsiFile(com.intellij.psi.PsiFile) CharTable(com.intellij.util.CharTable)

Example 12 with CharTable

use of com.intellij.util.CharTable in project intellij-community by JetBrains.

the class SharedImplUtil method addRange.

public static PsiElement addRange(PsiElement thisElement, PsiElement first, PsiElement last, ASTNode anchor, Boolean before) throws IncorrectOperationException {
    CheckUtil.checkWritable(thisElement);
    final CharTable table = findCharTableByTree(SourceTreeToPsiMap.psiElementToTree(thisElement));
    TreeElement copyFirst = null;
    ASTNode copyLast = null;
    ASTNode next = SourceTreeToPsiMap.psiElementToTree(last).getTreeNext();
    ASTNode parent = null;
    for (ASTNode element = SourceTreeToPsiMap.psiElementToTree(first); element != next; element = element.getTreeNext()) {
        TreeElement elementCopy = ChangeUtil.copyElement((TreeElement) element, table);
        if (element == first.getNode()) {
            copyFirst = elementCopy;
        }
        if (element == last.getNode()) {
            copyLast = elementCopy;
        }
        if (parent == null) {
            parent = elementCopy.getTreeParent();
        } else {
            if (elementCopy.getElementType() == TokenType.WHITE_SPACE)
                CodeEditUtil.setNodeGenerated(elementCopy, true);
            parent.addChild(elementCopy, null);
        }
    }
    if (copyFirst == null)
        return null;
    copyFirst = ((CompositeElement) SourceTreeToPsiMap.psiElementToTree(thisElement)).addInternal(copyFirst, copyLast, anchor, before);
    for (TreeElement element = copyFirst; element != null; element = element.getTreeNext()) {
        element = ChangeUtil.decodeInformation(element);
        if (element.getTreePrev() == null) {
            copyFirst = element;
        }
    }
    return SourceTreeToPsiMap.treeElementToPsi(copyFirst);
}
Also used : ASTNode(com.intellij.lang.ASTNode) FileASTNode(com.intellij.lang.FileASTNode) CharTable(com.intellij.util.CharTable)

Example 13 with CharTable

use of com.intellij.util.CharTable 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 14 with CharTable

use of com.intellij.util.CharTable in project intellij-community by JetBrains.

the class PsiLocalVariableImpl method normalizeDeclaration.

@Override
public void normalizeDeclaration() throws IncorrectOperationException {
    CheckUtil.checkWritable(this);
    final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this);
    final CompositeElement statement = getTreeParent();
    final PsiElement psiElement = SourceTreeToPsiMap.treeElementToPsi(statement);
    final PsiElement[] variables = psiElement instanceof PsiDeclarationStatement ? ((PsiDeclarationStatement) psiElement).getDeclaredElements() : PsiElement.EMPTY_ARRAY;
    if (variables.length > 1) {
        final PsiModifierList modifierList = getModifierList();
        final PsiTypeElement typeElement = getTypeElement();
        assert modifierList != null : getText();
        ASTNode last = statement;
        for (int i = 1; i < variables.length; i++) {
            ASTNode typeCopy = typeElement.copy().getNode();
            ASTNode modifierListCopy = modifierList.copy().getNode();
            CompositeElement variable = (CompositeElement) SourceTreeToPsiMap.psiToTreeNotNull(variables[i]);
            ASTNode comma = PsiImplUtil.skipWhitespaceAndCommentsBack(variable.getTreePrev());
            if (comma != null && comma.getElementType() == JavaTokenType.COMMA) {
                CodeEditUtil.removeChildren(statement, comma, variable.getTreePrev());
            }
            CodeEditUtil.removeChild(statement, variable);
            final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(statement);
            CompositeElement statement1 = Factory.createCompositeElement(DECLARATION_STATEMENT, charTableByTree, getManager());
            statement1.addChild(variable, null);
            ASTNode space = Factory.createSingleLeafElement(TokenType.WHITE_SPACE, " ", 0, 1, treeCharTab, getManager());
            variable.addChild(space, variable.getFirstChildNode());
            variable.addChild(typeCopy, variable.getFirstChildNode());
            if (modifierListCopy.getTextLength() > 0) {
                space = Factory.createSingleLeafElement(TokenType.WHITE_SPACE, " ", 0, 1, treeCharTab, getManager());
                variable.addChild(space, variable.getFirstChildNode());
            }
            variable.addChild(modifierListCopy, variable.getFirstChildNode());
            ASTNode semicolon = Factory.createSingleLeafElement(JavaTokenType.SEMICOLON, ";", 0, 1, treeCharTab, getManager());
            SourceTreeToPsiMap.psiToTreeNotNull(variables[i - 1]).addChild(semicolon, null);
            CodeEditUtil.addChild(statement.getTreeParent(), statement1, last.getTreeNext());
            last = statement1;
        }
    }
    JavaSharedImplUtil.normalizeBrackets(this);
}
Also used : ASTNode(com.intellij.lang.ASTNode) CharTable(com.intellij.util.CharTable)

Example 15 with CharTable

use of com.intellij.util.CharTable in project intellij-community by JetBrains.

the class PsiReferenceParameterListImpl method addInternal.

@Override
public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) {
    if (first == last && first.getElementType() == JavaElementType.TYPE) {
        if (getLastChildNode() != null && getLastChildNode().getElementType() == TokenType.ERROR_ELEMENT) {
            super.deleteChildInternal(getLastChildNode());
        }
    }
    final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this);
    if (getFirstChildNode() == null || getFirstChildNode().getElementType() != JavaTokenType.LT) {
        TreeElement lt = Factory.createSingleLeafElement(JavaTokenType.LT, "<", 0, 1, treeCharTab, getManager());
        super.addInternal(lt, lt, getFirstChildNode(), Boolean.TRUE);
    }
    if (getLastChildNode() == null || getLastChildNode().getElementType() != JavaTokenType.GT) {
        TreeElement gt = Factory.createSingleLeafElement(JavaTokenType.GT, ">", 0, 1, treeCharTab, getManager());
        super.addInternal(gt, gt, getLastChildNode(), Boolean.FALSE);
    }
    if (anchor == null) {
        if (before == null || before.booleanValue()) {
            anchor = findChildByRole(ChildRole.GT_IN_TYPE_LIST);
            before = Boolean.TRUE;
        } else {
            anchor = findChildByRole(ChildRole.LT_IN_TYPE_LIST);
            before = Boolean.FALSE;
        }
    }
    final TreeElement firstAdded = super.addInternal(first, last, anchor, before);
    if (first == last && first.getElementType() == JavaElementType.TYPE) {
        JavaSourceUtil.addSeparatingComma(this, first, TYPE_SET);
    }
    return firstAdded;
}
Also used : CharTable(com.intellij.util.CharTable)

Aggregations

CharTable (com.intellij.util.CharTable)35 ASTNode (com.intellij.lang.ASTNode)22 IElementType (com.intellij.psi.tree.IElementType)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 PsiElement (com.intellij.psi.PsiElement)3 PsiFile (com.intellij.psi.PsiFile)3 FileElement (com.intellij.psi.impl.source.tree.FileElement)3 FileASTNode (com.intellij.lang.FileASTNode)2 Language (com.intellij.lang.Language)2 LighterASTNode (com.intellij.lang.LighterASTNode)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiClass (com.intellij.psi.PsiClass)2 DummyHolder (com.intellij.psi.impl.source.DummyHolder)2 LeafElement (com.intellij.psi.impl.source.tree.LeafElement)2 TreeElement (com.intellij.psi.impl.source.tree.TreeElement)2 NotNull (org.jetbrains.annotations.NotNull)2 PlainTextLanguage (com.intellij.openapi.fileTypes.PlainTextLanguage)1 Project (com.intellij.openapi.project.Project)1 Pair (com.intellij.openapi.util.Pair)1 PomModel (com.intellij.pom.PomModel)1