Skip to content

Commit d7a7ca5

Browse files
authored
Merge pull request #232 from domaframework/fix/custom-error-levels-in-dao-method-inspection
Reflect Custom Error Levels in DAO Method Inspection
2 parents 342d3f2 + 6262604 commit d7a7ca5

File tree

6 files changed

+116
-27
lines changed

6 files changed

+116
-27
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.domaframework.doma.intellij.common.sql.validator.result
17+
18+
import com.intellij.codeInspection.ProblemsHolder
19+
import com.intellij.openapi.util.TextRange
20+
import com.intellij.psi.PsiElement
21+
import org.domaframework.doma.intellij.bundle.MessageBundle
22+
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
23+
import org.domaframework.doma.intellij.common.psi.PsiParentClass
24+
import org.domaframework.doma.intellij.inspection.dao.quickfix.GenerateSQLFileQuickFixFactory
25+
26+
class ValidationSqlFileExistResult(
27+
private val psiDaoMethod: PsiDaoMethod,
28+
override val identify: PsiElement?,
29+
override val shortName: String = "",
30+
) : ValidationResult(identify, null, shortName) {
31+
override fun setHighlight(
32+
highlightRange: TextRange,
33+
identify: PsiElement,
34+
holder: ProblemsHolder,
35+
parent: PsiParentClass?,
36+
) {
37+
val project = psiDaoMethod.psiMethod.project
38+
holder.registerProblem(
39+
identify,
40+
MessageBundle.message("inspection.invalid.dao.notExistSql"),
41+
problemHighlightType(project, shortName),
42+
GenerateSQLFileQuickFixFactory.createSql(psiDaoMethod),
43+
)
44+
}
45+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.domaframework.doma.intellij.common.sql.validator.result
17+
18+
import com.intellij.codeInspection.ProblemsHolder
19+
import com.intellij.openapi.util.TextRange
20+
import com.intellij.psi.PsiElement
21+
import com.intellij.psi.PsiParameter
22+
import com.intellij.psi.impl.source.PsiParameterImpl
23+
import org.domaframework.doma.intellij.bundle.MessageBundle
24+
import org.domaframework.doma.intellij.common.psi.PsiParentClass
25+
import org.domaframework.doma.intellij.inspection.dao.visitor.DaoMethodVariableInspectionVisitor.DaoMethodVariableVisitorResult
26+
27+
class ValidationUsedDaoMethodArgsResult(
28+
private val daoMethodVariableResult: DaoMethodVariableVisitorResult,
29+
override val identify: PsiElement?,
30+
override val shortName: String = "",
31+
) : ValidationResult(identify, null, shortName) {
32+
override fun setHighlight(
33+
highlightRange: TextRange,
34+
identify: PsiElement,
35+
holder: ProblemsHolder,
36+
parent: PsiParentClass?,
37+
) {
38+
val param = identify as? PsiParameter ?: return
39+
val project = identify.project
40+
val message =
41+
if (daoMethodVariableResult.deplicateForItemElements.contains(identify)) {
42+
MessageBundle.message("inspection.invalid.dao.duplicate")
43+
} else {
44+
MessageBundle.message(
45+
"inspection.invalid.dao.paramUse",
46+
param.name,
47+
)
48+
}
49+
holder.registerProblem(
50+
(param.originalElement as PsiParameterImpl).nameIdentifier,
51+
message,
52+
problemHighlightType(project, shortName),
53+
)
54+
}
55+
}

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/inspector/DaoMethodVariableInspection.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ class DaoMethodVariableInspection : AbstractBaseJavaLocalInspectionTool() {
3838
override fun buildVisitor(
3939
holder: ProblemsHolder,
4040
isOnTheFly: Boolean,
41-
): PsiElementVisitor = DaoMethodVariableInspectionVisitor(holder)
41+
): PsiElementVisitor = DaoMethodVariableInspectionVisitor(holder, this.shortName)
4242
}

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/inspector/SqlFileExistInspection.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ class SqlFileExistInspection : AbstractBaseJavaLocalInspectionTool() {
3838
override fun buildVisitor(
3939
holder: ProblemsHolder,
4040
isOnTheFly: Boolean,
41-
): PsiElementVisitor = SqlFileExistInspectionVisitor(holder)
41+
): PsiElementVisitor = SqlFileExistInspectionVisitor(holder, this.shortName)
4242
}

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/visitor/DaoMethodVariableInspectionVisitor.kt

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@
1515
*/
1616
package org.domaframework.doma.intellij.inspection.dao.visitor
1717

18-
import com.intellij.codeInspection.ProblemHighlightType
1918
import com.intellij.codeInspection.ProblemsHolder
2019
import com.intellij.psi.JavaElementVisitor
2120
import com.intellij.psi.PsiFile
2221
import com.intellij.psi.PsiMethod
2322
import com.intellij.psi.PsiParameter
24-
import com.intellij.psi.impl.source.PsiParameterImpl
25-
import org.domaframework.doma.intellij.bundle.MessageBundle
2623
import org.domaframework.doma.intellij.common.dao.getDaoClass
2724
import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType
2825
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
26+
import org.domaframework.doma.intellij.common.sql.validator.result.ValidationUsedDaoMethodArgsResult
2927
import org.domaframework.doma.intellij.extension.findFile
3028
import org.domaframework.doma.intellij.extension.psi.isCollector
3129
import org.domaframework.doma.intellij.extension.psi.isFunctionClazz
@@ -34,6 +32,7 @@ import org.domaframework.doma.intellij.extension.psi.methodParameters
3432

3533
class DaoMethodVariableInspectionVisitor(
3634
private val holder: ProblemsHolder,
35+
private val shotName: String,
3736
) : JavaElementVisitor() {
3837
override fun visitMethod(method: PsiMethod) {
3938
super.visitMethod(method)
@@ -55,25 +54,12 @@ class DaoMethodVariableInspectionVisitor(
5554
val result = findElementsInSqlFile(sqlFileManager, params)
5655
params.forEach { param ->
5756
if (!result.elements.contains(param)) {
58-
val message =
59-
if (result.deplicateForItemElements.contains(param)) {
60-
MessageBundle.message("inspection.invalid.dao.duplicate")
61-
} else {
62-
MessageBundle.message(
63-
"inspection.invalid.dao.paramUse",
64-
param.name,
65-
)
66-
}
67-
holder.registerProblem(
68-
(param.originalElement as PsiParameterImpl).nameIdentifier,
69-
message,
70-
ProblemHighlightType.ERROR,
71-
)
57+
ValidationUsedDaoMethodArgsResult(result, param, shotName).highlightElement(holder)
7258
}
7359
}
7460
}
7561

76-
private data class DaoMethodVariableVisitorResult(
62+
data class DaoMethodVariableVisitorResult(
7763
val elements: List<PsiParameter>,
7864
val deplicateForItemElements: List<PsiParameter>,
7965
)

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/visitor/SqlFileExistInspectionVisitor.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@
1515
*/
1616
package org.domaframework.doma.intellij.inspection.dao.visitor
1717

18-
import com.intellij.codeInspection.ProblemHighlightType
1918
import com.intellij.codeInspection.ProblemsHolder
2019
import com.intellij.psi.JavaElementVisitor
2120
import com.intellij.psi.PsiMethod
22-
import org.domaframework.doma.intellij.bundle.MessageBundle
2321
import org.domaframework.doma.intellij.common.dao.getDaoClass
2422
import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType
2523
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
26-
import org.domaframework.doma.intellij.inspection.dao.quickfix.GenerateSQLFileQuickFixFactory
24+
import org.domaframework.doma.intellij.common.sql.validator.result.ValidationSqlFileExistResult
2725

2826
class SqlFileExistInspectionVisitor(
2927
private val holder: ProblemsHolder,
28+
private val shortName: String,
3029
) : JavaElementVisitor() {
3130
// TODO Support Kotlin Project
3231
override fun visitMethod(method: PsiMethod) {
@@ -46,11 +45,15 @@ class SqlFileExistInspectionVisitor(
4645
) {
4746
val identifier = psiDaoMethod.psiMethod.nameIdentifier ?: return
4847
if (psiDaoMethod.sqlFile == null) {
49-
problemHolder.registerProblem(
48+
ValidationSqlFileExistResult(
49+
psiDaoMethod,
5050
identifier,
51-
MessageBundle.message("inspection.invalid.dao.notExistSql"),
52-
ProblemHighlightType.ERROR,
53-
GenerateSQLFileQuickFixFactory.createSql(psiDaoMethod),
51+
shortName,
52+
).setHighlight(
53+
identifier.textRange,
54+
identifier,
55+
problemHolder,
56+
null,
5457
)
5558
}
5659
}

0 commit comments

Comments
 (0)