From c6b5e8c40fbdd75b181e71683ef5e180f928ba60 Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Mon, 4 Jun 2018 17:40:55 +0300 Subject: [PATCH] Minor: Extract code for checking Dalvik identifiers to frontend --- .../jvm/checkers/dalvikIdentifierUtils.kt | 22 +++++++++++++++++++ .../inspection/IllegalIdentifierInspection.kt | 16 ++------------ 2 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/dalvikIdentifierUtils.kt diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/dalvikIdentifierUtils.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/dalvikIdentifierUtils.kt new file mode 100644 index 00000000000..9a31967278d --- /dev/null +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/dalvikIdentifierUtils.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2010-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. + */ + +@file:JvmName("DalvikIdentifierUtils") +package org.jetbrains.kotlin.resolve.jvm.checkers + +fun isValidDalvikIdentifier(identifier: String) = identifier.all { isValidDalvikCharacter(it) } + +// https://source.android.com/devices/tech/dalvik/dex-format.html#string-syntax +private fun isValidDalvikCharacter(c: Char) = when (c) { + in 'A'..'Z' -> true + in 'a'..'z' -> true + in '0'..'9' -> true + '$', '-', '_' -> true + in '\u00a1' .. '\u1fff' -> true + in '\u2010' .. '\u2027' -> true + in '\u2030' .. '\ud7ff' -> true + in '\ue000' .. '\uffef' -> true + else -> false +} \ No newline at end of file diff --git a/idea/idea-android/src/org/jetbrains/kotlin/android/inspection/IllegalIdentifierInspection.kt b/idea/idea-android/src/org/jetbrains/kotlin/android/inspection/IllegalIdentifierInspection.kt index cdc585590ab..a976937c986 100644 --- a/idea/idea-android/src/org/jetbrains/kotlin/android/inspection/IllegalIdentifierInspection.kt +++ b/idea/idea-android/src/org/jetbrains/kotlin/android/inspection/IllegalIdentifierInspection.kt @@ -39,6 +39,7 @@ import org.jetbrains.kotlin.idea.quickfix.RenameIdentifierFix import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.KtPsiUtil import org.jetbrains.kotlin.psi.KtVisitorVoid +import org.jetbrains.kotlin.resolve.jvm.checkers.isValidDalvikIdentifier import java.io.File class IllegalIdentifierInspection : AbstractKotlinInspection() { @@ -59,7 +60,7 @@ class IllegalIdentifierInspection : AbstractKotlinInspection() { // This is already an error if (unquotedName.isEmpty()) return - if (!unquotedName.all { isValidDalvikCharacter(it) } && checkAndroidFacet(element)) { + if (!isValidDalvikIdentifier(unquotedName) && checkAndroidFacet(element)) { if (element.isInUnitTests()) { return } @@ -94,19 +95,6 @@ class IllegalIdentifierInspection : AbstractKotlinInspection() { private fun checkAndroidFacet(element: PsiElement): Boolean { return element.getAndroidFacetForFile() != null || ApplicationManager.getApplication().isUnitTestMode } - - // https://source.android.com/devices/tech/dalvik/dex-format.html#string-syntax - private fun isValidDalvikCharacter(c: Char) = when (c) { - in 'A'..'Z' -> true - in 'a'..'z' -> true - in '0'..'9' -> true - '$', '-', '_' -> true - in '\u00a1' .. '\u1fff' -> true - in '\u2010' .. '\u2027' -> true - in '\u2030' .. '\ud7ff' -> true - in '\ue000' .. '\uffef' -> true - else -> false - } } }