diff options
Diffstat (limited to 'java/java-impl/src/com/intellij/spi/psi')
3 files changed, 261 insertions, 0 deletions
diff --git a/java/java-impl/src/com/intellij/spi/psi/SPIFile.java b/java/java-impl/src/com/intellij/spi/psi/SPIFile.java new file mode 100644 index 000000000000..9f1e8106878a --- /dev/null +++ b/java/java-impl/src/com/intellij/spi/psi/SPIFile.java @@ -0,0 +1,97 @@ +/* + * Copyright 2000-2013 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.spi.psi; + +import com.intellij.extapi.psi.PsiFileBase; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.fileTypes.FileType; +import com.intellij.openapi.util.Computable; +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.*; +import com.intellij.psi.util.ClassUtil; +import com.intellij.spi.SPIFileType; +import com.intellij.lang.spi.SPILanguage; +import com.intellij.util.ArrayUtil; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * User: anna + */ +public class SPIFile extends PsiFileBase { + public SPIFile(@NotNull FileViewProvider viewProvider) { + super(viewProvider, SPILanguage.INSTANCE); + } + + @Override + public PsiReference getReference() { + return new SPIFileName2ClassReference(this, ApplicationManager.getApplication().runReadAction(new Computable<PsiClass>() { + @Override + public PsiClass compute() { + return ClassUtil.findPsiClass(getManager(), getName(), null, true, getResolveScope()); + } + })); + } + + @NotNull + @Override + public FileType getFileType() { + return SPIFileType.INSTANCE; + } + + private static class SPIFileName2ClassReference extends PsiReferenceBase<PsiFile> { + private final PsiClass myClass; + + public SPIFileName2ClassReference(PsiFile file, PsiClass aClass) { + super(file, new TextRange(0, 0), false); + myClass = aClass; + } + + @Nullable + @Override + public PsiElement resolve() { + return myClass; + } + + @Override + public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { + final String className = ClassUtil.getJVMClassName(myClass); + if (className != null) { + final String newFileName = className.substring(0, className.lastIndexOf(myClass.getName())) + newElementName; + return getElement().setName(newFileName); + } + return getElement(); + } + + @Override + public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException { + if (element instanceof PsiClass) { + final String className = ClassUtil.getJVMClassName((PsiClass)element); + if (className != null) { + return getElement().setName(className); + } + } + return getElement(); + } + + @NotNull + @Override + public Object[] getVariants() { + return ArrayUtil.EMPTY_OBJECT_ARRAY; + } + } +} diff --git a/java/java-impl/src/com/intellij/spi/psi/SPIProviderElement.java b/java/java-impl/src/com/intellij/spi/psi/SPIProviderElement.java new file mode 100644 index 000000000000..17c47b0ffbcc --- /dev/null +++ b/java/java-impl/src/com/intellij/spi/psi/SPIProviderElement.java @@ -0,0 +1,127 @@ +/* + * Copyright 2000-2013 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.spi.psi; + +import com.intellij.extapi.psi.ASTWrapperPsiElement; +import com.intellij.lang.ASTNode; +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.*; +import com.intellij.psi.infos.ClassCandidateInfo; +import com.intellij.psi.scope.PsiScopeProcessor; +import com.intellij.psi.util.ClassUtil; +import com.intellij.spi.SPIFileType; +import com.intellij.util.ArrayUtil; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * User: anna + */ +public class SPIProviderElement extends ASTWrapperPsiElement implements PsiJavaReference { + public SPIProviderElement(ASTNode node) { + super(node); + } + + @Override + public void processVariants(PsiScopeProcessor processor) { + } + + @NotNull + @Override + public JavaResolveResult advancedResolve(boolean incompleteCode) { + final PsiElement resolve = resolve(); + if (resolve instanceof PsiClass) { + return new ClassCandidateInfo(resolve, PsiSubstitutor.EMPTY); + } + return JavaResolveResult.EMPTY; + } + + @NotNull + @Override + public JavaResolveResult[] multiResolve(boolean incompleteCode) { + final PsiElement resolve = resolve(); + if (resolve instanceof PsiClass) { + return new JavaResolveResult[]{new ClassCandidateInfo(resolve, PsiSubstitutor.EMPTY)}; + } + return JavaResolveResult.EMPTY_ARRAY; + } + + @Override + public PsiElement getElement() { + return this; + } + + @Override + public TextRange getRangeInElement() { + return new TextRange(0, getTextLength()); + } + + @Nullable + @Override + public PsiElement resolve() { + return ClassUtil.findPsiClass(getManager(), getText(), null, true, getResolveScope()); + } + + @NotNull + @Override + public String getCanonicalText() { + return getText(); + } + + @Override + public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { + final SPIProvidersElementList firstChild = + (SPIProvidersElementList)PsiFileFactory.getInstance(getProject()) + .createFileFromText("spi_dummy", SPIFileType.INSTANCE, newElementName).getFirstChild(); + return replace(firstChild.getElements().get(0)); + } + + @Override + public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException { + if (element instanceof PsiClass) { + final String className = ClassUtil.getJVMClassName((PsiClass)element); + if (className != null) { + return handleElementRename(className); + } + } + return null; + } + + @Override + public boolean isReferenceTo(PsiElement element) { + if (element instanceof PsiClass) { + return getText().equals(ClassUtil.getJVMClassName((PsiClass)element)); + } + return false; + } + + @NotNull + @Override + public Object[] getVariants() { + return ArrayUtil.EMPTY_OBJECT_ARRAY; + } + + @Override + public PsiReference getReference() { + return this; + } + + @Override + public boolean isSoft() { + return false; + } +} diff --git a/java/java-impl/src/com/intellij/spi/psi/SPIProvidersElementList.java b/java/java-impl/src/com/intellij/spi/psi/SPIProvidersElementList.java new file mode 100644 index 000000000000..75cc44cb700f --- /dev/null +++ b/java/java-impl/src/com/intellij/spi/psi/SPIProvidersElementList.java @@ -0,0 +1,37 @@ +/* + * Copyright 2000-2013 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.spi.psi; + +import com.intellij.extapi.psi.ASTWrapperPsiElement; +import com.intellij.lang.ASTNode; +import com.intellij.spi.parsing.SPIElementType; +import com.intellij.spi.parsing.SPIElementTypes; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** + * User: anna + */ +public class SPIProvidersElementList extends ASTWrapperPsiElement { + public SPIProvidersElementList(@NotNull ASTNode node) { + super(node); + } + + public List<SPIProviderElement> getElements() { + return findChildrenByType(SPIElementTypes.PROVIDER); + } +} |