From 185b6998f004e455307ca387f22fd3e062aadd91 Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Mon, 7 Jul 2025 18:36:57 -0700 Subject: [PATCH] lay a foundation for next edit service --- .../nextEdit/NextEditService.kt | 391 ++++++++++++++++++ 1 file changed, 391 insertions(+) create mode 100644 extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/nextEdit/NextEditService.kt 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 new file mode 100644 index 000000000..9095a6dd6 --- /dev/null +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/nextEdit/NextEditService.kt @@ -0,0 +1,391 @@ +package com.github.continuedev.continueintellijextension.nextEdit + +import com.github.continuedev.continueintellijextension.autocomplete.AutocompleteLookupListener +import com.github.continuedev.continueintellijextension.autocomplete.AutocompleteSpinnerWidget +import com.github.continuedev.continueintellijextension.autocomplete.ContinueInlayRenderer +import com.github.continuedev.continueintellijextension.autocomplete.PendingCompletion +import com.github.continuedev.continueintellijextension.autocomplete.addInlayElement +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.toUriOrNull +import com.github.continuedev.continueintellijextension.utils.uuid +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.WriteAction +import com.intellij.openapi.application.invokeLater +import com.intellij.openapi.application.runReadAction +import com.intellij.openapi.components.Service +import com.intellij.openapi.components.service +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.editor.InlayProperties +import com.intellij.openapi.fileEditor.FileDocumentManager +import com.intellij.openapi.project.Project +import com.intellij.openapi.util.TextRange +import com.intellij.openapi.wm.WindowManager +import com.intellij.psi.PsiDocumentManager + +@Service(Service.Level.PROJECT) +class NextEditService(private val project: Project) { + var pendingCompletion: PendingCompletion? = null + private val autocompleteLookupListener = project.service() + private val widget: AutocompleteSpinnerWidget? by lazy { + WindowManager.getInstance().getStatusBar(project) + ?.getWidget(AutocompleteSpinnerWidget.ID) as? AutocompleteSpinnerWidget + } + + // To avoid triggering another completion on partial acceptance, + // we need to keep track of whether the last change was a partial accept + var lastChangeWasPartialAccept = false + + fun triggerCompletion(editor: Editor) { + val settings = service() + if (!settings.continueState.enableTabAutocomplete) { + return + } + + if (pendingCompletion != null) { + clearCompletions(pendingCompletion!!.editor) + } + + // Set pending completion + val completionId = uuid() + val offset = editor.caretModel.primaryCaret.offset + pendingCompletion = PendingCompletion(editor, offset, completionId, null) + + // Request a completion from the core + val virtualFile = FileDocumentManager.getInstance().getFile(editor.document) + + val uri = virtualFile?.toUriOrNull() ?: return + + widget?.setLoading(true) + + val line = editor.caretModel.primaryCaret.logicalPosition.line + val column = editor.caretModel.primaryCaret.logicalPosition.column + val input = mapOf( + "completionId" to completionId, + "filepath" to uri, + "pos" to mapOf( + "line" to line, + "character" to column + ), + "clipboardText" to "", + "recentlyEditedRanges" to emptyList(), + "recentlyVisitedRanges" to emptyList(), + ) + + // TODO: Add message support for nextEdit/predict. + project.service().coreMessenger?.request( + "nextEdit/predict", + input, + null, + ({ response -> + if (pendingCompletion == null || pendingCompletion?.completionId == completionId) { + widget?.setLoading(false) + } + + val responseObject = response as Map<*, *> + val predictions = responseObject["content"] as List<*> + + if (predictions.isNotEmpty()) { + val prediction = predictions[0].toString() + // TODO: Check if the prediction is purely additive. + val result = checkFim( + editor.document.text, + prediction, + Pair(line, column) + ) + when (result) { + is FimResult.FimEdit -> { + // TODO: If so, render ghost text. + val finalTextToInsert = deduplicateCompletion(editor, offset, result.fimText) + if (shouldRenderCompletion(finalTextToInsert, offset, line, editor)) { + renderCompletion(editor, offset, finalTextToInsert) + pendingCompletion = PendingCompletion(editor, offset, completionId, finalTextToInsert) + } + } + is FimResult.NotFimEdit -> { + // TODO: Else, render a window. + } + } +// val finalTextToInsert = deduplicateCompletion(editor, offset, completion) +// +// if (shouldRenderCompletion(finalTextToInsert, offset, line, editor)) { +// renderCompletion(editor, offset, finalTextToInsert) +// pendingCompletion = PendingCompletion(editor, offset, completionId, finalTextToInsert) +// } + } + }) + ) + } + + 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 + } + + if (completion.lines().size == 1) { + return true + } + + val endOffset = editor.document.getLineEndOffset(line) + + // Do not render if completion is multi-line and caret is in middle of line + return offset <= endOffset && editor.document.getText(TextRange(offset, endOffset)).isBlank() + } + + private fun deduplicateCompletion(editor: Editor, offset: Int, completion: String): String { + // Check if completion matches the first 10 characters after the cursor + return ApplicationManager.getApplication().runReadAction { + val document = editor.document + val caretOffset = editor.caretModel.offset + + // Don't care about it if it's at the end of the document + if (caretOffset == document.textLength) return@runReadAction completion + + val N = 10 + var textAfterCursor = if (caretOffset + N <= document.textLength) { + document.getText(TextRange(caretOffset, caretOffset + N)) + } else { + document.getText(TextRange(caretOffset, document.textLength)) + } + + // Avoid truncating the completion text when the text after the cursor is blank + if (textAfterCursor.isBlank()) return@runReadAction completion + + // Determine the index of a newline character within the text following the cursor. + val newlineIndex = textAfterCursor.indexOf("\r\n").takeIf { it >= 0 } ?: textAfterCursor.indexOf('\n') + // If a newline character is found and the current line is not empty, truncate the text at that point. + if (newlineIndex > 0) { + textAfterCursor = textAfterCursor.substring(0, newlineIndex) + } + + val indexOfTextAfterCursorInCompletion = completion.indexOf(textAfterCursor) + if (indexOfTextAfterCursorInCompletion > 0) { + return@runReadAction completion.slice(0..indexOfTextAfterCursorInCompletion - 1) + } else if (indexOfTextAfterCursorInCompletion == 0) { + return@runReadAction "" + } + + return@runReadAction completion + } + } + + private fun renderCompletion(editor: Editor, offset: Int, completion: String) { + if (completion.isEmpty()) { + return + } + if (isInjectedFile(editor)) return + // Skip rendering completions if the code completion dropdown is already visible and the IDE completion side-by-side setting is disabled + if (shouldSkipRender()) { + return + } + + ApplicationManager.getApplication().invokeLater { + WriteAction.run { + // Clear existing completions + hideCompletions(editor) + + val properties = InlayProperties() + properties.relatesToPrecedingText(true) + properties.disableSoftWrapping(true) + + val lines = completion.lines() + pendingCompletion = pendingCompletion?.copy(text = lines.joinToString("\n")) + editor.addInlayElement(lines, offset, properties) + +// val attributes = TextAttributes().apply { +// backgroundColor = JBColor.GREEN +// } +// val key = TextAttributesKey.createTextAttributesKey("CONTINUE_AUTOCOMPLETE") +// key.let { editor.colorsScheme.setAttributes(it, attributes) } +// editor.markupModel.addLineHighlighter(key, editor.caretModel.logicalPosition.line, HighlighterLayer.LAST) + } + } + } + + fun accept() { + val completion = pendingCompletion ?: return + val text = completion.text ?: return + val editor = completion.editor + val offset = completion.offset + editor.document.insertString(offset, text) + + editor.caretModel.moveToOffset(offset + text.length) + + project.service().coreMessenger?.request( + "autocomplete/accept", + hashMapOf("completionId" to completion.completionId), + null, + ({}) + ) + invokeLater { + clearCompletions(editor, completion) + } + } + + private fun shouldSkipRender(): Boolean { + val settings = service() + return !settings.continueState.showIDECompletionSideBySide && !autocompleteLookupListener.isLookupEmpty() + } + + private fun splitKeepingDelimiters(input: String, delimiterPattern: String = "\\s+"): List { + val initialSplit = input.split("(?<=$delimiterPattern)|(?=$delimiterPattern)".toRegex()) + .filter { it.isNotEmpty() } + + val result = mutableListOf() + var currentDelimiter = "" + + for (part in initialSplit) { + if (part.matches(delimiterPattern.toRegex())) { + currentDelimiter += part + } else { + if (currentDelimiter.isNotEmpty()) { + result.add(currentDelimiter) + currentDelimiter = "" + } + result.add(part) + } + } + + if (currentDelimiter.isNotEmpty()) { + result.add(currentDelimiter) + + } + + return result + } + + fun partialAccept() { + val completion = pendingCompletion ?: return + val text = completion.text ?: return + val editor = completion.editor + val offset = completion.offset + + lastChangeWasPartialAccept = true + + // Split the text into words, keeping delimiters + val words = splitKeepingDelimiters(text) + println(words) + val word = words[0] + editor.document.insertString(offset, word) + editor.caretModel.moveToOffset(offset + word.length) + + // Remove the completion and re-display it + hideCompletions(editor) + completion.text = text.substring(word.length) + completion.offset += word.length + renderCompletion(editor, completion.offset, completion.text!!) + } + + private fun cancelCompletion(completion: PendingCompletion) { + // Send cancellation message to core + widget?.setLoading(false) + project.service().coreMessenger?.request("autocomplete/cancel", null, null, ({})) + } + + fun clearCompletions(editor: Editor, completion: PendingCompletion? = pendingCompletion) { + if (isInjectedFile(editor)) return + + if (completion != null) { + cancelCompletion(completion) + if (completion.completionId == pendingCompletion?.completionId) pendingCompletion = null + } + disposeInlayRenderer(editor) + } + + private fun isInjectedFile(editor: Editor): Boolean { + return runReadAction { + PsiDocumentManager.getInstance(project).getPsiFile(editor.document)?.isInjectedText() ?: false + } + } + + fun hideCompletions(editor: Editor) { + if (isInjectedFile(editor)) return + + disposeInlayRenderer(editor) + } + + private fun disposeInlayRenderer(editor: Editor) { + editor.inlayModel.getInlineElementsInRange(0, editor.document.textLength).forEach { + if (it.renderer is ContinueInlayRenderer) { + it.dispose() + } + } + editor.inlayModel.getBlockElementsInRange(0, editor.document.textLength).forEach { + if (it.renderer is ContinueInlayRenderer) { + it.dispose() + } + } + } +} \ No newline at end of file