Commit b281936e authored by Kirill.Krylov's avatar Kirill.Krylov Committed by intellij-monorepo-bot
Browse files

[inline-completion] IDEA-333597 add id to InlineCompletionProvider and use it, not kclass

GitOrigin-RevId: 61bb68dcf1f5f80a90bd4d252eb72801439d2c7f
parent 1b278d1d
Branches unavailable Tags unavailable
No related merge requests found
Showing with 32 additions and 18 deletions
+32 -18
......@@ -88,15 +88,15 @@ class InlineCompletionHandler internal constructor(
guardCaretModifications(request)
}
providerManager.getCache(provider::class)?.let { cached ->
println("CACHE" to provider to event)
//providerManager.getCache(provider::class)?.let { cached ->
// println("CACHE" to provider to event)
// TODO: add event logging)
//cached.forEach { newSession.context.renderElement(it, request.endOffset) }
//return
}
//}
executor.switchJobSafely(newSession::assignJob) {
invokeRequest(provider::class, request, newSession)
invokeRequest(provider.id, request, newSession)
}
}
......@@ -156,7 +156,7 @@ class InlineCompletionHandler internal constructor(
}
private suspend fun invokeRequest(
provider: KClass<out InlineCompletionProvider>,
providerId: InlineCompletionProviderID,
request: InlineCompletionRequest,
session: InlineCompletionSession,
) {
......@@ -185,7 +185,7 @@ class InlineCompletionHandler internal constructor(
}
.onCompletion {
val isActive = currentCoroutineContext().isActive
coroutineToIndicator { complete(isActive, editor, it, context, provider, suggestion) }
coroutineToIndicator { complete(isActive, editor, it, context, providerId, suggestion) }
it?.let(LOG::errorIfNotMessage)
}
.collectIndexed { index, it ->
......@@ -202,7 +202,7 @@ class InlineCompletionHandler internal constructor(
editor: Editor,
cause: Throwable?,
context: InlineCompletionContext,
provider: KClass<out InlineCompletionProvider>,
providerId: InlineCompletionProviderID,
suggestion: InlineCompletionSuggestion,
) {
trace(InlineCompletionEventType.Completion(cause, isActive))
......@@ -215,7 +215,7 @@ class InlineCompletionHandler internal constructor(
return
}
if (suggestion.useCache) {
providerManager.cacheSuggestion(provider, context.state.elements)
providerManager.cacheSuggestion(providerId, context.state.elements)
}
}
......
......@@ -26,6 +26,17 @@ import kotlinx.coroutines.flow.debounce
* @see InlineCompletionEvent
*/
interface InlineCompletionProvider {
/**
* Provider identifier, must match extension `id` in `plugin.xml`. Used to identify provider.
*
* - Prefer to use class qualified name as id to avoid duplicates
*
* ```
* <inline.completion.provider id="<id_here>" .../>
* ```
*/
val id: InlineCompletionProviderID
suspend fun getSuggestion(request: InlineCompletionRequest): InlineCompletionSuggestion
fun isEnabled(event: InlineCompletionEvent): Boolean
......@@ -38,8 +49,11 @@ interface InlineCompletionProvider {
}
object DUMMY : InlineCompletionProvider {
override val id = InlineCompletionProviderID("DUMMY")
override suspend fun getSuggestion(request: InlineCompletionRequest): InlineCompletionSuggestion = InlineCompletionSuggestion.EMPTY
override fun isEnabled(event: InlineCompletionEvent): Boolean = false
}
}
@JvmInline
value class InlineCompletionProviderID(val id: String)
......@@ -7,7 +7,6 @@ import com.intellij.util.application
import org.jetbrains.annotations.ApiStatus
import org.jetbrains.annotations.TestOnly
import org.jetbrains.concurrency.errorIfNotMessage
import kotlin.reflect.KClass
@ApiStatus.Experimental
class InlineCompletionProviderManager {
......@@ -20,24 +19,24 @@ class InlineCompletionProviderManager {
*
* Temporary works for one provider only due to API limitations //TODO: add task
*/
private val suggestions = mutableMapOf<KClass<out InlineCompletionProvider>, MutableList<List<InlineCompletionElement.Presentable>>>()
private var currentIndexes = mutableMapOf<KClass<out InlineCompletionProvider>, Int>()
private val suggestions = mutableMapOf<InlineCompletionProviderID, MutableList<List<InlineCompletionElement.Presentable>>>()
private var currentIndexes = mutableMapOf<InlineCompletionProviderID, Int>()
private var providerIndex: Int = 0
internal fun cacheSuggestion(providerCls: KClass<out InlineCompletionProvider>, elements: List<InlineCompletionElement.Presentable>) {
if (suggestions.containsKey(providerCls)) {
val cachesPerProvider = suggestions.getValue(providerCls)
internal fun cacheSuggestion(providerId: InlineCompletionProviderID, elements: List<InlineCompletionElement.Presentable>) {
if (suggestions.containsKey(providerId)) {
val cachesPerProvider = suggestions.getValue(providerId)
cachesPerProvider.add(elements)
currentIndexes[providerCls] = cachesPerProvider.size - 1
currentIndexes[providerId] = cachesPerProvider.size - 1
}
else {
suggestions[providerCls] = mutableListOf(elements)
currentIndexes[providerCls] = 0
suggestions[providerId] = mutableListOf(elements)
currentIndexes[providerId] = 0
}
}
internal fun getCache(providerCls: KClass<out InlineCompletionProvider>): List<InlineCompletionElement.Presentable>? {
internal fun getCache(providerCls: InlineCompletionProviderID): List<InlineCompletionElement.Presentable>? {
val index = currentIndexes[providerCls] ?: return null
val suggestion = suggestions[providerCls] ?: return null
return suggestion.getOrNull(index)
......
......@@ -36,6 +36,7 @@ class TerminalInlineCompletion(private val scope: CoroutineScope) {
}
class TerminalInlineCompletionProvider : InlineCompletionProvider {
override val id: InlineCompletionProviderID = InlineCompletionProviderID("TerminalInlineCompletionProvider")
override suspend fun getSuggestion(request: InlineCompletionRequest): InlineCompletionSuggestion {
val suggestion = flow {
withContext(Dispatchers.EDT) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment