diff --git a/src/main/kotlin/com/sourcegraph/cody/autocomplete/CodyAutocompleteManager.kt b/src/main/kotlin/com/sourcegraph/cody/autocomplete/CodyAutocompleteManager.kt index 252adfb10a..c6ef3b49d3 100644 --- a/src/main/kotlin/com/sourcegraph/cody/autocomplete/CodyAutocompleteManager.kt +++ b/src/main/kotlin/com/sourcegraph/cody/autocomplete/CodyAutocompleteManager.kt @@ -415,32 +415,22 @@ class CodyAutocompleteManager { .removeSuffix(originalTextAfterCursor) if (completionText.trim().isBlank()) return + // Determine if startInline or not val lineBreaks = listOf("\r\n", "\n", "\r") val startsInline = lineBreaks.none { separator -> completionText.startsWith(separator) } - var inlay: Inlay<*>? = null - if (startsInline) { - val renderer = - CodyAutocompleteSingleLineRenderer( - completionText.lines().first(), items, editor, AutocompleteRendererType.INLINE) - inlay = - inlayModel.addInlineElement(cursorOffset, /* relatesToPrecedingText = */ true, renderer) - } - val lines = completionText.lines() - if (lines.size > 1) { - val text = - (if (startsInline) lines.drop(1) else lines).dropWhile { it.isBlank() }.joinToString("\n") - val renderer = CodyAutocompleteBlockElementRenderer(text, items, editor) - val inlay2 = - inlayModel.addBlockElement( - /* offset = */ cursorOffset, - /* relatesToPrecedingText = */ true, - /* showAbove = */ false, - /* priority = */ Int.MAX_VALUE, - /* renderer = */ renderer) - if (inlay == null) { - inlay = inlay2 - } + val rendererType = if (startsInline) AutocompleteRendererType.INLINE else AutocompleteRendererType.BLOCK + val renderer = CodyAutocompleteRenderer(completionText, items, editor, rendererType) + + val inlay = if (startsInline) { + inlayModel.addInlineElement(cursorOffset, /* relatesToPrecedingText = */ true, renderer) + } else { + inlayModel.addBlockElement( + /* offset = */ cursorOffset, + /* relatesToPrecedingText = */ true, + /* showAbove = */ false, + /* priority = */ Int.MAX_VALUE, + /* renderer = */ renderer) } if (inlay?.bounds?.location != null) { diff --git a/src/main/kotlin/com/sourcegraph/cody/autocomplete/render/CodyAutocompleteBlockElementRenderer.kt b/src/main/kotlin/com/sourcegraph/cody/autocomplete/render/CodyAutocompleteBlockElementRenderer.kt deleted file mode 100644 index aeebd8e9a6..0000000000 --- a/src/main/kotlin/com/sourcegraph/cody/autocomplete/render/CodyAutocompleteBlockElementRenderer.kt +++ /dev/null @@ -1,47 +0,0 @@ -package com.sourcegraph.cody.autocomplete.render - -import com.intellij.openapi.editor.Editor -import com.intellij.openapi.editor.Inlay -import com.intellij.openapi.editor.impl.EditorImpl -import com.intellij.openapi.editor.markup.TextAttributes -import com.sourcegraph.cody.agent.protocol.AutocompleteItem -import java.awt.Font -import java.awt.Graphics -import java.awt.Rectangle - -/** Implements the logic to render an autocomplete item inline in the editor. */ -class CodyAutocompleteBlockElementRenderer( - text: String, - completionItems: List, - editor: Editor -) : CodyAutocompleteElementRenderer(text, completionItems, editor, AutocompleteRendererType.BLOCK) { - override fun calcWidthInPixels(inlay: Inlay<*>): Int { - val editor = inlay.editor as EditorImpl - val longestLine: String = - text.lines().maxWithOrNull(Comparator.comparingInt { it.length }) ?: "" - return editor.getFontMetrics(Font.PLAIN).stringWidth(longestLine) - } - - override fun calcHeightInPixels(inlay: Inlay<*>): Int { - val lineHeight = inlay.editor.lineHeight - val linesCount = text.lines().count() - return lineHeight * linesCount - } - - override fun paint( - inlay: Inlay<*>, - g: Graphics, - targetRegion: Rectangle, - textAttributes: TextAttributes - ) { - val fontInfo = fontInfoForText(text) - g.font = fontInfo.font - g.color = themeAttributes.foregroundColor - val x = targetRegion.x - val baseYOffset = fontYOffset(fontInfo).toInt() - for ((i, line) in text.lines().withIndex()) { - val y = targetRegion.y + baseYOffset + i * editor.lineHeight - g.drawString(line, x, y) - } - } -} diff --git a/src/main/kotlin/com/sourcegraph/cody/autocomplete/render/CodyAutocompleteRenderer.kt b/src/main/kotlin/com/sourcegraph/cody/autocomplete/render/CodyAutocompleteRenderer.kt new file mode 100644 index 0000000000..3e59293ba1 --- /dev/null +++ b/src/main/kotlin/com/sourcegraph/cody/autocomplete/render/CodyAutocompleteRenderer.kt @@ -0,0 +1,56 @@ +package com.sourcegraph.cody.autocomplete.render + +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.editor.Inlay +import com.intellij.openapi.editor.impl.EditorImpl +import com.intellij.openapi.editor.markup.TextAttributes +import com.sourcegraph.cody.agent.protocol.AutocompleteItem +import java.awt.Font +import java.awt.Graphics +import java.awt.Rectangle + +class CodyAutocompleteRenderer( + text: String, + completionItems: List, + editor: Editor, + type: AutocompleteRendererType +) : CodyAutocompleteElementRenderer(text, completionItems, editor, type) { + + override fun calcWidthInPixels(inlay: Inlay<*>): Int { + val editor = inlay.editor as EditorImpl + val longestLine: String = + text.lines().maxWithOrNull(Comparator.comparingInt { it.length }) ?: "" + return editor.getFontMetrics(Font.PLAIN).stringWidth(longestLine) + } + + override fun calcHeightInPixels(inlay: Inlay<*>): Int { + val lineHeight = inlay.editor.lineHeight + val linesCount = text.lines().count() + return lineHeight * linesCount + } + + override fun paint( + inlay: Inlay<*>, + g: Graphics, + targetRegion: Rectangle, + textAttributes: TextAttributes + ) { + val fontInfo = fontInfoForText(text) + g.font = fontInfo.font + g.color = themeAttributes.foregroundColor + val x = targetRegion.x + val baseYOffset = fontYOffset(fontInfo).toInt() + + if (type == AutocompleteRendererType.INLINE) { + // Single-line rendering + val y = targetRegion.y + baseYOffset + g.drawString(text, x, y) + } else { + // Block rendering + for ((i, line) in text.lines().withIndex()) { + val y = targetRegion.y + baseYOffset + i * editor.lineHeight + g.drawString(line, x, y) + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/sourcegraph/cody/autocomplete/render/CodyAutocompleteSingleLineRenderer.kt b/src/main/kotlin/com/sourcegraph/cody/autocomplete/render/CodyAutocompleteSingleLineRenderer.kt deleted file mode 100644 index a77682df4c..0000000000 --- a/src/main/kotlin/com/sourcegraph/cody/autocomplete/render/CodyAutocompleteSingleLineRenderer.kt +++ /dev/null @@ -1,29 +0,0 @@ -package com.sourcegraph.cody.autocomplete.render - -import com.intellij.openapi.editor.Editor -import com.intellij.openapi.editor.Inlay -import com.intellij.openapi.editor.markup.TextAttributes -import com.sourcegraph.cody.agent.protocol.AutocompleteItem -import java.awt.Graphics -import java.awt.Rectangle - -class CodyAutocompleteSingleLineRenderer( - text: String, - items: List, - editor: Editor, - type: AutocompleteRendererType -) : CodyAutocompleteElementRenderer(text, items, editor, type) { - override fun paint( - inlay: Inlay<*>, - g: Graphics, - targetRegion: Rectangle, - textAttributes: TextAttributes - ) { - val fontInfo = fontInfoForText(text) - g.font = fontInfo.font - g.color = themeAttributes.foregroundColor - val x = targetRegion.x - val y = targetRegion.y + fontYOffset(fontInfo).toInt() - g.drawString(text, x, y) - } -}