Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assertion error #1924

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this to the CodyAutocompleteRenderer?

Or at the very least, we could look at something like renderer.type instead of the startsInline we're hanging on to.

inlayModel.addInlineElement(cursorOffset, /* relatesToPrecedingText = */ true, renderer)
} else {
Copy link
Contributor

@pkukielka pkukielka Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, sorry for the long delay.

I'm not sure this is correct.
Multiline autocompletion can start inline, but then continue to the other lines.
So in fact 3 states are possible:

  • single inline autocompletion ('addInlineElement')
  • multiline completion which starts bellow current line (addBlockElement)
  • multiline completion which starts inline but also continues to the other lines (addInlineElement + addBlockElement)

inlayModel.addBlockElement(
/* offset = */ cursorOffset,
/* relatesToPrecedingText = */ true,
/* showAbove = */ false,
/* priority = */ Int.MAX_VALUE,
/* renderer = */ renderer)
}

if (inlay?.bounds?.location != null) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<AutocompleteItem>,
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)
}
}
}
}

This file was deleted.

Loading