diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/nextEdit/NextEditService.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/nextEdit/NextEditService.kt index ca2a7d312..f9279b9b9 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/nextEdit/NextEditService.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/nextEdit/NextEditService.kt @@ -1,5 +1,6 @@ package com.github.continuedev.continueintellijextension.nextEdit +import com.github.continuedev.continueintellijextension.FimResult import com.github.continuedev.continueintellijextension.autocomplete.AutocompleteLookupListener import com.github.continuedev.continueintellijextension.autocomplete.AutocompleteSpinnerWidget import com.github.continuedev.continueintellijextension.autocomplete.ContinueInlayRenderer @@ -8,6 +9,7 @@ import com.github.continuedev.continueintellijextension.autocomplete.addInlayEle import com.github.continuedev.continueintellijextension.autocomplete.isInjectedText import com.github.continuedev.continueintellijextension.services.ContinueExtensionSettings import com.github.continuedev.continueintellijextension.services.ContinuePluginService +import com.github.continuedev.continueintellijextension.utils.checkFim import com.github.continuedev.continueintellijextension.utils.toUriOrNull import com.github.continuedev.continueintellijextension.utils.uuid import com.intellij.openapi.application.ApplicationManager @@ -117,77 +119,6 @@ class NextEditService(private val project: Project) { ) } - sealed class FimResult { - data class FimEdit(val fimText: String) : FimResult() - object NotFimEdit : FimResult() - } - /** - * Check if the diff is indeed a FIM (Fill-In-Middle). - * @param oldEditRange Original string content. - * @param newEditRange New string content. - * @param cursorPosition The position of the cursor in the old string. - * @return A Pair where the first value is a boolean indicating if the change is purely additive (FIM) - * and the second value is the FIM text content or null if not a FIM. - */ - private fun checkFim( - oldEditRange: String, - newEditRange: String, - cursorPosition: Pair // line, character - ): FimResult { - // Find the common prefix - var prefixLength = 0 - while (prefixLength < oldEditRange.length && - prefixLength < newEditRange.length && - oldEditRange[prefixLength] == newEditRange[prefixLength]) { - prefixLength++ - } - - // Find the common suffix - var oldSuffixPos = oldEditRange.length - 1 - var newSuffixPos = newEditRange.length - 1 - - while (oldSuffixPos >= prefixLength && - newSuffixPos >= prefixLength && - oldEditRange[oldSuffixPos] == newEditRange[newSuffixPos]) { - oldSuffixPos-- - newSuffixPos-- - } - - // The old text is purely preserved if: - // 1. The prefix ends before or at the cursor. - // 2. The suffix starts after or at the cursor. - // 3. There's no gap between prefix and suffix in the old text. - - val suffixStartInOld = oldSuffixPos + 1 - val suffixStartInNew = newSuffixPos + 1 - - // Convert cursor position to an offset in the string. - // For simplicity, we need to calculate the cursor's position in the string. - // This requires knowledge of line endings in the oldEditRange. - val lines = oldEditRange.substring(0, prefixLength).split("\n") - val cursorOffset = if (lines.size > 1) { - lines.dropLast(1).sumOf { it.length + 1 } + cursorPosition.second - } else { - cursorPosition.second - } - - // Check if the cursor is positioned between the prefix and suffix. - val cursorBetweenPrefixAndSuffix = - prefixLength <= cursorOffset && cursorOffset <= suffixStartInOld - - // Check if the old text is completely preserved (no deletion). - val noTextDeleted = suffixStartInOld - prefixLength <= 0 - - val isFim = cursorBetweenPrefixAndSuffix && noTextDeleted - - return if (isFim) { - // Extract the content between prefix and suffix in the new string. - FimResult.FimEdit(newEditRange.substring(prefixLength, suffixStartInNew)) - } else { - FimResult.NotFimEdit - } - } - private fun shouldRenderCompletion(completion: String, offset: Int, line: Int, editor: Editor): Boolean { if (completion.isEmpty() || runReadAction { offset != editor.caretModel.offset }) { return false diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/types.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/types.kt index 5ff9e9454..dae734881 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/types.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/types.kt @@ -247,4 +247,9 @@ data class AcceptOrRejectDiffPayload( data class ShowFilePayload( val filepath: String -) \ No newline at end of file +) + +sealed class FimResult { + data class FimEdit(val fimText: String) : FimResult() + object NotFimEdit : FimResult() +} \ No newline at end of file diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/utils/Utils.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/utils/Utils.kt index 52a4884f0..888b59359 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/utils/Utils.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/utils/Utils.kt @@ -1,5 +1,6 @@ package com.github.continuedev.continueintellijextension.utils +import com.github.continuedev.continueintellijextension.FimResult import com.intellij.openapi.vfs.VirtualFile import java.net.NetworkInterface import java.util.* @@ -117,4 +118,71 @@ fun getOsAndArchTarget(): String { } return "$osStr-$arch" +} + +/** + * Check if the diff is indeed a FIM (Fill-In-Middle). + * @param oldEditRange Original string content. + * @param newEditRange New string content. + * @param cursorPosition The position of the cursor in the old string. + * @return A Pair where the first value is a boolean indicating if the change is purely additive (FIM) + * and the second value is the FIM text content or null if not a FIM. + */ +fun checkFim( + oldEditRange: String, + newEditRange: String, + cursorPosition: Pair // line, character +): FimResult { + // Find the common prefix + var prefixLength = 0 + while (prefixLength < oldEditRange.length && + prefixLength < newEditRange.length && + oldEditRange[prefixLength] == newEditRange[prefixLength]) { + prefixLength++ + } + + // Find the common suffix + var oldSuffixPos = oldEditRange.length - 1 + var newSuffixPos = newEditRange.length - 1 + + while (oldSuffixPos >= prefixLength && + newSuffixPos >= prefixLength && + oldEditRange[oldSuffixPos] == newEditRange[newSuffixPos]) { + oldSuffixPos-- + newSuffixPos-- + } + + // The old text is purely preserved if: + // 1. The prefix ends before or at the cursor. + // 2. The suffix starts after or at the cursor. + // 3. There's no gap between prefix and suffix in the old text. + + val suffixStartInOld = oldSuffixPos + 1 + val suffixStartInNew = newSuffixPos + 1 + + // Convert cursor position to an offset in the string. + // For simplicity, we need to calculate the cursor's position in the string. + // This requires knowledge of line endings in the oldEditRange. + val lines = oldEditRange.substring(0, prefixLength).split("\n") + val cursorOffset = if (lines.size > 1) { + lines.dropLast(1).sumOf { it.length + 1 } + cursorPosition.second + } else { + cursorPosition.second + } + + // Check if the cursor is positioned between the prefix and suffix. + val cursorBetweenPrefixAndSuffix = + prefixLength <= cursorOffset && cursorOffset <= suffixStartInOld + + // Check if the old text is completely preserved (no deletion). + val noTextDeleted = suffixStartInOld - prefixLength <= 0 + + val isFim = cursorBetweenPrefixAndSuffix && noTextDeleted + + return if (isFim) { + // Extract the content between prefix and suffix in the new string. + FimResult.FimEdit(newEditRange.substring(prefixLength, suffixStartInNew)) + } else { + FimResult.NotFimEdit + } } \ No newline at end of file