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