in sdk/src/main/kotlin/ai/jetbrains/code/mellum/sdk/lsp/MellumCompletionHandler.kt [36:59]
override fun completion(params: CompletionParams): CompletableFuture<Either<List<CompletionItem>, CompletionList>> {
return CompletableFuture.supplyAsync<Either<List<CompletionItem>, CompletionList>> {
val documentPosition = DocumentProvider.Position(params.position.line, params.position.character)
val completionResult = MellumCompletionService(
documentProvider, languageProvider, fileSystemProvider, workspaceProvider, ollamaClient, modelName
).getCompletion(
URI(params.textDocument.uri), documentPosition
)
val fileContent = runBlocking { documentProvider.charsByPath(URI(params.textDocument.uri)) }
?: return@supplyAsync Either.forLeft(emptyList())
val offset = documentPosition.toOffset(fileContent)
val textOnLineBeforeCursor = fileContent
.substring(0, offset)
.substringAfterLast("\n") // get the last line
.substringAfterLast(" ") // get the last word before completion if present
.trim()
val formatCompletionResult = textOnLineBeforeCursor + completionResult
val completionItem = CompletionItem(formatCompletionResult)
completionItem.insertText = formatCompletionResult
Either.forLeft(listOf(completionItem))
}
}