Skip to content

Commit 6fee511

Browse files
committed
delet indetaion to get corret offset
1 parent fa58868 commit 6fee511

File tree

2 files changed

+12
-37
lines changed

2 files changed

+12
-37
lines changed

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/service/CodeWhispererService.kt

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,16 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
228228
codewhispererEndToEndStart = System.nanoTime()
229229
}
230230

231-
val nextCaretPosition = calculateNextCaretPosition(requestContext, firstValidRecommendation)
232-
val nextFileContextInfo = createNextFileContextInfo(requestContext, firstValidRecommendation)
231+
val nextCaretPosition = CaretPosition(
232+
line = requestContext.caretPosition.line + firstValidRecommendation.recommendation.content().count { it == '\n' },
233+
offset = requestContext.caretPosition.offset + firstValidRecommendation.recommendation.content().length
234+
)
235+
236+
val nextFileContextInfo = requestContext.fileContextInfo.copy(
237+
caretContext = requestContext.fileContextInfo.caretContext.copy(
238+
leftFileContext = requestContext.fileContextInfo.caretContext.leftFileContext + firstValidRecommendation.recommendation.content()
239+
)
240+
)
233241

234242
val nextRequestContext = requestContext.copy(
235243
caretPosition = nextCaretPosition,
@@ -753,37 +761,6 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
753761
LOG.debug { "Promoted nextInvocationContext to current session and displayed next recommendation." }
754762
}
755763

756-
private fun calculateNextCaretPosition(
757-
currentRequestContext: RequestContext,
758-
firstValidRecommendation: DetailContext,
759-
): CaretPosition {
760-
val indent = currentRequestContext.fileContextInfo.caretContext.leftContextOnCurrentLine
761-
.takeWhile { it.isWhitespace() }
762-
val recommendedText = buildString {
763-
append(indent)
764-
append(firstValidRecommendation.recommendation.content())
765-
if (!endsWith("\n")) {
766-
append("\n")
767-
}
768-
}
769-
val lineCount = recommendedText.count { it == '\n' }
770-
771-
return CaretPosition(
772-
line = currentRequestContext.caretPosition.line + lineCount,
773-
offset = currentRequestContext.caretPosition.offset + recommendedText.length
774-
)
775-
}
776-
777-
private fun createNextFileContextInfo(
778-
requestContext: RequestContext,
779-
firstValidRecommendation: DetailContext,
780-
): FileContextInfo {
781-
val updatedCaretContext = requestContext.fileContextInfo.caretContext.copy(
782-
leftFileContext = requestContext.fileContextInfo.caretContext.leftFileContext + firstValidRecommendation.recommendation.content()
783-
)
784-
return requestContext.fileContextInfo.copy(caretContext = updatedCaretContext)
785-
}
786-
787764
private fun sendDiscardedUserDecisionEventForAll(
788765
requestContext: RequestContext,
789766
responseContext: ResponseContext,

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,12 @@ open class CodeWhispererTestBase {
241241
}
242242

243243
fun addUserInputAfterInvocation(userInput: String) {
244-
val codewhispererServiceSpy = spy(codewhispererService)
245244
val triggerTypeCaptor = argumentCaptor<TriggerTypeInfo>()
246245
val editorCaptor = argumentCaptor<Editor>()
247246
val projectCaptor = argumentCaptor<Project>()
248247
val psiFileCaptor = argumentCaptor<PsiFile>()
249248
val latencyContextCaptor = argumentCaptor<LatencyContext>()
250-
codewhispererServiceSpy.stub {
249+
codewhispererService.stub {
251250
onGeneric {
252251
getRequestContext(
253252
triggerTypeCaptor.capture(),
@@ -257,7 +256,7 @@ open class CodeWhispererTestBase {
257256
latencyContextCaptor.capture()
258257
)
259258
}.doAnswer {
260-
val requestContext = codewhispererServiceSpy.getRequestContext(
259+
val requestContext = codewhispererService.getRequestContext(
261260
triggerTypeCaptor.firstValue,
262261
editorCaptor.firstValue,
263262
projectCaptor.firstValue,
@@ -268,6 +267,5 @@ open class CodeWhispererTestBase {
268267
requestContext
269268
}.thenCallRealMethod()
270269
}
271-
ApplicationManager.getApplication().replaceService(CodeWhispererService::class.java, codewhispererServiceSpy, disposableRule.disposable)
272270
}
273271
}

0 commit comments

Comments
 (0)