From f5f5f963c6549570ef25a45864882eb40d4426f8 Mon Sep 17 00:00:00 2001 From: Will Lo Date: Wed, 19 Mar 2025 00:18:48 -0700 Subject: [PATCH] fix #5466 may cause ElementNotFoundException on calling list.last() when list is empty --- .../codewhisperer/util/CodeWhispererFileContextProvider.kt | 2 +- .../jetbrains/services/codewhisperer/util/CodeWhispererUtil.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeWhispererFileContextProvider.kt b/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeWhispererFileContextProvider.kt index e825535d5db..bf04a43695b 100644 --- a/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeWhispererFileContextProvider.kt +++ b/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeWhispererFileContextProvider.kt @@ -347,7 +347,7 @@ class DefaultCodeWhispererFileContextProvider(private val project: Project) : Fi } var curTotalLength = c.sumOf { it.content.length } - while (curTotalLength >= CodeWhispererConstants.CrossFile.MAX_TOTAL_LENGTH) { + while (curTotalLength >= CodeWhispererConstants.CrossFile.MAX_TOTAL_LENGTH && c.isNotEmpty()) { val last = c.last() c = c.dropLast(1) curTotalLength -= last.content.length diff --git a/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeWhispererUtil.kt b/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeWhispererUtil.kt index b789ab31baa..25a3f014df5 100644 --- a/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeWhispererUtil.kt +++ b/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeWhispererUtil.kt @@ -120,7 +120,7 @@ fun truncateLineByLine(input: String, l: Int): String { val shouldAddNewLineBack = input.last() == '\n' var lines = input.trim().split("\n") var curLen = input.length - while (curLen > maxLength) { + while (curLen > maxLength && lines.isNotEmpty()) { val last = lines.last() lines = lines.dropLast(1) curLen -= last.length + 1