Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
6 changes: 5 additions & 1 deletion src/main/java/org/domaframework/doma/intellij/Sql.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
extends("el_invocation_expr_group|el_primary_expr")=el_factor_expr
extends("el_logical_expr_group|el_factor_expr")=el_term_expr
extends(".*expr")=el_expr
mixin("el_primary_expr|el_class")="org.domaframework.doma.intellij.psi.SqlCustomExprImpl"
consumeTokenMethod("literal|word|.*directive|.*expr")="consumeTokenFast"
}

Expand Down Expand Up @@ -166,5 +167,8 @@ el_parameters ::= "(" (el_expr ("," el_expr)*)? ")" {pin=1}
// primary
el_primary_expr ::= el_literal_expr | el_id_expr | el_paren_expr
private el_literal_expr ::= EL_NULL | BOOLEAN | (EL_PLUS | EL_MINUS)? EL_NUMBER | EL_STRING | EL_CHAR
private el_id_expr ::= EL_IDENTIFIER
el_id_expr ::= EL_IDENTIFIER
{
mixin="org.domaframework.doma.intellij.psi.SqlElPrimaryExprImpl"
}
private el_paren_expr ::= "(" el_expr ")" {pin=1}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.psi;

import com.intellij.psi.PsiElement;

public interface SqlCustomElExpr extends PsiElement {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.psi;

import com.intellij.extapi.psi.ASTWrapperPsiElement;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiReference;
import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry;
import org.jetbrains.annotations.NotNull;

public class SqlCustomExprImpl extends ASTWrapperPsiElement implements SqlCustomElExpr {

public SqlCustomExprImpl(@NotNull ASTNode node) {
super(node);
}

@Override
public PsiReference @NotNull [] getReferences() {
return ReferenceProvidersRegistry.getReferencesFromProviders(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ import org.domaframework.doma.intellij.extension.psi.getIterableClazz
import org.domaframework.doma.intellij.extension.psi.isNotWhiteSpace
import org.domaframework.doma.intellij.extension.psi.methodParameters
import org.domaframework.doma.intellij.psi.SqlElClass
import org.domaframework.doma.intellij.psi.SqlElPrimaryExpr
import org.domaframework.doma.intellij.psi.SqlElFieldAccessExpr
import org.domaframework.doma.intellij.psi.SqlElIdExpr
import org.domaframework.doma.intellij.psi.SqlElStaticFieldAccessExpr
import org.domaframework.doma.intellij.psi.SqlTypes

/**
* Action to jump from SQL bind variable to Dao method argument and Entity class
Expand Down Expand Up @@ -80,8 +80,7 @@ class JumpToDeclarationFromSqlAction : AnAction() {

val elm = element ?: return
val elementText = elm.text ?: ""
val staticDirection = elm.parent
val staticDirective = getStaticDirective(staticDirection, elementText)
val staticDirective = getStaticDirective(elm, elementText)
if (staticDirective != null) {
e.presentation.isEnabledAndVisible = true
return
Expand Down Expand Up @@ -124,8 +123,7 @@ class JumpToDeclarationFromSqlAction : AnAction() {
val file = currentFile ?: return

val startTime = System.nanoTime()
val staticDirection = elm.parent
val staticDirective = getStaticDirective(staticDirection, elementText)
val staticDirective = getStaticDirective(elm, elementText)
if (staticDirective != null) {
BindVariableElement(staticDirective).jumpToEntity()
PluginLoggerUtil.countLoggingByAction(
Expand Down Expand Up @@ -166,16 +164,15 @@ class JumpToDeclarationFromSqlAction : AnAction() {
if (staticDirection == null) return null
val file: PsiFile = currentFile ?: return null
// Jump to class definition
if (staticDirection is SqlElClass) {
val classParent = PsiTreeUtil.getParentOfType(staticDirection, SqlElClass::class.java)
if (classParent != null) {
val psiStaticElement = PsiStaticElement(staticDirection.text, file)
return psiStaticElement.getRefClazz()
}

// Jump from field or method to definition (assuming the top element is static)
val staticAccessParent = staticDirection.parent
if (staticDirection is SqlElStaticFieldAccessExpr ||
staticAccessParent is SqlElStaticFieldAccessExpr
) {
val staticAccessParent = PsiTreeUtil.getParentOfType(staticDirection, SqlElStaticFieldAccessExpr::class.java)
if (staticAccessParent != null) {
val firstChildText =
staticAccessParent.children
.firstOrNull()
Expand Down Expand Up @@ -204,8 +201,7 @@ class JumpToDeclarationFromSqlAction : AnAction() {
private fun isNotBindVariable(it: PsiElement) =
(
it.parent?.elementType is IFileElementType &&
it.elementType != SqlTypes.EL_IDENTIFIER &&
it !is SqlElPrimaryExpr &&
it !is SqlElIdExpr &&
!it.isNotWhiteSpace()
)

Expand Down Expand Up @@ -258,18 +254,20 @@ class JumpToDeclarationFromSqlAction : AnAction() {
}

private fun getBlockCommentElements(element: PsiElement): List<PsiElement> {
val fieldAccessExpr = PsiTreeUtil.getParentOfType(element, SqlElFieldAccessExpr::class.java)
val nodeElm =
PsiTreeUtil
.getChildrenOfType(element.parent, PsiElement::class.java)
?.filter {
(
it.elementType == SqlTypes.EL_IDENTIFIER ||
it is SqlElPrimaryExpr
) &&
it.textOffset <= element.textOffset
}?.toList()
?.sortedBy { it.textOffset } ?: emptyList()
if (fieldAccessExpr != null) {
PsiTreeUtil
.getChildrenOfType(
fieldAccessExpr,
SqlElIdExpr::class.java,
)?.filter { it.textOffset <= element.textOffset }
} else {
listOf(element)
}
return nodeElm
?.toList()
?.sortedBy { it.textOffset } ?: emptyList()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import com.intellij.psi.util.elementType
import com.intellij.psi.util.prevLeaf
import com.intellij.psi.util.prevLeafs
import com.intellij.util.ProcessingContext
import org.domaframework.doma.intellij.psi.SqlElClass
import org.domaframework.doma.intellij.psi.SqlElIdExpr
import org.domaframework.doma.intellij.psi.SqlTypes

object PsiPatternUtil {
Expand Down Expand Up @@ -84,6 +86,22 @@ object PsiPatternUtil {
},
)

fun createReferencePattern(): PsiElementPattern.Capture<PsiElement> =
PlatformPatterns.psiElement().with(
object : PatternCondition<PsiElement>("PsiReferenceParentCondition") {
override fun accepts(
element: PsiElement,
context: ProcessingContext?,
): Boolean {
if (element is SqlElClass) return true
if (element is SqlElIdExpr) {
return PsiTreeUtil.getParentOfType(element, SqlElClass::class.java) == null
}
return false
}
},
)

/**
* Get the string to search from the cursor position to the start of a block comment or a blank space
* @return search Keyword
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement
import org.domaframework.doma.intellij.common.psi.PsiPatternUtil
import org.domaframework.doma.intellij.contributor.sql.provider.SqlParameterCompletionProvider
import org.domaframework.doma.intellij.setting.SqlLanguage
import org.jetbrains.kotlin.idea.completion.or

/**
Expand All @@ -41,7 +42,7 @@ open class SqlCompletionContributor : CompletionContributor() {
.inFile(
PlatformPatterns
.psiFile()
.withName(StandardPatterns.string().endsWith(".sql")),
.withLanguage(SqlLanguage.INSTANCE),
),
PsiPatternUtil
.createPattern(PsiComment::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ import org.domaframework.doma.intellij.psi.SqlElFieldAccessExpr
import org.domaframework.doma.intellij.psi.SqlElForDirective
import org.domaframework.doma.intellij.psi.SqlElGeExpr
import org.domaframework.doma.intellij.psi.SqlElGtExpr
import org.domaframework.doma.intellij.psi.SqlElIdExpr
import org.domaframework.doma.intellij.psi.SqlElIfDirective
import org.domaframework.doma.intellij.psi.SqlElLeExpr
import org.domaframework.doma.intellij.psi.SqlElLtExpr
import org.domaframework.doma.intellij.psi.SqlElNeExpr
import org.domaframework.doma.intellij.psi.SqlElOrExpr
import org.domaframework.doma.intellij.psi.SqlElParameters
import org.domaframework.doma.intellij.psi.SqlElPrimaryExpr
import org.domaframework.doma.intellij.psi.SqlTypes
import org.jetbrains.kotlin.idea.base.util.module

Expand Down Expand Up @@ -176,14 +176,9 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
if (parent is SqlElFieldAccessExpr) {
blocks =
PsiTreeUtil
.getChildrenOfTypeAsList(parent, PsiElement::class.java)
.filter {
(
it is SqlElPrimaryExpr ||
it.elementType == SqlTypes.EL_IDENTIFIER
) &&
it.parent !is SqlElClass
}.toList()
.getChildrenOfTypeAsList(parent, SqlElIdExpr::class.java)
.filter { it.parent !is SqlElClass }
.toList()
if (blocks.isEmpty()) {
val parent =
PsiTreeUtil.findFirstParent(parent) {
Expand All @@ -194,10 +189,9 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(

blocks =
PsiTreeUtil
.getChildrenOfTypeAsList(parent, PsiElement::class.java)
.getChildrenOfTypeAsList(parent, SqlElIdExpr::class.java)
.filter {
it.elementType == SqlTypes.EL_IDENTIFIER &&
(targetElement.startOffsetInParent) >= it.startOffsetInParent
targetElement.startOffsetInParent >= it.startOffsetInParent
}.toList()
}
} else {
Expand Down Expand Up @@ -321,7 +315,7 @@ class SqlParameterCompletionProvider : CompletionProvider<CompletionParameters>(
}

private fun getSearchElementText(elm: PsiElement): String =
if (elm is SqlElPrimaryExpr || elm.elementType == SqlTypes.EL_IDENTIFIER) {
if (elm.elementType == SqlTypes.EL_IDENTIFIER) {
elm.text
} else {
""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,17 @@ import org.domaframework.doma.intellij.psi.SqlCustomElCommentExpr
import org.domaframework.doma.intellij.psi.SqlElClass
import org.domaframework.doma.intellij.psi.SqlElElseifDirective
import org.domaframework.doma.intellij.psi.SqlElForDirective
import org.domaframework.doma.intellij.psi.SqlElIdExpr
import org.domaframework.doma.intellij.psi.SqlElIfDirective
import org.domaframework.doma.intellij.psi.SqlElPrimaryExpr
import org.domaframework.doma.intellij.psi.SqlElStaticFieldAccessExpr
import org.domaframework.doma.intellij.psi.SqlTypes
import kotlin.invoke

val SqlElStaticFieldAccessExpr.accessElements: List<PsiElement>
get() {
return PsiTreeUtil
.getChildrenOfType(this, PsiElement::class.java)
?.filter {
(
it.elementType == SqlTypes.EL_IDENTIFIER ||
it is SqlElPrimaryExpr
)
}?.sortedBy { it.textOffset }
.getChildrenOfType(this, SqlElIdExpr::class.java)
?.sortedBy { it.textOffset }
?.toList()
?: emptyList()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 org.domaframework.doma.intellij.psi.SqlElFieldAccessExpr
import org.domaframework.doma.intellij.psi.SqlElPrimaryExpr

fun SqlElPrimaryExpr.isFirstElement(): Boolean =
if (this.parent is SqlElFieldAccessExpr) {
this.parent.children.indexOf(this) == 0
} else {
true
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,9 @@ package org.domaframework.doma.intellij.extension.psi

import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.elementType
import org.domaframework.doma.intellij.psi.SqlElForDirective
import org.domaframework.doma.intellij.psi.SqlElPrimaryExpr
import org.domaframework.doma.intellij.psi.SqlTypes
import org.domaframework.doma.intellij.psi.SqlElIdExpr

fun SqlElForDirective.getForItem(): PsiElement? =
PsiTreeUtil
.getChildrenOfType(this, PsiElement::class.java)
?.firstOrNull {
it.isNotWhiteSpace() &&
(it is SqlElPrimaryExpr || it.elementType == SqlTypes.EL_IDENTIFIER)
}
.getChildOfType(this, SqlElIdExpr::class.java)
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import com.intellij.psi.PsiMethod
import com.intellij.psi.PsiParameter
import com.intellij.psi.PsiRecursiveElementVisitor
import com.intellij.psi.impl.source.PsiParameterImpl
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.elementType
import org.domaframework.doma.intellij.bundle.MessageBundle
import org.domaframework.doma.intellij.common.dao.getDaoClass
import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType
Expand All @@ -36,7 +38,8 @@ import org.domaframework.doma.intellij.extension.psi.isCollector
import org.domaframework.doma.intellij.extension.psi.isFunctionClazz
import org.domaframework.doma.intellij.extension.psi.isSelectOption
import org.domaframework.doma.intellij.extension.psi.methodParameters
import org.domaframework.doma.intellij.psi.SqlElPrimaryExpr
import org.domaframework.doma.intellij.psi.SqlElStaticFieldAccessExpr
import org.domaframework.doma.intellij.psi.SqlTypes

/**
* Check if Dao method arguments are used in the corresponding SQL file
Expand Down Expand Up @@ -94,11 +97,16 @@ class DaoMethodVariableInspector : AbstractBaseJavaLocalInspectionTool() {
object : PsiRecursiveElementVisitor() {
// Recursively explore child elements in a file with PsiRecursiveElementVisitor.
override fun visitElement(element: PsiElement) {
if (element is SqlElPrimaryExpr) {
if (element.elementType == SqlTypes.EL_IDENTIFIER && element.prevSibling?.elementType != SqlTypes.DOT) {
iterator = args.minus(elements.toSet()).iterator()
while (iterator.hasNext()) {
val arg = iterator.next()
if (element.text == arg.name) {
val fieldAccessExpr =
PsiTreeUtil.getParentOfType(
element,
SqlElStaticFieldAccessExpr::class.java,
)
if (fieldAccessExpr == null && element.text == arg.name) {
elements.add(arg)
break
}
Expand Down
Loading
Loading