Skip to content

Commit d61e083

Browse files
committed
fix(editor): add error handling for text updates and dependencies
- Wrap `document.replaceString` in a try-catch block to log errors. - Add error handling for dependency processing to complete the future exceptionally on failure.
1 parent 5f34372 commit d61e083

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/code/CodeHighlightSketch.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.intellij.openapi.actionSystem.DataProvider
1414
import com.intellij.openapi.application.ApplicationManager
1515
import com.intellij.openapi.application.ReadAction
1616
import com.intellij.openapi.command.WriteCommandAction
17+
import com.intellij.openapi.diagnostic.logger
1718
import com.intellij.openapi.editor.Document
1819
import com.intellij.openapi.editor.Editor
1920
import com.intellij.openapi.editor.EditorFactory
@@ -143,7 +144,11 @@ open class CodeHighlightSketch(
143144

144145
val document = editorFragment?.editor?.document
145146
val normalizedText = StringUtil.convertLineSeparators(text)
146-
document?.replaceString(0, document.textLength, normalizedText)
147+
try {
148+
document?.replaceString(0, document.textLength, normalizedText)
149+
} catch (e: Throwable) {
150+
logger<CodeHighlightSketch>().error("Error updating editor text", e)
151+
}
147152

148153
val lineCount = document?.lineCount ?: 0
149154
if (lineCount > editorLineThreshold) {

exts/ext-dependencies/src/233/main/kotlin/cc/unitmesh/dependencies/DependenciesFunctionProvider.kt

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,26 @@ class DependenciesFunctionProvider : ToolchainFunctionProvider {
2828
val future = CompletableFuture<String>()
2929
val task = object : Task.Backgroundable(project, "Processing context", false) {
3030
override fun run(indicator: ProgressIndicator) {
31-
val deps: List<Package> = runReadAction {
32-
ProjectDependenciesModel.supportedModels(project).map {
33-
modules.map { module ->
34-
it.declaredDependencies(module)
35-
}.flatten()
36-
}.flatten().map {
37-
it.pkg
38-
}
39-
}
40-
41-
val result = "Here is the project dependencies:\n```\n" + deps.joinToString("") {
42-
val namespace = it.namespace ?: ""
43-
"$namespace ${it.name} ${it.version}" + "\n"
44-
} + "```"
45-
46-
future.complete(result)
31+
try {
32+
val deps: List<Package> = runReadAction {
33+
ProjectDependenciesModel.supportedModels(project).map {
34+
modules.map { module ->
35+
it.declaredDependencies(module)
36+
}.flatten()
37+
}.flatten().map {
38+
it.pkg
39+
}
40+
}
41+
42+
val result = "Here is the project dependencies:\n```\n" + deps.joinToString("") {
43+
val namespace = it.namespace ?: ""
44+
"$namespace ${it.name} ${it.version}" + "\n"
45+
} + "```"
46+
47+
future.complete(result)
48+
} catch (e: Exception) {
49+
future.completeExceptionally(e)
50+
}
4751
}
4852
}
4953

0 commit comments

Comments
 (0)