1515 */
1616package org.domaframework.doma.intellij.formatter.processor
1717
18+ import com.intellij.application.options.CodeStyle
1819import com.intellij.lang.injection.InjectedLanguageManager
1920import com.intellij.openapi.command.WriteCommandAction
2021import com.intellij.openapi.editor.Document
2122import com.intellij.openapi.fileTypes.FileTypeManager
2223import com.intellij.openapi.project.Project
24+ import com.intellij.psi.JavaPsiFacade
2325import com.intellij.psi.PsiDocumentManager
2426import com.intellij.psi.PsiFile
2527import 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