diff options
-rw-r--r-- | src/Analysis/CompilerAPI.kt | 89 | ||||
-rw-r--r-- | src/Kotlin/ContentBuilder.kt | 12 | ||||
-rw-r--r-- | src/Kotlin/DocumentationBuilder.kt | 6 | ||||
-rw-r--r-- | test/data/comments/directive.kt | 8 |
4 files changed, 12 insertions, 103 deletions
diff --git a/src/Analysis/CompilerAPI.kt b/src/Analysis/CompilerAPI.kt index 20e91e1d..d915e23a 100644 --- a/src/Analysis/CompilerAPI.kt +++ b/src/Analysis/CompilerAPI.kt @@ -52,92 +52,3 @@ fun DeclarationDescriptor.isUserCode() = is CallableMemberDescriptor -> getKind() == CallableMemberDescriptor.Kind.DECLARATION else -> true } - -public fun getPackageInnerScope(descriptor: PackageFragmentDescriptor): JetScope { - val module = descriptor.getContainingDeclaration() - val packageView = module.getPackage(descriptor.fqName) - val packageScope = packageView!!.getMemberScope() - return packageScope -} - -public fun getClassInnerScope(outerScope: JetScope, descriptor: ClassDescriptor): JetScope { - val redeclarationHandler = RedeclarationHandler.DO_NOTHING - - val headerScope = WritableScopeImpl(outerScope, descriptor, redeclarationHandler, "Class ${descriptor.getName()} header scope") - for (typeParameter in descriptor.getTypeConstructor().getParameters()) { - headerScope.addTypeParameterDescriptor(typeParameter) - } - for (constructor in descriptor.getConstructors()) { - headerScope.addFunctionDescriptor(constructor) - } - headerScope.addLabeledDeclaration(descriptor) - headerScope.changeLockLevel(WritableScope.LockLevel.READING) - - val classScope = ChainedScope(descriptor, "Class ${descriptor.getName()} scope", descriptor.getDefaultType().getMemberScope(), headerScope) - return classScope -} - -public fun getFunctionInnerScope(outerScope: JetScope, descriptor: FunctionDescriptor): JetScope { - val redeclarationHandler = RedeclarationHandler.DO_NOTHING - - val functionScope = WritableScopeImpl(outerScope, descriptor, redeclarationHandler, "Function ${descriptor.getName()} scope") - val receiver = descriptor.getExtensionReceiverParameter() - if (receiver != null) { - functionScope.setImplicitReceiver(receiver) - } - for (typeParameter in descriptor.getTypeParameters()) { - functionScope.addTypeParameterDescriptor(typeParameter) - } - for (valueParameterDescriptor in descriptor.getValueParameters()) { - functionScope.addVariableDescriptor(valueParameterDescriptor) - } - functionScope.addLabeledDeclaration(descriptor) - functionScope.changeLockLevel(WritableScope.LockLevel.READING) - return functionScope -} - -public fun getPropertyInnerScope(outerScope: JetScope, descriptor: PropertyDescriptor): JetScope { - val redeclarationHandler = RedeclarationHandler.DO_NOTHING - - val propertyScope = WritableScopeImpl(outerScope, descriptor, redeclarationHandler, "Property ${descriptor.getName()} scope") - val receiver = descriptor.getExtensionReceiverParameter() - if (receiver != null) { - propertyScope.setImplicitReceiver(receiver) - } - for (typeParameter in descriptor.getTypeParameters()) { - propertyScope.addTypeParameterDescriptor(typeParameter) - } - for (valueParameterDescriptor in descriptor.getValueParameters()) { - propertyScope.addVariableDescriptor(valueParameterDescriptor) - } - for (accessor in descriptor.getAccessors()) { - propertyScope.addFunctionDescriptor(accessor) - } - propertyScope.addLabeledDeclaration(descriptor) - propertyScope.changeLockLevel(WritableScope.LockLevel.READING) - return propertyScope -} - -fun getResolutionScope(descriptor: DeclarationDescriptor): JetScope { - when (descriptor) { - is PackageFragmentDescriptor -> - return getPackageInnerScope(descriptor) - - is PackageViewDescriptor -> - return descriptor.getMemberScope() - - is ClassDescriptor -> - return getClassInnerScope(getResolutionScope(descriptor.getContainingDeclaration()), descriptor) - - is FunctionDescriptor -> - return getFunctionInnerScope(getResolutionScope(descriptor.getContainingDeclaration()), descriptor) - - is PropertyDescriptor -> - return getPropertyInnerScope(getResolutionScope(descriptor.getContainingDeclaration()), descriptor) - } - - if (descriptor is DeclarationDescriptorNonRoot) - return getResolutionScope(descriptor.getContainingDeclaration()) - - throw IllegalArgumentException("Cannot find resolution scope for root $descriptor") -} diff --git a/src/Kotlin/ContentBuilder.kt b/src/Kotlin/ContentBuilder.kt index 35beec54..b5647ff0 100644 --- a/src/Kotlin/ContentBuilder.kt +++ b/src/Kotlin/ContentBuilder.kt @@ -5,6 +5,7 @@ import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.resolve.* import org.jetbrains.kotlin.resolve.scopes.* import org.jetbrains.kotlin.name.* +import org.jetbrains.kotlin.idea.kdoc.getResolutionScope import org.intellij.markdown.* import org.jetbrains.kotlin.psi.JetDeclarationWithBody import org.jetbrains.kotlin.psi.JetBlockExpression @@ -119,7 +120,7 @@ public fun DocumentationBuilder.buildInlineContentTo(tree: MarkdownNode, target: } fun DocumentationBuilder.functionBody(descriptor: DeclarationDescriptor, functionName: String): ContentNode { - val scope = getResolutionScope(descriptor) + val scope = getResolutionScope(session, descriptor) val rootPackage = session.getModuleDescriptor().getPackage(FqName.ROOT)!! val rootScope = rootPackage.getMemberScope() val symbol = resolveInScope(functionName, scope) ?: resolveInScope(functionName, rootScope) @@ -149,22 +150,19 @@ fun DocumentationBuilder.functionBody(descriptor: DeclarationDescriptor, functio 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 = currentScope.getLocalVariable(symbolName) ?: - currentScope.getProperties(symbolName).firstOrNull() ?: - currentScope.getFunctions(symbolName).firstOrNull() ?: - currentScope.getClassifier(symbolName) ?: - currentScope.getPackage(symbolName) + val partSymbol = currentScope.getAllDescriptors().filter { it.getName() == symbolName }.firstOrNull() if (partSymbol == null) { symbol = null break } - currentScope = getResolutionScope(partSymbol) + currentScope = getResolutionScope(session, partSymbol) symbol = partSymbol } diff --git a/src/Kotlin/DocumentationBuilder.kt b/src/Kotlin/DocumentationBuilder.kt index 041b7617..1b37dc51 100644 --- a/src/Kotlin/DocumentationBuilder.kt +++ b/src/Kotlin/DocumentationBuilder.kt @@ -51,7 +51,7 @@ class DocumentationBuilder(val session: ResolveSession, val options: Documentati if (kdoc is KDocSection) { val tags = kdoc.getTags() tags.forEach { - if (it.getName() == "code") { + if (it.getName() == "sample") { content.append(functionBody(descriptor, it.getContent())) } else { val section = content.addSection(displayName(it.getName()), it.getSubjectName()) @@ -529,7 +529,7 @@ class DocumentationBuilder(val session: ResolveSession, val options: Documentati } } - fun getResolutionScope(node: DocumentationNode): DeclarationDescriptor { + fun getDescriptorForNode(node: DocumentationNode): DeclarationDescriptor { val descriptor = nodeToDescriptor[node] ?: throw IllegalArgumentException("Node is not known to this context") return descriptor } @@ -543,7 +543,7 @@ class DocumentationBuilder(val session: ResolveSession, val options: Documentati private fun resolveContentLink(node: DocumentationNode, content: ContentNode): ContentNode { if (content is ContentExternalLink) { val referenceText = content.href - val symbols = resolveKDocLink(session, getResolutionScope(node), null, referenceText.split('.').toList()) + val symbols = resolveKDocLink(session, getDescriptorForNode(node), null, referenceText.split('.').toList()) // don't include unresolved links in generated doc // assume that if an href doesn't contain '/', it's not an attempt to reference an external file if (symbols.isNotEmpty() || "/" !in referenceText) { diff --git a/test/data/comments/directive.kt b/test/data/comments/directive.kt index 471ca9bb..b27f5a48 100644 --- a/test/data/comments/directive.kt +++ b/test/data/comments/directive.kt @@ -1,10 +1,10 @@ /** * Summary * - * @code example1 - * @code example2 - * @code X.example3 - * @code X.Y.example4 + * @sample example1 + * @sample example2 + * @sample X.example3 + * @sample X.Y.example4 */ val property = "test" |