Move NameShortness to a separate file and refactor it to be an interface
This commit is contained in:
+1
-1
@@ -61,7 +61,7 @@ public abstract class AbstractAnnotationDescriptorResolveTest extends KotlinLite
|
|||||||
@Override
|
@Override
|
||||||
public Unit invoke(DescriptorRendererOptions options) {
|
public Unit invoke(DescriptorRendererOptions options) {
|
||||||
options.setVerbose(true);
|
options.setVerbose(true);
|
||||||
options.setNameShortness(NameShortness.SHORT);
|
options.setNameShortness(NameShortness.SHORT.INSTANCE);
|
||||||
options.setModifiers(DescriptorRendererModifier.ALL);
|
options.setModifiers(DescriptorRendererModifier.ALL);
|
||||||
return Unit.INSTANCE;
|
return Unit.INSTANCE;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ public class RecursiveDescriptorComparator {
|
|||||||
options.setExcludedAnnotationClasses(Collections.singleton(new FqName(ExpectedLoadErrorsUtil.ANNOTATION_CLASS_NAME)));
|
options.setExcludedAnnotationClasses(Collections.singleton(new FqName(ExpectedLoadErrorsUtil.ANNOTATION_CLASS_NAME)));
|
||||||
options.setOverrideRenderingPolicy(OverrideRenderingPolicy.RENDER_OPEN_OVERRIDE);
|
options.setOverrideRenderingPolicy(OverrideRenderingPolicy.RENDER_OPEN_OVERRIDE);
|
||||||
options.setIncludePropertyConstant(true);
|
options.setIncludePropertyConstant(true);
|
||||||
options.setNameShortness(NameShortness.FULLY_QUALIFIED);
|
options.setNameShortness(NameShortness.FULLY_QUALIFIED.INSTANCE);
|
||||||
options.setVerbose(true);
|
options.setVerbose(true);
|
||||||
options.setModifiers(DescriptorRendererModifier.ALL);
|
options.setModifiers(DescriptorRendererModifier.ALL);
|
||||||
return Unit.INSTANCE;
|
return Unit.INSTANCE;
|
||||||
|
|||||||
@@ -226,12 +226,6 @@ enum class RenderingFormat {
|
|||||||
HTML
|
HTML
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class NameShortness {
|
|
||||||
SHORT,
|
|
||||||
FULLY_QUALIFIED,
|
|
||||||
SOURCE_CODE_QUALIFIED // for local declarations qualified up to function scope
|
|
||||||
}
|
|
||||||
|
|
||||||
enum class OverrideRenderingPolicy {
|
enum class OverrideRenderingPolicy {
|
||||||
RENDER_OVERRIDE,
|
RENDER_OVERRIDE,
|
||||||
RENDER_OPEN,
|
RENDER_OPEN,
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.types.*
|
|||||||
import org.jetbrains.kotlin.types.ErrorUtils.UninferredParameterTypeConstructor
|
import org.jetbrains.kotlin.types.ErrorUtils.UninferredParameterTypeConstructor
|
||||||
import org.jetbrains.kotlin.types.TypeUtils.CANT_INFER_FUNCTION_PARAM_TYPE
|
import org.jetbrains.kotlin.types.TypeUtils.CANT_INFER_FUNCTION_PARAM_TYPE
|
||||||
import org.jetbrains.kotlin.types.error.MissingDependencyErrorClass
|
import org.jetbrains.kotlin.types.error.MissingDependencyErrorClass
|
||||||
|
import org.jetbrains.kotlin.types.typeUtil.builtIns
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
internal class DescriptorRendererImpl(
|
internal class DescriptorRendererImpl(
|
||||||
@@ -122,27 +123,7 @@ internal class DescriptorRendererImpl(
|
|||||||
if (ErrorUtils.isError(klass)) {
|
if (ErrorUtils.isError(klass)) {
|
||||||
return klass.typeConstructor.toString()
|
return klass.typeConstructor.toString()
|
||||||
}
|
}
|
||||||
when (nameShortness) {
|
return nameShortness.renderClassifier(klass, this)
|
||||||
NameShortness.SHORT -> {
|
|
||||||
val qualifiedNameElements = ArrayList<Name>()
|
|
||||||
|
|
||||||
// for nested classes qualified name should be used
|
|
||||||
var current: DeclarationDescriptor? = klass
|
|
||||||
do {
|
|
||||||
qualifiedNameElements.add(current!!.name)
|
|
||||||
current = current.containingDeclaration
|
|
||||||
}
|
|
||||||
while (current is ClassDescriptor)
|
|
||||||
|
|
||||||
return renderFqName(qualifiedNameElements.asReversed())
|
|
||||||
}
|
|
||||||
|
|
||||||
NameShortness.FULLY_QUALIFIED -> return renderFqName(DescriptorUtils.getFqName(klass))
|
|
||||||
|
|
||||||
NameShortness.SOURCE_CODE_QUALIFIED -> return qualifiedNameForSourceCode(klass)
|
|
||||||
|
|
||||||
else -> throw IllegalArgumentException()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TYPES RENDERING */
|
/* TYPES RENDERING */
|
||||||
@@ -221,7 +202,8 @@ internal class DescriptorRendererImpl(
|
|||||||
return lowerRendered + "!"
|
return lowerRendered + "!"
|
||||||
}
|
}
|
||||||
|
|
||||||
val kotlinCollectionsPrefix = if (nameShortness != NameShortness.SHORT) "kotlin.collections." else ""
|
|
||||||
|
val kotlinCollectionsPrefix = nameShortness.renderClassifier(type.builtIns.collection, this).substringBefore("Collection")
|
||||||
val mutablePrefix = "Mutable"
|
val mutablePrefix = "Mutable"
|
||||||
// java.util.List<Foo> -> (Mutable)List<Foo!>!
|
// java.util.List<Foo> -> (Mutable)List<Foo!>!
|
||||||
val simpleCollection = replacePrefixes(lowerRendered, kotlinCollectionsPrefix + mutablePrefix, upperRendered, kotlinCollectionsPrefix, kotlinCollectionsPrefix + "(" + mutablePrefix + ")")
|
val simpleCollection = replacePrefixes(lowerRendered, kotlinCollectionsPrefix + mutablePrefix, upperRendered, kotlinCollectionsPrefix, kotlinCollectionsPrefix + "(" + mutablePrefix + ")")
|
||||||
@@ -230,7 +212,7 @@ internal class DescriptorRendererImpl(
|
|||||||
val mutableEntry = replacePrefixes(lowerRendered, kotlinCollectionsPrefix + "MutableMap.MutableEntry", upperRendered, kotlinCollectionsPrefix + "Map.Entry", kotlinCollectionsPrefix + "(Mutable)Map.(Mutable)Entry")
|
val mutableEntry = replacePrefixes(lowerRendered, kotlinCollectionsPrefix + "MutableMap.MutableEntry", upperRendered, kotlinCollectionsPrefix + "Map.Entry", kotlinCollectionsPrefix + "(Mutable)Map.(Mutable)Entry")
|
||||||
if (mutableEntry != null) return mutableEntry
|
if (mutableEntry != null) return mutableEntry
|
||||||
|
|
||||||
val kotlinPrefix = if (nameShortness != NameShortness.SHORT) "kotlin." else ""
|
val kotlinPrefix = nameShortness.renderClassifier(type.builtIns.array, this).substringBefore("Array")
|
||||||
// Foo[] -> Array<(out) Foo!>!
|
// Foo[] -> Array<(out) Foo!>!
|
||||||
val array = replacePrefixes(lowerRendered, kotlinPrefix + escape("Array<"), upperRendered, kotlinPrefix + escape("Array<out "), kotlinPrefix + escape("Array<(out) "))
|
val array = replacePrefixes(lowerRendered, kotlinPrefix + escape("Array<"), upperRendered, kotlinPrefix + escape("Array<out "), kotlinPrefix + escape("Array<(out) "))
|
||||||
if (array != null) return array
|
if (array != null) return array
|
||||||
@@ -297,8 +279,7 @@ internal class DescriptorRendererImpl(
|
|||||||
override fun renderTypeConstructor(typeConstructor: TypeConstructor): String {
|
override fun renderTypeConstructor(typeConstructor: TypeConstructor): String {
|
||||||
val cd = typeConstructor.declarationDescriptor
|
val cd = typeConstructor.declarationDescriptor
|
||||||
return when (cd) {
|
return when (cd) {
|
||||||
is TypeParameterDescriptor -> renderName(cd.getName())
|
is TypeParameterDescriptor, is ClassDescriptor -> renderClassifierName(cd)
|
||||||
is ClassDescriptor -> renderClassifierName(cd)
|
|
||||||
null -> typeConstructor.toString()
|
null -> typeConstructor.toString()
|
||||||
else -> error("Unexpected classifier: " + cd.javaClass)
|
else -> error("Unexpected classifier: " + cd.javaClass)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ internal class DescriptorRendererOptionsImpl : DescriptorRendererOptions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override var nameShortness by property(NameShortness.SOURCE_CODE_QUALIFIED)
|
override var nameShortness: NameShortness by property(NameShortness.SOURCE_CODE_QUALIFIED)
|
||||||
override var withDefinedIn by property(true)
|
override var withDefinedIn by property(true)
|
||||||
override var modifiers: Set<DescriptorRendererModifier> by property(DescriptorRendererModifier.DEFAULTS)
|
override var modifiers: Set<DescriptorRendererModifier> by property(DescriptorRendererModifier.DEFAULTS)
|
||||||
override var startFromName by property(false)
|
override var startFromName by property(false)
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2016 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.renderer
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
|
import org.jetbrains.kotlin.name.Name
|
||||||
|
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
interface NameShortness {
|
||||||
|
object SHORT : NameShortness {
|
||||||
|
override fun renderClassifier(classifier: ClassifierDescriptor, renderer: DescriptorRenderer): String {
|
||||||
|
if (classifier is TypeParameterDescriptor) return renderer.renderName(classifier.name)
|
||||||
|
|
||||||
|
val qualifiedNameElements = ArrayList<Name>()
|
||||||
|
|
||||||
|
// for nested classes qualified name should be used
|
||||||
|
var current: DeclarationDescriptor? = classifier
|
||||||
|
do {
|
||||||
|
qualifiedNameElements.add(current!!.name)
|
||||||
|
current = current.containingDeclaration
|
||||||
|
}
|
||||||
|
while (current is ClassDescriptor)
|
||||||
|
|
||||||
|
return renderFqName(qualifiedNameElements.asReversed())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
object FULLY_QUALIFIED : NameShortness {
|
||||||
|
override fun renderClassifier(classifier: ClassifierDescriptor, renderer: DescriptorRenderer): String {
|
||||||
|
if (classifier is TypeParameterDescriptor) return renderer.renderName(classifier.name)
|
||||||
|
|
||||||
|
return renderer.renderFqName(DescriptorUtils.getFqName(classifier))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// for local declarations qualified up to function scope
|
||||||
|
object SOURCE_CODE_QUALIFIED : NameShortness {
|
||||||
|
override fun renderClassifier(classifier: ClassifierDescriptor, renderer: DescriptorRenderer): String {
|
||||||
|
return qualifiedNameForSourceCode(classifier)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun qualifiedNameForSourceCode(descriptor: ClassifierDescriptor): String {
|
||||||
|
val nameString = descriptor.name.render()
|
||||||
|
if (descriptor is TypeParameterDescriptor) {
|
||||||
|
return nameString
|
||||||
|
}
|
||||||
|
val qualifier = qualifierName(descriptor.containingDeclaration)
|
||||||
|
return if (qualifier != null && qualifier != "") qualifier + "." + nameString else nameString
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun qualifierName(descriptor: DeclarationDescriptor): String? = when (descriptor) {
|
||||||
|
is ClassDescriptor -> qualifiedNameForSourceCode(descriptor)
|
||||||
|
is PackageFragmentDescriptor -> descriptor.fqName.toUnsafe().render()
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun renderClassifier(classifier: ClassifierDescriptor, renderer: DescriptorRenderer): String
|
||||||
|
}
|
||||||
@@ -16,27 +16,10 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.renderer
|
package org.jetbrains.kotlin.renderer
|
||||||
|
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
|
||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.name.FqNameUnsafe
|
import org.jetbrains.kotlin.name.FqNameUnsafe
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
|
|
||||||
fun qualifiedNameForSourceCode(descriptor: ClassifierDescriptor): String {
|
|
||||||
val nameString = descriptor.name.render()
|
|
||||||
if (descriptor is TypeParameterDescriptor) {
|
|
||||||
return nameString
|
|
||||||
}
|
|
||||||
val qualifier = qualifierName(descriptor.containingDeclaration)
|
|
||||||
return if (qualifier != null && qualifier != "") qualifier + "." + nameString else nameString
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun qualifierName(descriptor: DeclarationDescriptor): String? = when (descriptor) {
|
|
||||||
is ClassDescriptor -> qualifiedNameForSourceCode(descriptor)
|
|
||||||
is PackageFragmentDescriptor -> descriptor.fqName.toUnsafe().render()
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fun Name.render(): String {
|
fun Name.render(): String {
|
||||||
return if (this.shouldBeEscaped()) '`' + asString() + '`' else asString()
|
return if (this.shouldBeEscaped()) '`' + asString() + '`' else asString()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user