Skip to content

Commit

Permalink
feat(EditorInteractionProvider): enhance task creation and error hand…
Browse files Browse the repository at this point in the history
…ling #29

The code has been updated to improve task creation and error handling in the EditorInteractionProvider. The changes include adding a new parameter 'isReplacement' to the 'createTask' function and moving the null check for 'context.editor' inside the function. Additionally, the handling for 'AppendCursor' and 'AppendCursorStream' interaction types has been updated to create a task and run it asynchronously.
  • Loading branch information
phodal committed Jul 1, 2024
1 parent 34eb6fe commit e0f34a3
Showing 1 changed file with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,20 @@ class EditorInteractionProvider : LocationInteractionProvider {
val result: String = ""

Check warning on line 24 in core/src/main/kotlin/com/phodal/shirecore/provider/impl/EditorInteractionProvider.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Obvious explicit type

Explicitly given type is redundant here

when (context.interactionType) {
InteractionType.AppendCursor -> TODO()
InteractionType.AppendCursorStream -> TODO()
InteractionType.AppendCursor,
InteractionType.AppendCursorStream,
-> {
val task = createTask(context, msgs, isReplacement = false)

if (task == null) {
ShirelangNotifications.error(context.project, "Failed to create code completion task.")
return ""
}

ProgressManager.getInstance()
.runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator(task))
}

InteractionType.OutputFile -> {
val fileName = targetFile?.name
val task = FileGenerateTask(context.project, msgs, fileName)
Expand All @@ -34,12 +46,7 @@ class EditorInteractionProvider : LocationInteractionProvider {
}

InteractionType.ReplaceSelection -> {
if (context.editor == null) {
ShirelangNotifications.error(context.project, "Editor is null, please open a file to continue.")
return ""
}

val task = createTask(context.editor, context, msgs)
val task = createTask(context, msgs, true)

if (task == null) {
ShirelangNotifications.error(context.project, "Failed to create code completion task.")
Expand Down Expand Up @@ -67,16 +74,23 @@ class EditorInteractionProvider : LocationInteractionProvider {
}

private fun createTask(
editor: Editor,
context: LocationInteractionContext,
msgs: List<ChatMessage>,
isReplacement: Boolean,
): CodeCompletionTask? {
if (context.editor == null) {
ShirelangNotifications.error(context.project, "Editor is null, please open a file to continue.")
return null
}

val editor = context.editor

val offset = editor.caretModel.offset
val element = SelectElementStrategy.resolvePsiElement(context.project, editor)
val userPrompt = msgs.filter { it.role == ChatRole.User }.joinToString("\n") { it.content }

val request = runReadAction {
CodeCompletionRequest.create(editor, offset, element, null, userPrompt, isReplacement = true)
CodeCompletionRequest.create(editor, offset, element, null, userPrompt, isReplacement = isReplacement)
} ?: return null

val task = CodeCompletionTask(request)
Expand Down

0 comments on commit e0f34a3

Please sign in to comment.