|
| 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.util |
| 17 | + |
| 18 | +import com.intellij.lang.ASTNode |
| 19 | +import org.domaframework.doma.intellij.formatter.block.SqlBlock |
| 20 | +import org.domaframework.doma.intellij.formatter.block.SqlKeywordBlock |
| 21 | +import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock |
| 22 | +import org.domaframework.doma.intellij.formatter.block.conflict.OnConflictKeywordType |
| 23 | +import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictClauseBlock |
| 24 | +import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionKeywordGroupBlock |
| 25 | +import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock |
| 26 | +import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateViewGroupBlock |
| 27 | +import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineGroupBlock |
| 28 | +import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlExistsGroupBlock |
| 29 | +import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlInGroupBlock |
| 30 | +import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlLateralGroupBlock |
| 31 | +import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlWhereGroupBlock |
| 32 | + |
| 33 | +/** |
| 34 | + * Factory class for creating SQL keyword blocks based on indent type and context |
| 35 | + */ |
| 36 | +class SqlKeywordBlockFactory( |
| 37 | + private val sqlBlockFormattingCtx: SqlBlockFormattingContext, |
| 38 | +) { |
| 39 | + fun createInlineBlock( |
| 40 | + child: ASTNode, |
| 41 | + lastGroupBlock: SqlBlock?, |
| 42 | + ): SqlBlock = |
| 43 | + if (!SqlKeywordUtil.isSetLineKeyword( |
| 44 | + child.text, |
| 45 | + lastGroupBlock?.getNodeText() ?: "", |
| 46 | + ) |
| 47 | + ) { |
| 48 | + SqlInlineGroupBlock(child, sqlBlockFormattingCtx) |
| 49 | + } else { |
| 50 | + SqlKeywordBlock(child, IndentType.INLINE, sqlBlockFormattingCtx) |
| 51 | + } |
| 52 | + |
| 53 | + fun createAttachedBlock( |
| 54 | + child: ASTNode, |
| 55 | + lastGroupBlock: SqlBlock?, |
| 56 | + ): SqlBlock { |
| 57 | + if (lastGroupBlock is SqlCreateKeywordGroupBlock) { |
| 58 | + lastGroupBlock.setCreateQueryType(child.text) |
| 59 | + } |
| 60 | + return SqlKeywordBlock(child, IndentType.ATTACHED, sqlBlockFormattingCtx) |
| 61 | + } |
| 62 | + |
| 63 | + fun createOptionsBlock( |
| 64 | + keywordText: String, |
| 65 | + child: ASTNode, |
| 66 | + lastGroupBlock: SqlBlock?, |
| 67 | + ): SqlBlock { |
| 68 | + // Handle AS keyword for CREATE VIEW |
| 69 | + if (keywordText == "as") { |
| 70 | + val parentCreateBlock = findParentCreateBlock(lastGroupBlock) |
| 71 | + if (parentCreateBlock?.createType == CreateQueryType.VIEW) { |
| 72 | + return SqlCreateViewGroupBlock(child, sqlBlockFormattingCtx) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Handle LATERAL keyword |
| 77 | + if (keywordText == "lateral") { |
| 78 | + return SqlLateralGroupBlock(child, sqlBlockFormattingCtx) |
| 79 | + } |
| 80 | + |
| 81 | + // Handle IN keyword |
| 82 | + if (keywordText == "in") { |
| 83 | + return SqlInGroupBlock(child, sqlBlockFormattingCtx) |
| 84 | + } |
| 85 | + |
| 86 | + // Handle EXISTS/NOT EXISTS keywords |
| 87 | + if (SqlKeywordUtil.isExistsKeyword(keywordText)) { |
| 88 | + return createExistsBlock(keywordText, child, lastGroupBlock) |
| 89 | + } |
| 90 | + |
| 91 | + return SqlKeywordBlock(child, IndentType.OPTIONS, sqlBlockFormattingCtx) |
| 92 | + } |
| 93 | + |
| 94 | + fun createConflictBlock( |
| 95 | + keywordText: String, |
| 96 | + child: ASTNode, |
| 97 | + lastGroupBlock: SqlBlock?, |
| 98 | + ): SqlBlock = |
| 99 | + if (lastGroupBlock is SqlConflictClauseBlock) { |
| 100 | + lastGroupBlock.conflictType = OnConflictKeywordType.of(keywordText) |
| 101 | + SqlKeywordBlock(child, IndentType.CONFLICT, sqlBlockFormattingCtx) |
| 102 | + } else { |
| 103 | + SqlConflictClauseBlock(child, sqlBlockFormattingCtx) |
| 104 | + } |
| 105 | + |
| 106 | + private fun findParentCreateBlock(block: SqlBlock?): SqlCreateKeywordGroupBlock? = |
| 107 | + when (block) { |
| 108 | + is SqlCreateKeywordGroupBlock -> block |
| 109 | + else -> block?.parentBlock as? SqlCreateKeywordGroupBlock |
| 110 | + } |
| 111 | + |
| 112 | + private fun createExistsBlock( |
| 113 | + keywordText: String, |
| 114 | + child: ASTNode, |
| 115 | + lastGroupBlock: SqlBlock?, |
| 116 | + ): SqlBlock { |
| 117 | + // If already in EXISTS group, just return a keyword block |
| 118 | + if (lastGroupBlock is SqlExistsGroupBlock) { |
| 119 | + return SqlKeywordBlock(child, IndentType.OPTIONS, sqlBlockFormattingCtx) |
| 120 | + } |
| 121 | + |
| 122 | + // Check for ELSE condition or NOT keyword outside WHERE/condition context |
| 123 | + val shouldCreateExistsGroup = |
| 124 | + when { |
| 125 | + lastGroupBlock is SqlElConditionLoopCommentBlock && lastGroupBlock.conditionType.isElse() -> true |
| 126 | + keywordText == "not" && !isInConditionContext(lastGroupBlock) -> true |
| 127 | + else -> false |
| 128 | + } |
| 129 | + |
| 130 | + return if (shouldCreateExistsGroup) { |
| 131 | + SqlExistsGroupBlock(child, sqlBlockFormattingCtx) |
| 132 | + } else { |
| 133 | + SqlKeywordBlock(child, IndentType.OPTIONS, sqlBlockFormattingCtx) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private fun isInConditionContext(block: SqlBlock?): Boolean = block is SqlConditionKeywordGroupBlock || block is SqlWhereGroupBlock |
| 138 | +} |
0 commit comments