|
| 1 | +package com.tabbyml.intellijtabby.inlineChat |
| 2 | + |
| 3 | +import com.intellij.codeInsight.hints.* |
| 4 | +import com.intellij.lang.Language |
| 5 | +import com.intellij.openapi.components.serviceOrNull |
| 6 | +import com.intellij.openapi.editor.Editor |
| 7 | +import com.intellij.psi.PsiElement |
| 8 | +import com.intellij.psi.PsiFile |
| 9 | +import com.tabbyml.intellijtabby.lsp.ConnectionService |
| 10 | +import kotlinx.coroutines.CoroutineScope |
| 11 | +import org.eclipse.lsp4j.CodeLens |
| 12 | +import org.eclipse.lsp4j.CodeLensParams |
| 13 | +import org.eclipse.lsp4j.TextDocumentIdentifier |
| 14 | +import java.util.concurrent.CompletableFuture |
| 15 | +import java.util.concurrent.ExecutionException |
| 16 | +import kotlinx.coroutines.Dispatchers |
| 17 | +import kotlinx.coroutines.launch |
| 18 | +import javax.swing.JComponent |
| 19 | +import javax.swing.JPanel |
| 20 | + |
| 21 | + |
| 22 | +class CodeLensInlayHintsProvider : |
| 23 | + InlayHintsProvider<Any> { |
| 24 | + private val scope = CoroutineScope(Dispatchers.IO) |
| 25 | + |
| 26 | + override fun getCollectorFor( |
| 27 | + file: PsiFile, |
| 28 | + editor: Editor, |
| 29 | + settings: Any, |
| 30 | + sink: InlayHintsSink |
| 31 | + ): InlayHintsCollector? { |
| 32 | + return object : FactoryInlayHintsCollector(editor) { |
| 33 | + override fun collect(element: PsiElement, editor: Editor, sink: InlayHintsSink): Boolean { |
| 34 | + try { |
| 35 | + val codeLenses = getCodeLenses(element.containingFile).get() ?: return false |
| 36 | + for (codeLens in codeLenses) { |
| 37 | + val range = codeLens.range |
| 38 | + if (range == null || codeLens.command == null) continue |
| 39 | + |
| 40 | + val start = range.start |
| 41 | + val line = start.line |
| 42 | + val offset = editor.document.getLineStartOffset(line) |
| 43 | + |
| 44 | + // Create the inlay presentation for the CodeLens |
| 45 | + val presentation = factory.smallText(codeLens.command.title) |
| 46 | + |
| 47 | + // Add to the editor |
| 48 | + sink.addBlockElement(offset, true, true, 0, presentation) |
| 49 | + } |
| 50 | + } catch (e: InterruptedException) { |
| 51 | + // Handle exceptions |
| 52 | + } catch (e: ExecutionException) { |
| 53 | + // Handle exceptions |
| 54 | + } |
| 55 | + return true |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + fun getCodeLenses(psiFile: PsiFile): CompletableFuture<List<CodeLens>?> { |
| 61 | + val virtualFile = psiFile.virtualFile |
| 62 | + ?: return CompletableFuture.completedFuture(listOf()) |
| 63 | + val uri = virtualFile.url.replace("file://", "file:/") |
| 64 | + val params = CodeLensParams(TextDocumentIdentifier(uri)) |
| 65 | + return CompletableFuture<List<CodeLens>?>().also { future -> |
| 66 | + scope.launch { |
| 67 | + try { |
| 68 | + val server = psiFile.project.serviceOrNull<ConnectionService>()?.getServerAsync() ?: run { |
| 69 | + future.complete(null) |
| 70 | + return@launch |
| 71 | + } |
| 72 | + val result = server.textDocumentFeature.codeLens(params) |
| 73 | + future.complete(result.get()) |
| 74 | + } catch (e: Exception) { |
| 75 | + future.completeExceptionally(e) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + override fun createSettings(): NoSettings { |
| 82 | + return NoSettings() |
| 83 | + } |
| 84 | + |
| 85 | + override fun isLanguageSupported(language: Language): Boolean { |
| 86 | + // Specify which languages should support CodeLens |
| 87 | + return true // or check specific languages |
| 88 | + } |
| 89 | + |
| 90 | + override val key: SettingsKey<Any> = SettingsKey("Tabby") |
| 91 | + |
| 92 | + override val name: String = "Tabby CodeLens" |
| 93 | + override val previewText: String = "Tabby CodeLens" |
| 94 | + |
| 95 | + override fun createConfigurable(settings: Any): ImmediateConfigurable { |
| 96 | + return object : ImmediateConfigurable { |
| 97 | + override val cases: List<ImmediateConfigurable.Case> = emptyList() |
| 98 | + override fun createComponent(listener: ChangeListener): JComponent { |
| 99 | + return JPanel() |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments