From 0a156bcd682d2312f3a85c683e69064591bddf04 Mon Sep 17 00:00:00 2001 From: Carl-Robert Linnupuu Date: Wed, 27 Nov 2024 10:50:13 +0000 Subject: [PATCH] fix: NPE for remaining editor completion state (#777) --- .../codegpt/codecompletions/CodeCompletionInsertAction.kt | 7 +++---- .../codegpt/codecompletions/CodeCompletionInsertHandler.kt | 2 +- .../CodeCompletionSuggestionUpdateAdapter.kt | 2 +- .../codecompletions/DebouncedCodeCompletionProvider.kt | 7 ++----- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/ee/carlrobert/codegpt/codecompletions/CodeCompletionInsertAction.kt b/src/main/kotlin/ee/carlrobert/codegpt/codecompletions/CodeCompletionInsertAction.kt index ebc289c4..07a36fed 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/codecompletions/CodeCompletionInsertAction.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/codecompletions/CodeCompletionInsertAction.kt @@ -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 { diff --git a/src/main/kotlin/ee/carlrobert/codegpt/codecompletions/CodeCompletionInsertHandler.kt b/src/main/kotlin/ee/carlrobert/codegpt/codecompletions/CodeCompletionInsertHandler.kt index e1756723..4070c051 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/codecompletions/CodeCompletionInsertHandler.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/codecompletions/CodeCompletionInsertHandler.kt @@ -14,7 +14,7 @@ class CodeCompletionInsertHandler : InlineCompletionInsertHandler { elements: List ) { 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) diff --git a/src/main/kotlin/ee/carlrobert/codegpt/codecompletions/CodeCompletionSuggestionUpdateAdapter.kt b/src/main/kotlin/ee/carlrobert/codegpt/codecompletions/CodeCompletionSuggestionUpdateAdapter.kt index d48c31b6..bb4a9020 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/codecompletions/CodeCompletionSuggestionUpdateAdapter.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/codecompletions/CodeCompletionSuggestionUpdateAdapter.kt @@ -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)) } diff --git a/src/main/kotlin/ee/carlrobert/codegpt/codecompletions/DebouncedCodeCompletionProvider.kt b/src/main/kotlin/ee/carlrobert/codegpt/codecompletions/DebouncedCodeCompletionProvider.kt index 568362c0..46a5264e 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/codecompletions/DebouncedCodeCompletionProvider.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/codecompletions/DebouncedCodeCompletionProvider.kt @@ -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) }