Skip to content

Commit f1d8023

Browse files
committed
Implement line break formatting rule for FROM following WHERE DISTINCT.
1 parent 06e32c9 commit f1d8023

File tree

6 files changed

+45
-15
lines changed

6 files changed

+45
-15
lines changed

src/main/java/org/domaframework/doma/intellij/Sql.flex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import org.domaframework.doma.intellij.psi.SqlTypes;
3737
"conflict",
3838
"constraint",
3939
"column",
40+
"collate",
4041
"comment",
4142
"create",
4243
"cross",
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.domaframework.doma.intellij.formatter.block.group.keyword
16+
package org.domaframework.doma.intellij.formatter.block.group.keyword.second
1717

1818
import com.intellij.lang.ASTNode
1919
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.group.keyword.SqlKeywordGroupBlock
2022
import org.domaframework.doma.intellij.formatter.util.IndentType
2123
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2224
import org.domaframework.doma.intellij.formatter.util.SqlKeywordUtil
@@ -46,8 +48,12 @@ open class SqlSecondKeywordBlock(
4648
}
4749

4850
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
49-
lastGroup?.let {
50-
return !SqlKeywordUtil.isSetLineKeyword(getNodeText(), lastGroup.getNodeText())
51+
lastGroup?.let { last ->
52+
val prevKeyword = last.childBlocks.dropLast(1).findLast { it is SqlKeywordBlock }
53+
prevKeyword?.let { prev ->
54+
return !SqlKeywordUtil.isSetLineKeyword(getNodeText(), prev.getNodeText())
55+
}
56+
return !SqlKeywordUtil.isSetLineKeyword(getNodeText(), last.getNodeText())
5157
}
5258
return true
5359
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/processor/SqlSetParentGroupProcessor.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ class SqlSetParentGroupProcessor(
155155
return@setParentGroups lastGroupBlock
156156
}
157157
} else if (lastIndentLevel == currentIndentLevel) {
158+
val prevKeyword = lastGroupBlock.childBlocks.findLast { it is SqlKeywordBlock }
159+
prevKeyword?.let { prev ->
160+
if (SqlKeywordUtil.isSetLineKeyword(childBlock.getNodeText(), prev.getNodeText())) {
161+
updateGroupBlockLastGroupParentAddGroup(
162+
lastGroupBlock,
163+
childBlock,
164+
)
165+
return
166+
}
167+
}
168+
158169
blockBuilder.removeLastGroupTopNodeIndexHistory()
159170
updateGroupBlockLastGroupParentAddGroup(
160171
lastGroupBlock,

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlKeywordUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class SqlKeywordUtil {
296296
private val SET_LINE_KEYWORDS =
297297
mapOf(
298298
"into" to setOf("insert"),
299-
"from" to setOf("delete"),
299+
"from" to setOf("delete", "distinct"),
300300
"distinct" to setOf("select"),
301301
"table" to setOf("create", "alter", "rename", "truncate", "drop"),
302302
"index" to setOf("create", "alter", "rename", "truncate", "drop"),
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
insert into users (username, email)
2-
values ('user', '[email protected]')
3-
on CONFLICT(username) ON constraint do update set email = EXCLUDED.email, created_at = CURRENT_TIMESTAMP
1+
insert into employees (name, manager_id)
2+
SELECT name, user_id
3+
FROM user_settings
4+
WHERE user_id = /*employee.id*/0 AND name = /*employee.name*/'name'
5+
on conflict (id) do
6+
update set name = EXCLUDED.name
7+
WHERE employees.name is Distinct from EXCLUDED.name
8+
returning id, manager_id, name
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
INSERT INTO users
2-
(username
3-
, email)
4-
VALUES ('user'
5-
6-
ON CONFLICT (username) ON CONSTRAINT
1+
INSERT INTO employees
2+
(name
3+
, manager_id)
4+
SELECT name
5+
, user_id
6+
FROM user_settings
7+
WHERE user_id = /*employee.id*/0
8+
AND name = /*employee.name*/'name'
9+
ON CONFLICT (id)
710
DO UPDATE
8-
SET email = EXCLUDED.email
9-
, created_at = CURRENT_TIMESTAMP
11+
SET name = EXCLUDED.name
12+
WHERE employees.name IS DISTINCT
13+
FROM EXCLUDED.name
14+
RETURNING id
15+
, manager_id
16+
, name

0 commit comments

Comments
 (0)