| /* |
| * Copyright 2000-2014 JetBrains s.r.o. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.intellij.psi.impl.source.tree; |
| |
| import com.intellij.lang.ASTNode; |
| import com.intellij.lang.Language; |
| import com.intellij.openapi.diagnostic.Logger; |
| import com.intellij.openapi.project.Project; |
| import com.intellij.psi.*; |
| import com.intellij.psi.impl.CheckUtil; |
| import com.intellij.psi.impl.ResolveScopeManager; |
| import com.intellij.psi.impl.SharedPsiElementImplUtil; |
| import com.intellij.psi.impl.source.SourceTreeToPsiMap; |
| import com.intellij.psi.scope.PsiScopeProcessor; |
| import com.intellij.psi.search.GlobalSearchScope; |
| import com.intellij.psi.search.SearchScope; |
| import com.intellij.psi.tree.IElementType; |
| import com.intellij.util.IncorrectOperationException; |
| import org.jetbrains.annotations.NotNull; |
| |
| public class OwnBufferLeafPsiElement extends LeafElement implements PsiElement { |
| private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.source.tree.LeafPsiElement"); |
| |
| public OwnBufferLeafPsiElement(@NotNull IElementType type, CharSequence text) { |
| super(type, text); |
| } |
| |
| @Override |
| @NotNull |
| public PsiElement[] getChildren() { |
| return PsiElement.EMPTY_ARRAY; |
| } |
| |
| @Override |
| public PsiElement getFirstChild() { |
| return null; |
| } |
| |
| @Override |
| public PsiElement getLastChild() { |
| return null; |
| } |
| |
| @Override |
| public void acceptChildren(@NotNull PsiElementVisitor visitor) { |
| } |
| |
| @Override |
| public PsiElement getParent() { |
| return SharedImplUtil.getParent(this); |
| } |
| |
| @Override |
| public PsiElement getNextSibling() { |
| return SharedImplUtil.getNextSibling(this); |
| } |
| |
| @Override |
| public PsiElement getPrevSibling() { |
| return SharedImplUtil.getPrevSibling(this); |
| } |
| |
| @Override |
| public PsiFile getContainingFile() { |
| PsiFile file = SharedImplUtil.getContainingFile(this); |
| if (file == null || !file.isValid()) throw new PsiInvalidElementAccessException(this); |
| return file; |
| } |
| |
| @Override |
| public PsiElement findElementAt(int offset) { |
| return this; |
| } |
| |
| @Override |
| public PsiReference findReferenceAt(int offset) { |
| return SharedPsiElementImplUtil.findReferenceAt(this, offset); |
| } |
| |
| @Override |
| public PsiElement copy() { |
| ASTNode elementCopy = copyElement(); |
| return SourceTreeToPsiMap.treeElementToPsi(elementCopy); |
| } |
| |
| @Override |
| public boolean isValid() { |
| return SharedImplUtil.isValid(this); |
| } |
| |
| @Override |
| public boolean isWritable() { |
| return SharedImplUtil.isWritable(this); |
| } |
| |
| @Override |
| public PsiReference getReference() { |
| return null; |
| } |
| |
| @Override |
| @NotNull |
| public PsiReference[] getReferences() { |
| return SharedPsiElementImplUtil.getReferences(this); |
| } |
| |
| @Override |
| public PsiElement add(@NotNull PsiElement element) throws IncorrectOperationException { |
| throw new IncorrectOperationException(); |
| } |
| |
| @Override |
| public PsiElement addBefore(@NotNull PsiElement element, PsiElement anchor) throws IncorrectOperationException { |
| throw new IncorrectOperationException(); |
| } |
| |
| @Override |
| public PsiElement addAfter(@NotNull PsiElement element, PsiElement anchor) throws IncorrectOperationException { |
| throw new IncorrectOperationException(); |
| } |
| |
| @Override |
| public void checkAdd(@NotNull PsiElement element) throws IncorrectOperationException { |
| throw new IncorrectOperationException(); |
| } |
| |
| @Override |
| public PsiElement addRange(PsiElement first, PsiElement last) throws IncorrectOperationException { |
| throw new IncorrectOperationException(); |
| } |
| |
| @Override |
| public PsiElement addRangeBefore(@NotNull PsiElement first, @NotNull PsiElement last, PsiElement anchor) |
| throws IncorrectOperationException { |
| throw new IncorrectOperationException(); |
| } |
| |
| @Override |
| public PsiElement addRangeAfter(PsiElement first, PsiElement last, PsiElement anchor) |
| throws IncorrectOperationException { |
| throw new IncorrectOperationException(); |
| } |
| |
| @Override |
| public void delete() throws IncorrectOperationException { |
| LOG.assertTrue(getTreeParent() != null); |
| CheckUtil.checkWritable(this); |
| getTreeParent().deleteChildInternal(this); |
| this.invalidate(); |
| } |
| |
| @Override |
| public void checkDelete() throws IncorrectOperationException { |
| CheckUtil.checkWritable(this); |
| } |
| |
| @Override |
| public void deleteChildRange(PsiElement first, PsiElement last) throws IncorrectOperationException { |
| throw new IncorrectOperationException(); |
| } |
| |
| @Override |
| public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperationException { |
| LOG.assertTrue(getTreeParent() != null); |
| CheckUtil.checkWritable(this); |
| TreeElement elementCopy = ChangeUtil.copyToElement(newElement); |
| getTreeParent().replaceChildInternal(this, elementCopy); |
| elementCopy = ChangeUtil.decodeInformation(elementCopy); |
| final PsiElement result = SourceTreeToPsiMap.treeElementToPsi(elementCopy); |
| |
| this.invalidate(); |
| return result; |
| } |
| |
| public String toString() { |
| return "PsiElement" + "(" + getElementType().toString() + ")"; |
| } |
| |
| @Override |
| public void accept(@NotNull PsiElementVisitor visitor) { |
| visitor.visitElement(this); |
| } |
| |
| @Override |
| public boolean processDeclarations(@NotNull PsiScopeProcessor processor, |
| @NotNull ResolveState state, |
| PsiElement lastParent, |
| @NotNull PsiElement place) { |
| return true; |
| } |
| |
| @Override |
| public PsiElement getContext() { |
| return getParent(); |
| } |
| |
| @Override |
| public PsiElement getNavigationElement() { |
| return this; |
| } |
| |
| @Override |
| public PsiElement getOriginalElement() { |
| return this; |
| } |
| |
| @Override |
| public boolean isPhysical() { |
| PsiFile file = getContainingFile(); |
| return file != null && file.isPhysical(); |
| } |
| |
| @Override |
| @NotNull |
| public GlobalSearchScope getResolveScope() { |
| return ResolveScopeManager.getElementResolveScope(this); |
| } |
| |
| @Override |
| @NotNull |
| public SearchScope getUseScope() { |
| return ResolveScopeManager.getElementUseScope(this); |
| } |
| |
| @Override |
| @NotNull |
| public Project getProject() { |
| final PsiManager manager = getManager(); |
| if (manager == null) throw new PsiInvalidElementAccessException(this); |
| |
| return manager.getProject(); |
| } |
| |
| @Override |
| @NotNull |
| public Language getLanguage() { |
| return getElementType().getLanguage(); |
| } |
| |
| @Override |
| public ASTNode getNode() { |
| return this; |
| } |
| |
| @Override |
| public PsiElement getPsi() { |
| return this; |
| } |
| |
| @Override |
| public boolean isEquivalentTo(final PsiElement another) { |
| return this == another; |
| } |
| } |