Skip to content

Commit

Permalink
feat(completion): add support for automatic colon insertion and caret…
Browse files Browse the repository at this point in the history
… positioning after builtin commands completion. #101

This commit enhances the builtin command completion feature by automatically inserting a colon and positioning the caret after the command, improving the user experience and reducing the number of keystrokes required for command completion.
  • Loading branch information
phodal committed Mar 16, 2024
1 parent 6f0e1e6 commit 5920d9b
Showing 1 changed file with 13 additions and 3 deletions.
@@ -1,15 +1,16 @@
package cc.unitmesh.devti.language.completion

import cc.unitmesh.devti.language.DevInIcons
import com.intellij.codeInsight.AutoPopupController
import com.intellij.codeInsight.completion.*
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.icons.AllIcons
import com.intellij.util.ProcessingContext
import javax.swing.Icon

enum class BuiltinCommand(val agentName: String, val description: String, val icon: Icon) {
FILE("file", "Read the content of a file", AllIcons.Actions.AddFile),
REV("rev", "Read git change by file", AllIcons.Vcs.History),
enum class BuiltinCommand(val agentName: String, val description: String, val icon: Icon, val hasCompletion: Boolean = false) {
FILE("file", "Read the content of a file", AllIcons.Actions.AddFile, true),
REV("rev", "Read git change by file", AllIcons.Vcs.History, true),
SYMBOL("symbol", "Read content by Java/Kotlin canonicalName", AllIcons.Actions.GroupBy),
WRITE("write", "Write content to a file, /write:/path/to/file:L1-L2", AllIcons.Actions.Edit),
;
Expand All @@ -36,6 +37,15 @@ class BuiltinCommandProvider : CompletionProvider<CompletionParameters>() {
LookupElementBuilder.create(it.agentName)
.withIcon(it.icon)
.withTypeText(it.description, true)
.withInsertHandler { context, _ ->
if (!it.hasCompletion) return@withInsertHandler

context.document.insertString(context.tailOffset, ":")
context.editor.caretModel.moveCaretRelatively(1, 0, false, false, false)

val editor = context.editor
AutoPopupController.getInstance(editor.project!!).scheduleAutoPopup(editor)
}
)
}
}
Expand Down

0 comments on commit 5920d9b

Please sign in to comment.