@@ -19,14 +19,27 @@ import com.intellij.lang.injection.InjectedLanguageManager
1919import com.intellij.openapi.util.TextRange
2020import com.intellij.psi.PsiElement
2121import com.intellij.psi.PsiFile
22- import com.intellij.psi.PsiLiteralExpression
2322import com.intellij.psi.codeStyle.CodeStyleSettings
2423import org.domaframework.doma.intellij.common.dao.getDaoClass
2524import org.domaframework.doma.intellij.common.isJavaOrKotlinFileType
2625import org.domaframework.doma.intellij.common.isSupportFileType
26+ import org.domaframework.doma.intellij.common.util.InjectionSqlUtil
2727import org.domaframework.doma.intellij.formatter.visitor.DaoInjectionSqlVisitor
2828import org.domaframework.doma.intellij.formatter.visitor.FormattingTask
2929
30+ /* *
31+ * Post-processor for SQL injection formatting.
32+ *
33+ * This processor handles SQL formatting in two contexts:
34+ * 1. **File formatting**: When formatting entire DAO files (Java/Kotlin) containing SQL annotations
35+ * 2. **Code formatting**: When formatting injected SQL fragments within string literals
36+ *
37+ * The context is determined by checking:
38+ * - If the source is an injected fragment (`InjectedLanguageManager.isInjectedFragment()` returns true),
39+ * it's being called from code formatting for a specific SQL string literal
40+ * - If the source is a regular DAO file (Java/Kotlin with @Dao annotation),
41+ * it's being called from file formatting to process all SQL strings in the file
42+ */
3043class SqlInjectionPostProcessor : SqlPostProcessor () {
3144 override fun processElement (
3245 element : PsiElement ,
@@ -38,49 +51,53 @@ class SqlInjectionPostProcessor : SqlPostProcessor() {
3851 rangeToReformat : TextRange ,
3952 settings : CodeStyleSettings ,
4053 ): TextRange {
41- if (! shouldProcessFile (source)) {
54+ if (! isProcessFile (source)) {
4255 return rangeToReformat
4356 }
4457
4558 val manager = InjectedLanguageManager .getInstance(source.project)
4659 if (manager.isInjectedFragment(source)) {
47- processInjectedFragment(source, manager )
60+ processInjectedFragment(source)
4861 } else {
4962 processRegularFile(source)
5063 }
5164
5265 return rangeToReformat
5366 }
5467
55- private fun shouldProcessFile (source : PsiFile ): Boolean {
68+ private fun isProcessFile (source : PsiFile ): Boolean {
5669 val manager = InjectedLanguageManager .getInstance(source.project)
5770 val isInjectedSql = if (isSupportFileType(source)) manager.isInjectedFragment(source) else false
5871 val isDaoFile = isJavaOrKotlinFileType(source) && getDaoClass(source) != null
5972
6073 return isInjectedSql || isDaoFile
6174 }
6275
63- private fun processInjectedFragment (
64- source : PsiFile ,
65- manager : InjectedLanguageManager ,
66- ) {
67- val host = manager.getInjectionHost(source) as ? PsiLiteralExpression ? : return
76+ /* *
77+ * Processes all SQL injections in a DAO file during file formatting.
78+ * This is called when formatting an entire DAO file containing SQL annotations.
79+ */
80+ private fun processInjectedFragment (source : PsiFile ) {
81+ val host = InjectionSqlUtil .getLiteralExpressionHost(source) ? : return
6882 val originalText = host.value?.toString() ? : return
6983
7084 val visitor = DaoInjectionSqlVisitor (source.project)
71- val formattingTask = FormattingTask (host, originalText)
85+ val formattingTask = FormattingTask (host, originalText, host.isTextBlock )
7286
7387 visitor.convertExpressionToTextBlock(formattingTask.expression)
7488 visitor.processFormattingTask(formattingTask) { text ->
7589 processDocumentText(text)
7690 }
7791 }
7892
93+ /* *
94+ * Processes injected SQL fragments during code formatting.
95+ * This is called when formatting a specific SQL string literal within a DAO file.
96+ */
7997 private fun processRegularFile (source : PsiFile ) {
8098 val visitor = DaoInjectionSqlVisitor (source.project)
8199 source.accept(visitor)
82100 visitor.processAllTextBlock()
83- visitor.initFormattingTasks()
84101 source.accept(visitor)
85102 visitor.processAllReFormat { text ->
86103 processDocumentText(text)
0 commit comments