Skip to content

Commit 990f0fa

Browse files
committed
fix: fix inaly issues
1 parent 11edbd9 commit 990f0fa

File tree

5 files changed

+61
-27
lines changed

5 files changed

+61
-27
lines changed

src/main/kotlin/cc/unitmesh/devti/actions/LLMApplyInlaysAction.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,20 @@ class LLMApplyInlaysAction : EditorAction(ApplyInlaysHandler()), DumbAware {
4242
val blockIndent = CodeStyle.getIndentOptions(project, document).INDENT_SIZE
4343
val caretOffset = editor.caretModel.offset
4444
val line = document.getLineNumber(caretOffset)
45-
if (isNonEmptyLinePrefix(document, line, caretOffset)) {
45+
46+
val caretOffsetAfterTab = EditorUtilCopy.indentLine(project, editor, line, blockIndent, caretOffset)
47+
// if (isNonEmptyLinePrefix(document, line, caretOffset) || caretOffsetAfterTab < caretOffset) {
48+
// return false
49+
// }
50+
51+
val instance = LLMInlayManager.getInstance()
52+
val tabRange = TextRange.create(caretOffset, caretOffsetAfterTab)
53+
54+
if (instance.countCompletionInlays(editor, tabRange) > 0) {
4655
return false
4756
}
48-
val caretOffsetAfterTab = EditorUtilCopy.indentLine(project, editor, line, blockIndent, caretOffset)
4957

50-
return caretOffsetAfterTab >= caretOffset
58+
return true
5159
}
5260

5361

@@ -61,9 +69,10 @@ class LLMApplyInlaysAction : EditorAction(ApplyInlaysHandler()), DumbAware {
6169
}
6270

6371
override fun doExecute(editor: Editor, caret: Caret?, dataContext: DataContext) {
64-
if (editor.isDisposed) return@doExecute
72+
if (editor.isDisposed) return
6573
val project = editor.project ?: return
66-
if (project.isDisposed) return@doExecute
74+
if (project.isDisposed) return
75+
6776

6877
// todo: update this to use the new API
6978
logger.info("doExecute for applyInlays")

src/main/kotlin/cc/unitmesh/devti/editor/inlay/LLMInlayManager.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.intellij.openapi.Disposable
55
import com.intellij.openapi.application.ApplicationManager
66
import com.intellij.openapi.editor.Editor
77
import com.intellij.openapi.project.Project
8+
import com.intellij.openapi.util.TextRange
89
import com.intellij.util.concurrency.annotations.RequiresEdt
910

1011
interface LLMInlayManager : Disposable {
@@ -25,6 +26,7 @@ interface LLMInlayManager : Disposable {
2526
fun editorModified(editor: Editor, changeOffset: Int)
2627

2728
fun editorModified(editor: Editor)
29+
fun countCompletionInlays(editor: Editor, tabRange: TextRange) : Int
2830

2931
companion object {
3032
fun getInstance(): LLMInlayManager {

src/main/kotlin/cc/unitmesh/devti/editor/inlay/LLMInlayManagerImpl.kt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.intellij.openapi.project.Project
1818
import com.intellij.openapi.util.Disposer
1919
import com.intellij.openapi.util.Key
2020
import com.intellij.openapi.util.KeyWithDefaultValue
21+
import com.intellij.openapi.util.TextRange
2122
import com.intellij.psi.util.PsiUtilBase
2223
import com.intellij.util.concurrency.annotations.RequiresBackgroundThread
2324
import com.intellij.util.concurrency.annotations.RequiresEdt
@@ -41,11 +42,8 @@ class LLMInlayManagerImpl : LLMInlayManager {
4142
var isAvailable: Boolean? = KEY_EDITOR_SUPPORTED[editor]
4243
if (isAvailable == null) {
4344
isAvailable = editor !is EditorWindow && editor !is ImaginaryEditor && (
44-
editor !is EditorEx ||
45-
!editor.isEmbeddedIntoDialogWrapper
46-
) &&
47-
!editor.isViewer &&
48-
!editor.isOneLineMode
45+
editor !is EditorEx || !editor.isEmbeddedIntoDialogWrapper) &&
46+
!editor.isViewer && !editor.isOneLineMode
4947

5048
KEY_EDITOR_SUPPORTED[editor] = isAvailable
5149
}
@@ -147,6 +145,21 @@ class LLMInlayManagerImpl : LLMInlayManager {
147145
editorModified(editor, editor.caretModel.offset)
148146
}
149147

148+
override fun countCompletionInlays(editor: Editor, tabRange: TextRange): Int {
149+
val inlays = collectInlays(editor, tabRange.startOffset, tabRange.endOffset)
150+
if (inlays.isEmpty()) return 0
151+
152+
val completionCount = inlays.count {
153+
it.getInlay()?.renderer?.javaClass?.name?.contains("LLMInlayRenderer") ?: false
154+
}
155+
156+
if (completionCount > 0) {
157+
logger.debug("Completion inlays found: $completionCount")
158+
}
159+
160+
return completionCount
161+
}
162+
150163
private fun disposeInlays(renderers: List<LLMInlayRenderer>) {
151164
logger.debug("Disposing inlays: " + renderers.size)
152165
for (renderer in renderers) {

src/main/kotlin/cc/unitmesh/devti/editor/presentation/EditorUtilCopy.kt

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,27 @@ package cc.unitmesh.devti.editor.presentation
33
import com.intellij.openapi.editor.Editor
44
import com.intellij.openapi.editor.actions.EditorActionUtil
55
import com.intellij.openapi.project.Project
6+
import kotlin.math.min
67

78
object EditorUtilCopy {
8-
@JvmOverloads
9+
fun indentLine(project: Project?, editor: Editor, lineNumber: Int, indent: Int, caretOffset: Int): Int {
10+
return indentLine(
11+
project,
12+
editor,
13+
lineNumber,
14+
indent,
15+
caretOffset,
16+
EditorActionUtil.shouldUseSmartTabs(project, editor)
17+
)
18+
}
19+
920
fun indentLine(
1021
project: Project?,
1122
editor: Editor,
1223
lineNumber: Int,
1324
indent: Int,
1425
caretOffset: Int,
15-
shouldUseSmartTabs: Boolean = EditorActionUtil.shouldUseSmartTabs(project, editor),
26+
shouldUseSmartTabs: Boolean
1627
): Int {
1728
val editorSettings = editor.settings
1829
val tabSize = editorSettings.getTabSize(project)
@@ -27,9 +38,7 @@ object EditorUtilCopy {
2738
lineEnd = document.getLineEndOffset(lineNumber)
2839
spacesEnd = lineStart
2940
var inTabs = true
30-
while (spacesEnd <= lineEnd &&
31-
spacesEnd != lineEnd
32-
) {
41+
while (spacesEnd <= lineEnd && spacesEnd != lineEnd) {
3342
val c = text[spacesEnd]
3443
if (c != '\t') {
3544
if (inTabs) {
@@ -47,37 +56,37 @@ object EditorUtilCopy {
4756
}
4857
}
4958
var newCaretOffset = caretOffset
50-
if (newCaretOffset >= lineStart && newCaretOffset < lineEnd && spacesEnd == lineEnd) {
59+
if (newCaretOffset in lineStart until lineEnd && spacesEnd == lineEnd) {
5160
spacesEnd = newCaretOffset
52-
tabsEnd = Math.min(spacesEnd, tabsEnd)
61+
tabsEnd = min(spacesEnd, tabsEnd)
5362
}
5463
val oldLength = getSpaceWidthInColumns(text, lineStart, spacesEnd, tabSize)
55-
tabsEnd = getSpaceWidthInColumns(text, lineStart, tabsEnd, tabSize)
64+
val tabsEnd2 = getSpaceWidthInColumns(text, lineStart, tabsEnd, tabSize)
5665
var newLength = oldLength + indent
5766
if (newLength < 0) {
5867
newLength = 0
5968
}
60-
tabsEnd += indent
61-
if (tabsEnd < 0) {
62-
tabsEnd = 0
69+
var tabsEnd3 = tabsEnd2 + indent
70+
if (tabsEnd3 < 0) {
71+
tabsEnd3 = 0
6372
}
6473
if (!shouldUseSmartTabs) {
65-
tabsEnd = newLength
74+
tabsEnd3 = newLength
6675
}
6776
val buf = StringBuilder(newLength)
6877
var i = 0
6978
while (i < newLength) {
70-
if (tabSize > 0 && editorSettings.isUseTabCharacter(project) && i + tabSize <= tabsEnd) {
79+
if (tabSize > 0 && editorSettings.isUseTabCharacter(project) && i + tabSize <= tabsEnd3) {
7180
buf.append('\t')
7281
i += tabSize
73-
continue
82+
} else {
83+
buf.append(' ')
84+
i++
7485
}
75-
buf.append(' ')
76-
i++
7786
}
7887
val newSpacesEnd = lineStart + buf.length
7988
if (newCaretOffset >= spacesEnd) {
80-
newCaretOffset += buf.length - spacesEnd - lineStart
89+
newCaretOffset += buf.length - (spacesEnd - lineStart)
8190
} else if (newCaretOffset >= lineStart && newCaretOffset > newSpacesEnd) {
8291
newCaretOffset = newSpacesEnd
8392
}

src/main/resources/META-INF/autodev-core.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
implementation="cc.unitmesh.devti.runconfig.command.AutoDevFeatureConfigurationProducer"/>
2323
<runConfigurationProducer
2424
implementation="cc.unitmesh.devti.runconfig.command.CompositeAutoBaseRunConfigurationProducer"/>
25+
2526
<configurationType implementation="cc.unitmesh.devti.runconfig.AutoDevConfigurationType"/>
2627

2728
<!-- Run Configurations -->

0 commit comments

Comments
 (0)