|
| 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.formatter.handler |
| 17 | + |
| 18 | +import com.intellij.lang.ASTNode |
| 19 | +import org.domaframework.doma.intellij.formatter.block.SqlBlock |
| 20 | +import org.domaframework.doma.intellij.formatter.block.SqlCommaBlock |
| 21 | +import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictClauseBlock |
| 22 | +import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictExpressionSubGroupBlock |
| 23 | +import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionKeywordGroupBlock |
| 24 | +import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionalExpressionGroupBlock |
| 25 | +import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlReturningGroupBlock |
| 26 | +import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlValuesGroupBlock |
| 27 | +import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock |
| 28 | +import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock |
| 29 | +import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlValuesParamGroupBlock |
| 30 | +import org.domaframework.doma.intellij.formatter.block.word.SqlAliasBlock |
| 31 | +import org.domaframework.doma.intellij.formatter.block.word.SqlTableBlock |
| 32 | +import org.domaframework.doma.intellij.formatter.block.word.SqlWordBlock |
| 33 | +import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext |
| 34 | +import org.domaframework.doma.intellij.formatter.util.SqlKeywordUtil |
| 35 | + |
| 36 | +object NotQueryGroupHandler { |
| 37 | + private const val IN_KEYWORD = "in" |
| 38 | + private const val RETURNING_KEYWORD = "returning" |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates an appropriate [org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock] based on the last group context and node type. |
| 42 | + * Returns null if no specific subgroup is needed. |
| 43 | + */ |
| 44 | + fun getSubGroup( |
| 45 | + lastGroup: SqlBlock?, |
| 46 | + child: ASTNode, |
| 47 | + sqlBlockFormattingCtx: SqlBlockFormattingContext, |
| 48 | + ): SqlBlock? = |
| 49 | + when { |
| 50 | + hasInKeyword(lastGroup) -> SqlParallelListBlock(child, sqlBlockFormattingCtx) |
| 51 | + lastGroup is SqlConditionKeywordGroupBlock -> createConditionalExpressionGroup(child, sqlBlockFormattingCtx) |
| 52 | + hasFunctionOrAliasContext(lastGroup) -> createFunctionOrValueBlock(lastGroup, child, sqlBlockFormattingCtx) |
| 53 | + lastGroup is SqlConflictClauseBlock -> SqlConflictExpressionSubGroupBlock(child, sqlBlockFormattingCtx) |
| 54 | + hasValuesContext(lastGroup) -> SqlValuesParamGroupBlock(child, sqlBlockFormattingCtx) |
| 55 | + else -> null |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates a keyword group block for specific keywords. |
| 60 | + * Currently only handles 'returning' keyword. |
| 61 | + */ |
| 62 | + fun getKeywordGroup( |
| 63 | + child: ASTNode, |
| 64 | + sqlBlockFormattingCtx: SqlBlockFormattingContext, |
| 65 | + ): SqlBlock? { |
| 66 | + val keyword = child.text.lowercase() |
| 67 | + return when (keyword) { |
| 68 | + RETURNING_KEYWORD -> SqlReturningGroupBlock(child, sqlBlockFormattingCtx) |
| 69 | + else -> null |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Checks if the last group has an 'IN' keyword as its last option keyword. |
| 75 | + */ |
| 76 | + private fun hasInKeyword(lastGroup: SqlBlock?): Boolean { |
| 77 | + val lastKeyword = |
| 78 | + lastGroup |
| 79 | + ?.childBlocks |
| 80 | + ?.lastOrNull { SqlKeywordUtil.isOptionSqlKeyword(it.getNodeText()) } |
| 81 | + return lastKeyword?.getNodeText()?.lowercase() == IN_KEYWORD |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Creates a conditional expression group block. |
| 86 | + */ |
| 87 | + private fun createConditionalExpressionGroup( |
| 88 | + child: ASTNode, |
| 89 | + sqlBlockFormattingCtx: SqlBlockFormattingContext, |
| 90 | + ): SqlConditionalExpressionGroupBlock = SqlConditionalExpressionGroupBlock(child, sqlBlockFormattingCtx) |
| 91 | + |
| 92 | + /** |
| 93 | + * Checks if the last group has a word block context that requires function or alias handling. |
| 94 | + */ |
| 95 | + private fun hasFunctionOrAliasContext(lastGroup: SqlBlock?): Boolean = lastGroup?.childBlocks?.lastOrNull() is SqlWordBlock |
| 96 | + |
| 97 | + /** |
| 98 | + * Creates either a function parameter block or values parameter block based on the previous child type. |
| 99 | + */ |
| 100 | + private fun createFunctionOrValueBlock( |
| 101 | + lastGroup: SqlBlock?, |
| 102 | + child: ASTNode, |
| 103 | + sqlBlockFormattingCtx: SqlBlockFormattingContext, |
| 104 | + ): SqlBlock { |
| 105 | + val prevChild = lastGroup?.childBlocks?.lastOrNull() |
| 106 | + return when { |
| 107 | + prevChild is SqlAliasBlock || prevChild is SqlTableBlock -> |
| 108 | + SqlValuesParamGroupBlock(child, sqlBlockFormattingCtx) |
| 109 | + else -> |
| 110 | + SqlFunctionParamBlock(child, sqlBlockFormattingCtx) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Checks if the context indicates a values parameter group should be created. |
| 116 | + */ |
| 117 | + private fun hasValuesContext(lastGroup: SqlBlock?): Boolean = |
| 118 | + lastGroup is SqlValuesGroupBlock || |
| 119 | + (lastGroup is SqlCommaBlock && lastGroup.parentBlock is SqlValuesGroupBlock) |
| 120 | +} |
0 commit comments