diff --git a/src/main/kotlin/org/domaframework/doma/intellij/action/dao/GenerateSqlAction.kt b/src/main/kotlin/org/domaframework/doma/intellij/action/dao/GenerateSqlAction.kt index 6ee6e95b..5e2e8b72 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/action/dao/GenerateSqlAction.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/action/dao/GenerateSqlAction.kt @@ -37,14 +37,16 @@ class GenerateSqlAction : AnAction() { e.presentation.isEnabledAndVisible = false currentFile = e.getData(CommonDataKeys.PSI_FILE) ?: return val editor = e.getData(CommonDataKeys.EDITOR) ?: return - getDaoClass(currentFile!!) ?: return - val element = currentFile!!.findElementAt(editor.caretModel.offset) ?: return + val project = e.project ?: return + val file: PsiFile = currentFile ?: return + getDaoClass(file) ?: return + val element = file.findElementAt(editor.caretModel.offset) ?: return val method = PsiTreeUtil.getParentOfType(element, PsiMethod::class.java) ?: return - val psiDaoMethod = PsiDaoMethod(e.project!!, method) + val psiDaoMethod = PsiDaoMethod(project, method) e.presentation.isEnabledAndVisible = psiDaoMethod.isUseSqlFileMethod() && - isJavaOrKotlinFileType(currentFile!!) && + isJavaOrKotlinFileType(file) && psiDaoMethod.sqlFile == null } @@ -61,7 +63,8 @@ class GenerateSqlAction : AnAction() { ) val project = e.project ?: return val editor = e.getData(CommonDataKeys.EDITOR) ?: return - val element = currentFile!!.findElementAt(editor.caretModel.offset) ?: return + val file: PsiFile = currentFile ?: return + val element = file.findElementAt(editor.caretModel.offset) ?: return val method = PsiTreeUtil.getParentOfType(element, PsiMethod::class.java) ?: return val psiDaoMethod = PsiDaoMethod(project, method) psiDaoMethod.generateSqlFile() diff --git a/src/main/kotlin/org/domaframework/doma/intellij/action/dao/JumpToSQLFromDaoAction.kt b/src/main/kotlin/org/domaframework/doma/intellij/action/dao/JumpToSQLFromDaoAction.kt index 7a6b19c2..ff284d0e 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/action/dao/JumpToSQLFromDaoAction.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/action/dao/JumpToSQLFromDaoAction.kt @@ -34,14 +34,16 @@ class JumpToSQLFromDaoAction : AnAction() { override fun update(e: AnActionEvent) { e.presentation.isEnabledAndVisible = false currentFile = e.getData(CommonDataKeys.PSI_FILE) ?: return + val file: PsiFile = currentFile ?: return - if (!isJavaOrKotlinFileType(currentFile!!)) return - getDaoClass(currentFile!!) ?: return + if (!isJavaOrKotlinFileType(file)) return + getDaoClass(file) ?: return val editor = e.getData(CommonDataKeys.EDITOR) ?: return - val element = currentFile!!.findElementAt(editor.caretModel.offset) ?: return + val element = file.findElementAt(editor.caretModel.offset) ?: return + val project = e.project ?: return val method = PsiTreeUtil.getParentOfType(element, PsiMethod::class.java) ?: return - val psiDaoMethod = PsiDaoMethod(e.project!!, method) + val psiDaoMethod = PsiDaoMethod(project, method) e.presentation.isEnabledAndVisible = psiDaoMethod.isUseSqlFileMethod() && @@ -60,10 +62,9 @@ class JumpToSQLFromDaoAction : AnAction() { startTime, ) val editor = e.getData(CommonDataKeys.EDITOR) ?: return - val element = currentFile!!.findElementAt(editor.caretModel.offset) ?: return + val element = currentFile?.findElementAt(editor.caretModel.offset) ?: return val method = PsiTreeUtil.getParentOfType(element, PsiMethod::class.java) ?: return val project = e.project ?: return - val psiDaoMethod = PsiDaoMethod(project, method) PluginLoggerUtil.countLoggingByAction( @@ -72,6 +73,7 @@ class JumpToSQLFromDaoAction : AnAction() { inputEvent, startTime, ) - jumpSqlFromDao(project, psiDaoMethod.sqlFile!!) + val sqlFile = psiDaoMethod.sqlFile ?: return + jumpSqlFromDao(project, sqlFile) } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/action/sql/JumpToDaoFromSQLAction.kt b/src/main/kotlin/org/domaframework/doma/intellij/action/sql/JumpToDaoFromSQLAction.kt index 5dc47a3a..6be7d8a8 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/action/sql/JumpToDaoFromSQLAction.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/action/sql/JumpToDaoFromSQLAction.kt @@ -35,9 +35,10 @@ class JumpToDaoFromSQLAction : AnAction() { override fun update(e: AnActionEvent) { e.presentation.isEnabledAndVisible = false currentFile = e.getData(CommonDataKeys.PSI_FILE) ?: return - findDaoMethod(currentFile!!) ?: return + val file = currentFile ?: return + findDaoMethod(file) ?: return e.presentation.isEnabledAndVisible = - isSupportFileType(currentFile!!) + isSupportFileType(file) } override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT @@ -51,11 +52,12 @@ class JumpToDaoFromSQLAction : AnAction() { inputEvent, startTime, ) - + val file = currentFile ?: return val project = e.project ?: return - val daoFile = findDaoFile(project, currentFile!!) ?: return + val daoFile = findDaoFile(project, file) ?: return - jumpToDaoMethod(project, currentFile?.virtualFile?.nameWithoutExtension!!, daoFile) + val nameWithoutExtension = file.virtualFile?.nameWithoutExtension ?: return + jumpToDaoMethod(project, nameWithoutExtension, daoFile) PluginLoggerUtil.countLoggingByAction( this::class.java.simpleName, "JumpToDao", diff --git a/src/main/kotlin/org/domaframework/doma/intellij/action/sql/JumpToDeclarationFromSqlAction.kt b/src/main/kotlin/org/domaframework/doma/intellij/action/sql/JumpToDeclarationFromSqlAction.kt index 17200d7e..e6a29991 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/action/sql/JumpToDeclarationFromSqlAction.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/action/sql/JumpToDeclarationFromSqlAction.kt @@ -31,6 +31,7 @@ import com.intellij.psi.util.PsiTypesUtil import com.intellij.psi.util.elementType import org.domaframework.doma.intellij.common.PluginLoggerUtil import org.domaframework.doma.intellij.common.dao.findDaoMethod +import org.domaframework.doma.intellij.common.dao.getDaoClass import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType import org.domaframework.doma.intellij.common.psi.PsiParentClass import org.domaframework.doma.intellij.common.psi.PsiStaticElement @@ -58,31 +59,35 @@ class JumpToDeclarationFromSqlAction : AnAction() { val caretOffset = editor.caretModel.primaryCaret.selectionStart currentFile = e.getData(CommonDataKeys.PSI_FILE) ?: return - currentFile?.let { element = it.findElementAt(caretOffset) ?: return } - if (element == null) return + element = currentFile?.findElementAt(caretOffset) ?: return + var file: PsiFile = currentFile ?: return val project = element?.project ?: return - if (isJavaOrKotlinFileType(currentFile ?: return)) { + if (isJavaOrKotlinFileType(file) && getDaoClass(file) != null) { val injectedLanguageManager = InjectedLanguageManager.getInstance(project) - val literal = PsiTreeUtil.getParentOfType(element, PsiLiteralExpression::class.java) + val literal = PsiTreeUtil.getParentOfType(element, PsiLiteralExpression::class.java) ?: return currentFile = injectedLanguageManager - .getInjectedPsiFiles(literal!!) + .getInjectedPsiFiles(literal) ?.firstOrNull() ?.first as? PsiFile - element = currentFile?.findElementAt(countInjectionOffset(literal, caretOffset)) + ?: return + file = currentFile ?: return + element = file.findElementAt(countInjectionOffset(literal, caretOffset)) ?: return } - findDaoMethod(currentFile ?: return) ?: return + findDaoMethod(file) ?: return - val staticDirection = element?.parent - val staticDirective = getStaticDirective(staticDirection, element!!.text) + val elm = element ?: return + val elementText = elm.text ?: "" + val staticDirection = elm.parent + val staticDirective = getStaticDirective(staticDirection, elementText) if (staticDirective != null) { e.presentation.isEnabledAndVisible = true return } - val targetElement = getBlockCommentElements(element ?: return) + val targetElement = getBlockCommentElements(elm) if (targetElement.isNotEmpty()) e.presentation.isEnabledAndVisible = true } @@ -114,9 +119,13 @@ class JumpToDeclarationFromSqlAction : AnAction() { override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT override fun actionPerformed(e: AnActionEvent) { + val elm = element ?: return + val elementText = elm.text ?: "" + val file = currentFile ?: return + val startTime = System.nanoTime() - val staticDirection = element?.parent - val staticDirective = getStaticDirective(staticDirection, element!!.text) + val staticDirection = elm.parent + val staticDirective = getStaticDirective(staticDirection, elementText) if (staticDirective != null) { BindVariableElement(staticDirective).jumpToEntity() PluginLoggerUtil.countLoggingByAction( @@ -127,20 +136,20 @@ class JumpToDeclarationFromSqlAction : AnAction() { ) return } - if (element == null) return + // TODO Since the update also checks whether the action is to be executed, // delete it if it is unnecessary. - if (isNotBindVariable(element!!)) return + if (isNotBindVariable(elm)) return - val targetElement = getBlockCommentElements(element!!) + val targetElement = getBlockCommentElements(elm) if (targetElement.isEmpty()) return - val daoMethod = currentFile?.let { file -> findDaoMethod(file) } ?: return - when (element!!.textOffset) { + val daoMethod = findDaoMethod(file) ?: return + when (elm.textOffset) { targetElement.first().textOffset -> jumpToDaoMethodParameter( daoMethod, - element!!, + elm, logActionFileType, e, startTime, @@ -155,10 +164,10 @@ class JumpToDeclarationFromSqlAction : AnAction() { elementName: String, ): PsiElement? { if (staticDirection == null) return null - + val file: PsiFile = currentFile ?: return null // Jump to class definition if (staticDirection is SqlElClass) { - val psiStaticElement = PsiStaticElement(staticDirection.text, currentFile!!) + val psiStaticElement = PsiStaticElement(staticDirection.text, file) return psiStaticElement.getRefClazz() } @@ -167,12 +176,14 @@ class JumpToDeclarationFromSqlAction : AnAction() { if (staticDirection is SqlElStaticFieldAccessExpr || staticAccessParent is SqlElStaticFieldAccessExpr ) { + val firstChildText = + staticAccessParent.children + .firstOrNull() + ?.text ?: "" val psiStaticElement = PsiStaticElement( - staticAccessParent.children - .firstOrNull() - ?.text ?: "", - currentFile!!, + firstChildText, + file, ) val javaClazz = psiStaticElement.getRefClazz() ?: return null val psiParentClass = PsiParentClass(PsiTypesUtil.getClassType(javaClazz)) @@ -300,9 +311,9 @@ class JumpToDeclarationFromSqlAction : AnAction() { parentClass .findMethod(elm.text) ?.let { - if (it.returnType == null) return null + val returnType = it.returnType ?: return null val bindVal = - getBindVariableIfLastIndex(index, it.returnType!!, it.originalElement) + getBindVariableIfLastIndex(index, returnType, it.originalElement) if (bindVal != null) return bindVal } if (!isExistProperty) return null diff --git a/src/main/kotlin/org/domaframework/doma/intellij/common/dao/JumpActionFunctions.kt b/src/main/kotlin/org/domaframework/doma/intellij/common/dao/JumpActionFunctions.kt index 9537449d..1e41df81 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/common/dao/JumpActionFunctions.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/common/dao/JumpActionFunctions.kt @@ -63,7 +63,8 @@ private fun getJavaFunctionOffset( targetMethodName: String, ) { try { - PsiNavigateUtil.navigate(findUseSqlDaoMethod(file, targetMethodName)!!) + val dapMethod = findUseSqlDaoMethod(file, targetMethodName) ?: return + PsiNavigateUtil.navigate(dapMethod) } catch (e: Exception) { rethrow(e) } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiDaoMethod.kt b/src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiDaoMethod.kt index 42565aa8..992c5c49 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiDaoMethod.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiDaoMethod.kt @@ -46,7 +46,7 @@ import java.io.IOException * Class that handles Dao method information */ class PsiDaoMethod( - val psiProject: Project, + private val psiProject: Project, val psiMethod: PsiMethod, ) { private val isTest = false @@ -132,7 +132,7 @@ class PsiDaoMethod( psiFileFactory .createFileFromText( "tempFile", - Language.findLanguageByID(SqlLanguage.INSTANCE.id)!!, + Language.findLanguageByID(SqlLanguage.INSTANCE.id) ?: return, valueText, ).virtualFile } @@ -154,11 +154,14 @@ class PsiDaoMethod( throw IncorrectOperationException(e) } + val virtualFile = + VfsUtil.findRelativeFile(rootDir, *parenDirPathSpirit) + ?: return@runWriteCommandAction val sqlOutputDirPath = PsiManager .getInstance(psiProject) - .findDirectory(VfsUtil.findRelativeFile(rootDir, *parenDirPathSpirit)!!) - val sqlVirtualFile = sqlOutputDirPath!!.createFile(sqlFileName).virtualFile + .findDirectory(virtualFile) ?: return@runWriteCommandAction + val sqlVirtualFile = sqlOutputDirPath.createFile(sqlFileName).virtualFile FileEditorManager .getInstance(psiProject) .openFile(sqlVirtualFile, true) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/contributor/sql/provider/SqlParameterCompletionProvider.kt b/src/main/kotlin/org/domaframework/doma/intellij/contributor/sql/provider/SqlParameterCompletionProvider.kt index 72e43e4e..d070d526 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/contributor/sql/provider/SqlParameterCompletionProvider.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/contributor/sql/provider/SqlParameterCompletionProvider.kt @@ -112,8 +112,8 @@ class SqlParameterCompletionProvider : CompletionProvider( it !is PsiErrorElement } if (!pos.isNotWhiteSpace() && !isRightFactor(prevElm)) return - - val blockElements = getAccessElementTextBlocks(parameters.originalPosition!!) + val originalPosition = parameters.originalPosition ?: return + val blockElements = getAccessElementTextBlocks(originalPosition) generateCompletionList( blockElements, originalFile, @@ -146,7 +146,7 @@ class SqlParameterCompletionProvider : CompletionProvider( prevElm?.elementType == SqlTypes.EL_ASTERISK || prevElm?.elementType == SqlTypes.EL_SLASH || prevElm?.elementType == SqlTypes.EL_PERCENT || - prevElm?.isNotWhiteSpace()!! + prevElm?.isNotWhiteSpace() == true ) private fun getAccessElementTextBlocks(targetElement: PsiElement): List { diff --git a/src/main/kotlin/org/domaframework/doma/intellij/extension/psi/DomaAnnotationType.kt b/src/main/kotlin/org/domaframework/doma/intellij/extension/psi/DomaAnnotationType.kt index f53877f7..792ea27d 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/extension/psi/DomaAnnotationType.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/extension/psi/DomaAnnotationType.kt @@ -40,7 +40,7 @@ enum class DomaAnnotationType( fun isRequireSqlTemplate(): Boolean = this == Select || this == Script || this == SqlProcessor - fun useSqlFileOption(): Boolean = + private fun useSqlFileOption(): Boolean = this == Insert || this == Update || this == Delete || diff --git a/src/main/kotlin/org/domaframework/doma/intellij/extension/psi/PsiFileExtensions.kt b/src/main/kotlin/org/domaframework/doma/intellij/extension/psi/PsiFileExtensions.kt deleted file mode 100644 index 72460283..00000000 --- a/src/main/kotlin/org/domaframework/doma/intellij/extension/psi/PsiFileExtensions.kt +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright Doma Tools Authors - * - * 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 - * - * https://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.domaframework.doma.intellij.extension.psi - -import com.intellij.lang.injection.InjectedLanguageManager -import com.intellij.openapi.project.Project -import com.intellij.psi.PsiFile - -/** - * For processing inside Sql annotations, get it as an injected custom language - */ -fun PsiFile.initPsiFileAndElement( - project: Project, - caretOffset: Int, -): PsiFile? { - val injectedLanguageManager = InjectedLanguageManager.getInstance(project) - val element = - injectedLanguageManager.findInjectedElementAt(this, caretOffset) ?: return null - return element.containingFile -} diff --git a/src/main/kotlin/org/domaframework/doma/intellij/extension/psi/PsiParameterExtension.kt b/src/main/kotlin/org/domaframework/doma/intellij/extension/psi/PsiParameterExtension.kt index 45652dc8..e6daea2d 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/extension/psi/PsiParameterExtension.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/extension/psi/PsiParameterExtension.kt @@ -29,7 +29,8 @@ fun PsiParameter.getIterableClazz(annotationType: DomaAnnotationType): PsiParent if (immediate.name == "List" && annotationType.isBatchAnnotation()) { val listType = (this.type as PsiClassReferenceType).parameters.firstOrNull() - return PsiParentClass(listType!!) + ?: return PsiParentClass(this.type) + return PsiParentClass(listType) } } return PsiParentClass(this.type) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/inspector/SqlFileExistInspector.kt b/src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/inspector/SqlFileExistInspector.kt index 206452e5..983ecb71 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/inspector/SqlFileExistInspector.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/inspector/SqlFileExistInspector.kt @@ -65,9 +65,10 @@ class SqlFileExistInspector : AbstractBaseJavaLocalInspectionTool() { psiDaoMethod: PsiDaoMethod, problemHolder: ProblemsHolder, ) { + val identifier = psiDaoMethod.psiMethod.nameIdentifier ?: return if (psiDaoMethod.sqlFile == null) { problemHolder.registerProblem( - psiDaoMethod.psiMethod.nameIdentifier!!, + identifier, MessageBundle.message("inspection.sql.not.exist.error"), ProblemHighlightType.ERROR, GenerateSQLFileQuickFixFactory.createSql(psiDaoMethod),