Skip to content

Commit

Permalink
feat(inlay): use inlay mode for code completion
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jul 22, 2023
1 parent 71a2d91 commit aa230c7
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 68 deletions.
Expand Up @@ -46,11 +46,8 @@ class LLMApplyInlaysAction : EditorAction(ApplyInlaysHandler()), DumbAware {
return false
}
val caretOffsetAfterTab = EditorUtilCopy.indentLine(project, editor, line, blockIndent, caretOffset)
if (caretOffsetAfterTab < caretOffset) {
return false
}

return true
return caretOffsetAfterTab >= caretOffset
}


Expand Down

This file was deleted.

This file was deleted.

Expand Up @@ -55,7 +55,8 @@ class LLMCommandListener(private val project: Project) : CommandListener {
val commandEndState = createCommandState(editor)
if (isDocumentModification(commandStartState, commandEndState)) {
logger.debug("command modified document: " + event.commandName)
editorManager.editorModified(editor)
// TODO: we used intention
// editorManager.editorModified(editor)
} else if (isCaretPositionChange(commandStartState, commandEndState)) {
editorManager.disposeInlays(editor, InlayDisposeContext.CaretChange)
}
Expand Down
Expand Up @@ -143,7 +143,7 @@ class LLMInlayManagerImpl : LLMInlayManager {
@RequiresBackgroundThread
private fun requestCompletions(editor: Editor, changeOffset: Int, onFirstCompletion: Consumer<String>?) {
val element = PsiUtilBase.getElementAtCaret(editor) ?: return
val request = CompletionTaskRequest.create(editor, changeOffset, element) ?: return
val request = CompletionTaskRequest.create(editor, changeOffset, element, null) ?: return

KEY_LAST_REQUEST[editor] = request
CodeCompletionTask(request).execute(onFirstCompletion)
Expand Down
@@ -1,6 +1,7 @@
package cc.unitmesh.devti.intentions.editor

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.editor.inlay.LLMInlayManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.actions.EditorActionUtil
Expand Down Expand Up @@ -42,9 +43,13 @@ class CodeCompletionIntention : AbstractChatIntention() {
val element = PsiUtilBase.getElementAtCaret(editor) ?: file
val suffix = document.getText(TextRange(offset, suffixEnd))

val request = CompletionTaskRequest.create(editor, offset, element) ?: return
val task = CodeCompletionTask(request)
ProgressManager.getInstance().runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator(task))
val request = CompletionTaskRequest.create(editor, offset, element, prefix) ?: return
// val task = CodeCompletionTask(request)
// ProgressManager.getInstance().runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator(task))

val llmInlayManager = LLMInlayManager.getInstance()
llmInlayManager
.editorModified(editor, offset)
}

companion object {
Expand Down
Expand Up @@ -25,7 +25,6 @@ import com.intellij.psi.codeStyle.CodeStyleManager
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.util.concurrent.Flow.*
import java.util.function.Consumer
import kotlin.jvm.internal.Ref

Expand All @@ -42,7 +41,7 @@ class CompletionTaskRequest(
val editor: Editor
) : Disposable {
companion object {
fun create(editor: Editor, offset: Int, element: PsiElement): CompletionTaskRequest? {
fun create(editor: Editor, offset: Int, element: PsiElement, prefix: String?): CompletionTaskRequest? {
val project = editor.project ?: return null

val document = editor.document
Expand All @@ -62,7 +61,7 @@ class CompletionTaskRequest(
useTabs,
tabWidth,
uri,
document.text,
prefix ?: document.text,
offset,
documentVersion,
element,
Expand Down Expand Up @@ -122,7 +121,13 @@ class CodeCompletionTask(
codeMessage,
writeActionGroupId,
{
insertStringAndSaveChange(project, it, request.editor.document, currentOffset.element, false)
insertStringAndSaveChange(
project,
it,
request.editor.document,
currentOffset.element,
false
)
}
)

Expand Down
29 changes: 11 additions & 18 deletions src/main/resources/META-INF/autodev-core.xml
Expand Up @@ -31,9 +31,9 @@
<notificationGroup id="AI notification group" displayType="STICKY_BALLOON" bundle="messages.AutoDevBundle"
key="notification.group.name"/>

<!-- <editorFactoryListener implementation="cc.unitmesh.devti.editor.inlay.AutoDevEditorListener"/>-->
<!-- <typedHandler order="first, before completionAutoPopup"-->
<!-- implementation="cc.unitmesh.devti.editor.inlay.TypeOverHandler"/>-->
<!-- <editorFactoryListener implementation="cc.unitmesh.devti.editor.inlay.AutoDevEditorListener"/>-->
<!-- <typedHandler order="first, before completionAutoPopup"-->
<!-- implementation="cc.unitmesh.devti.editor.inlay.TypeOverHandler"/>-->

<intentionAction>
<className>cc.unitmesh.devti.intentions.AIAssistantIntention</className>
Expand Down Expand Up @@ -91,14 +91,14 @@
dynamic="true"/>
</extensionPoints>

<!-- <applicationListeners>-->
<!-- <listener topic="cc.unitmesh.devti.editor.inlay.LLMInlayListener"-->
<!-- class="cc.unitmesh.devti.editor.inlay.LLMInlayGotItListener"/>-->
<!-- </applicationListeners>-->
<!-- <projectListeners>-->
<!-- <listener topic="com.intellij.openapi.command.CommandListener"-->
<!-- class="cc.unitmesh.devti.editor.inlay.LLMCommandListener"/>-->
<!-- </projectListeners>-->
<!-- <applicationListeners>-->
<!-- <listener topic="cc.unitmesh.devti.editor.inlay.LLMInlayListener"-->
<!-- class="cc.unitmesh.devti.editor.inlay.LLMInlayGotItListener"/>-->
<!-- </applicationListeners>-->
<projectListeners>
<listener topic="com.intellij.openapi.command.CommandListener"
class="cc.unitmesh.devti.editor.inlay.LLMCommandListener"/>
</projectListeners>

<extensions defaultExtensionNs="cc.unitmesh">
<aiAssistantIntention>
Expand All @@ -119,13 +119,6 @@
<override-text place="EditorPopup" text="Accept"/>
</action>

<action id="copilot.disposeInlays"
class="cc.unitmesh.devti.actions.LLMDisposeInlaysAction">
<keyboard-shortcut first-keystroke="ESCAPE" keymap="$default"/>
<override-text place="MainMenu" text="Hide Completions in Editor"/>
</action>


<group id="AutoDevIntentionsActionGroup" class="cc.unitmesh.devti.intentions.editor.IntentionsActionGroup">
<add-to-group group-id="ShowIntentionsGroup" relative-to-action="ShowIntentionActions" anchor="after"/>
</group>
Expand Down

0 comments on commit aa230c7

Please sign in to comment.