fix: Skip duplicate checks at document end or blank text

Prevent autocomplete from truncating when the cursor is at the end of the document or when the text after the cursor is blank.
This commit is contained in:
liuk 2024-12-25 23:03:53 +08:00
parent 62e9f35890
commit decd04eddd

View file

@ -120,6 +120,10 @@ class AutocompleteService(private val project: Project) {
return ApplicationManager.getApplication().runReadAction<String> {
val document = editor.document
val caretOffset = editor.caretModel.offset
// Don't care about it if it's at the end of the document
if (caretOffset == document.textLength) return@runReadAction completion
val N = 10
var textAfterCursor = if (caretOffset + N <= document.textLength) {
document.getText(com.intellij.openapi.util.TextRange(caretOffset, caretOffset + N))
@ -127,6 +131,9 @@ class AutocompleteService(private val project: Project) {
document.getText(com.intellij.openapi.util.TextRange(caretOffset, document.textLength))
}
// Avoid truncating the completion text when the text after the cursor is blank
if (textAfterCursor.isBlank()) return@runReadAction completion
// Determine the index of a newline character within the text following the cursor.
val newlineIndex = textAfterCursor.indexOf("\r\n").takeIf { it >= 0 } ?: textAfterCursor.indexOf('\n')
// If a newline character is found and the current line is not empty, truncate the text at that point.