Skip to content

Commit

Permalink
🐛 Fix editor indent after apply
Browse files Browse the repository at this point in the history
  • Loading branch information
lauvsong committed Jun 18, 2023
1 parent b7010d7 commit aa4dd41
Showing 1 changed file with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ import com.intellij.ui.components.JBLoadingPanel
import com.intellij.ui.components.JBScrollPane
import com.lauvsong.refactorgpt.dto.Refactored
import com.lauvsong.refactorgpt.service.ChatGptService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.awt.BorderLayout
import java.awt.Dimension
import javax.swing.Action
import javax.swing.BorderFactory
import javax.swing.JButton
import javax.swing.JComponent
import javax.swing.JPanel
import javax.swing.JSplitPane
import javax.swing.SwingUtilities

class RefactorGptDialog(
private val editor: Editor,
Expand All @@ -36,12 +40,10 @@ class RefactorGptDialog(
private val loadingPanel = JBLoadingPanel(BorderLayout(), disposable)
private val applyButton = JButton("Apply")
private val editorFactory: EditorFactory = EditorFactory.getInstance()
private val originalCodeEditor: EditorEx by lazy {
editorFactory.createViewer(editorFactory.createDocument(selectedCode), project) as EditorEx
}
private val refactoredCodeEditor: EditorEx by lazy {
private val originalCodeEditor: EditorEx =
editorFactory.createViewer(editorFactory.createDocument(selectedCode.trimIndent()), project) as EditorEx
private val refactoredCodeEditor: EditorEx =
editorFactory.createEditor(editorFactory.createDocument(""), project) as EditorEx
}
private val chatGptService = ChatGptService()
private val fileExtension = FileDocumentManager.getInstance().getFile(editor.document)?.extension ?: "txt"

Expand Down Expand Up @@ -88,6 +90,7 @@ class RefactorGptDialog(
isContinuousLayout = true
dividerSize = 15
}
SwingUtilities.invokeLater { splitPane.setDividerLocation(0.5) }

val contentPanel = JPanel(BorderLayout())
contentPanel.border = BorderFactory.createEmptyBorder(10, 10, 10, 10)
Expand Down Expand Up @@ -187,12 +190,19 @@ class RefactorGptDialog(
val endOffset = selectionModel.selectionEnd

replaceString(startOffset, endOffset, refactoredCode)

PsiDocumentManager.getInstance(project).commitDocument(this)

PsiDocumentManager.getInstance(project).getPsiFile(this)
?.let { file ->
CodeStyleManager.getInstance(project).reformatText(file, startOffset, endOffset)
}
}
}
}

private fun adjustIndentation(refactoredCode: String): String {
val document = editorFactory.createDocument(refactoredCode)
val document = editorFactory.createDocument(refactoredCode.trimIndent())
val file = PsiDocumentManager.getInstance(project).getPsiFile(document)

if (file != null) {
Expand Down

0 comments on commit aa4dd41

Please sign in to comment.