Skip to content

Commit

Permalink
refactor(code): move insertStringAndSaveChange to InsertUtil
Browse files Browse the repository at this point in the history
Consolidate the `insertStringAndSaveChange` function into `InsertUtil` to reduce code duplication and improve maintainability. This change involves removing the function from multiple files and ensuring it is properly imported and used where needed. Additionally, a validation check for `startOffset` was added to prevent out-of-bounds errors.
  • Loading branch information
phodal committed Feb 11, 2025
1 parent dd38fb8 commit 255870a
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 50 deletions.
26 changes: 2 additions & 24 deletions core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/AutoDevInput.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cc.unitmesh.devti.gui.chat.ui

import cc.unitmesh.devti.settings.LanguageChangedCallback.placeholder
import cc.unitmesh.devti.util.InsertUtil
import cc.unitmesh.devti.util.parser.CodeFence.Companion.findLanguage
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.*
Expand All @@ -22,8 +23,6 @@ import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.codeStyle.CodeStyleManager
import com.intellij.temporary.gui.block.findDocument
import com.intellij.testFramework.LightVirtualFile
import com.intellij.ui.EditorTextField
Expand Down Expand Up @@ -165,32 +164,11 @@ class AutoDevInput(
"intentions.write.action",
{
val document = this.editor?.document ?: return@runWriteCommandAction
insertStringAndSaveChange(project, text, document, document.textLength, false)
InsertUtil.insertStringAndSaveChange(project, text, document, document.textLength, false)
})
}
}

fun insertStringAndSaveChange(
project: Project,
content: String,
document: Document,
startOffset: Int,
withReformat: Boolean,
) {
if (startOffset < 0 || startOffset > document.textLength) return

document.insertString(startOffset, content)
PsiDocumentManager.getInstance(project).commitDocument(document)

if (!withReformat) return

val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document)
psiFile?.let { file ->
val reformatRange = TextRange(startOffset, startOffset + content.length)
CodeStyleManager.getInstance(project).reformatText(file, listOf(reformatRange))
}
}

fun VirtualFile.relativePath(project: Project): String {
if (this is LightVirtualFile) return ""
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ import cc.unitmesh.devti.util.AutoDevCoroutineScope
import cc.unitmesh.devti.intentions.action.CodeCompletionBaseIntention
import cc.unitmesh.devti.statusbar.AutoDevStatus
import cc.unitmesh.devti.statusbar.AutoDevStatusService
import cc.unitmesh.devti.util.InsertUtil
import com.intellij.lang.LanguageCommenters
import com.intellij.openapi.application.invokeLater
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.ScrollType
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.Task
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.codeStyle.CodeStyleManager
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import java.util.function.Consumer
Expand Down Expand Up @@ -52,7 +48,7 @@ class CodeCompletionTask(private val request: CodeCompletionRequest) :
suggestion.append(it)
invokeLater {
WriteCommandAction.runWriteCommandAction(project, codeMessage, writeActionGroupId, {
insertStringAndSaveChange(project, it, editor.document, currentOffset.element, false)
InsertUtil.insertStringAndSaveChange(project, it, editor.document, currentOffset.element, false)
})

currentOffset.element += it.length
Expand Down Expand Up @@ -108,24 +104,5 @@ class CodeCompletionTask(private val request: CodeCompletionRequest) :

companion object {
private val logger = logger<CodeCompletionBaseIntention>()

fun insertStringAndSaveChange(
project: Project,
suggestion: String,
document: Document,
startOffset: Int,
withReformat: Boolean
) {
document.insertString(startOffset, suggestion)
PsiDocumentManager.getInstance(project).commitDocument(document)

if (!withReformat) return

val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document)
psiFile?.let { file ->
val reformatRange = TextRange(startOffset, startOffset + suggestion.length)
CodeStyleManager.getInstance(project).reformatText(file, listOf(reformatRange))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package cc.unitmesh.devti.sketch.run
import com.intellij.execution.configuration.EnvironmentVariablesData
import com.intellij.execution.configurations.PathEnvironmentVariableUtil
import com.intellij.execution.wsl.WSLDistribution
import com.intellij.execution.wsl.WSLUtil
import com.intellij.execution.wsl.WslDistributionManager
import com.intellij.openapi.util.SystemInfo
import com.intellij.openapi.util.text.StringUtil
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/kotlin/cc/unitmesh/devti/util/InsertUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ object InsertUtil {
startOffset: Int,
withReformat: Boolean,
) {
if (startOffset < 0 || startOffset > document.textLength) return

document.insertString(startOffset, content)
PsiDocumentManager.getInstance(project).commitDocument(document)

Expand Down

0 comments on commit 255870a

Please sign in to comment.