Some string and minor optimizations

This commit is contained in:
Ivan Kochurkin
2021-06-06 14:57:30 +03:00
parent bc3c05a3bd
commit a0aaf42fb1
6 changed files with 17 additions and 10 deletions
@@ -1967,7 +1967,7 @@ class DeclarationsConverter(
private val extensionFunctionAnnotation = buildAnnotationCall { private val extensionFunctionAnnotation = buildAnnotationCall {
annotationTypeRef = buildResolvedTypeRef { annotationTypeRef = buildResolvedTypeRef {
type = ConeClassLikeTypeImpl( type = ConeClassLikeTypeImpl(
ConeClassLikeLookupTagImpl(ClassId.fromString(EXTENSION_FUNCTION_ANNOTATION)), ConeClassLikeLookupTagImpl(EXTENSION_FUNCTION_ANNOTATION),
emptyArray(), emptyArray(),
false false
) )
@@ -38,7 +38,6 @@ import org.jetbrains.kotlin.fir.types.impl.FirTypeArgumentListImpl
import org.jetbrains.kotlin.fir.types.impl.FirTypePlaceholderProjection import org.jetbrains.kotlin.fir.types.impl.FirTypePlaceholderProjection
import org.jetbrains.kotlin.lexer.KtTokens.* import org.jetbrains.kotlin.lexer.KtTokens.*
import org.jetbrains.kotlin.name.CallableId import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.LocalCallableIdConstructor
import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
@@ -2225,7 +2224,7 @@ open class RawFirBuilder(
private val extensionFunctionAnnotation = buildAnnotationCall { private val extensionFunctionAnnotation = buildAnnotationCall {
annotationTypeRef = buildResolvedTypeRef { annotationTypeRef = buildResolvedTypeRef {
type = ConeClassLikeTypeImpl( type = ConeClassLikeTypeImpl(
ConeClassLikeLookupTagImpl(ClassId.fromString(EXTENSION_FUNCTION_ANNOTATION)), ConeClassLikeLookupTagImpl(EXTENSION_FUNCTION_ANNOTATION),
emptyArray(), emptyArray(),
isNullable = false isNullable = false
) )
@@ -70,12 +70,12 @@ fun FirSourceElement.getWholeQualifierSourceIfPossible(stepsToWholeQualifier: In
return when (this) { return when (this) {
is FirRealPsiSourceElement<*> -> { is FirRealPsiSourceElement<*> -> {
val qualifiersChain = generateSequence(psi) { it.parent } val qualifiersChain = generateSequence(psi) { it.parent }
val wholeQualifier = qualifiersChain.drop(stepsToWholeQualifier).first() val wholeQualifier = qualifiersChain.elementAt(stepsToWholeQualifier)
wholeQualifier.toFirPsiSourceElement() as FirRealPsiSourceElement wholeQualifier.toFirPsiSourceElement() as FirRealPsiSourceElement
} }
is FirLightSourceElement -> { is FirLightSourceElement -> {
val qualifiersChain = generateSequence(lighterASTNode) { treeStructure.getParent(it) } val qualifiersChain = generateSequence(lighterASTNode) { treeStructure.getParent(it) }
val wholeQualifier = qualifiersChain.drop(stepsToWholeQualifier).first() val wholeQualifier = qualifiersChain.elementAt(stepsToWholeQualifier)
wholeQualifier.toFirLightSourceElement( wholeQualifier.toFirLightSourceElement(
treeStructure, treeStructure,
startOffset = this.startOffset, startOffset = this.startOffset,
@@ -72,13 +72,13 @@ val FirFunctionTypeRef.parametersCount: Int
else else
valueParameters.size valueParameters.size
const val EXTENSION_FUNCTION_ANNOTATION = "kotlin/ExtensionFunctionType" val EXTENSION_FUNCTION_ANNOTATION = ClassId.fromString("kotlin/ExtensionFunctionType")
val FirAnnotationCall.isExtensionFunctionAnnotationCall: Boolean val FirAnnotationCall.isExtensionFunctionAnnotationCall: Boolean
get() = (this as? FirAnnotationCall)?.let { annotationCall -> get() = (this as? FirAnnotationCall)?.let { annotationCall ->
(annotationCall.annotationTypeRef as? FirResolvedTypeRef)?.let { typeRef -> (annotationCall.annotationTypeRef as? FirResolvedTypeRef)?.let { typeRef ->
(typeRef.type as? ConeClassLikeType)?.let { (typeRef.type as? ConeClassLikeType)?.let {
it.lookupTag.classId.asString() == EXTENSION_FUNCTION_ANNOTATION it.lookupTag.classId == EXTENSION_FUNCTION_ANNOTATION
} }
} }
} == true } == true
@@ -105,8 +105,16 @@ public final class ClassId {
@NotNull @NotNull
public static ClassId fromString(@NotNull String string, boolean isLocal) { public static ClassId fromString(@NotNull String string, boolean isLocal) {
String packageName = StringsKt.substringBeforeLast(string, '/', "").replace('/', '.'); int lastSlashIndex = string.lastIndexOf("/");
String className = StringsKt.substringAfterLast(string, '/', string); String packageName;
String className;
if (lastSlashIndex == -1) {
packageName = "";
className = string;
} else {
packageName = string.substring(0, lastSlashIndex).replace('/', '.');
className = string.substring(lastSlashIndex + 1);
}
return new ClassId(new FqName(packageName), new FqName(className), isLocal); return new ClassId(new FqName(packageName), new FqName(className), isLocal);
} }
@@ -42,7 +42,7 @@ object NameUtils {
// NB `uppercase` uses Locale.ROOT and is locale-independent. // NB `uppercase` uses Locale.ROOT and is locale-independent.
// See Javadoc on java.lang.String.toUpperCase() for more details. // See Javadoc on java.lang.String.toUpperCase() for more details.
if (Character.isJavaIdentifierStart(str[0])) if (Character.isJavaIdentifierStart(str[0]))
str.substring(0, 1).uppercase() + str.substring(1) str[0].uppercase() + str.substring(1)
else else
"_$str" "_$str"