@@ -33,12 +33,17 @@ import com.intellij.openapi.wm.ToolWindowManager
3333import com.intellij.ui.JBColor
3434import com.intellij.ui.jcef.JBCefApp
3535import com.jetbrains.rd.util.AtomicInteger
36- import kotlinx.coroutines.*
36+ import kotlinx.coroutines.CoroutineScope
37+ import kotlinx.coroutines.async
38+ import kotlinx.coroutines.delay
3739import kotlinx.coroutines.flow.catch
3840import kotlinx.coroutines.flow.onEach
3941import kotlinx.coroutines.flow.toList
42+ import kotlinx.coroutines.launch
43+ import kotlinx.coroutines.runBlocking
4044import kotlinx.coroutines.sync.Mutex
4145import kotlinx.coroutines.sync.withLock
46+ import kotlinx.coroutines.withContext
4247import org.apache.commons.text.StringEscapeUtils
4348import software.amazon.awssdk.services.codewhispererruntime.model.InlineChatUserDecision
4449import software.aws.toolkits.core.utils.debug
@@ -52,10 +57,8 @@ import software.aws.toolkits.jetbrains.services.amazonq.QWebviewPanel
5257import software.aws.toolkits.jetbrains.services.amazonq.auth.AuthController
5358import software.aws.toolkits.jetbrains.services.amazonq.toolwindow.AMAZON_Q_WINDOW_ID
5459import software.aws.toolkits.jetbrains.services.codewhisperer.customization.CodeWhispererModelConfigurator
55- import software.aws.toolkits.jetbrains.services.codewhisperer.language.programmingLanguage
5660import software.aws.toolkits.jetbrains.services.cwc.clients.chat.model.ChatRequestData
5761import software.aws.toolkits.jetbrains.services.cwc.clients.chat.model.TriggerType
58- import software.aws.toolkits.jetbrains.services.cwc.clients.chat.v1.ChatSessionFactoryV1
5962import software.aws.toolkits.jetbrains.services.cwc.controller.chat.messenger.ChatPromptHandler
6063import software.aws.toolkits.jetbrains.services.cwc.controller.chat.telemetry.TelemetryHelper
6164import software.aws.toolkits.jetbrains.services.cwc.controller.chat.userIntent.UserIntentRecognizer
@@ -64,7 +67,6 @@ import software.aws.toolkits.jetbrains.services.cwc.editor.context.ExtractionTri
6467import software.aws.toolkits.jetbrains.services.cwc.inline.listeners.InlineChatFileListener
6568import software.aws.toolkits.jetbrains.services.cwc.messages.ChatMessage
6669import software.aws.toolkits.jetbrains.services.cwc.messages.ChatMessageType
67- import software.aws.toolkits.jetbrains.services.cwc.storage.ChatSessionInfo
6870import software.aws.toolkits.jetbrains.services.cwc.storage.ChatSessionStorage
6971import software.aws.toolkits.telemetry.FeatureId
7072import java.util.UUID
@@ -96,7 +98,6 @@ class InlineChatController(
9698 project.messageBus.connect(this ).subscribe(FileEditorManagerListener .FILE_EDITOR_MANAGER , listener)
9799 }
98100
99-
100101 data class InlineChatMetrics (
101102 val requestId : String ,
102103 val inputLength : Int? = null ,
@@ -123,6 +124,7 @@ class InlineChatController(
123124 }
124125
125126 val popupCancelHandler: (editor: Editor ) -> Unit = { editor ->
127+ isAbandoned.set(true )
126128 if (canPopupAbort.get() && currentPopup != null ) {
127129 undoChanges()
128130 restoreSelection(editor)
@@ -140,7 +142,7 @@ class InlineChatController(
140142 metrics?.charactersAdded = metrics?.numSuggestionAddChars
141143 metrics?.charactersRemoved = metrics?.numSuggestionDelChars
142144 }
143- if (metrics?.requestId?.isNotEmpty() == true ){
145+ if (metrics?.requestId?.isNotEmpty() == true ) {
144146 telemetryHelper.recordInlineChatTelemetry(
145147 metrics?.requestId!! ,
146148 metrics?.inputLength,
@@ -241,13 +243,13 @@ class InlineChatController(
241243
242244 private fun highlightCodeWithBackgroundColor (editor : Editor , startOffset : Int , endOffset : Int , isGreen : Boolean ) {
243245 val greenBackgroundAttributes = TextAttributes ().apply {
244- backgroundColor = JBColor (0x66BB6A , 0x006400 )
245- effectColor = JBColor (0x66BB6A , 0x006400 )
246+ backgroundColor = JBColor (0xAADEAA , 0x447152 )
247+ effectColor = JBColor (0xAADEAA , 0x447152 )
246248 }
247249
248250 val redBackgroundAttributes = TextAttributes ().apply {
249- backgroundColor = JBColor (0xEF9A9A , 0x8B0000 )
250- effectColor = JBColor (0xEF9A9A , 0x8B0000 )
251+ backgroundColor = JBColor (0xFFC8BD , 0x8F5247 )
252+ effectColor = JBColor (0xFFC8BD , 0x8F5247 )
251253 }
252254 val attributes = if (isGreen) greenBackgroundAttributes else redBackgroundAttributes
253255 rangeHighlighter = editor.markupModel.addRangeHighlighter(
@@ -284,7 +286,7 @@ class InlineChatController(
284286 .replace(" =>" , " =>" )
285287
286288 private fun processNewCode (editor : Editor , line : Int , code : String , prevMessage : String ) {
287- if (isAbandoned.get()) return
289+ if (isAbandoned.get()) return
288290 runBlocking {
289291 logger.debug { " received inline chat recommendation with code: \n $code " }
290292 var insertLine = line
@@ -336,37 +338,37 @@ class InlineChatController(
336338 DiffRow .Tag .DELETE -> {
337339 val startOffset = getLineStartOffset(editor.document, currentDocumentLine)
338340 val endOffset = getLineEndOffset(editor.document, currentDocumentLine, true )
339- highlightString(editor, startOffset, endOffset, false )
341+ highlightString(editor, startOffset, endOffset, false )
340342 currentDocumentLine++
341343 }
342344
343345 DiffRow .Tag .CHANGE -> {
344346 val startOffset = getLineStartOffset(editor.document, currentDocumentLine)
345347 val endOffset = getLineEndOffset(editor.document, currentDocumentLine, true )
346- highlightString(editor, startOffset, endOffset, false )
347- val insetStartOffset = getLineStartOffset(editor.document, currentDocumentLine+ 1 )
348- val insertEndOffset = getLineEndOffset(editor.document, currentDocumentLine+ 1 , true )
349- highlightString(editor, insetStartOffset, insertEndOffset, true )
350- currentDocumentLine+ = 2
348+ highlightString(editor, startOffset, endOffset, false )
349+ val insetStartOffset = getLineStartOffset(editor.document, currentDocumentLine + 1 )
350+ val insertEndOffset = getLineEndOffset(editor.document, currentDocumentLine + 1 , true )
351+ highlightString(editor, insetStartOffset, insertEndOffset, true )
352+ currentDocumentLine + = 2
351353 }
352354
353355 DiffRow .Tag .INSERT -> {
354356 val insetStartOffset = getLineStartOffset(editor.document, currentDocumentLine)
355357 val insertEndOffset = getLineEndOffset(editor.document, currentDocumentLine, true )
356- highlightString(editor, insetStartOffset, insertEndOffset, true )
358+ highlightString(editor, insetStartOffset, insertEndOffset, true )
357359 currentDocumentLine++
358360 }
359361 }
360362 }
361363 }
362364
363- private fun applyChunk (recommendation : String , editor : Editor , startLine : Int , endLine : Int , diff : List < DiffRow > ) {
365+ private fun applyChunk (recommendation : String , editor : Editor , startLine : Int , endLine : Int ) {
364366 val startOffset = getLineStartOffset(editor.document, startLine)
365367 val endOffset = getLineEndOffset(editor.document, endLine)
366368 replaceString(editor.document, startOffset, endOffset, recommendation)
367369 }
368370
369- private fun constructPatch (diff : List <DiffRow >): String {
371+ private fun constructPatch (diff : List <DiffRow >): String {
370372 var patchString = " "
371373 diff.forEach { row ->
372374 when (row.tag) {
@@ -379,7 +381,7 @@ class InlineChatController(
379381 }
380382
381383 DiffRow .Tag .CHANGE -> {
382- patchString + = row.oldLine + " \n "
384+ patchString + = row.oldLine + " \n "
383385 patchString + = row.newLine + " \n "
384386 }
385387
@@ -392,7 +394,7 @@ class InlineChatController(
392394 }
393395
394396 private fun finalComputation (selectedCode : String , finalMessage : String? ) {
395- if (finalMessage == null ) {
397+ if (finalMessage == null ) {
396398 throw Exception (" No suggestions from Q; please try a different instruction." )
397399 }
398400 var numSuggestionAddChars = 0
@@ -440,9 +442,9 @@ class InlineChatController(
440442 }
441443
442444 private fun processChatDiff (selectedCode : String , event : ChatMessage , editor : Editor , selectionRange : RangeMarker ) {
443- if (isAbandoned.get()) return
445+ if (isAbandoned.get()) return
444446 if (event.message?.isNotEmpty() == true ) {
445- logger.info { " inline chat recommendation: \n ${event.message} " }
447+ logger.info { " inline chat recommendation: \n ${event.message} " }
446448 runBlocking {
447449 val recommendation = unescape(event.message)
448450 val selection = selectedCode.split(" \n " )
@@ -460,10 +462,10 @@ class InlineChatController(
460462 }
461463 launch(EDT ) {
462464 removeSelection(editor)
463- applyChunk(patchString, editor, startLine, endLine, diff )
465+ applyChunk(patchString, editor, startLine, endLine)
464466 processHighlights(diff, startLine, editor)
465467 }.join()
466- acceptAction = {
468+ acceptAction = {
467469 val startOffset = getLineStartOffset(editor.document, startLine)
468470 val endOffset = getLineEndOffset(editor.document, endLine)
469471 replaceString(editor.document, startOffset, endOffset, recommendation)
@@ -498,7 +500,7 @@ class InlineChatController(
498500 private fun getLineEndOffset (document : Document , row : Int , includeLastNewLine : Boolean = false): Int = ReadAction .compute<Int , Throwable > {
499501 if (row == document.lineCount - 1 ) {
500502 document.getLineEndOffset(row)
501- } else if (row < document.lineCount - 1 ){
503+ } else if (row < document.lineCount - 1 ) {
502504 val lineEnd = document.getLineEndOffset(row)
503505 if (includeLastNewLine) lineEnd + 1 else lineEnd
504506 } else {
@@ -538,20 +540,17 @@ class InlineChatController(
538540 }
539541 }
540542
541- private fun highlightString (editor : Editor , start : Int , end : Int , isInsert : Boolean ): RangeMarker {
542- var rangeMarker: RangeMarker ? = null
543+ private fun highlightString (editor : Editor , start : Int , end : Int , isInsert : Boolean ) {
543544 ApplicationManager .getApplication().invokeAndWait {
544545 CommandProcessor .getInstance().runUndoTransparentAction {
545546 WriteCommandAction .runWriteCommandAction(project) {
546- rangeMarker = editor.document.createRangeMarker(start, end)
547- highlightCodeWithBackgroundColor(editor, rangeMarker!! .startOffset, rangeMarker!! .endOffset, isInsert)
547+ highlightCodeWithBackgroundColor(editor, start, end, isInsert)
548548 }
549549 }
550550 }
551- return rangeMarker!!
552551 }
553552
554- private fun removeHighlighter (editor : Editor ){
553+ private fun removeHighlighter (editor : Editor ) {
555554 ApplicationManager .getApplication().invokeAndWait {
556555 CommandProcessor .getInstance().runUndoTransparentAction {
557556 WriteCommandAction .runWriteCommandAction(project) {
@@ -674,7 +673,7 @@ class InlineChatController(
674673 codeIntent = true , responseStartLatency = firstResponseLatency, responseEndLatency = lastResponseLatency
675674 )
676675 }
677- if (finalMessage != null ) {
676+ if (finalMessage != null ) {
678677 try {
679678 finalComputation(selectedCode, finalMessage.message)
680679 } catch (e: Exception ) {
0 commit comments