Search in sources :

Example 26 with FileElement

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

the class CodeStyleManagerImpl method doFindWhiteSpaceNode.

@NotNull
private static Pair<PsiElement, CharTable> doFindWhiteSpaceNode(@NotNull PsiFile file, int offset) {
    ASTNode astNode = SourceTreeToPsiMap.psiElementToTree(file);
    if (!(astNode instanceof FileElement)) {
        return new Pair<>(null, null);
    }
    PsiElement elementAt = InjectedLanguageUtil.findInjectedElementNoCommit(file, offset);
    final CharTable charTable = ((FileElement) astNode).getCharTable();
    if (elementAt == null) {
        elementAt = findElementInTreeWithFormatterEnabled(file, offset);
    }
    if (elementAt == null) {
        return new Pair<>(null, charTable);
    }
    ASTNode node = elementAt.getNode();
    if (node == null || node.getElementType() != TokenType.WHITE_SPACE) {
        return new Pair<>(null, charTable);
    }
    return Pair.create(elementAt, charTable);
}
Also used : FileElement(com.intellij.psi.impl.source.tree.FileElement) CharTable(com.intellij.util.CharTable) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with FileElement

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

the class JavaResolveUtil method resolveImaginarySuperCallInThisPlace.

/**
   * @return the constructor (or a class if there are none)
   * which the "{@code super();}" no-args call resolves to if inserted in the {@code place} (typically it would be inserted in the sub class constructor)
   * No code modifications happen in this method; it's used for resolving multiple overloaded constructors.
   */
public static PsiElement resolveImaginarySuperCallInThisPlace(@NotNull PsiMember place, @NotNull Project project, @NotNull PsiClass superClassWhichTheSuperCallMustResolveTo) {
    PsiExpressionListImpl expressionList = new PsiExpressionListImpl();
    final DummyHolder result = DummyHolderFactory.createHolder(PsiManager.getInstance(project), place);
    final FileElement holder = result.getTreeElement();
    holder.rawAddChildren((TreeElement) expressionList.getNode());
    return PsiResolveHelper.SERVICE.getInstance(project).resolveConstructor(PsiTypesUtil.getClassType(superClassWhichTheSuperCallMustResolveTo), expressionList, place).getElement();
}
Also used : DummyHolder(com.intellij.psi.impl.source.DummyHolder) PsiExpressionListImpl(com.intellij.psi.impl.source.tree.java.PsiExpressionListImpl) FileElement(com.intellij.psi.impl.source.tree.FileElement)

Example 28 with FileElement

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

the class SmartPsiElementPointersTest method assertTreeLoaded.

private static void assertTreeLoaded(PsiFileImpl file, boolean loaded) {
    FileElement treeElement = file.getTreeElement();
    assertEquals(loaded, treeElement != null);
    StubTree stubTree = file.getStubTree();
    assertEquals(loaded, stubTree == null);
}
Also used : StubTree(com.intellij.psi.stubs.StubTree) FileElement(com.intellij.psi.impl.source.tree.FileElement)

Example 29 with FileElement

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

the class PomModelImpl method getContainingFileByTree.

@Nullable
private static PsiFile getContainingFileByTree(@NotNull final PsiElement changeScope) {
    // there could be pseudo physical trees (JSPX/JSP/etc.) which must not translate
    // any changes to document and not to fire any PSI events
    final PsiFile psiFile;
    final ASTNode node = changeScope.getNode();
    if (node == null) {
        psiFile = changeScope.getContainingFile();
    } else {
        final FileElement fileElement = TreeUtil.getFileElement((TreeElement) node);
        // Hack. the containing tree can be invalidated if updating supplementary trees like HTML in JSP.
        if (fileElement == null)
            return null;
        psiFile = (PsiFile) fileElement.getPsi();
    }
    return psiFile.getNode() != null ? psiFile : null;
}
Also used : ASTNode(com.intellij.lang.ASTNode) FileElement(com.intellij.psi.impl.source.tree.FileElement) Nullable(org.jetbrains.annotations.Nullable)

Example 30 with FileElement

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

the class XmlTextImpl method _splitText.

@Nullable
private XmlText _splitText(final int displayOffset) throws IncorrectOperationException {
    final XmlTag xmlTag = (XmlTag) getParent();
    if (displayOffset == 0)
        return this;
    final int length = getValue().length();
    if (displayOffset >= length) {
        return null;
    }
    final PomModel model = PomManager.getModel(xmlTag.getProject());
    final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
    class MyTransaction extends PomTransactionBase {

        private XmlTextImpl myRight;

        MyTransaction() {
            super(xmlTag, aspect);
        }

        @Override
        @Nullable
        public PomModelEvent runInner() throws IncorrectOperationException {
            final String oldText = getValue();
            final int physicalOffset = displayToPhysical(displayOffset);
            PsiElement childElement = findElementAt(physicalOffset);
            if (childElement != null && childElement.getNode().getElementType() == XmlTokenType.XML_DATA_CHARACTERS) {
                FileElement holder = DummyHolderFactory.createHolder(getManager(), null).getTreeElement();
                int splitOffset = physicalOffset - childElement.getStartOffsetInParent();
                myRight = (XmlTextImpl) ASTFactory.composite(XmlElementType.XML_TEXT);
                CodeEditUtil.setNodeGenerated(myRight, true);
                holder.rawAddChildren(myRight);
                PsiElement e = childElement;
                while (e != null) {
                    CodeEditUtil.setNodeGenerated(e.getNode(), true);
                    e = e.getNextSibling();
                }
                String leftText = childElement.getText().substring(0, splitOffset);
                String rightText = childElement.getText().substring(splitOffset);
                LeafElement rightElement = ASTFactory.leaf(XmlTokenType.XML_DATA_CHARACTERS, holder.getCharTable().intern(rightText));
                CodeEditUtil.setNodeGenerated(rightElement, true);
                LeafElement leftElement = ASTFactory.leaf(XmlTokenType.XML_DATA_CHARACTERS, holder.getCharTable().intern(leftText));
                CodeEditUtil.setNodeGenerated(leftElement, true);
                rawInsertAfterMe(myRight);
                myRight.rawAddChildren(rightElement);
                if (childElement.getNextSibling() != null) {
                    myRight.rawAddChildren((TreeElement) childElement.getNextSibling());
                }
                ((TreeElement) childElement).rawRemove();
                XmlTextImpl.this.rawAddChildren(leftElement);
            } else {
                final PsiFile containingFile = xmlTag.getContainingFile();
                final FileElement holder = DummyHolderFactory.createHolder(containingFile.getManager(), null, ((PsiFileImpl) containingFile).getTreeElement().getCharTable()).getTreeElement();
                final XmlTextImpl rightText = (XmlTextImpl) ASTFactory.composite(XmlElementType.XML_TEXT);
                CodeEditUtil.setNodeGenerated(rightText, true);
                holder.rawAddChildren(rightText);
                ((ASTNode) xmlTag).addChild(rightText, getTreeNext());
                final String value = getValue();
                setValue(value.substring(0, displayOffset));
                rightText.setValue(value.substring(displayOffset));
                CodeEditUtil.setNodeGenerated(rightText, true);
                myRight = rightText;
            }
            clearCaches();
            myRight.clearCaches();
            return createEvent(new XmlTextChangedImpl(XmlTextImpl.this, oldText), new XmlTagChildAddImpl(xmlTag, myRight));
        }

        public XmlText getResult() {
            return myRight;
        }
    }
    final MyTransaction transaction = new MyTransaction();
    model.runTransaction(transaction);
    return transaction.getResult();
}
Also used : PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) TreeElement(com.intellij.psi.impl.source.tree.TreeElement) XmlTagChildAddImpl(com.intellij.pom.xml.impl.events.XmlTagChildAddImpl) XmlAspect(com.intellij.pom.xml.XmlAspect) ASTNode(com.intellij.lang.ASTNode) PomModel(com.intellij.pom.PomModel) FileElement(com.intellij.psi.impl.source.tree.FileElement) XmlTextChangedImpl(com.intellij.pom.xml.impl.events.XmlTextChangedImpl) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

FileElement (com.intellij.psi.impl.source.tree.FileElement)33 NotNull (org.jetbrains.annotations.NotNull)10 ASTNode (com.intellij.lang.ASTNode)7 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)6 PsiManagerEx (com.intellij.psi.impl.PsiManagerEx)5 FileManager (com.intellij.psi.impl.file.impl.FileManager)5 Project (com.intellij.openapi.project.Project)4 PsiFile (com.intellij.psi.PsiFile)4 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)4 SingleRootFileViewProvider (com.intellij.psi.SingleRootFileViewProvider)3 Nullable (org.jetbrains.annotations.Nullable)3 Lexer (com.intellij.lexer.Lexer)2 Disposable (com.intellij.openapi.Disposable)2 Document (com.intellij.openapi.editor.Document)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 PomModel (com.intellij.pom.PomModel)2 PomTransactionBase (com.intellij.pom.impl.PomTransactionBase)2 GeneratedMarkerVisitor (com.intellij.psi.impl.GeneratedMarkerVisitor)2 DummyHolder (com.intellij.psi.impl.source.DummyHolder)2 LeafElement (com.intellij.psi.impl.source.tree.LeafElement)2