fix: NPE for remaining editor completion state (#777)

This commit is contained in:
Carl-Robert Linnupuu 2024-11-27 10:50:13 +00:00
parent 4611a21ac3
commit 0a156bcd68
4 changed files with 7 additions and 11 deletions

View file

@ -14,7 +14,6 @@ import com.intellij.openapi.editor.actionSystem.EditorAction
import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler
import com.intellij.psi.PsiDocumentManager
import com.intellij.util.concurrency.ThreadingAssertions
import ee.carlrobert.codegpt.CodeGPTKeys
import ee.carlrobert.codegpt.CodeGPTKeys.REMAINING_EDITOR_COMPLETION
class CodeCompletionInsertAction :
@ -33,7 +32,7 @@ class CodeCompletionInsertAction :
if (elements.isEmpty()) {
val textToInsert = context.textToInsert()
val remainingCompletion = REMAINING_EDITOR_COMPLETION.get(editor)
val remainingCompletion = REMAINING_EDITOR_COMPLETION.get(editor) ?: ""
if (remainingCompletion.isNotEmpty()) {
REMAINING_EDITOR_COMPLETION.set(editor, remainingCompletion.removePrefix(textToInsert))
}
@ -87,7 +86,7 @@ class CodeCompletionInsertAction :
val endOffset = element.textRange.endOffset
editor.caretModel.moveToOffset(endOffset)
val remainingCompletionLine = REMAINING_EDITOR_COMPLETION.get(editor)
val remainingCompletionLine = (REMAINING_EDITOR_COMPLETION.get(editor) ?: "")
.removePrefix(element.text)
processRemainingCompletion(remainingCompletionLine, editor, endOffset)
@ -98,7 +97,7 @@ class CodeCompletionInsertAction :
val lineEndOffset = editor.document.getLineEndOffset(lineNumber)
editor.caretModel.moveToOffset(lineEndOffset)
val remainingText = REMAINING_EDITOR_COMPLETION.get(editor)
val remainingText = REMAINING_EDITOR_COMPLETION.get(editor) ?: ""
val remainingCompletionLine = if (element.originalText.length > remainingText.length) {
remainingText.removePrefix(element.text)
} else {

View file

@ -14,7 +14,7 @@ class CodeCompletionInsertHandler : InlineCompletionInsertHandler {
elements: List<InlineCompletionElement>
) {
val editor = environment.editor
val remainingCompletion = REMAINING_EDITOR_COMPLETION.get(editor)
val remainingCompletion = REMAINING_EDITOR_COMPLETION.get(editor) ?: ""
if (remainingCompletion.isNotEmpty()) {
InlineCompletion.getHandlerOrNull(editor)?.invoke(
InlineCompletionEvent.DirectCall(editor, editor.caretModel.currentCaret)

View file

@ -44,7 +44,7 @@ class CodeCompletionSuggestionUpdateAdapter :
}
private fun updateRemainingCompletion(editor: Editor, textToInsert: String) {
val remainingCompletion = REMAINING_EDITOR_COMPLETION.get(editor)
val remainingCompletion = REMAINING_EDITOR_COMPLETION.get(editor) ?: ""
if (remainingCompletion.isNotEmpty()) {
REMAINING_EDITOR_COMPLETION.set(editor, remainingCompletion.removePrefix(textToInsert))
}

View file

@ -54,11 +54,8 @@ class DebouncedCodeCompletionProvider : DebouncedInlineCompletionProvider() {
override suspend fun getSuggestionDebounced(request: InlineCompletionRequest): InlineCompletionSuggestion {
val editor = request.editor
val remainingCompletion = REMAINING_EDITOR_COMPLETION.get(editor)
if (request.event is InlineCompletionEvent.DirectCall
&& remainingCompletion != null
&& remainingCompletion.isNotEmpty()
) {
val remainingCompletion = REMAINING_EDITOR_COMPLETION.get(editor) ?: ""
if (request.event is InlineCompletionEvent.DirectCall && remainingCompletion.isNotEmpty()) {
return sendNextSuggestion(remainingCompletion.extractUntilNewline(), request)
}