This commit is contained in:
Dmitry Jemerov
2017-12-29 15:55:50 +01:00
parent 79ff36592d
commit 429bf5bb98
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2017 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.
* Copyright 2000-2017 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.
*/
package org.jetbrains.kotlin.idea.inspections
@@ -37,16 +26,17 @@ import java.awt.BorderLayout
import java.util.regex.PatternSyntaxException
import javax.swing.JPanel
abstract class NamingConventionInspection(private val entityName: String,
defaultNamePattern: String) : AbstractKotlinInspection() {
abstract class NamingConventionInspection(
private val entityName: String,
defaultNamePattern: String
) : AbstractKotlinInspection() {
protected var nameRegex: Regex? = defaultNamePattern.toRegex()
var namePattern: String = defaultNamePattern
set(value) {
field = value
nameRegex = try {
value.toRegex()
}
catch(e: PatternSyntaxException) {
} catch (e: PatternSyntaxException) {
null
}
}
@@ -55,9 +45,11 @@ abstract class NamingConventionInspection(private val entityName: String,
val name = element.name
val nameIdentifier = element.nameIdentifier
if (name != null && nameIdentifier != null && nameRegex?.matches(name) == false) {
holder.registerProblem(element.nameIdentifier!!,
"$entityName name <code>#ref</code> doesn't match regex '$namePattern' #loc",
RenameIdentifierFix())
holder.registerProblem(
element.nameIdentifier!!,
"$entityName name <code>#ref</code> doesn't match regex '$namePattern' #loc",
RenameIdentifierFix()
)
}
}
@@ -101,10 +93,11 @@ class FunctionNameInspection : NamingConventionInspection("Function", "[a-z][A-Z
}
}
abstract class PropertyNameInspectionBase protected constructor(private val kind: PropertyKind,
entityName: String,
defaultNamePattern: String)
: NamingConventionInspection(entityName, defaultNamePattern) {
abstract class PropertyNameInspectionBase protected constructor(
private val kind: PropertyKind,
entityName: String,
defaultNamePattern: String
) : NamingConventionInspection(entityName, defaultNamePattern) {
protected enum class PropertyKind { NORMAL, PRIVATE, OBJECT, CONST, LOCAL }
@@ -147,9 +140,11 @@ class PackageNameInspection : NamingConventionInspection("Package", "[a-z][A-Za-
override fun visitPackageDirective(directive: KtPackageDirective) {
val qualifiedName = directive.qualifiedName
if (qualifiedName.isNotEmpty() && nameRegex?.matches(qualifiedName) == false) {
holder.registerProblem(directive.packageNameExpression!!,
"Package name <code>#ref</code> doesn't match regex '$namePattern' #loc",
RenamePackageFix())
holder.registerProblem(
directive.packageNameExpression!!,
"Package name <code>#ref</code> doesn't match regex '$namePattern' #loc",
RenamePackageFix()
)
}
}
}