From 9ec235c011e264abd9002750a9113a55aa1a713b Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Thu, 4 Jun 2015 14:20:20 +0300 Subject: [PATCH] Introduce KotlinNamesValidator It's needed to prohibit invalid kotlin identifiers in refactorings #EA-69048 Fixed #EA-69063 Fixed --- .../core/refactoring/KotlinNamesValidator.kt | 29 +++++++++ idea/src/META-INF/plugin.xml | 1 + .../refactoring/KotlinNamesValidatorTest.kt | 63 +++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 idea/idea-core/src/org/jetbrains/kotlin/idea/core/refactoring/KotlinNamesValidator.kt create mode 100644 idea/tests/org/jetbrains/kotlin/idea/refactoring/KotlinNamesValidatorTest.kt diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/refactoring/KotlinNamesValidator.kt b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/refactoring/KotlinNamesValidator.kt new file mode 100644 index 00000000000..f09a39e743a --- /dev/null +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/refactoring/KotlinNamesValidator.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2010-2015 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.idea.core.refactoring + +import com.intellij.lang.refactoring.NamesValidator +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.lexer.JetKeywordToken +import org.jetbrains.kotlin.lexer.JetTokens + +public class KotlinNamesValidator : NamesValidator { + val KEYWORD_SET = JetTokens.KEYWORDS.getTypes().filterIsInstance().map { it.getValue() }.toHashSet() + + override fun isKeyword(name: String, project: Project?): Boolean = name in KEYWORD_SET + override fun isIdentifier(name: String, project: Project?): Boolean = JetNameSuggester.isIdentifier(name) +} diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index e871e89007a..9fbfbed53a2 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -235,6 +235,7 @@ + diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/KotlinNamesValidatorTest.kt b/idea/tests/org/jetbrains/kotlin/idea/refactoring/KotlinNamesValidatorTest.kt new file mode 100644 index 00000000000..e1a65a9707c --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/KotlinNamesValidatorTest.kt @@ -0,0 +1,63 @@ +/* + * Copyright 2010-2015 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.idea.refactoring + +import com.intellij.lang.refactoring.NamesValidator +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase +import org.jetbrains.kotlin.idea.core.refactoring.KotlinNamesValidator +import org.junit.Assert + +public class KotlinNamesValidatorTest : LightCodeInsightFixtureTestCase() { + val validator: NamesValidator = KotlinNamesValidator() + + private fun isKeyword(string: String) = validator.isKeyword(string, null) + private fun isIdentifier(string: String) = validator.isIdentifier(string, null) + + override fun setUp() { + super.setUp() + myFixture.configureByFiles() + } + + public fun testKeywords() { + Assert.assertTrue(isKeyword("val")); + Assert.assertTrue(isKeyword("class")); + Assert.assertTrue(isKeyword("fun")); + + Assert.assertFalse(isKeyword("constructor")); + Assert.assertFalse(isKeyword("123")); + Assert.assertFalse(isKeyword("a.c")); + Assert.assertFalse(isKeyword("-")); + } + + public fun testIdentifiers() { + Assert.assertTrue(isIdentifier("abc")); + Assert.assertTrue(isIdentifier("q_q")); + Assert.assertTrue(isIdentifier("constructor")); + Assert.assertTrue(isIdentifier("`val`")); + + Assert.assertFalse(isIdentifier("val")); + Assert.assertFalse(isIdentifier("class")); + Assert.assertFalse(isIdentifier("fun")); + + Assert.assertFalse(isIdentifier("123")); + Assert.assertFalse(isIdentifier("a.c")); + Assert.assertFalse(isIdentifier("-")); + Assert.assertFalse(isIdentifier("``")); + Assert.assertFalse(isIdentifier("")); + Assert.assertFalse(isIdentifier(" '")); + } +}