Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() &&
Expand All @@ -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(
Expand All @@ -72,6 +73,7 @@ class JumpToSQLFromDaoAction : AnAction() {
inputEvent,
startTime,
)
jumpSqlFromDao(project, psiDaoMethod.sqlFile!!)
val sqlFile = psiDaoMethod.sqlFile ?: return
jumpSqlFromDao(project, sqlFile)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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()
}

Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -132,7 +132,7 @@ class PsiDaoMethod(
psiFileFactory
.createFileFromText(
"tempFile",
Language.findLanguageByID(SqlLanguage.INSTANCE.id)!!,
Language.findLanguageByID(SqlLanguage.INSTANCE.id) ?: return,
valueText,
).virtualFile
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
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,
Expand Down Expand Up @@ -146,7 +146,7 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
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<PsiElement> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading