|
| 1 | +// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package software.aws.toolkits.jetbrains.services.amazonq.lsp.codewhisperer |
| 5 | + |
| 6 | +import com.intellij.codeInsight.lookup.Lookup |
| 7 | +import com.intellij.codeInsight.lookup.LookupEvent |
| 8 | +import com.intellij.codeInsight.lookup.LookupListener |
| 9 | +import com.intellij.codeInsight.lookup.LookupManagerListener |
| 10 | +import com.intellij.codeInsight.lookup.impl.LookupImpl |
| 11 | +import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLspService |
| 12 | +import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.textDocument.InlineCompletionContext |
| 13 | +import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.textDocument.InlineCompletionListWithReferences |
| 14 | +import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.textDocument.InlineCompletionTriggerKind |
| 15 | +import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.textDocument.InlineCompletionWithReferencesParams |
| 16 | +import com.intellij.openapi.Disposable |
| 17 | +import com.intellij.openapi.editor.Editor |
| 18 | +import org.eclipse.lsp4j.Position |
| 19 | +import org.eclipse.lsp4j.TextDocumentIdentifier |
| 20 | +import com.intellij.openapi.project.Project |
| 21 | + |
| 22 | +class AWSTextDocumentServiceHandler( |
| 23 | + private val project: Project, |
| 24 | + serverInstance: Disposable |
| 25 | +) : LookupManagerListener { |
| 26 | + |
| 27 | + init { |
| 28 | + project.messageBus.connect(serverInstance).subscribe( |
| 29 | + LookupManagerListener.TOPIC, |
| 30 | + this |
| 31 | + ) |
| 32 | + } |
| 33 | + |
| 34 | + override fun activeLookupChanged(oldLookup: Lookup?, newLookup: Lookup?) { |
| 35 | + if (oldLookup != null || newLookup == null) return |
| 36 | + |
| 37 | + newLookup.addLookupListener(object : LookupListener { |
| 38 | + override fun itemSelected(event: LookupEvent) { |
| 39 | + val editor = event.lookup.editor |
| 40 | + if (!(event.lookup as LookupImpl).isShown) { |
| 41 | + cleanup() |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + handleInlineCompletion(editor) |
| 46 | + cleanup() |
| 47 | + } |
| 48 | + |
| 49 | + override fun lookupCanceled(event: LookupEvent) { |
| 50 | + cleanup() |
| 51 | + } |
| 52 | + |
| 53 | + private fun cleanup() { |
| 54 | + newLookup.removeLookupListener(this) |
| 55 | + } |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + private fun handleInlineCompletion(editor: Editor) { |
| 60 | + AmazonQLspService.executeIfRunning(project) { server -> |
| 61 | + val params = buildInlineCompletionParams(editor) |
| 62 | + server.inlineCompletionWithReferences(params) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private fun buildInlineCompletionParams(editor: Editor): InlineCompletionWithReferencesParams { |
| 67 | + return InlineCompletionWithReferencesParams().apply { |
| 68 | + textDocument = TextDocumentIdentifier(getDocumentUri(editor)) |
| 69 | + position = Position( |
| 70 | + editor.caretModel.logicalPosition.line, |
| 71 | + editor.caretModel.logicalPosition.column |
| 72 | + ) |
| 73 | + context = InlineCompletionContext().apply { |
| 74 | + triggerKind = InlineCompletionTriggerKind.Invoke |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments