Skip to content

Commit 255870a

Browse files
committed
refactor(code): move insertStringAndSaveChange to InsertUtil
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.
1 parent dd38fb8 commit 255870a

File tree

4 files changed

+6
-50
lines changed

4 files changed

+6
-50
lines changed

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/AutoDevInput.kt

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cc.unitmesh.devti.gui.chat.ui
22

33
import cc.unitmesh.devti.settings.LanguageChangedCallback.placeholder
4+
import cc.unitmesh.devti.util.InsertUtil
45
import cc.unitmesh.devti.util.parser.CodeFence.Companion.findLanguage
56
import com.intellij.openapi.Disposable
67
import com.intellij.openapi.actionSystem.*
@@ -22,8 +23,6 @@ import com.intellij.openapi.project.guessProjectDir
2223
import com.intellij.openapi.util.TextRange
2324
import com.intellij.openapi.util.io.FileUtil
2425
import com.intellij.openapi.vfs.VirtualFile
25-
import com.intellij.psi.PsiDocumentManager
26-
import com.intellij.psi.codeStyle.CodeStyleManager
2726
import com.intellij.temporary.gui.block.findDocument
2827
import com.intellij.testFramework.LightVirtualFile
2928
import com.intellij.ui.EditorTextField
@@ -165,32 +164,11 @@ class AutoDevInput(
165164
"intentions.write.action",
166165
{
167166
val document = this.editor?.document ?: return@runWriteCommandAction
168-
insertStringAndSaveChange(project, text, document, document.textLength, false)
167+
InsertUtil.insertStringAndSaveChange(project, text, document, document.textLength, false)
169168
})
170169
}
171170
}
172171

173-
fun insertStringAndSaveChange(
174-
project: Project,
175-
content: String,
176-
document: Document,
177-
startOffset: Int,
178-
withReformat: Boolean,
179-
) {
180-
if (startOffset < 0 || startOffset > document.textLength) return
181-
182-
document.insertString(startOffset, content)
183-
PsiDocumentManager.getInstance(project).commitDocument(document)
184-
185-
if (!withReformat) return
186-
187-
val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document)
188-
psiFile?.let { file ->
189-
val reformatRange = TextRange(startOffset, startOffset + content.length)
190-
CodeStyleManager.getInstance(project).reformatText(file, listOf(reformatRange))
191-
}
192-
}
193-
194172
fun VirtualFile.relativePath(project: Project): String {
195173
if (this is LightVirtualFile) return ""
196174
try {

core/src/main/kotlin/cc/unitmesh/devti/intentions/action/task/CodeCompletionTask.kt

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@ import cc.unitmesh.devti.util.AutoDevCoroutineScope
77
import cc.unitmesh.devti.intentions.action.CodeCompletionBaseIntention
88
import cc.unitmesh.devti.statusbar.AutoDevStatus
99
import cc.unitmesh.devti.statusbar.AutoDevStatusService
10+
import cc.unitmesh.devti.util.InsertUtil
1011
import com.intellij.lang.LanguageCommenters
1112
import com.intellij.openapi.application.invokeLater
1213
import com.intellij.openapi.command.WriteCommandAction
1314
import com.intellij.openapi.diagnostic.logger
14-
import com.intellij.openapi.editor.Document
1515
import com.intellij.openapi.editor.ScrollType
1616
import com.intellij.openapi.progress.ProgressIndicator
1717
import com.intellij.openapi.progress.Task
18-
import com.intellij.openapi.project.Project
19-
import com.intellij.openapi.util.TextRange
20-
import com.intellij.psi.PsiDocumentManager
21-
import com.intellij.psi.codeStyle.CodeStyleManager
2218
import kotlinx.coroutines.flow.Flow
2319
import kotlinx.coroutines.launch
2420
import java.util.function.Consumer
@@ -52,7 +48,7 @@ class CodeCompletionTask(private val request: CodeCompletionRequest) :
5248
suggestion.append(it)
5349
invokeLater {
5450
WriteCommandAction.runWriteCommandAction(project, codeMessage, writeActionGroupId, {
55-
insertStringAndSaveChange(project, it, editor.document, currentOffset.element, false)
51+
InsertUtil.insertStringAndSaveChange(project, it, editor.document, currentOffset.element, false)
5652
})
5753

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

109105
companion object {
110106
private val logger = logger<CodeCompletionBaseIntention>()
111-
112-
fun insertStringAndSaveChange(
113-
project: Project,
114-
suggestion: String,
115-
document: Document,
116-
startOffset: Int,
117-
withReformat: Boolean
118-
) {
119-
document.insertString(startOffset, suggestion)
120-
PsiDocumentManager.getInstance(project).commitDocument(document)
121-
122-
if (!withReformat) return
123-
124-
val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document)
125-
psiFile?.let { file ->
126-
val reformatRange = TextRange(startOffset, startOffset + suggestion.length)
127-
CodeStyleManager.getInstance(project).reformatText(file, listOf(reformatRange))
128-
}
129-
}
130107
}
131108
}

core/src/main/kotlin/cc/unitmesh/devti/sketch/run/ShellUtil.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package cc.unitmesh.devti.sketch.run
44
import com.intellij.execution.configuration.EnvironmentVariablesData
55
import com.intellij.execution.configurations.PathEnvironmentVariableUtil
66
import com.intellij.execution.wsl.WSLDistribution
7-
import com.intellij.execution.wsl.WSLUtil
87
import com.intellij.execution.wsl.WslDistributionManager
98
import com.intellij.openapi.util.SystemInfo
109
import com.intellij.openapi.util.text.StringUtil

core/src/main/kotlin/cc/unitmesh/devti/util/InsertUtil.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ object InsertUtil {
2020
startOffset: Int,
2121
withReformat: Boolean,
2222
) {
23+
if (startOffset < 0 || startOffset > document.textLength) return
24+
2325
document.insertString(startOffset, content)
2426
PsiDocumentManager.getInstance(project).commitDocument(document)
2527

0 commit comments

Comments
 (0)