Skip to content

Commit 15ca2fe

Browse files
authored
fix(cwspr): include copied code in codePercentage metrics (#4177)
1 parent 31184c6 commit 15ca2fe

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type" : "bugfix",
3+
"description" : "CodeWhisperer: Include copied code in percentage code written metrics"
4+
}

plugins/toolkit/jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/telemetry/CodeWhispererCodeCoverageTracker.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ abstract class CodeWhispererCodeCoverageTracker(
8989
runReadAction {
9090
// also increment total tokens because accepted tokens are part of it
9191
incrementTotalTokens(rangeMarker.document, originalRecommendation.length)
92+
// avoid counting CodeWhisperer inserted suggestion twice in total tokens
93+
if (rangeMarker.textRange.length in 2..49 && originalRecommendation.trim().isNotEmpty()) {
94+
incrementTotalTokens(rangeMarker.document, -rangeMarker.textRange.length)
95+
}
9296
}
9397
}
9498
}
@@ -118,14 +122,18 @@ abstract class CodeWhispererCodeCoverageTracker(
118122
if (event.oldTimeStamp == 0L) return
119123
}
120124
// only count total tokens when it is a user keystroke input
121-
// do not count doc changes from copy & paste of >=2 characters
125+
// do not count doc changes from copy & paste of >=50 characters
122126
// do not count other changes from formatter, git command, etc
123127
// edge case: event can be from user hit enter with indentation where change is \n\t\t, count as 1 char increase in total chars
124128
// when event is auto closing [{(', there will be 2 separated events, both count as 1 char increase in total chars
125129
val text = event.newFragment.toString()
126130
if ((event.newLength == 1 && event.oldLength == 0) || (text.startsWith('\n') && text.trim().isEmpty())) {
127131
incrementTotalTokens(event.document, 1)
128132
return
133+
} else if (event.newLength < 50 && text.trim().isNotEmpty()) {
134+
// count doc changes from <50 multi character input as total user written code
135+
// ignore all white space changes, this usually comes from IntelliJ formatting
136+
incrementTotalTokens(event.document, event.newLength)
129137
}
130138
}
131139

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ internal class CodeWhispererCodeCoverageTrackerTestPython : CodeWhispererCodeCov
245245
}
246246

247247
@Test
248-
fun `test tracker is not listening to multi char input and will not increment totalTokens - add new code`() {
248+
fun `test tracker is not listening to multi char input more than 50 and will not increment totalTokens - add new code`() {
249249
sut = spy(TestCodePercentageTracker(project, TOTAL_SECONDS_IN_MINUTE, CodeWhispererPython.INSTANCE))
250250
CodeWhispererCodeCoverageTracker.getInstancesMap()[CodeWhispererPython.INSTANCE] = sut
251251
sut.activateTrackerIfNotActive()
@@ -259,15 +259,15 @@ internal class CodeWhispererCodeCoverageTrackerTestPython : CodeWhispererCodeCov
259259
val captor = argumentCaptor<DocumentEvent>()
260260
verify(sut, Times(1)).documentChanged(captor.capture())
261261
assertThat(captor.firstValue.newFragment.toString()).isEqualTo(pythonTestLeftContext)
262-
assertThat(sut.totalTokensSize).isEqualTo(0)
262+
assertThat(sut.totalTokensSize).isEqualTo(pythonTestLeftContext.length)
263263

264-
val anotherCode = "(x, y):"
264+
val anotherCode = "(x, y):".repeat(8)
265265
runInEdtAndWait {
266266
WriteCommandAction.runWriteCommandAction(project) {
267267
fixture.editor.appendString(anotherCode)
268268
}
269269
}
270-
assertThat(sut.totalTokensSize).isEqualTo(0)
270+
assertThat(sut.totalTokensSize).isEqualTo(pythonTestLeftContext.length)
271271
}
272272

273273
@Test

0 commit comments

Comments
 (0)