Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support IDE version 2023.3.* #20

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ org.gradle.caching=true
systemProp.org.gradle.unsafe.kotlin.assignment = true

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 232
pluginSinceBuild = 233
pluginUntilBuild = 233.*

pluginGroup = co.huggingface.llmintellij
pluginName = llm-intellij
pluginRepositoryUrl = https://github.com/huggingface/llm-intellij
pluginVersion = 0.0.2
pluginVersion = 0.0.3

# IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension
platformType = IU
platformVersion = 2023.2.2
platformVersion = 2023.3.3

platformPlugins =

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@ import co.huggingface.llmintellij.lsp.CompletionParams
import co.huggingface.llmintellij.lsp.LlmLsGetCompletionsRequest
import co.huggingface.llmintellij.lsp.LlmLsServerSupportProvider
import co.huggingface.llmintellij.lsp.Position
import com.intellij.codeInsight.inline.completion.InlineCompletionElement
import com.intellij.codeInsight.inline.completion.InlineCompletionEvent
import com.intellij.codeInsight.inline.completion.InlineCompletionProvider
import com.intellij.codeInsight.inline.completion.InlineCompletionProviderID
import com.intellij.codeInsight.inline.completion.InlineCompletionRequest
import com.intellij.codeInsight.inline.completion.InlineCompletionSuggestion
import com.intellij.codeInsight.inline.completion.elements.InlineCompletionGrayTextElement
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.util.Computable
import com.intellij.platform.lsp.api.LspServerManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.launch

class LlmLsCompletionProvider: InlineCompletionProvider {
class LlmLsCompletionProvider : InlineCompletionProvider {
private val logger = Logger.getInstance("inlineCompletion")

override suspend fun getProposals(request: InlineCompletionRequest): Flow<InlineCompletionElement> =
channelFlow {
override val id: InlineCompletionProviderID = InlineCompletionProviderID("LlmLsCompletionProvider")

override suspend fun getSuggestion(request: InlineCompletionRequest): InlineCompletionSuggestion {
return InlineCompletionSuggestion.Default(suggestionFlow = channelFlow {
val project = request.editor.project
if (project == null) {
logger.error("could not find project")
Expand All @@ -30,28 +35,31 @@ class LlmLsCompletionProvider: InlineCompletionProvider {
val secrets = SecretsService.instance
val lspServer = LspServerManager.getInstance(project).getServersForProvider(LlmLsServerSupportProvider::class.java).firstOrNull()
if (lspServer != null) {
val textDocument = lspServer.requestExecutor.getDocumentIdentifier(request.file.virtualFile)
val caretPosition = request.editor.caretModel.offset
val line = request.document.getLineNumber(caretPosition)
val column = caretPosition - request.document.getLineStartOffset(line)
val position = Position(line, column)
val queryParams = settings.queryParams
val fimParams = settings.fim
val tokenizerConfig = settings.tokenizer
val params = CompletionParams(textDocument, position, request_params = queryParams, fim = fimParams, api_token = secrets.getSecretSetting(), model = settings.model, tokens_to_clear = settings.tokensToClear, tokenizer_config = tokenizerConfig, context_window = settings.contextWindow)
val params = ApplicationManager.getApplication().runReadAction(Computable {
val textDocument = lspServer.requestExecutor.getDocumentIdentifier(request.file.virtualFile)
val caretPosition = request.editor.caretModel.offset
val line = request.document.getLineNumber(caretPosition)
val column = caretPosition - request.document.getLineStartOffset(line)
val position = Position(line, column)
val queryParams = settings.queryParams
val fimParams = settings.fim
val tokenizerConfig = settings.tokenizer
CompletionParams(textDocument, position, request_params = queryParams, fim = fimParams, api_token = secrets.getSecretSetting(), model = settings.model, tokens_to_clear = settings.tokensToClear, tokenizer_config = tokenizerConfig, context_window = settings.contextWindow)
})
lspServer.requestExecutor.sendRequestAsync(LlmLsGetCompletionsRequest(lspServer, params)) { response ->
CoroutineScope(Dispatchers.Default).launch {
if (response != null) {
for (completion in response.completions) {
send(InlineCompletionElement(completion.generated_text))
send(InlineCompletionGrayTextElement(completion.generated_text))
}
}
}
}
}
}
awaitClose()
}
})
}

override fun isEnabled(event: InlineCompletionEvent): Boolean {
val settings = LlmSettingsState.instance
Expand Down