Skip to content

Commit 1fa7803

Browse files
committed
Fix to pass the actual SQL file so that the formatting PostProcessor is applied to SQL generated by the action.
1 parent 7a6ab1e commit 1fa7803

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/main/kotlin/org/domaframework/doma/intellij/action/dao/SqlAnnotationConverter.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ class SqlAnnotationConverter(
186186
// Add at the beginning
187187
modifierList.add(sqlAnnotation)
188188
}
189-
// PSI変更をコミットしてロック解除
190189
val psiFile = method.containingFile
191190
val document = PsiDocumentManager.getInstance(project).getDocument(psiFile)
192191
if (document != null) {
@@ -241,11 +240,13 @@ class SqlAnnotationConverter(
241240
val newPsiDaoMethod = PsiDaoMethod(project, method)
242241
val sqlFile = newPsiDaoMethod.sqlFile ?: return@runWriteCommandAction
243242
val psiFile = project.findFile(sqlFile) ?: return@runWriteCommandAction
244-
val document = PsiDocumentManager.getInstance(project).getDocument(psiFile) ?: return@runWriteCommandAction
243+
244+
val documentManager = PsiDocumentManager.getInstance(project)
245+
val document = documentManager.getDocument(psiFile) ?: return@runWriteCommandAction
245246

246247
// Replace the default content with the actual SQL content
247248
document.setText(content)
248-
PsiDocumentManager.getInstance(project).commitDocument(document)
249+
documentManager.commitDocument(document)
249250

250251
// Format Sql
251252
formatSql(psiFile)

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package org.domaframework.doma.intellij.formatter.processor
1717

1818
import com.intellij.lang.injection.InjectedLanguageManager
1919
import com.intellij.openapi.command.WriteCommandAction
20+
import com.intellij.openapi.editor.Document
2021
import com.intellij.openapi.fileTypes.FileTypeManager
2122
import com.intellij.openapi.project.Project
2223
import com.intellij.psi.PsiDocumentManager
@@ -76,10 +77,8 @@ class InjectionSqlFormatter(
7677
removeSpace: (String) -> String,
7778
) {
7879
val result = SqlFormatPreProcessor().updateDocument(sqlFile, sqlFile.textRange)
79-
val formattedText = result.document?.text ?: return
80-
81-
val tmpFormatted = formatAsTemporarySqlFile(formattedText)
82-
val document = documentManager.getDocument(sqlFile) ?: return
80+
val document = result.document ?: return
81+
val tmpFormatted = formatAsTemporarySqlFile(document)
8382
document.replaceString(
8483
sqlFile.textRange.startOffset,
8584
sqlFile.textRange.endOffset,
@@ -105,6 +104,23 @@ class InjectionSqlFormatter(
105104
* Formats SQL text by creating a temporary SQL file and applying code style.
106105
* Returns original text if formatting fails.
107106
*/
107+
private fun formatAsTemporarySqlFile(document: Document): String =
108+
runCatching {
109+
val tempFileName = "${TEMP_FILE_PREFIX}${SQL_FILE_EXTENSION}"
110+
val fileType = fileTypeManager.getFileTypeByExtension("sql")
111+
val tempSqlFile =
112+
PsiFileFactory
113+
.getInstance(project)
114+
.createFileFromText(tempFileName, fileType, document.text)
115+
116+
document.setText(tempSqlFile.text)
117+
documentManager.commitDocument(document)
118+
val psiFile = documentManager.getPsiFile(document) ?: return@runCatching tempSqlFile.text
119+
120+
codeStyleManager.reformatText(psiFile, 0, tempSqlFile.textLength)
121+
psiFile.text
122+
}.getOrDefault(document.text)
123+
108124
private fun formatAsTemporarySqlFile(sqlText: String): String =
109125
runCatching {
110126
val tempFileName = "${TEMP_FILE_PREFIX}${SQL_FILE_EXTENSION}"

0 commit comments

Comments
 (0)