don't show duplicate completions in the middle/at the start of lines in intellij

This commit is contained in:
Nate 2024-09-29 23:33:21 -07:00
parent a3fa8653b9
commit 314d55eb2c

View file

@ -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