Search in sources :

Example 6 with TreeElement

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

the class GroovyChangeUtilSupport method decodeInformation.

@Override
public TreeElement decodeInformation(TreeElement element, final Map<Object, Object> decodingState) {
    if (element instanceof CompositeElement) {
        if (element.getElementType() == GroovyElementTypes.REFERENCE_ELEMENT || element.getElementType() == GroovyElementTypes.REFERENCE_EXPRESSION) {
            GrReferenceElement ref = (GrReferenceElement) SourceTreeToPsiMap.treeElementToPsi(element);
            final PsiMember refMember = element.getCopyableUserData(REFERENCED_MEMBER_KEY);
            if (refMember != null) {
                element.putCopyableUserData(REFERENCED_MEMBER_KEY, null);
                PsiElement refElement1 = ref.resolve();
                if (!refMember.getManager().areElementsEquivalent(refMember, refElement1)) {
                    try {
                        if (!(refMember instanceof PsiClass) || ref.getQualifier() == null) {
                            // can restore only if short (otherwise qualifier should be already restored)
                            ref = (GrReferenceElement) ref.bindToElement(refMember);
                        }
                    } catch (IncorrectOperationException ignored) {
                    }
                    return (TreeElement) SourceTreeToPsiMap.psiElementToTree(ref);
                }
            }
            return element;
        }
    }
    return null;
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 7 with TreeElement

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

the class PsiImplUtil method getTextSkipWhiteSpaceAndComments.

/**
   * see {@link com.intellij.psi.impl.source.tree.AstBufferUtil#getTextSkippingWhitespaceComments(com.intellij.lang.ASTNode)}
   */
public static String getTextSkipWhiteSpaceAndComments(ASTNode node) {
    final TreeElement treeElement = (TreeElement) node;
    final int length;
    {
        final GroovyBufferVisitor lengthVisitor = new GroovyBufferVisitor(true, true, 0, null);
        treeElement.acceptTree(lengthVisitor);
        length = lengthVisitor.getEnd();
    }
    final char[] buffer = new char[length];
    {
        final GroovyBufferVisitor textVisitor = new GroovyBufferVisitor(true, true, 0, buffer);
        treeElement.acceptTree(textVisitor);
    }
    return StringFactory.createShared(buffer);
}
Also used : TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 8 with TreeElement

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

the class LowLevelSearchUtil method processTreeUp.

/**
   * @return null to stop or last found TreeElement
   * to be reused via <code>lastElement<code/> param in subsequent calls to avoid full tree rescan (n^2->n).
   */
private static TreeElement processTreeUp(@NotNull Project project, @NotNull TextOccurenceProcessor processor, @NotNull PsiElement scope, @NotNull StringSearcher searcher, final int offset, final boolean processInjectedPsi, @NotNull ProgressIndicator progress, TreeElement lastElement) {
    if (scope instanceof PsiCompiledElement) {
        throw new IllegalArgumentException("Scope is compiled, can't scan: " + scope);
    }
    final int scopeStartOffset = scope.getTextRange().getStartOffset();
    final int patternLength = searcher.getPatternLength();
    ASTNode scopeNode = scope.getNode();
    boolean useTree = scopeNode != null;
    assert scope.isValid();
    int start;
    TreeElement leafNode = null;
    PsiElement leafElement = null;
    if (useTree) {
        leafNode = findNextLeafElementAt(scopeNode, lastElement, offset);
        if (leafNode == null)
            return lastElement;
        start = offset - leafNode.getStartOffset() + scopeStartOffset;
    } else {
        if (scope instanceof PsiFile) {
            leafElement = ((PsiFile) scope).getViewProvider().findElementAt(offset, scope.getLanguage());
        } else {
            leafElement = scope.findElementAt(offset);
        }
        if (leafElement == null)
            return lastElement;
        assert leafElement.isValid();
        start = offset - leafElement.getTextRange().getStartOffset() + scopeStartOffset;
    }
    if (start < 0) {
        throw new AssertionError("offset=" + offset + "; scopeStartOffset=" + scopeStartOffset + "; leafElement=" + leafElement + ";  scope=" + scope + "; leafElement.isValid(): " + (leafElement == null ? null : leafElement.isValid()));
    }
    InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(project);
    lastElement = leafNode;
    boolean contains = false;
    PsiElement prev = null;
    TreeElement prevNode = null;
    PsiElement run = null;
    while (run != scope) {
        progress.checkCanceled();
        if (useTree) {
            start += prevNode == null ? 0 : prevNode.getStartOffsetInParent();
            prevNode = leafNode;
            run = leafNode.getPsi();
        } else {
            start += prev == null ? 0 : prev.getStartOffsetInParent();
            prev = run;
            run = leafElement;
        }
        //do not compute if already contains
        if (!contains)
            contains = run.getTextLength() - start >= patternLength;
        if (contains) {
            if (processInjectedPsi) {
                Boolean result = processInjectedFile(run, processor, searcher, progress, injectedLanguageManager);
                if (result != null) {
                    return result.booleanValue() ? lastElement : null;
                }
            }
            if (!processor.execute(run, start)) {
                return null;
            }
        }
        if (useTree) {
            leafNode = leafNode.getTreeParent();
            if (leafNode == null)
                break;
        } else {
            leafElement = leafElement.getParent();
            if (leafElement == null)
                break;
        }
    }
    assert run == scope : "Malbuilt PSI; scopeNode: " + scope + "; containingFile:" + PsiTreeUtil.getParentOfType(scope, PsiFile.class, false) + "; leafNode: " + run + "; isAncestor=" + PsiTreeUtil.isAncestor(scope, run, false) + "; in same file: " + (PsiTreeUtil.getParentOfType(scope, PsiFile.class, false) == PsiTreeUtil.getParentOfType(run, PsiFile.class, false));
    return lastElement;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) ASTNode(com.intellij.lang.ASTNode) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 9 with TreeElement

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

the class DeclarationMover method beforeMove.

@Override
public void beforeMove(@NotNull final Editor editor, @NotNull final MoveInfo info, final boolean down) {
    super.beforeMove(editor, info, down);
    if (myEnumToInsertSemicolonAfter != null) {
        TreeElement semicolon = Factory.createSingleLeafElement(JavaTokenType.SEMICOLON, ";", 0, 1, null, myEnumToInsertSemicolonAfter.getManager());
        try {
            PsiElement inserted = myEnumToInsertSemicolonAfter.getParent().addAfter(semicolon.getPsi(), myEnumToInsertSemicolonAfter);
            inserted = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(inserted);
            final LogicalPosition position = editor.offsetToLogicalPosition(inserted.getTextRange().getEndOffset());
            info.toMove2 = new LineRange(position.line + 1, position.line + 1);
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        } finally {
            myEnumToInsertSemicolonAfter = null;
        }
    }
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) IncorrectOperationException(com.intellij.util.IncorrectOperationException) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 10 with TreeElement

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

the class PsiModifierListImpl method setModifierProperty.

@Override
public void setModifierProperty(@NotNull String name, boolean value) throws IncorrectOperationException {
    checkSetModifierProperty(name, value);
    PsiElement parent = getParent();
    PsiElement grandParent = parent != null ? parent.getParent() : null;
    IElementType type = NAME_TO_KEYWORD_TYPE_MAP.get(name);
    CompositeElement treeElement = (CompositeElement) getNode();
    // changes horizontal position of parameters list start, hence, we need to reformat them in order to preserve alignment.
    if (parent instanceof PsiMethod) {
        PsiMethod method = (PsiMethod) parent;
        CodeEditUtil.markToReformat(method.getParameterList().getNode(), true);
    }
    if (value) {
        if (type == JavaTokenType.PUBLIC_KEYWORD || type == JavaTokenType.PRIVATE_KEYWORD || type == JavaTokenType.PROTECTED_KEYWORD || type == null) /* package-private */
        {
            if (type != JavaTokenType.PUBLIC_KEYWORD) {
                setModifierProperty(PUBLIC, false);
            }
            if (type != JavaTokenType.PRIVATE_KEYWORD) {
                setModifierProperty(PRIVATE, false);
            }
            if (type != JavaTokenType.PROTECTED_KEYWORD) {
                setModifierProperty(PROTECTED, false);
            }
            if (type == null)
                return;
        }
        if (parent instanceof PsiField && grandParent instanceof PsiClass && ((PsiClass) grandParent).isInterface()) {
            if (type == JavaTokenType.PUBLIC_KEYWORD || type == JavaTokenType.STATIC_KEYWORD || type == JavaTokenType.FINAL_KEYWORD)
                return;
        } else if (parent instanceof PsiMethod && grandParent instanceof PsiClass && ((PsiClass) grandParent).isInterface()) {
            if (type == JavaTokenType.PUBLIC_KEYWORD || type == JavaTokenType.ABSTRACT_KEYWORD)
                return;
        } else if (parent instanceof PsiClass && grandParent instanceof PsiClass && ((PsiClass) grandParent).isInterface()) {
            if (type == JavaTokenType.PUBLIC_KEYWORD)
                return;
        } else if (parent instanceof PsiAnnotationMethod && grandParent instanceof PsiClass && ((PsiClass) grandParent).isAnnotationType()) {
            if (type == JavaTokenType.PUBLIC_KEYWORD || type == JavaTokenType.ABSTRACT_KEYWORD)
                return;
        }
        if (treeElement.findChildByType(type) == null) {
            TreeElement keyword = Factory.createSingleLeafElement(type, name, null, getManager());
            treeElement.addInternal(keyword, keyword, null, null);
        }
    } else {
        if (type == null) /* package-private */
        {
            //?
            throw new IncorrectOperationException("Cannot reset package-private modifier.");
        }
        ASTNode child = treeElement.findChildByType(type);
        if (child != null) {
            SourceTreeToPsiMap.treeToPsiNotNull(child).delete();
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) IncorrectOperationException(com.intellij.util.IncorrectOperationException) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Aggregations

TreeElement (com.intellij.psi.impl.source.tree.TreeElement)43 ASTNode (com.intellij.lang.ASTNode)12 IncorrectOperationException (com.intellij.util.IncorrectOperationException)5 LeafElement (com.intellij.psi.impl.source.tree.LeafElement)4 PsiElement (com.intellij.psi.PsiElement)3 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)3 Nullable (org.jetbrains.annotations.Nullable)3 Document (com.intellij.openapi.editor.Document)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PomModel (com.intellij.pom.PomModel)2 PomTransactionBase (com.intellij.pom.impl.PomTransactionBase)2 XmlAspect (com.intellij.pom.xml.XmlAspect)2 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)2 FileElement (com.intellij.psi.impl.source.tree.FileElement)2 TokenSet (com.intellij.psi.tree.TokenSet)2 CharTable (com.intellij.util.CharTable)2 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)1 Project (com.intellij.openapi.project.Project)1