|
| 1 | +// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package software.aws.toolkits.jetbrains.services.codewhisperer.inlay |
| 4 | + |
| 5 | +import com.intellij.codeInsight.inline.completion.render.InlineBlockElementRenderer |
| 6 | +import com.intellij.openapi.editor.Editor |
| 7 | +import com.intellij.openapi.editor.EditorCustomElementRenderer |
| 8 | +import com.intellij.openapi.editor.markup.TextAttributes |
| 9 | +import com.intellij.xdebugger.ui.DebuggerColors |
| 10 | + |
| 11 | +// from 232-241.1, we have `InlineSuffixRenderer`, but with 241.2+ it becomes `InlineCompletionLineRenderer` |
| 12 | +// for both line and block inlays. Also InlineBlockElementRenderer is deprecated |
| 13 | +// 242 is not yet handled by this |
| 14 | +object InlineCompletionRemoteRendererFactory { |
| 15 | + private var hasOldLineConstructor = true |
| 16 | + private val lineConstructor = run { |
| 17 | + val clazz = |
| 18 | + try { |
| 19 | + Class.forName("com.intellij.codeInsight.inline.completion.render.InlineSuffixRenderer") |
| 20 | + } catch (e: ClassNotFoundException) { |
| 21 | + hasOldLineConstructor = false |
| 22 | + Class.forName("com.intellij.codeInsight.inline.completion.render.InlineCompletionLineRenderer") |
| 23 | + } |
| 24 | + if (hasOldLineConstructor) { |
| 25 | + clazz.getConstructor(Editor::class.java, String::class.java) |
| 26 | + } else { |
| 27 | + clazz.getConstructor(Editor::class.java, String::class.java, TextAttributes::class.java) |
| 28 | + } |
| 29 | + } |
| 30 | + private var hasNewBlockConstructor = true |
| 31 | + private val blockConstructor = run { |
| 32 | + val clazz = |
| 33 | + try { |
| 34 | + Class.forName("com.intellij.codeInsight.inline.completion.render.InlineCompletionLineRenderer") |
| 35 | + } catch (e: ClassNotFoundException) { |
| 36 | + hasNewBlockConstructor = false |
| 37 | + Class.forName("com.intellij.codeInsight.inline.completion.render.InlineBlockElementRenderer") |
| 38 | + } |
| 39 | + clazz.getConstructor(Editor::class.java, List::class.java) |
| 40 | + } |
| 41 | + |
| 42 | + fun createLineInlay(editor: Editor, text: String): EditorCustomElementRenderer = |
| 43 | + ( |
| 44 | + if (hasOldLineConstructor) { |
| 45 | + lineConstructor.newInstance(editor, text) |
| 46 | + } else { |
| 47 | + lineConstructor.newInstance(editor, text, editor.colorsScheme.getAttributes(DebuggerColors.INLINED_VALUES_EXECUTION_LINE)) |
| 48 | + } |
| 49 | + ) as EditorCustomElementRenderer |
| 50 | + |
| 51 | + fun createBlockInlays(editor: Editor, block: List<String>): List<EditorCustomElementRenderer> = |
| 52 | + if (hasNewBlockConstructor) { |
| 53 | + // 241.2+ |
| 54 | + val textBlockClazz = Class.forName("com.intellij.codeInsight.inline.completion.render.InlineCompletionRenderTextBlock") |
| 55 | + val textBlockConstructor = textBlockClazz.getConstructor(String::class.java, TextAttributes::class.java) |
| 56 | + block.map { |
| 57 | + blockConstructor.newInstance( |
| 58 | + editor, |
| 59 | + listOf(textBlockConstructor.newInstance(it, editor.colorsScheme.getAttributes(DebuggerColors.INLINED_VALUES_EXECUTION_LINE))) |
| 60 | + ) as EditorCustomElementRenderer |
| 61 | + } |
| 62 | + } else { |
| 63 | + listOf(InlineBlockElementRenderer(editor, block)) |
| 64 | + } |
| 65 | +} |
0 commit comments