Refactor: extract mapping package to files to a separate component
Make it extensible
This commit is contained in:
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.resolve;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import kotlin.Pair;
|
||||
@@ -40,7 +39,6 @@ import org.jetbrains.kotlin.descriptors.impl.*;
|
||||
import org.jetbrains.kotlin.diagnostics.Errors;
|
||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation;
|
||||
import org.jetbrains.kotlin.lexer.KtTokens;
|
||||
import org.jetbrains.kotlin.name.FqName;
|
||||
import org.jetbrains.kotlin.name.Name;
|
||||
import org.jetbrains.kotlin.psi.*;
|
||||
import org.jetbrains.kotlin.psi.psiUtil.PsiUtilsKt;
|
||||
@@ -67,7 +65,8 @@ import java.util.*;
|
||||
import static org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget.*;
|
||||
import static org.jetbrains.kotlin.diagnostics.Errors.*;
|
||||
import static org.jetbrains.kotlin.lexer.KtTokens.*;
|
||||
import static org.jetbrains.kotlin.resolve.BindingContext.*;
|
||||
import static org.jetbrains.kotlin.resolve.BindingContext.CONSTRUCTOR;
|
||||
import static org.jetbrains.kotlin.resolve.BindingContext.TYPE_ALIAS;
|
||||
import static org.jetbrains.kotlin.resolve.DescriptorUtils.*;
|
||||
import static org.jetbrains.kotlin.resolve.ModifiersChecker.resolveMemberModalityFromModifiers;
|
||||
import static org.jetbrains.kotlin.resolve.ModifiersChecker.resolveVisibilityFromModifiers;
|
||||
@@ -1343,16 +1342,4 @@ public class DescriptorResolver {
|
||||
public static ClassDescriptor getContainingClass(@NotNull LexicalScope scope) {
|
||||
return getParentOfType(scope.getOwnerDescriptor(), ClassDescriptor.class, false);
|
||||
}
|
||||
|
||||
public static void registerFileInPackage(@NotNull BindingTrace trace, @NotNull KtFile file) {
|
||||
// Register files corresponding to this package
|
||||
// The trace currently does not support bi-di multimaps that would handle this task nicer
|
||||
FqName fqName = file.getPackageFqName();
|
||||
Collection<KtFile> files = trace.get(PACKAGE_TO_FILES, fqName);
|
||||
if (files == null) {
|
||||
files = Sets.newIdentityHashSet();
|
||||
}
|
||||
files.add(file);
|
||||
trace.record(BindingContext.PACKAGE_TO_FILES, fqName, files);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.resolve
|
||||
|
||||
import com.google.common.collect.Sets
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.resolve.BindingContext.PACKAGE_TO_FILES
|
||||
|
||||
interface FilePreprocessorExtension {
|
||||
fun preprocessFile(file: KtFile)
|
||||
}
|
||||
|
||||
class FilePreprocessor(
|
||||
private val trace: BindingTrace,
|
||||
private val extensions: Iterable<FilePreprocessorExtension>
|
||||
) {
|
||||
fun preprocessFile(file: KtFile) {
|
||||
registerFileByPackage(file)
|
||||
|
||||
for (extension in extensions) {
|
||||
extension.preprocessFile(file)
|
||||
}
|
||||
}
|
||||
|
||||
private fun registerFileByPackage(file: KtFile) {
|
||||
// Register files corresponding to this package
|
||||
// The trace currently does not support bi-di multimaps that would handle this task nicer
|
||||
val fqName = file.packageFqName
|
||||
val files = trace.get(PACKAGE_TO_FILES, fqName) ?: Sets.newIdentityHashSet()
|
||||
files.add(file)
|
||||
trace.record(PACKAGE_TO_FILES, fqName, files)
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,8 @@ class LazyTopDownAnalyzer(
|
||||
private val identifierChecker: IdentifierChecker,
|
||||
private val languageVersionSettings: LanguageVersionSettings,
|
||||
private val deprecationResolver: DeprecationResolver,
|
||||
private val classifierUsageCheckers: Iterable<ClassifierUsageChecker>
|
||||
private val classifierUsageCheckers: Iterable<ClassifierUsageChecker>,
|
||||
private val filePreprocessor: FilePreprocessor
|
||||
) {
|
||||
fun analyzeDeclarations(
|
||||
topDownAnalysisMode: TopDownAnalysisMode,
|
||||
@@ -91,7 +92,7 @@ class LazyTopDownAnalyzer(
|
||||
}
|
||||
|
||||
override fun visitKtFile(file: KtFile) {
|
||||
DescriptorResolver.registerFileInPackage(trace, file)
|
||||
filePreprocessor.preprocessFile(file)
|
||||
registerDeclarations(file.declarations)
|
||||
val packageDirective = file.packageDirective
|
||||
assert(file.isScript() || packageDirective != null) { "No package in a non-script file: " + file }
|
||||
|
||||
Reference in New Issue
Block a user