Skip to content

Commit c1d42ff

Browse files
committed
Bug Fix: Apply fix does not apply at proper line.
1 parent 5b29f05 commit c1d42ff

File tree

3 files changed

+16
-66
lines changed

3 files changed

+16
-66
lines changed

plugins/toolkit/jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/codescan/listeners/CodeWhispererCodeScanEditorMouseMotionListener.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhisperer
4242
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants.CODE_SCAN_ISSUE_POPUP_DELAY_IN_SECONDS
4343
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants.CODE_SCAN_ISSUE_TITLE_MAX_LENGTH
4444
import software.aws.toolkits.jetbrains.services.codewhisperer.util.runIfIdcConnectionOrTelemetryEnabled
45-
import software.aws.toolkits.jetbrains.utils.applyPatch
4645
import software.aws.toolkits.jetbrains.utils.notifyError
4746
import software.aws.toolkits.resources.message
4847
import software.aws.toolkits.telemetry.Result
@@ -397,14 +396,23 @@ class CodeWhispererCodeScanEditorMouseMotionListener(private val project: Projec
397396
WriteCommandAction.runWriteCommandAction(issue.project) {
398397
val document = FileDocumentManager.getInstance().getDocument(issue.file) ?: return@runWriteCommandAction
399398

400-
val documentContent = document.text
401-
val updatedContent = applyPatch(issue.suggestedFixes[0].code, documentContent, issue.file.name)
402-
if (updatedContent == null) {
403-
// println(applyPatchToContent(issue.suggestedFixes[0].code, documentContent))
404-
throw Error("Patch apply failed.")
399+
val linesToDelete = issue.suggestedFixes[0].code
400+
.split("\n")
401+
.count { it.startsWith("-") }
402+
403+
val linesToInsert = issue.suggestedFixes[0].code
404+
.split("\n")
405+
.filter { it.startsWith("+") }
406+
.map { it.removePrefix("+") }
407+
408+
if (document != null) {
409+
val startLineOffset = document.getLineStartOffset(issue.startLine - 1)
410+
val endLineOffset = document.getLineEndOffset(issue.startLine + linesToDelete - 2)
411+
document.replaceString(startLineOffset, endLineOffset, linesToInsert.joinToString("\n"))
412+
PsiDocumentManager.getInstance(project).commitDocument(document)
405413
}
406-
document.replaceString(document.getLineStartOffset(0), document.getLineEndOffset(document.lineCount - 1), updatedContent)
407-
PsiDocumentManager.getInstance(issue.project).commitDocument(document)
414+
FileDocumentManager.getInstance().saveDocument(document)
415+
408416
CodeWhispererTelemetryService.getInstance().sendCodeScanIssueApplyFixEvent(issue, Result.Succeeded)
409417
hidePopup()
410418
CodeWhispererCodeScanManager.getInstance(issue.project).updateScanNodesForIssuesOutOfTextRange(issue)

plugins/toolkit/jetbrains-core/src/software/aws/toolkits/jetbrains/utils/TextUtils.kt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ package software.aws.toolkits.jetbrains.utils
55

66
import com.intellij.lang.Language
77
import com.intellij.openapi.command.CommandProcessor
8-
import com.intellij.openapi.diff.impl.patch.PatchReader
9-
import com.intellij.openapi.diff.impl.patch.TextFilePatch
10-
import com.intellij.openapi.diff.impl.patch.apply.PlainSimplePatchApplier
118
import com.intellij.openapi.project.Project
129
import com.intellij.openapi.util.text.StringUtil
1310
import com.intellij.psi.PsiFileFactory
@@ -30,15 +27,3 @@ fun formatText(project: Project, language: Language, content: String): String {
3027
* (e.g. Update Complete)
3128
*/
3229
fun String.toHumanReadable() = StringUtil.toTitleCase(toLowerCase().replace('_', ' '))
33-
34-
fun generateUnifiedPatch(patch: String, filePath: String): TextFilePatch {
35-
val unifiedPatch = "--- $filePath\n+++ $filePath\n$patch"
36-
val patchReader = PatchReader(unifiedPatch)
37-
val patches = patchReader.readTextPatches()
38-
return patches[0]
39-
}
40-
41-
fun applyPatch(patch: String, fileContent: String, filePath: String): String? {
42-
val unifiedPatch = generateUnifiedPatch(patch, filePath)
43-
return PlainSimplePatchApplier.apply(fileContent, unifiedPatch.hunks)
44-
}

plugins/toolkit/jetbrains-core/tst/software/aws/toolkits/jetbrains/utils/TextUtilsTest.kt

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -94,47 +94,4 @@ class TextUtilsTest {
9494
val actual = convertMarkdownToHTML(input)
9595
assertThat(actual).isEqualTo(expected)
9696
}
97-
98-
@Test
99-
fun canApplyPatchSuccessfully() {
100-
val inputPatch = "@@ -1,3 +1,3 @@\n first line\n-second line\n+third line\n forth line"
101-
val inputFilePath = "dummy.py"
102-
val fileContent = "first line\nsecond line\nforth line"
103-
val actual = applyPatch(inputPatch, fileContent, inputFilePath)
104-
val expected = "first line\nthird line\nforth line"
105-
assertThat(actual).isEqualTo(expected)
106-
}
107-
108-
@Test
109-
fun canReturnNullWhenApplyPatchFails() {
110-
val inputPatch = "@@ -1,3 +1,3 @@\n first line\n-second line\n+third line\n forth line"
111-
val inputFilePath = "dummy.py"
112-
val fileContent = "first line\nThree line\nforth line"
113-
val actual = applyPatch(inputPatch, fileContent, inputFilePath)
114-
val expected = null
115-
assertThat(actual).isEqualTo(expected)
116-
}
117-
118-
@Test
119-
fun shouldHaveZeroHunkSizeForIncorrectPatchGenerated() {
120-
val inputPatch = " first line\n-second line\n+third line\n forth line"
121-
val inputFilePath = "dummy.py"
122-
val actual = generateUnifiedPatch(inputPatch, inputFilePath)
123-
assertThat(actual.hunks.size).isEqualTo(0)
124-
}
125-
126-
@Test
127-
fun shouldHaveHunksForCorrectPatchGenerated() {
128-
val inputPatch = "@@ -1,3 +1,3 @@\n first line\n-second line\n+third line\n forth line"
129-
val inputFilePath = "dummy.py"
130-
val actual = generateUnifiedPatch(inputPatch, inputFilePath)
131-
assertThat(actual.hunks.size).isEqualTo(1)
132-
val hunk = actual.hunks[0]
133-
assertThat(hunk.startLineAfter).isEqualTo(0)
134-
assertThat(hunk.startLineBefore).isEqualTo(0)
135-
assertThat(hunk.endLineAfter).isEqualTo(3)
136-
assertThat(hunk.endLineBefore).isEqualTo(3)
137-
val inputPatchLines = inputPatch.split("\n")
138-
hunk.lines.forEachIndexed { index, patchLine -> assertThat(inputPatchLines[index + 1].substring(1)).isEqualTo(patchLine.text) }
139-
}
14097
}

0 commit comments

Comments
 (0)