[psi] avoid usages of KtFile in kotlin psi
^KT-65223 fixed
This commit is contained in:
@@ -29,6 +29,7 @@ import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.psi.psiUtil.ClassIdCalculator
|
||||
import org.jetbrains.kotlin.psi.psiUtil.isKtFile
|
||||
import org.jetbrains.kotlin.psi.stubs.KotlinClassOrObjectStub
|
||||
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
|
||||
|
||||
@@ -94,7 +95,7 @@ abstract class KtClassOrObject :
|
||||
return getOrCreateBody().addBefore(declaration, anchorAfter) as T
|
||||
}
|
||||
|
||||
fun isTopLevel(): Boolean = stub?.isTopLevel() ?: (parent is KtFile)
|
||||
fun isTopLevel(): Boolean = stub?.isTopLevel() ?: isKtFile(parent)
|
||||
|
||||
override fun getClassId(): ClassId? {
|
||||
stub?.let { return it.getClassId() }
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.kotlin.psi
|
||||
|
||||
import com.intellij.psi.NavigatablePsiElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.PsiReference
|
||||
|
||||
interface KtElement : NavigatablePsiElement, KtPureElement {
|
||||
@@ -30,7 +31,7 @@ interface KtElement : NavigatablePsiElement, KtPureElement {
|
||||
|
||||
fun KtElement.getModificationStamp(): Long {
|
||||
return when (this) {
|
||||
is KtFile -> this.modificationStamp
|
||||
is PsiFile -> this.modificationStamp
|
||||
is KtDeclarationStub<*> -> this.modificationStamp
|
||||
is KtSuperTypeList -> this.modificationStamp
|
||||
is KtScriptInitializer -> this.getModificationStamp()
|
||||
|
||||
@@ -25,6 +25,8 @@ import org.jetbrains.kotlin.KtNodeTypes;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt.isKtFile;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public abstract class KtFunctionNotStubbed extends KtTypeParameterListOwnerNotStubbed implements KtFunction {
|
||||
|
||||
@@ -90,6 +92,6 @@ public abstract class KtFunctionNotStubbed extends KtTypeParameterListOwnerNotSt
|
||||
@Override
|
||||
public boolean isLocal() {
|
||||
PsiElement parent = getParent();
|
||||
return !(parent instanceof KtFile || parent instanceof KtClassBody);
|
||||
return !(isKtFile(parent) || parent instanceof KtClassBody);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,8 @@ import org.jetbrains.kotlin.psi.typeRefHelpers.TypeRefHelpersKt;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt.isKtFile;
|
||||
|
||||
public class KtNamedFunction extends KtTypeParameterListOwnerStub<KotlinFunctionStub>
|
||||
implements KtFunction, KtDeclarationWithInitializer {
|
||||
public KtNamedFunction(@NotNull ASTNode node) {
|
||||
@@ -246,7 +248,7 @@ public class KtNamedFunction extends KtTypeParameterListOwnerStub<KotlinFunction
|
||||
@Override
|
||||
public boolean isLocal() {
|
||||
PsiElement parent = getParent();
|
||||
return !(parent instanceof KtFile || parent instanceof KtClassBody || parent.getParent() instanceof KtScript);
|
||||
return !(isKtFile(parent) || parent instanceof KtClassBody || parent.getParent() instanceof KtScript);
|
||||
}
|
||||
|
||||
public boolean isAnonymous() {
|
||||
@@ -259,7 +261,7 @@ public class KtNamedFunction extends KtTypeParameterListOwnerStub<KotlinFunction
|
||||
return stub.isTopLevel();
|
||||
}
|
||||
|
||||
return getParent() instanceof KtFile;
|
||||
return isKtFile(getParent());
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unused", "MethodMayBeStatic"}) //keep for compatibility with potential plugins
|
||||
|
||||
@@ -37,6 +37,7 @@ import java.util.List;
|
||||
|
||||
import static org.jetbrains.kotlin.KtNodeTypes.PROPERTY_DELEGATE;
|
||||
import static org.jetbrains.kotlin.lexer.KtTokens.EQ;
|
||||
import static org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt.isKtFile;
|
||||
|
||||
public class KtProperty extends KtTypeParameterListOwnerStub<KotlinPropertyStub>
|
||||
implements KtVariableDeclaration {
|
||||
@@ -82,7 +83,7 @@ public class KtProperty extends KtTypeParameterListOwnerStub<KotlinPropertyStub>
|
||||
return stub.isTopLevel();
|
||||
}
|
||||
|
||||
return getParent() instanceof KtFile;
|
||||
return isKtFile(getParent());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.KtNodeTypes
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.psi.psiUtil.ClassIdCalculator
|
||||
import org.jetbrains.kotlin.psi.psiUtil.isKtFile
|
||||
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
|
||||
import org.jetbrains.kotlin.psi.stubs.KotlinTypeAliasStub
|
||||
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
|
||||
@@ -35,7 +36,7 @@ class KtTypeAlias : KtTypeParameterListOwnerStub<KotlinTypeAliasStub>, KtNamedDe
|
||||
visitor.visitTypeAlias(this, data)
|
||||
|
||||
fun isTopLevel(): Boolean =
|
||||
stub?.isTopLevel() ?: (parent is KtFile)
|
||||
stub?.isTopLevel() ?: isKtFile(parent)
|
||||
|
||||
@IfNotParsed
|
||||
fun getTypeAliasKeyword(): PsiElement? =
|
||||
|
||||
@@ -12,6 +12,7 @@ import com.intellij.psi.*
|
||||
import com.intellij.psi.stubs.StubElement
|
||||
import com.intellij.psi.tree.TokenSet
|
||||
import org.jetbrains.kotlin.KtNodeTypes
|
||||
import org.jetbrains.kotlin.idea.KotlinLanguage
|
||||
import org.jetbrains.kotlin.lexer.KotlinLexer
|
||||
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
@@ -642,7 +643,7 @@ fun String.quoteIfNeeded(): String = if (this.isIdentifier()) this else "`$this`
|
||||
|
||||
fun PsiElement.isTopLevelKtOrJavaMember(): Boolean {
|
||||
return when (this) {
|
||||
is KtDeclaration -> parent is KtFile
|
||||
is KtDeclaration -> isKtFile(parent)
|
||||
is PsiClass -> containingClass == null && this.qualifiedName != null
|
||||
else -> false
|
||||
}
|
||||
@@ -715,3 +716,9 @@ tailrec fun KtTypeElement.unwrapNullability(): KtTypeElement? {
|
||||
else -> this
|
||||
}
|
||||
}
|
||||
|
||||
internal fun isKtFile(parent: PsiElement?): Boolean {
|
||||
//avoid loading KtFile which depends on java psi, which is not available in some setup
|
||||
//e.g. remote dev https://youtrack.jetbrains.com/issue/GTW-7554
|
||||
return parent is PsiFile && parent.language == KotlinLanguage.INSTANCE
|
||||
}
|
||||
Reference in New Issue
Block a user