@@ -19,6 +19,7 @@ import org.assertj.core.util.VisibleForTesting
1919import software.amazon.awssdk.services.codewhispererruntime.model.CodeWhispererRuntimeException
2020import software.aws.toolkits.core.utils.debug
2121import software.aws.toolkits.core.utils.getLogger
22+ import software.aws.toolkits.core.utils.warn
2223import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnection
2324import software.aws.toolkits.jetbrains.services.codewhisperer.credentials.CodeWhispererClientAdaptor
2425import software.aws.toolkits.jetbrains.services.codewhisperer.customization.CodeWhispererModelConfigurator
@@ -154,14 +155,28 @@ class CodeWhispererUserModificationTracker(private val project: Project) : Dispo
154155 private fun emitTelemetryOnSuggestion (acceptedSuggestion : AcceptedSuggestionEntry ) {
155156 val file = acceptedSuggestion.vFile
156157
157- if (file == null || (! file.isValid)) {
158+ if (file == null || (! file.isValid) || ! acceptedSuggestion.range.isValid ) {
158159 sendModificationTelemetry(acceptedSuggestion, null )
159160 sendUserModificationTelemetryToServiceAPI(acceptedSuggestion)
160161 } else {
161162 // Will remove this later when we truly don't need toolkit user modification telemetry anymore
162163 val document = runReadAction {
163164 FileDocumentManager .getInstance().getDocument(file)
164165 }
166+ val start = acceptedSuggestion.range.startOffset
167+ val end = acceptedSuggestion.range.endOffset
168+ if (document != null ) {
169+ if (start < 0 || end < start || end > document.textLength) {
170+ LOG .warn {
171+ " Invalid range for suggestion ${acceptedSuggestion.requestId} : " +
172+ " start=$start , end=$end , docLength=${document.textLength} "
173+ }
174+ sendModificationTelemetry(acceptedSuggestion, null )
175+ sendUserModificationTelemetryToServiceAPI(acceptedSuggestion)
176+ return
177+ }
178+ }
179+
165180 val currentString = document?.getText(
166181 TextRange (acceptedSuggestion.range.startOffset, acceptedSuggestion.range.endOffset)
167182 )
0 commit comments