Skip to content

Commit 5920d9b

Browse files
committed
feat(completion): add support for automatic colon insertion and caret 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.
1 parent 6f0e1e6 commit 5920d9b

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

exts/devin-lang/src/main/kotlin/cc/unitmesh/devti/language/completion/BuiltinCommandProvider.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package cc.unitmesh.devti.language.completion
22

33
import cc.unitmesh.devti.language.DevInIcons
4+
import com.intellij.codeInsight.AutoPopupController
45
import com.intellij.codeInsight.completion.*
56
import com.intellij.codeInsight.lookup.LookupElementBuilder
67
import com.intellij.icons.AllIcons
78
import com.intellij.util.ProcessingContext
89
import javax.swing.Icon
910

10-
enum class BuiltinCommand(val agentName: String, val description: String, val icon: Icon) {
11-
FILE("file", "Read the content of a file", AllIcons.Actions.AddFile),
12-
REV("rev", "Read git change by file", AllIcons.Vcs.History),
11+
enum class BuiltinCommand(val agentName: String, val description: String, val icon: Icon, val hasCompletion: Boolean = false) {
12+
FILE("file", "Read the content of a file", AllIcons.Actions.AddFile, true),
13+
REV("rev", "Read git change by file", AllIcons.Vcs.History, true),
1314
SYMBOL("symbol", "Read content by Java/Kotlin canonicalName", AllIcons.Actions.GroupBy),
1415
WRITE("write", "Write content to a file, /write:/path/to/file:L1-L2", AllIcons.Actions.Edit),
1516
;
@@ -36,6 +37,15 @@ class BuiltinCommandProvider : CompletionProvider<CompletionParameters>() {
3637
LookupElementBuilder.create(it.agentName)
3738
.withIcon(it.icon)
3839
.withTypeText(it.description, true)
40+
.withInsertHandler { context, _ ->
41+
if (!it.hasCompletion) return@withInsertHandler
42+
43+
context.document.insertString(context.tailOffset, ":")
44+
context.editor.caretModel.moveCaretRelatively(1, 0, false, false, false)
45+
46+
val editor = context.editor
47+
AutoPopupController.getInstance(editor.project!!).scheduleAutoPopup(editor)
48+
}
3949
)
4050
}
4151
}

0 commit comments

Comments
 (0)