[FE] Support analyzable files throughout all 'KtPsiFactory' API

Before, the only way of getting analyzable elements was to create an
analyzable file by using 'createAnalyzableFile()'. So made all utilities
available in 'KtPsiFactory' useless as they delegate to 'createFile()'
that always set the 'doNotAnalyze' flag.

The new behavior is to pass the 'analysisContext' instead if it is
passed to the 'KtPsiFactory' constructor.

The newly appeared API is going to be used in the Kotlin's UAST
implementation.
This commit is contained in:
Yan Zhulanow
2022-10-04 02:15:30 +09:00
committed by teamcity
parent f3c4ae8bbf
commit ea3f550b58
37 changed files with 102 additions and 86 deletions
@@ -121,7 +121,7 @@ public abstract class AbstractParsingTest extends KtParsingTestCase {
}
private PsiFile createFile(@NotNull String filePath, @NotNull IElementType fileType, @NotNull String fileContent) {
KtPsiFactory psiFactory = KtPsiFactoryKt.KtPsiFactory(myProject);
KtPsiFactory psiFactory = new KtPsiFactory(myProject);
if (fileType == KtNodeTypes.EXPRESSION_CODE_FRAGMENT) {
return psiFactory.createExpressionCodeFragment(fileContent, null);
@@ -86,7 +86,7 @@ public class KtPsiUtilTest extends KotlinTestWithEnvironment {
public void testIsLocalClass() throws IOException {
String text = FileUtil.loadFile(new File(KtTestUtil.getTestDataPathBase() + "/psiUtil/isLocalClass.kt"), true);
KtClass aClass = KtPsiFactoryKt.KtPsiFactory(getProject()).createClass(text);
KtClass aClass = new KtPsiFactory(getProject()).createClass(text);
@SuppressWarnings("unchecked")
Collection<KtClassOrObject> classOrObjects = PsiTreeUtil.collectElementsOfType(aClass, KtClassOrObject.class);
@@ -123,7 +123,7 @@ public class KtPsiUtilTest extends KotlinTestWithEnvironment {
private ImportPath getImportPathFromParsed(String text) {
KtImportDirective importDirective =
PsiTreeUtil.findChildOfType(KtPsiFactoryKt.KtPsiFactory(getProject()).createFile(text), KtImportDirective.class);
PsiTreeUtil.findChildOfType(new KtPsiFactory(getProject()).createFile(text), KtImportDirective.class);
assertNotNull("At least one import directive is expected", importDirective);
@@ -25,7 +25,7 @@ import org.jetbrains.kotlin.incremental.components.NoLookupLocation;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.KtFile;
import org.jetbrains.kotlin.psi.KtPsiFactoryKt;
import org.jetbrains.kotlin.psi.KtPsiFactory;
import org.jetbrains.kotlin.renderer.DescriptorRenderer;
import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil;
import org.jetbrains.kotlin.test.ConfigurationKind;
@@ -70,7 +70,7 @@ public class BoundsSubstitutorTest extends KotlinTestWithEnvironment {
//}
private void doTest(String text, String expected) {
KtFile ktFile = KtPsiFactoryKt.KtPsiFactory(getProject()).createFile("fun.kt", text);
KtFile ktFile = new KtPsiFactory(getProject()).createFile("fun.kt", text);
ModuleDescriptor module = JvmResolveUtil.analyze(ktFile, getEnvironment()).getModuleDescriptor();
Collection<? extends SimpleFunctionDescriptor> functions =
module.getPackage(FqName.ROOT).getMemberScope().getContributedFunctions(Name.identifier("f"), NoLookupLocation.FROM_TEST);
@@ -92,8 +92,7 @@ public class DefaultModalityModifiersTest extends KotlinTestWithEnvironment {
@NotNull
private LexicalScope createScope(@NotNull MemberScope libraryScope) {
KtFile file =
KtPsiFactoryKt.KtPsiFactory(getProject()).createFile("abstract class C { abstract fun foo(); abstract val a: Int }");
KtFile file = new KtPsiFactory(getProject()).createFile("abstract class C { abstract fun foo(); abstract val a: Int }");
KtDeclaration aClass = file.getDeclarations().get(0);
assert aClass instanceof KtClass;
AnalysisResult bindingContext = JvmResolveUtil.analyzeAndCheckForErrors(file, getEnvironment());
@@ -129,7 +128,7 @@ public class DefaultModalityModifiersTest extends KotlinTestWithEnvironment {
}
private void testClassModality(String classDeclaration, ClassKind kind, Modality expectedModality) {
KtClass aClass = KtPsiFactoryKt.KtPsiFactory(getProject()).createClass(classDeclaration);
KtClass aClass = new KtPsiFactory(getProject()).createClass(classDeclaration);
ClassDescriptorWithResolutionScopes classDescriptor = createClassDescriptor(kind, aClass);
assertEquals(expectedModality, classDescriptor.getModality());
@@ -137,7 +136,7 @@ public class DefaultModalityModifiersTest extends KotlinTestWithEnvironment {
private void testFunctionModality(String classWithFunction, ClassKind kind, Modality expectedFunctionModality) {
KtClass aClass = KtPsiFactoryKt.KtPsiFactory(getProject()).createClass(classWithFunction);
KtClass aClass = new KtPsiFactory(getProject()).createClass(classWithFunction);
ClassDescriptorWithResolutionScopes classDescriptor = createClassDescriptor(kind, aClass);
List<KtDeclaration> declarations = aClass.getDeclarations();
@@ -150,7 +149,7 @@ public class DefaultModalityModifiersTest extends KotlinTestWithEnvironment {
}
private void testPropertyModality(String classWithProperty, ClassKind kind, Modality expectedPropertyModality) {
KtClass aClass = KtPsiFactoryKt.KtPsiFactory(getProject()).createClass(classWithProperty);
KtClass aClass = new KtPsiFactory(getProject()).createClass(classWithProperty);
ClassDescriptorWithResolutionScopes classDescriptor = createClassDescriptor(kind, aClass);
List<KtDeclaration> declarations = aClass.getDeclarations();
@@ -166,7 +165,7 @@ public class DefaultModalityModifiersTest extends KotlinTestWithEnvironment {
private void testPropertyAccessorModality(String classWithPropertyWithAccessor, ClassKind kind, Modality expectedPropertyAccessorModality, boolean isGetter) {
KtClass aClass = KtPsiFactoryKt.KtPsiFactory(getProject()).createClass(classWithPropertyWithAccessor);
KtClass aClass = new KtPsiFactory(getProject()).createClass(classWithPropertyWithAccessor);
ClassDescriptorWithResolutionScopes classDescriptor = createClassDescriptor(kind, aClass);
List<KtDeclaration> declarations = aClass.getDeclarations();
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.container.DslKt;
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
import org.jetbrains.kotlin.descriptors.ModuleDescriptor;
import org.jetbrains.kotlin.psi.KtNamedFunction;
import org.jetbrains.kotlin.psi.KtPsiFactoryKt;
import org.jetbrains.kotlin.psi.KtPsiFactory;
import org.jetbrains.kotlin.resolve.FunctionDescriptorResolver;
import org.jetbrains.kotlin.resolve.OverloadChecker;
import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator;
@@ -168,7 +168,7 @@ public class KotlinOverloadTest extends KotlinTestWithEnvironment {
}
private FunctionDescriptor makeFunction(String funDecl) {
KtNamedFunction function = KtPsiFactoryKt.KtPsiFactory(getProject()).createFunction(funDecl);
KtNamedFunction function = new KtPsiFactory(getProject()).createFunction(funDecl);
LexicalScope scope = TypeTestUtilsKt.builtInPackageAsLexicalScope(module);
return functionDescriptorResolver.resolveFunctionDescriptor(
module, scope, function, DummyTraces.DUMMY_TRACE, DataFlowInfoFactory.EMPTY, null
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.container.DslKt;
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
import org.jetbrains.kotlin.descriptors.ModuleDescriptor;
import org.jetbrains.kotlin.psi.KtNamedFunction;
import org.jetbrains.kotlin.psi.KtPsiFactoryKt;
import org.jetbrains.kotlin.psi.KtPsiFactory;
import org.jetbrains.kotlin.resolve.FunctionDescriptorResolver;
import org.jetbrains.kotlin.resolve.OverridingUtil;
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfoFactory;
@@ -169,7 +169,7 @@ public class KotlinOverridingTest extends KotlinTestWithEnvironment {
}
private FunctionDescriptor makeFunction(String funDecl) {
KtNamedFunction function = KtPsiFactoryKt.KtPsiFactory(getProject()).createFunction(funDecl);
KtNamedFunction function = new KtPsiFactory(getProject()).createFunction(funDecl);
LexicalScope scope = TypeTestUtilsKt.builtInPackageAsLexicalScope(module);
return functionDescriptorResolver.resolveFunctionDescriptor(
module, scope, function, DummyTraces.DUMMY_TRACE, DataFlowInfoFactory.EMPTY, null
@@ -25,10 +25,7 @@ import org.jetbrains.kotlin.descriptors.PackageFragmentProvider;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl;
import org.jetbrains.kotlin.descriptors.impl.ReceiverParameterDescriptorImpl;
import org.jetbrains.kotlin.psi.KtExpression;
import org.jetbrains.kotlin.psi.KtFile;
import org.jetbrains.kotlin.psi.KtPsiFactoryKt;
import org.jetbrains.kotlin.psi.KtTypeReference;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.BindingTraceContext;
import org.jetbrains.kotlin.resolve.TypeResolver;
@@ -532,7 +529,7 @@ public class KotlinTypeCheckerTest extends KotlinTestWithEnvironment {
private void assertType(String expression, KotlinType expectedType) {
Project project = getProject();
KtExpression ktExpression = KtPsiFactoryKt.KtPsiFactory(project).createExpression(expression);
KtExpression ktExpression = new KtPsiFactory(project).createExpression(expression);
KotlinType type = expressionTypingServices.getType(
scopeWithImports, ktExpression, TypeUtils.NO_EXPECTED_TYPE,
DataFlowInfoFactory.EMPTY, InferenceSession.Companion.getDefault(),
@@ -562,7 +559,7 @@ public class KotlinTypeCheckerTest extends KotlinTestWithEnvironment {
private void assertType(LexicalScope scope, String expression, String expectedTypeStr) {
Project project = getProject();
KtExpression ktExpression = KtPsiFactoryKt.KtPsiFactory(project).createExpression(expression);
KtExpression ktExpression = new KtPsiFactory(project).createExpression(expression);
KotlinType type = expressionTypingServices.getType(
scope, ktExpression, TypeUtils.NO_EXPECTED_TYPE,
DataFlowInfoFactory.EMPTY, InferenceSession.Companion.getDefault(),
@@ -585,7 +582,7 @@ public class KotlinTypeCheckerTest extends KotlinTestWithEnvironment {
}
private KotlinType makeType(LexicalScope scope, String typeStr) {
KtTypeReference typeReference = KtPsiFactoryKt.KtPsiFactory(getProject()).createType(typeStr);
KtTypeReference typeReference = new KtPsiFactory(getProject()).createType(typeStr);
return typeResolver.resolveTypeWithPossibleIntersections(scope, typeReference, DummyTraces.DUMMY_TRACE);
}
}
@@ -32,7 +32,7 @@ import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages;
import org.jetbrains.kotlin.incremental.components.NoLookupLocation;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.KtFile;
import org.jetbrains.kotlin.psi.KtPsiFactoryKt;
import org.jetbrains.kotlin.psi.KtPsiFactory;
import org.jetbrains.kotlin.psi.KtTypeReference;
import org.jetbrains.kotlin.renderer.DescriptorRenderer;
import org.jetbrains.kotlin.resolve.*;
@@ -80,7 +80,7 @@ public class TypeSubstitutorTest extends KotlinTestWithEnvironment {
private LexicalScope getContextScope() throws IOException {
// todo comments
String text = FileUtil.loadFile(new File("compiler/testData/type-substitutor.kt"), true);
KtFile ktFile = KtPsiFactoryKt.KtPsiFactory(getProject()).createFile(text);
KtFile ktFile = new KtPsiFactory(getProject()).createFile(text);
AnalysisResult analysisResult = JvmResolveUtil.analyze(ktFile, getEnvironment());
ModuleDescriptor module = analysisResult.getModuleDescriptor();
@@ -145,7 +145,7 @@ public class TypeSubstitutorTest extends KotlinTestWithEnvironment {
}
private KotlinType resolveType(String typeStr) {
KtTypeReference jetTypeReference = KtPsiFactoryKt.KtPsiFactory(getProject()).createType(typeStr);
KtTypeReference jetTypeReference = new KtPsiFactory(getProject()).createType(typeStr);
AnalyzingUtils.checkForSyntacticErrors(jetTypeReference);
BindingTrace trace = new BindingTraceContext();
KotlinType type = container.getTypeResolver().resolveType(scope, jetTypeReference, trace, true);
@@ -30,7 +30,7 @@ import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.KtPsiFactoryKt;
import org.jetbrains.kotlin.psi.KtPsiFactory;
import org.jetbrains.kotlin.psi.KtTypeProjection;
import org.jetbrains.kotlin.psi.KtTypeReference;
import org.jetbrains.kotlin.resolve.TypeResolver;
@@ -206,8 +206,7 @@ public class TypeUnifierTest extends KotlinTestWithEnvironment {
}
);
KtTypeProjection projection = KtPsiFactoryKt
.KtPsiFactory(getProject()).createTypeArguments("<" + typeStr + ">").getArguments().get(0);
KtTypeProjection projection = new KtPsiFactory(getProject()).createTypeArguments("<" + typeStr + ">").getArguments().get(0);
KtTypeReference typeReference = projection.getTypeReference();
assert typeReference != null;