|
| 1 | +package ai.codesetu.completion |
| 2 | + |
| 3 | +import ai.codesetu.provider.CodeSetuProviderClient |
| 4 | +import ai.codesetu.settings.CodeSetuSettingsState |
| 5 | +import com.intellij.codeInsight.inline.completion.DebouncedInlineCompletionProvider |
| 6 | +import com.intellij.codeInsight.inline.completion.InlineCompletionEvent |
| 7 | +import com.intellij.codeInsight.inline.completion.InlineCompletionProviderID |
| 8 | +import com.intellij.codeInsight.inline.completion.InlineCompletionRequest |
| 9 | +import com.intellij.codeInsight.inline.completion.elements.InlineCompletionGrayTextElement |
| 10 | +import com.intellij.codeInsight.inline.completion.suggestion.InlineCompletionSingleSuggestion |
| 11 | +import com.intellij.codeInsight.inline.completion.suggestion.InlineCompletionSuggestion |
| 12 | +import com.intellij.openapi.application.readAction |
| 13 | +import com.intellij.openapi.util.UserDataHolderBase |
| 14 | +import java.util.concurrent.atomic.AtomicReference |
| 15 | +import kotlin.time.Duration |
| 16 | +import kotlin.time.Duration.Companion.milliseconds |
| 17 | +import kotlinx.coroutines.Dispatchers |
| 18 | +import kotlinx.coroutines.flow.flowOf |
| 19 | +import kotlinx.coroutines.withContext |
| 20 | + |
| 21 | +/** |
| 22 | + * Ghost-text fill-in-the-middle completions as you type. Extends the platform's |
| 23 | + * [DebouncedInlineCompletionProvider] so a burst of keystrokes only fires one |
| 24 | + * request and a superseding keystroke cancels the in-flight coroutine. A |
| 25 | + * one-entry cache serves an identical re-request (same prefix + suffix) without |
| 26 | + * another round-trip. Mirrors the VS Code inline-completion provider. |
| 27 | + */ |
| 28 | +class CodeSetuInlineCompletionProvider : DebouncedInlineCompletionProvider() { |
| 29 | + private val client = CodeSetuProviderClient() |
| 30 | + private val cache = AtomicReference<CacheEntry?>(null) |
| 31 | + |
| 32 | + override val id = InlineCompletionProviderID("CodeSetu") |
| 33 | + |
| 34 | + override fun isEnabled(event: InlineCompletionEvent): Boolean { |
| 35 | + if (!CodeSetuSettingsState.getInstance().state.inlineCompletionsEnabled) return false |
| 36 | + // Trigger while typing and on an explicit manual call; ignore the noisier |
| 37 | + // events (lookup, focus) that would fire requests the user didn't ask for. |
| 38 | + return event is InlineCompletionEvent.DocumentChange || event is InlineCompletionEvent.DirectCall |
| 39 | + } |
| 40 | + |
| 41 | + override suspend fun getDebounceDelay(request: InlineCompletionRequest): Duration = |
| 42 | + CodeSetuSettingsState.getInstance().state.fimDebounceMs.coerceAtLeast(0).milliseconds |
| 43 | + |
| 44 | + override suspend fun getSuggestionDebounced( |
| 45 | + request: InlineCompletionRequest, |
| 46 | + ): InlineCompletionSuggestion { |
| 47 | + val settings = CodeSetuSettingsState.getInstance().state |
| 48 | + val (text, offset) = readAction { request.document.text to request.endOffset } |
| 49 | + val fim = buildFimContext(text, offset, settings.fimMaxPrefixChars, settings.fimMaxSuffixChars) |
| 50 | + if (fim.prompt.isEmpty() && fim.suffix.isEmpty()) return InlineCompletionSuggestion.Empty |
| 51 | + |
| 52 | + cache.get()?.let { entry -> |
| 53 | + if (entry.prompt == fim.prompt && entry.suffix == fim.suffix) return grayText(entry.text) |
| 54 | + } |
| 55 | + |
| 56 | + val completion = withContext(Dispatchers.IO) { |
| 57 | + runCatching { |
| 58 | + client.completeFim( |
| 59 | + prompt = fim.prompt, |
| 60 | + suffix = fim.suffix, |
| 61 | + maxTokens = settings.fimMaxTokens, |
| 62 | + stop = listOf("\n\n", "\n```"), |
| 63 | + ) |
| 64 | + }.getOrDefault("") |
| 65 | + } |
| 66 | + |
| 67 | + if (completion.isEmpty()) return InlineCompletionSuggestion.Empty |
| 68 | + cache.set(CacheEntry(fim.prompt, fim.suffix, completion)) |
| 69 | + return grayText(completion) |
| 70 | + } |
| 71 | + |
| 72 | + private fun grayText(text: String): InlineCompletionSuggestion = |
| 73 | + InlineCompletionSingleSuggestion.build( |
| 74 | + UserDataHolderBase(), |
| 75 | + flowOf(InlineCompletionGrayTextElement(text)), |
| 76 | + ) |
| 77 | + |
| 78 | + private data class CacheEntry(val prompt: String, val suffix: String, val text: String) |
| 79 | +} |
0 commit comments