Introduce KotlinNamesValidator

It's needed to prohibit invalid kotlin identifiers in refactorings

 #EA-69048 Fixed
 #EA-69063 Fixed
This commit is contained in:
Denis Zharkov
2015-06-04 14:20:20 +03:00
parent 6e97f85863
commit 9ec235c011
3 changed files with 93 additions and 0 deletions
@@ -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<JetKeywordToken>().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)
}
+1
View File
@@ -235,6 +235,7 @@
<gotoSymbolContributor implementation="org.jetbrains.kotlin.idea.caches.JetGotoSymbolContributor"/>
<gotoClassContributor implementation="org.jetbrains.kotlin.idea.caches.JetGotoClassContributor"/>
<lang.importOptimizer language="jet" implementationClass="org.jetbrains.kotlin.idea.imports.KotlinImportOptimizer"/>
<lang.namesValidator language="jet" implementationClass="org.jetbrains.kotlin.idea.core.refactoring.KotlinNamesValidator"/>
<fileTypeFactory implementation="org.jetbrains.kotlin.idea.JetFileFactory"/>
<fileTypeFactory implementation="org.jetbrains.kotlin.idea.KotlinJavaScriptMetaFileTypeFactory"/>
@@ -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(" '"));
}
}