Skip to content

Commit 02fb061

Browse files
authored
fix(amazonq): update Q inline cross file config (#4432)
1 parent 2722dc5 commit 02fb061

File tree

5 files changed

+90
-49
lines changed

5 files changed

+90
-49
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type" : "feature",
3+
"description" : "Update cross file config for Q inline suggestion"
4+
}

plugins/toolkit/jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeWhispererConstants.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ object CodeWhispererConstants {
135135
}
136136
}
137137
object CrossFile {
138-
const val CHUNK_SIZE = 60
138+
const val CHUNK_SIZE = 200
139+
const val NUMER_OF_LINE_IN_CHUNK = 50
140+
const val NUMBER_OF_CHUNK_TO_FETCH = 10
139141
}
140142

141143
object Utg {

plugins/toolkit/jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeWhispererUtil.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import software.aws.toolkits.jetbrains.services.codewhisperer.learn.LearnCodeWhi
3232
import software.aws.toolkits.jetbrains.services.codewhisperer.model.Chunk
3333
import software.aws.toolkits.jetbrains.services.codewhisperer.service.CodeWhispererService
3434
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.isTelemetryEnabled
35+
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants.CrossFile.NUMBER_OF_CHUNK_TO_FETCH
36+
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants.CrossFile.NUMER_OF_LINE_IN_CHUNK
3537
import software.aws.toolkits.jetbrains.settings.AwsSettings
3638
import software.aws.toolkits.jetbrains.utils.isQExpired
3739
import software.aws.toolkits.jetbrains.utils.notifyError
@@ -111,13 +113,13 @@ suspend fun String.toCodeChunk(path: String): List<Chunk> {
111113
fun VirtualFile.toCodeChunk(path: String): Sequence<Chunk> = sequence {
112114
var prevChunk: String? = null
113115
inputStream.bufferedReader(Charsets.UTF_8).useLines {
114-
val iter = it.chunked(10).iterator()
116+
val iter = it.chunked(NUMER_OF_LINE_IN_CHUNK).iterator()
115117
while (iter.hasNext()) {
116118
val currentChunk = iter.next().joinToString("\n").trimEnd()
117119

118120
// chunk[0]
119121
if (prevChunk == null) {
120-
val first3Lines = currentChunk.split("\n").take(3).joinToString("\n").trimEnd()
122+
val first3Lines = currentChunk.split("\n").take(NUMBER_OF_CHUNK_TO_FETCH).joinToString("\n").trimEnd()
121123
yield(Chunk(content = first3Lines, path = path, nextChunk = currentChunk))
122124
} else {
123125
// chunk[1]...chunk[n-1]

plugins/toolkit/jetbrains-core/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererFileContextProviderTest.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ class CodeWhispererFileContextProviderTest {
7575
ApplicationManager.getApplication().replaceService(CodeWhispererUserGroupSettings::class.java, userGroupSetting, disposableRule.disposable)
7676

7777
whenever(userGroupSetting.getUserGroup()).thenReturn(CodeWhispererUserGroup.Control)
78-
assertThat(CodeWhispererConstants.CrossFile.CHUNK_SIZE).isEqualTo(60)
78+
assertThat(CodeWhispererConstants.CrossFile.CHUNK_SIZE).isEqualTo(200)
7979

8080
whenever(userGroupSetting.getUserGroup()).thenReturn(CodeWhispererUserGroup.CrossFile)
81-
assertThat(CodeWhispererConstants.CrossFile.CHUNK_SIZE).isEqualTo(60)
81+
assertThat(CodeWhispererConstants.CrossFile.CHUNK_SIZE).isEqualTo(200)
8282
}
8383

8484
@Test
@@ -245,7 +245,7 @@ class CodeWhispererFileContextProviderTest {
245245
}
246246

247247
@Test
248-
fun `test extractCodeChunksFromFiles should read files from file producers to get 60 chunks`() {
248+
fun `test extractCodeChunksFromFiles should read files from file producers to get 200 chunks`() {
249249
val psiFiles = setupFixture(fixture)
250250
val virtualFiles = psiFiles.mapNotNull { it.virtualFile }
251251
val javaMainPsiFile = psiFiles.first()
@@ -266,6 +266,11 @@ class CodeWhispererFileContextProviderTest {
266266
"""public class UtilClass {
267267
| public static int util() {};
268268
| public static String util2() {};
269+
| private static void helper() {};
270+
| public static final int constant1;
271+
| public static final int constant2;
272+
| public static final int constant3;
273+
|}
269274
""".trimMargin()
270275
)
271276

@@ -284,7 +289,8 @@ class CodeWhispererFileContextProviderTest {
284289
assertThat(result[2].content).isEqualTo(
285290
"""public class MyController {
286291
| @Get
287-
| public Response getRecommendation(Request: req) {}
292+
| public Response getRecommendation(Request: req) {}
293+
|}
288294
""".trimMargin()
289295
)
290296

plugins/toolkit/jetbrains-core/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererUtilTest.kt

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class CodeWhispererUtilTest {
106106
"""public class Main {
107107
| public static void main() {
108108
| }
109+
|}
109110
""".trimMargin()
110111
)
111112
assertThat(result[1].content).isEqualTo(
@@ -119,18 +120,26 @@ class CodeWhispererUtilTest {
119120

120121
@Test
121122
fun `toCodeChunk case_2`() {
122-
val psiFile = fixture.configureByText("Sample.java", codeSample33Lines)
123+
val psiFile = fixture.configureByText("Sample.java", codeSample33Lines.repeat(2))
123124

124125
val result = runBlocking {
125126
psiFile.virtualFile.toCodeChunk("fake/path")
126127
}.toList()
127128

128-
assertThat(result).hasSize(5)
129+
assertThat(result).hasSize(3)
129130

130131
// 0th
131132
assertThat(result[0].content).isEqualTo(
132-
"""public int runBinarySearchRecursively(int[] sortedArray, int key, int low, int high) {
133-
| int middle = low + ((high - low) / 2);
133+
"""|public int runBinarySearchRecursively(int[] sortedArray, int key, int low, int high) {
134+
| int middle = low + ((high - low) / 2);
135+
|
136+
| if (high < low) {
137+
| return -1;
138+
| }
139+
|
140+
| if (key == sortedArray[middle]) {
141+
| return middle;
142+
| } else if (key < sortedArray[middle]) {
134143
""".trimMargin()
135144
)
136145
assertThat(result[0].path).isEqualTo("fake/path")
@@ -148,54 +157,72 @@ class CodeWhispererUtilTest {
148157
| if (key == sortedArray[middle]) {
149158
| return middle;
150159
| } else if (key < sortedArray[middle]) {
160+
| return runBinarySearchRecursively(sortedArray, key, low, middle - 1);
161+
| } else {
162+
| return runBinarySearchRecursively(sortedArray, key, middle + 1, high);
163+
| }
164+
|}
165+
|
166+
|public int runBinarySearchIteratively(int[] sortedArray, int key, int low, int high) {
167+
| int index = Integer.MAX_VALUE;
168+
|
169+
| while (low <= high) {
170+
| int mid = low + ((high - low) / 2);
171+
| if (sortedArray[mid] < key) {
172+
| low = mid + 1;
173+
| } else if (sortedArray[mid] > key) {
174+
| high = mid - 1;
175+
| } else if (sortedArray[mid] == key) {
176+
| index = mid;
177+
| break;
178+
| }
179+
| }
180+
|
181+
| return index;
182+
|}
183+
|public int runBinarySearchRecursively(int[] sortedArray, int key, int low, int high) {
184+
| int middle = low + ((high - low) / 2);
185+
|
186+
| if (high < low) {
187+
| return -1;
188+
| }
189+
|
190+
| if (key == sortedArray[middle]) {
191+
| return middle;
192+
| } else if (key < sortedArray[middle]) {
193+
| return runBinarySearchRecursively(sortedArray, key, low, middle - 1);
194+
| } else {
195+
| return runBinarySearchRecursively(sortedArray, key, middle + 1, high);
196+
| }
197+
|}
198+
|
199+
|public int runBinarySearchIteratively(int[] sortedArray, int key, int low, int high) {
151200
""".trimMargin()
152201
)
153202
assertThat(result[1].path).isEqualTo("fake/path")
154203
assertThat(result[1].nextChunk).isEqualTo(result[2].content)
155204

156205
// 2nd
157206
assertThat(result[2].content).isEqualTo(
158-
"""| return runBinarySearchRecursively(sortedArray, key, low, middle - 1);
159-
| } else {
160-
| return runBinarySearchRecursively(sortedArray, key, middle + 1, high);
161-
| }
162-
|}
163-
|
164-
|public int runBinarySearchIteratively(int[] sortedArray, int key, int low, int high) {
165-
| int index = Integer.MAX_VALUE;
166-
|
167-
| while (low <= high) {
207+
""" | int index = Integer.MAX_VALUE;
208+
|
209+
| while (low <= high) {
210+
| int mid = low + ((high - low) / 2);
211+
| if (sortedArray[mid] < key) {
212+
| low = mid + 1;
213+
| } else if (sortedArray[mid] > key) {
214+
| high = mid - 1;
215+
| } else if (sortedArray[mid] == key) {
216+
| index = mid;
217+
| break;
218+
| }
219+
| }
220+
|
221+
| return index;
222+
|}
168223
""".trimMargin()
169224
)
170225
assertThat(result[2].path).isEqualTo("fake/path")
171-
assertThat(result[2].nextChunk).isEqualTo(result[3].content)
172-
173-
// 3rd
174-
assertThat(result[3].content).isEqualTo(
175-
"""| int mid = low + ((high - low) / 2);
176-
| if (sortedArray[mid] < key) {
177-
| low = mid + 1;
178-
| } else if (sortedArray[mid] > key) {
179-
| high = mid - 1;
180-
| } else if (sortedArray[mid] == key) {
181-
| index = mid;
182-
| break;
183-
| }
184-
| }
185-
""".trimMargin()
186-
)
187-
assertThat(result[3].path).isEqualTo("fake/path")
188-
assertThat(result[3].nextChunk).isEqualTo(result[4].content)
189-
190-
// 4th
191-
assertThat(result[4].content).isEqualTo(
192-
"""|
193-
| return index;
194-
|}
195-
""".trimMargin()
196-
)
197-
assertThat(result[4].path).isEqualTo("fake/path")
198-
assertThat(result[4].nextChunk).isEqualTo(result[4].content)
199226
}
200227

201228
@Test

0 commit comments

Comments
 (0)