Skip to content

Commit cb32e0f

Browse files
committed
refactor(devti): optimize LLMInlayRenderer and LLMInlayManagerImpl
- Simplify LLMInlayRenderer class structure and logic- Improve inlay management in LLMInlayManagerImpl - Remove redundant methods and properties - Enhance code readability and maintainability
1 parent 8f9ea98 commit cb32e0f

File tree

2 files changed

+13
-32
lines changed

2 files changed

+13
-32
lines changed

core/src/main/kotlin/cc/unitmesh/devti/inlay/codecomplete/LLMInlayManagerImpl.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ class LLMInlayManagerImpl : LLMInlayManager {
124124
renderer.apply {
125125
val inlay: Inlay<EditorCustomElementRenderer>? = editor.inlayModel
126126
.addBlockElement(changeOffset, true, false, 0, this)
127-
inlay?.let {
128-
renderer.setInlay(inlay)
129-
}
127+
?: return@apply
128+
129+
renderer.inlay = inlay
130130
}
131131
}
132132
}
@@ -140,7 +140,7 @@ class LLMInlayManagerImpl : LLMInlayManager {
140140
val inlays = collectInlays(editor, tabRange.startOffset, tabRange.endOffset)
141141
if (inlays.isEmpty()) return 0
142142

143-
val completionCount = inlays.count { it.getInlay()?.renderer is LLMInlayRenderer }
143+
val completionCount = inlays.count { it.inlay?.renderer is LLMInlayRenderer }
144144

145145
if (completionCount > 0) {
146146
logger.debug("Completion inlays found: $completionCount")
@@ -152,10 +152,8 @@ class LLMInlayManagerImpl : LLMInlayManager {
152152
private fun disposeInlays(renderers: List<LLMInlayRenderer>) {
153153
logger.debug("Disposing inlays: " + renderers.size)
154154
for (renderer in renderers) {
155-
val inlay = renderer.getInlay()
156-
if (inlay != null) {
157-
Disposer.dispose((inlay as Disposable?)!!)
158-
}
155+
if (renderer.inlay == null) continue
156+
Disposer.dispose((renderer.inlay as Disposable?)!!)
159157
}
160158
}
161159

core/src/main/kotlin/cc/unitmesh/devti/inlay/codecomplete/presentation/LLMInlayRenderer.kt

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,19 @@ import com.intellij.openapi.editor.EditorCustomElementRenderer
55
import com.intellij.openapi.editor.Inlay
66
import com.intellij.openapi.editor.markup.TextAttributes
77
import com.intellij.temporary.inlay.presentation.LLMTextInlayPainter
8-
import org.jetbrains.annotations.NonNls
98
import java.awt.Graphics
109
import java.awt.Rectangle
1110

1211
class LLMInlayRenderer(editor: Editor, lines: List<String?>) : EditorCustomElementRenderer {
13-
private val lines: List<String>
14-
private val content: String
12+
private val lines: List<String> = PresentationUtil.replaceLeadingTabs(lines, 4)
13+
private val content: String = lines.joinToString("\n")
14+
private val textAttributes: TextAttributes = PresentationUtil.getTextAttributes(editor)
15+
var inlay: Inlay<EditorCustomElementRenderer>? = null
16+
1517
private var cachedWidth = -1
1618
private var cachedHeight = -1
1719

18-
private val textAttributes: TextAttributes
19-
private var inlay: Inlay<EditorCustomElementRenderer>? = null
20-
21-
init {
22-
this.lines = PresentationUtil.replaceLeadingTabs(lines, 4)
23-
content = lines.joinToString("\n")
24-
textAttributes = PresentationUtil.getTextAttributes(editor)
25-
}
26-
27-
fun getInlay(): Inlay<EditorCustomElementRenderer>? {
28-
return inlay
29-
}
30-
31-
fun setInlay(inlay: Inlay<EditorCustomElementRenderer>) {
32-
this.inlay = inlay
33-
}
34-
35-
override fun getContextMenuGroupId(inlay: Inlay<*>): @NonNls String {
36-
return "copilot.inlayContextMenu"
37-
}
20+
override fun getContextMenuGroupId(inlay: Inlay<*>): String = "autodev.inlayContextMenu"
3821

3922
override fun calcHeightInPixels(inlay: Inlay<*>): Int {
4023
return if (cachedHeight < 0) {
@@ -47,7 +30,7 @@ class LLMInlayRenderer(editor: Editor, lines: List<String?>) : EditorCustomEleme
4730
override fun calcWidthInPixels(inlay: Inlay<*>): Int {
4831
if (cachedWidth < 0) {
4932
val width = LLMTextInlayPainter.calculateWidth(inlay.editor, content, lines)
50-
return Math.max(1, width).also { cachedWidth = it }
33+
return 1.coerceAtLeast(width).also { cachedWidth = it }
5134
}
5235
return cachedWidth
5336
}

0 commit comments

Comments
 (0)