Skip to content

Commit

Permalink
fix: edit code popover loading state
Browse files Browse the repository at this point in the history
  • Loading branch information
carlrobertoh committed Jun 30, 2024
1 parent fbf88bc commit 42eb7a3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class EditCodeCompletionListener(
}

override fun onError(error: ErrorDetails, ex: Throwable) {
observableProperties.loading.set(false)
OverlayUtil.showNotification(
"Something went wrong while requesting completion. Please try again.",
NotificationType.ERROR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,28 @@ class EditCodeSubmissionHandler(
private val previousSourceRef = AtomicReference<String?>(null)

suspend fun handleSubmit(userPrompt: String) {
try {
observableProperties.loading.set(true)
observableProperties.submitted.set(true)
observableProperties.loading.set(true)
observableProperties.submitted.set(true)

previousSourceRef.getAndSet(editor.document.text)
val (selectionTextRange, selectedText) = readAction {
editor.selectionModel.run {
Pair(
TextRange(selectionStart, selectionEnd),
editor.selectionModel.selectedText ?: ""
)
}
previousSourceRef.getAndSet(editor.document.text)
val (selectionTextRange, selectedText) = readAction {
editor.selectionModel.run {
Pair(
TextRange(selectionStart, selectionEnd),
editor.selectionModel.selectedText ?: ""
)
}
runInEdt { editor.selectionModel.removeSelection() }

// TODO: Support other providers
CompletionClientProvider.getCodeGPTClient().getChatCompletionAsync(
CompletionRequestProvider.buildEditCodeRequest(
"$userPrompt\n\n$selectedText",
service<CodeGPTServiceSettings>().state.chatCompletionSettings.model
),
EditCodeCompletionListener(editor, observableProperties, selectionTextRange)
)
} finally {
observableProperties.loading.set(false)
}
runInEdt { editor.selectionModel.removeSelection() }

// TODO: Support other providers
CompletionClientProvider.getCodeGPTClient().getChatCompletionAsync(
CompletionRequestProvider.buildEditCodeRequest(
"$userPrompt\n\n$selectedText",
service<CodeGPTServiceSettings>().state.chatCompletionSettings.model
),
EditCodeCompletionListener(editor, observableProperties, selectionTextRange)
)
}

fun handleAccept() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class EditCodePopover(private val editor: Editor) {
popup.cancel()
}
.visibleIf(observableProperties.submitted)
.enabledIf(observableProperties.loading.not())
cell(AsyncProcessIcon("edit_code_spinner")).visibleIf(observableProperties.loading)
link(CodeGPTBundle.get("shared.discard")) {
submissionHandler.handleReject()
Expand Down

0 comments on commit 42eb7a3

Please sign in to comment.