@@ -23,6 +23,7 @@ import com.intellij.openapi.util.Key
23
23
import com.intellij.openapi.util.TextRange
24
24
import com.intellij.openapi.wm.ToolWindowManager
25
25
import com.intellij.ui.awt.RelativePoint
26
+ import software.amazon.awssdk.services.codewhisperer.model.Recommendation
26
27
import software.amazon.awssdk.services.codewhisperer.model.Reference
27
28
import software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEditorUtil.getPopupPositionAboveText
28
29
import software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEditorUtil.getRelativePathToContentRoot
@@ -66,34 +67,17 @@ class CodeWhispererCodeReferenceManager(private val project: Project) {
66
67
val (requestContext, _, recommendationContext) = states
67
68
val (_, editor, _, caretPosition) = requestContext
68
69
val (_, detail, reformattedDetail) = recommendationContext.details[selectedIndex]
69
- val userInput = recommendationContext.userInputSinceInvocation
70
70
val startOffset = caretPosition.offset
71
71
val relativePath = getRelativePathToContentRoot(editor)
72
72
reformattedDetail.references().forEachIndexed { i, reference ->
73
- // start and end could intersect with the userInput, we do not want to show reference for the
74
- // userInput part, so we truncate the range to exclude userInput here if there's an overlap.
75
- if (reference.recommendationContentSpan().end() <= userInput.length) return @forEachIndexed
76
-
77
- // Now there's at least some valid range, truncate start if applicable
78
- val spanStartExcludeUserInput = maxOf(reference.recommendationContentSpan().start(), userInput.length)
79
- val start = startOffset + spanStartExcludeUserInput
73
+ val start = startOffset + reference.recommendationContentSpan().start()
80
74
val end = startOffset + reference.recommendationContentSpan().end()
81
- val startLine = editor.document.getLineNumber(start)
82
- val endLine = editor.document.getLineNumber(end)
83
- val lineNums = if (startLine == endLine) {
84
- (startLine + 1 ).toString()
85
- } else {
86
- " ${startLine + 1 } to ${endLine + 1 } "
87
- }
88
-
89
- val originalSpan = detail.references()[i].recommendationContentSpan()
75
+ val lineNums = getReferenceLineNums(editor, start, end)
90
76
91
77
// There is an unformatted recommendation(directly from response) and reformatted one. We want to get
92
78
// the line number, start/end offset of the reformatted one because it's the one inserted to the editor.
93
79
// However, the one that shows in the tool window record should show the original recommendation, as below.
94
- val originalContentLines = detail.content().substring(
95
- maxOf(originalSpan.start(), userInput.length), originalSpan.end()
96
- ).split(" \n " )
80
+ val originalContentLines = getOriginalContentLines(detail, i)
97
81
98
82
codeReferenceComponents.contentPanel.apply {
99
83
add(
@@ -115,6 +99,24 @@ class CodeWhispererCodeReferenceManager(private val project: Project) {
115
99
}
116
100
}
117
101
102
+ fun getReferenceLineNums (editor : Editor , start : Int , end : Int ): String {
103
+ val startLine = editor.document.getLineNumber(start)
104
+ val endLine = editor.document.getLineNumber(end)
105
+ val lineNums = if (startLine == endLine) {
106
+ (startLine + 1 ).toString()
107
+ } else {
108
+ " ${startLine + 1 } to ${endLine + 1 } "
109
+ }
110
+ return lineNums
111
+ }
112
+
113
+ fun getOriginalContentLines (detail : Recommendation , i : Int ): List <String > {
114
+ val originalSpan = detail.references()[i].recommendationContentSpan()
115
+ return detail.content()
116
+ .substring(originalSpan.start(), originalSpan.end())
117
+ .split(" \n " )
118
+ }
119
+
118
120
private fun insertHighLightContext (editor : Editor , start : Int , end : Int , reference : Reference ) {
119
121
val codeContent = editor.document.getText(TextRange .create(start, end))
120
122
val referenceContent = message(
0 commit comments