From 314d55eb2cb18f95d686c908db7d57eed95ba647 Mon Sep 17 00:00:00 2001 From: Nate Date: Sun, 29 Sep 2024 23:33:21 -0700 Subject: [PATCH] don't show duplicate completions in the middle/at the start of lines in intellij --- .../autocomplete/AutocompleteService.kt | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/autocomplete/AutocompleteService.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/autocomplete/AutocompleteService.kt index 877308fa4..79dc71230 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/autocomplete/AutocompleteService.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/autocomplete/AutocompleteService.kt @@ -94,8 +94,7 @@ class AutocompleteService(private val project: Project) { if (completions.isNotEmpty()) { val completion = completions[0].toString() - if (completion.isNotEmpty() && (completion.lines().size === 1 || column >= lineLength)) { - // Do not render if completion is multi-line and caret is in middle of line + if (shouldRenderCompletion(completion, column, lineLength, editor)) { renderCompletion(editor, offset, completion) pendingCompletion = pendingCompletion?.copy(text = completion) @@ -106,6 +105,28 @@ class AutocompleteService(private val project: Project) { })) } + private fun shouldRenderCompletion(completion: String, column: Int, lineLength: Int, editor: Editor): Boolean { + if (completion.isEmpty()) { + return false + } + + // Check if completion matches the first 10 characters after the cursor + val document = editor.document + val caretOffset = editor.caretModel.offset + val textAfterCursor = if (caretOffset + 10 <= document.textLength) { + document.getText(com.intellij.openapi.util.TextRange(caretOffset, caretOffset + 10)) + } else { + document.getText(com.intellij.openapi.util.TextRange(caretOffset, document.textLength)) + } + + if (completion.startsWith(textAfterCursor)) { + return false + } + + // Do not render if completion is multi-line and caret is in middle of line + return completion.lines().size > 1 && column < lineLength + } + private fun renderCompletion(editor: Editor, offset: Int, text: String) { if (text.isEmpty()) { return