diff options
author | Ilya Ryzhenkov <orangy@jetbrains.com> | 2014-10-13 20:14:45 +0400 |
---|---|---|
committer | Ilya Ryzhenkov <orangy@jetbrains.com> | 2014-10-13 20:14:45 +0400 |
commit | 1cb3af902c8f2e3f73e7c78781373f1ab5788772 (patch) | |
tree | 88aaa6a10f0da26d28e9c2792313a2a11cf28356 | |
parent | efd1947722587d15bf1c81a0fd7ca722a7bc6fa8 (diff) | |
download | dokka-1cb3af902c8f2e3f73e7c78781373f1ab5788772.tar.gz dokka-1cb3af902c8f2e3f73e7c78781373f1ab5788772.tar.bz2 dokka-1cb3af902c8f2e3f73e7c78781373f1ab5788772.zip |
Remove BindingContext and migrate to ResolveSession, discover symbols for {code ...} directive.
-rw-r--r-- | src/Analysis/AnalysisEnvironment.kt | 37 | ||||
-rw-r--r-- | src/Analysis/CommentsAPI.kt | 4 | ||||
-rw-r--r-- | src/Analysis/CompilerAPI.kt | 4 | ||||
-rw-r--r-- | src/Formats/StructuredFormatService.kt | 3 | ||||
-rw-r--r-- | src/Kotlin/ContentBuilder.kt | 41 | ||||
-rw-r--r-- | src/Kotlin/DocumentationBuilder.kt | 7 | ||||
-rw-r--r-- | src/Model/Content.kt | 1 | ||||
-rw-r--r-- | src/main.kt | 6 | ||||
-rw-r--r-- | test/src/TestAPI.kt | 6 |
9 files changed, 58 insertions, 51 deletions
diff --git a/src/Analysis/AnalysisEnvironment.kt b/src/Analysis/AnalysisEnvironment.kt index 90231b84..3c7f6767 100644 --- a/src/Analysis/AnalysisEnvironment.kt +++ b/src/Analysis/AnalysisEnvironment.kt @@ -11,6 +11,7 @@ import org.jetbrains.jet.cli.common.* import org.jetbrains.jet.cli.jvm.* import com.intellij.openapi.util.* import org.jetbrains.jet.lang.descriptors.ModuleDescriptor +import org.jetbrains.jet.lang.resolve.lazy.ResolveSession /** * Kotlin as a service entry point @@ -30,28 +31,28 @@ public class AnalysisEnvironment(val messageCollector: MessageCollector, body: A * Executes [processor] when analysis is complete. * $processor is a function to receive compiler environment, module and context for symbol resolution */ - public fun withContext<T>(processor: (JetCoreEnvironment, ModuleDescriptor, BindingContext) -> T): T { + public fun withContext<T>(processor: (JetCoreEnvironment, ResolveSession) -> T): T { val environment = JetCoreEnvironment.createForProduction(this, configuration) val resolveSession = environment.analyze() resolveSession.forceResolveAll() - return processor(environment, resolveSession.getModuleDescriptor(), resolveSession.getBindingContext()) + return processor(environment, resolveSession) } /** * Executes [processor] when analysis is complete. * $processor is a function to receive compiler module and context for symbol resolution */ - public fun withContext<T>(processor: (ModuleDescriptor, BindingContext) -> T): T { - return withContext { environment, module, context -> processor(module, context) } + public fun withContext<T>(processor: (ResolveSession) -> T): T { + return withContext { environment, session -> processor(session) } } /** * Streams files into [processor] and returns a stream of its results * $processor is a function to receive context for symbol resolution and file for processing */ - public fun streamFiles<T>(processor: (BindingContext, JetFile) -> T): Stream<T> { - return withContext { environment, module, context -> - environment.getSourceFiles().stream().map { file -> processor(context, file) } + public fun streamFiles<T>(processor: (ResolveSession, JetFile) -> T): Stream<T> { + return withContext { environment, session -> + environment.getSourceFiles().stream().map { file -> processor(session, file) } } } @@ -59,19 +60,9 @@ public class AnalysisEnvironment(val messageCollector: MessageCollector, body: A * Runs [processor] for each file and collects its results into single list * $processor is a function to receive context for symbol resolution and file for processing */ - public fun processFiles<T>(processor: (BindingContext, JetFile) -> T): List<T> { - return withContext { environment, module, context -> - environment.getSourceFiles().map { file -> processor(context, file) } - } - } - - /** - * Runs [processor] for each file and collects its results into single list - * $processor is a function to receive context for symbol resolution, module and file for processing - */ - public fun processFiles<T>(processor: (BindingContext, ModuleDescriptor, JetFile) -> T): List<T> { - return withContext { environment, module, context -> - environment.getSourceFiles().map { file -> processor(context, module, file) } + public fun processFiles<T>(processor: (ResolveSession, JetFile) -> T): List<T> { + return withContext { environment, session -> + environment.getSourceFiles().map { file -> processor(session, file) } } } @@ -79,9 +70,9 @@ public class AnalysisEnvironment(val messageCollector: MessageCollector, body: A * Runs [processor] for each file and collects its results into single list * $processor is a function to receive context for symbol resolution and file for processing */ - public fun processFilesFlat<T>(processor: (BindingContext, JetFile) -> List<T>): List<T> { - return withContext { environment, module, context -> - environment.getSourceFiles().flatMap { file -> processor(context, file) } + public fun processFilesFlat<T>(processor: (ResolveSession, JetFile) -> List<T>): List<T> { + return withContext { environment, session -> + environment.getSourceFiles().flatMap { file -> processor(session, file) } } } diff --git a/src/Analysis/CommentsAPI.kt b/src/Analysis/CommentsAPI.kt index a17b6aa4..2407de76 100644 --- a/src/Analysis/CommentsAPI.kt +++ b/src/Analysis/CommentsAPI.kt @@ -5,8 +5,8 @@ import org.jetbrains.jet.lang.resolve.* import org.jetbrains.jet.kdoc.psi.api.* import org.jetbrains.jet.lang.psi.* -fun BindingContext.getDocumentationElements(descriptor: DeclarationDescriptor): List<KDoc> { - val psiElement = DescriptorToSourceUtils.descriptorToDeclaration(descriptor) +fun DeclarationDescriptor.getDocumentationElements(): List<KDoc> { + val psiElement = DescriptorToSourceUtils.descriptorToDeclaration(this) if (psiElement == null) return listOf() diff --git a/src/Analysis/CompilerAPI.kt b/src/Analysis/CompilerAPI.kt index 43d0304e..8216c549 100644 --- a/src/Analysis/CompilerAPI.kt +++ b/src/Analysis/CompilerAPI.kt @@ -46,8 +46,6 @@ fun JetCoreEnvironment.analyze(): ResolveSession { return resolverForProject.resolverForModule(module).lazyResolveSession } -fun BindingContext.getPackageFragment(file: JetFile): PackageFragmentDescriptor? = get(BindingContext.FILE_TO_PACKAGE_FRAGMENT, file) - fun DeclarationDescriptor.isUserCode() = when (this) { is PackageViewDescriptor -> false @@ -122,7 +120,7 @@ public fun getPropertyInnerScope(outerScope: JetScope, descriptor: PropertyDescr return propertyScope } -fun BindingContext.getResolutionScope(descriptor: DeclarationDescriptor): JetScope { +fun getResolutionScope(descriptor: DeclarationDescriptor): JetScope { when (descriptor) { is PackageFragmentDescriptor -> return getPackageInnerScope(descriptor) diff --git a/src/Formats/StructuredFormatService.kt b/src/Formats/StructuredFormatService.kt index 4285f8c9..5287e688 100644 --- a/src/Formats/StructuredFormatService.kt +++ b/src/Formats/StructuredFormatService.kt @@ -58,6 +58,9 @@ public abstract class StructuredFormatService(val locationService: LocationServi is ContentParagraph -> { appendText(this, formatText(location, content.children)) } + is ContentBlockCode -> { + appendBlockCode(this, formatText(location, content.children)) + } else -> append(formatText(location, content.children)) } }.toString() diff --git a/src/Kotlin/ContentBuilder.kt b/src/Kotlin/ContentBuilder.kt index 58e41bec..635fc74f 100644 --- a/src/Kotlin/ContentBuilder.kt +++ b/src/Kotlin/ContentBuilder.kt @@ -5,6 +5,8 @@ import java.util.ArrayDeque import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor import org.jetbrains.jet.lang.resolve.name.Name import org.jetbrains.jet.lang.resolve.DescriptorToSourceUtils +import org.jetbrains.jet.lang.resolve.scopes.JetScope +import org.jetbrains.jet.lang.resolve.name.FqName public fun DocumentationBuilder.buildContent(tree: MarkdownTree, descriptor: DeclarationDescriptor): Content { val nodeStack = ArrayDeque<ContentNode>() @@ -100,29 +102,40 @@ public fun DocumentationBuilder.buildContent(tree: MarkdownTree, descriptor: Dec fun DocumentationBuilder.functionBody(descriptor: DeclarationDescriptor, functionName: String): ContentNode { - var scope = context.getResolutionScope(descriptor) + val scope = getResolutionScope(descriptor) + val rootPackage = session.getModuleDescriptor().getPackage(FqName.ROOT)!! + val rootScope = rootPackage.getMemberScope() + val symbol = resolveInScope(functionName, scope) ?: resolveInScope(functionName, rootScope) + if (symbol == null) + return ContentBlockCode().let() { it.append(ContentText("Unresolved: $functionName")); it } + val psiElement = DescriptorToSourceUtils.descriptorToDeclaration(symbol) + if (psiElement == null) + return ContentBlockCode().let() { it.append(ContentText("Source not found: $functionName")); it } + + return ContentBlockCode().let() { it.append(ContentText(psiElement.getText())); it } +} + +private fun DocumentationBuilder.resolveInScope(functionName: String, scope: JetScope): DeclarationDescriptor? { + var currentScope = scope val parts = functionName.split('.') var symbol: DeclarationDescriptor? = null for (part in parts) { // short name val symbolName = Name.guess(part) - val partSymbol = scope.getLocalVariable(symbolName) ?: - scope.getProperties(symbolName).firstOrNull() ?: - scope.getFunctions(symbolName).firstOrNull() ?: - scope.getClassifier(symbolName) ?: - scope.getPackage(symbolName) + val partSymbol = currentScope.getLocalVariable(symbolName) ?: + currentScope.getProperties(symbolName).firstOrNull() ?: + currentScope.getFunctions(symbolName).firstOrNull() ?: + currentScope.getClassifier(symbolName) ?: + currentScope.getPackage(symbolName) - if (partSymbol == null) + if (partSymbol == null) { + symbol = null break - scope = context.getResolutionScope(partSymbol) + } + currentScope = getResolutionScope(partSymbol) symbol = partSymbol } - if (symbol != null) { - val psi = DescriptorToSourceUtils.descriptorToDeclaration(descriptor) - - - } - return ContentCode().let() { it.append(ContentText("inline")); it } + return symbol }
\ No newline at end of file diff --git a/src/Kotlin/DocumentationBuilder.kt b/src/Kotlin/DocumentationBuilder.kt index b18902e4..cee374d9 100644 --- a/src/Kotlin/DocumentationBuilder.kt +++ b/src/Kotlin/DocumentationBuilder.kt @@ -11,10 +11,11 @@ import org.jetbrains.jet.lang.resolve.name.Name import org.jetbrains.jet.lang.resolve.scopes.JetScope import org.jetbrains.jet.lang.psi.JetFile import org.jetbrains.jet.lang.resolve.name.FqName +import org.jetbrains.jet.lang.resolve.lazy.ResolveSession public data class DocumentationOptions(val includeNonPublic: Boolean = false) -class DocumentationBuilder(val context: BindingContext, val options: DocumentationOptions) { +class DocumentationBuilder(val session: ResolveSession, val options: DocumentationOptions) { val visibleToDocumentation = setOf(Visibilities.INTERNAL, Visibilities.PROTECTED, Visibilities.PUBLIC) val descriptorToNode = hashMapOf<DeclarationDescriptor, DocumentationNode>() val nodeToDescriptor = hashMapOf<DocumentationNode, DeclarationDescriptor>() @@ -22,7 +23,7 @@ class DocumentationBuilder(val context: BindingContext, val options: Documentati val packages = hashMapOf<FqName, DocumentationNode>() fun parseDocumentation(descriptor: DeclarationDescriptor): Content { - val docText = context.getDocumentationElements(descriptor).map { it.extractText() }.join("\n") + val docText = descriptor.getDocumentationElements().map { it.extractText() }.join("\n") val tree = MarkdownProcessor.parse(docText) //println(tree.toTestString()) val content = buildContent(tree, descriptor) @@ -295,7 +296,7 @@ class DocumentationBuilder(val context: BindingContext, val options: Documentati fun getResolutionScope(node: DocumentationNode): JetScope { val descriptor = nodeToDescriptor[node] ?: throw IllegalArgumentException("Node is not known to this context") - return context.getResolutionScope(descriptor) + return getResolutionScope(descriptor) } fun resolveContentLinks(node: DocumentationNode, content: ContentNode) { diff --git a/src/Model/Content.kt b/src/Model/Content.kt index 9f716209..3ec3d341 100644 --- a/src/Model/Content.kt +++ b/src/Model/Content.kt @@ -28,6 +28,7 @@ public class ContentParagraph() : ContentBlock() public class ContentEmphasis() : ContentBlock() public class ContentStrong() : ContentBlock() public class ContentCode() : ContentBlock() +public class ContentBlockCode() : ContentBlock() public class ContentNodeLink(val node : DocumentationNode) : ContentBlock() public class ContentExternalLink(val href : String) : ContentBlock() public class ContentList() : ContentBlock() diff --git a/src/main.kt b/src/main.kt index e234a110..e2ea0ce7 100644 --- a/src/main.kt +++ b/src/main.kt @@ -59,7 +59,7 @@ public fun main(args: Array<String>) { println("Analysing sources and libraries... ") val startAnalyse = System.currentTimeMillis() - val documentation = environment.withContext { environment, module, context -> + val documentation = environment.withContext { environment, session -> val fragmentFiles = environment.getSourceFiles().filter { val sourceFile = File(it.getVirtualFile()!!.getPath()) samples.none { sample -> @@ -68,10 +68,10 @@ public fun main(args: Array<String>) { canonicalSource.startsWith(canonicalSample) } } - val fragments = fragmentFiles.map { context.getPackageFragment(it) }.filterNotNull().distinct() + val fragments = fragmentFiles.map { session.getPackageFragment(it.getPackageFqName()) }.filterNotNull().distinct() val documentationModule = DocumentationModule(arguments.moduleName) val options = DocumentationOptions() - val documentationBuilder = DocumentationBuilder(context, options) + val documentationBuilder = DocumentationBuilder(session, options) with(documentationBuilder) { val descriptors = hashMapOf<String, List<DeclarationDescriptor>>() diff --git a/test/src/TestAPI.kt b/test/src/TestAPI.kt index b885a15f..ccef8919 100644 --- a/test/src/TestAPI.kt +++ b/test/src/TestAPI.kt @@ -30,15 +30,15 @@ public fun verifyModel(vararg files: String, verifier: (DocumentationModule) -> val options = DocumentationOptions(includeNonPublic = true) - val documentation = environment.withContext { environment, module, context -> - val fragments = environment.getSourceFiles().map { context.getPackageFragment(it) }.filterNotNull().distinct() + val documentation = environment.withContext { environment, session -> + val fragments = environment.getSourceFiles().map { session.getPackageFragment(it.getPackageFqName()) }.filterNotNull().distinct() val descriptors = hashMapOf<String, List<DeclarationDescriptor>>() for ((name, parts) in fragments.groupBy { it.fqName }) { descriptors.put(name.asString(), parts.flatMap { it.getMemberScope().getAllDescriptors() }) } val documentationModule = DocumentationModule("test") - val documentationBuilder = DocumentationBuilder(context, options) + val documentationBuilder = DocumentationBuilder(session, options) with(documentationBuilder) { documentationModule.appendFragments(fragments) } |