Skip to content

Commit f258ea9

Browse files
authored
fix(amazonq): Crossfile query config not configured correctly (#5028)
1 parent 11397c3 commit f258ea9

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeWhispererFileContextProvider.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,12 @@ class DefaultCodeWhispererFileContextProvider(private val project: Project) : Fi
379379
}
380380
}
381381

382-
// takeLast(11) will extract 10 lines (exclusing current line) of left context as the query parameter
383-
fun generateQuery(fileContext: FileContextInfo) = fileContext.caretContext.leftFileContext.split("\n").takeLast(11).joinToString("\n")
382+
// takeLast NUMBER_OF_LINE_IN_CHUNK of lines (exclusing current line) in left context as the query
383+
fun generateQuery(fileContext: FileContextInfo) = fileContext.caretContext.leftFileContext
384+
.split("\n")
385+
.dropLast(1)
386+
.takeLast(CodeWhispererConstants.CrossFile.NUMBER_OF_LINE_IN_CHUNK)
387+
.joinToString("\n")
384388

385389
companion object {
386390
private val LOG = getLogger<DefaultCodeWhispererFileContextProvider>()

plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererFileContextProviderTest.kt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import org.mockito.kotlin.stub
3131
import org.mockito.kotlin.times
3232
import org.mockito.kotlin.verify
3333
import org.mockito.kotlin.whenever
34+
import software.aws.toolkits.core.utils.test.aStringWithLineCount
3435
import software.aws.toolkits.jetbrains.services.amazonq.CodeWhispererFeatureConfigService
3536
import software.aws.toolkits.jetbrains.services.amazonq.project.EncoderServer
3637
import software.aws.toolkits.jetbrains.services.amazonq.project.InlineBm25Chunk
@@ -47,6 +48,8 @@ import software.aws.toolkits.jetbrains.services.codewhisperer.language.languages
4748
import software.aws.toolkits.jetbrains.services.codewhisperer.language.languages.CodeWhispererRuby
4849
import software.aws.toolkits.jetbrains.services.codewhisperer.language.languages.CodeWhispererTsx
4950
import software.aws.toolkits.jetbrains.services.codewhisperer.language.languages.CodeWhispererTypeScript
51+
import software.aws.toolkits.jetbrains.services.codewhisperer.model.CaretContext
52+
import software.aws.toolkits.jetbrains.services.codewhisperer.model.FileContextInfo
5053
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants
5154
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CrossFileStrategy
5255
import software.aws.toolkits.jetbrains.services.codewhisperer.util.DefaultCodeWhispererFileContextProvider
@@ -94,6 +97,44 @@ class CodeWhispererFileContextProviderTest {
9497
project.replaceService(ProjectContextController::class.java, mockProjectContext, disposableRule.disposable)
9598
}
9699

100+
@Test
101+
fun `generateQuery should use last 50 lines (excluding the current line) of left context`() {
102+
val fileContext = FileContextInfo(
103+
CaretContext(
104+
leftFileContext = aStringWithLineCount(100),
105+
rightFileContext = "",
106+
leftContextOnCurrentLine = ""
107+
),
108+
"Foo.java",
109+
CodeWhispererJava.INSTANCE,
110+
""
111+
)
112+
113+
val actual = sut.generateQuery(fileContext)
114+
val expected = aStringWithLineCount(lineCount = 50, start = 49)
115+
assertThat(actual).isEqualTo(expected)
116+
assertThat(actual.split("\n").size).isEqualTo(expected.split("\n").size)
117+
}
118+
119+
@Test
120+
fun `generateQuery should use last 50 lines (excluding the current line) of left context if current caret is at the beginning of the line`() {
121+
val fileContext = FileContextInfo(
122+
CaretContext(
123+
leftFileContext = aStringWithLineCount(100) + "\n",
124+
rightFileContext = "",
125+
leftContextOnCurrentLine = ""
126+
),
127+
"Foo.java",
128+
CodeWhispererJava.INSTANCE,
129+
""
130+
)
131+
132+
val actual = sut.generateQuery(fileContext)
133+
val expected = aStringWithLineCount(lineCount = 50, start = 50)
134+
assertThat(actual).isEqualTo(expected)
135+
assertThat(actual.split("\n").size).isEqualTo(expected.split("\n").size)
136+
}
137+
97138
@Test
98139
fun `extractSupplementalFileContext should timeout 50ms`() = runTest {
99140
featureConfigService.stub { on { getInlineCompletion() } doReturn false }

0 commit comments

Comments
 (0)