Skip to content

Commit

Permalink
fix: Skip duplicate checks at document end or blank text
Browse files Browse the repository at this point in the history
Prevent autocomplete from truncating when the cursor is at the end of the document or when the text after the cursor is blank.
  • Loading branch information
lkk214 committed Dec 25, 2024
1 parent 62e9f35 commit decd04e
Showing 1 changed file with 7 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,20 @@ 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))
} else {
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.
Expand Down

0 comments on commit decd04e

Please sign in to comment.