Skip to content

Commit 6215ac8

Browse files
committed
Fixed indentation generation to ensure injected SQL starts immediately after """, regardless of code style settings.
1 parent 4854769 commit 6215ac8

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
*/
1616
package org.domaframework.doma.intellij.formatter.processor
1717

18+
import com.intellij.application.options.CodeStyle
1819
import com.intellij.lang.injection.InjectedLanguageManager
1920
import com.intellij.openapi.command.WriteCommandAction
2021
import com.intellij.openapi.editor.Document
2122
import com.intellij.openapi.fileTypes.FileTypeManager
2223
import com.intellij.openapi.project.Project
24+
import com.intellij.psi.JavaPsiFacade
2325
import com.intellij.psi.PsiDocumentManager
2426
import com.intellij.psi.PsiFile
2527
import com.intellij.psi.PsiFileFactory
@@ -36,18 +38,24 @@ class InjectionSqlFormatter(
3638
private const val SQL_FILE_EXTENSION = ".sql"
3739
private const val TRIPLE_QUOTE = "\"\"\""
3840
private const val WRITE_COMMAND_NAME = "Format Injected SQL"
39-
private const val BASE_INDENT = "\t\t\t"
4041
private val COMMENT_START_REGEX = Regex("^[ \t]*/[*][ \t]*\\*")
4142
}
4243

44+
private val baseIndent = createSpaceIndent(project)
45+
46+
private fun createSpaceIndent(project: Project): String {
47+
val settings = CodeStyle.getSettings(project)
48+
val java = settings.indentOptions
49+
val indentSize = java.INDENT_SIZE
50+
val prefixLen = "@Sql(\"\"\"".length
51+
return StringUtil.SINGLE_SPACE.repeat(indentSize.plus(prefixLen))
52+
}
53+
4354
private val injectionManager by lazy { InjectedLanguageManager.getInstance(project) }
4455
private val documentManager by lazy { PsiDocumentManager.getInstance(project) }
4556
private val codeStyleManager by lazy { CodeStyleManager.getInstance(project) }
4657
private val fileTypeManager by lazy { FileTypeManager.getInstance() }
47-
private val elementFactory by lazy {
48-
com.intellij.psi.JavaPsiFacade
49-
.getElementFactory(project)
50-
}
58+
private val elementFactory by lazy { JavaPsiFacade.getElementFactory(project) }
5159

5260
fun processFormattingTask(
5361
task: FormattingTask,
@@ -188,10 +196,8 @@ class InjectionSqlFormatter(
188196
when {
189197
line.isBlank() -> line
190198
else ->
191-
BASE_INDENT +
192-
line.removePrefix(
193-
BASE_INDENT,
194-
)
199+
baseIndent +
200+
line
195201
}
196202
}
197203

0 commit comments

Comments
 (0)