partial accept jetbrains

This commit is contained in:
Nate Sesti 2024-07-13 13:29:52 -07:00
parent 9eea0a76c9
commit 6dd812d1bc
10 changed files with 91 additions and 21 deletions

View file

@ -12,10 +12,15 @@ import com.intellij.openapi.util.TextRange
class AutocompleteCaretListener: CaretListener {
override fun caretPositionChanged(event: CaretEvent) {
val caret = event.caret ?: return
val oldPosition = event.oldPosition
val offset = caret.offset
val editor = caret.editor
val autocompleteService = editor.project?.service<AutocompleteService>() ?: return
if (autocompleteService.lastChangeWasPartialAccept) {
autocompleteService.lastChangeWasPartialAccept = false
return
}
val pending = autocompleteService.pendingCompletion;
if (pending != null && pending.editor == editor && pending.offset == offset) {
return
@ -29,10 +34,16 @@ class AutocompleteDocumentListener(private val editorManager: FileEditorManager,
if (editor != editorManager.selectedTextEditor) {
return
}
val service = editor.project?.service<AutocompleteService>() ?: return
if (service.lastChangeWasPartialAccept) {
return
}
// Invoke later is important, otherwise the completion will be triggered before the document is updated
// causing the old caret offset to be used
invokeLater {
editor.project?.service<AutocompleteService>()?.triggerCompletion(editor)
service.triggerCompletion(editor)
}
}
}

View file

@ -17,10 +17,10 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.WindowManager
data class PendingCompletion (
val editor: Editor,
val offset: Int,
val completionId: String,
val text: String?
val editor: Editor,
var offset: Int,
val completionId: String,
var text: String?
)
@Service(Service.Level.PROJECT)
@ -29,6 +29,10 @@ class AutocompleteService(private val project: Project) {
private val autocompleteLookupListener = project.service<AutocompleteLookupListener>()
private var widget: AutocompleteSpinnerWidget? = null
// To avoid triggering another completion on partial acceptance,
// we need to keep track of whether the last change was a partial accept
var lastChangeWasPartialAccept = false
init {
val statusBar = WindowManager.getInstance().getStatusBar(project)
widget = statusBar.getWidget("AutocompleteSpinnerWidget") as? AutocompleteSpinnerWidget
@ -77,7 +81,7 @@ class AutocompleteService(private val project: Project) {
if (completions.isNotEmpty()) {
val completion = completions[0].toString()
if (completion.lines().size === 1 || column >= lineLength) {
if (completion.isNotEmpty() && (completion.lines().size === 1 || column >= lineLength)) {
// Do not render if completion is multi-line and caret is in middle of line
renderCompletion(editor, offset, completion)
pendingCompletion = pendingCompletion?.copy(text = completion)
@ -90,6 +94,9 @@ class AutocompleteService(private val project: Project) {
}
private fun renderCompletion(editor: Editor, offset: Int, text: String) {
if (text.isEmpty()) {
return
}
// Don't render completions when code completion dropdown is visible
if (!autocompleteLookupListener.isLookupEmpty()) {
return
@ -130,6 +137,54 @@ class AutocompleteService(private val project: Project) {
}
}
private fun splitKeepingDelimiters(input: String, delimiterPattern: String = "\\s+"): List<String> {
val initialSplit = input.split("(?<=$delimiterPattern)|(?=$delimiterPattern)".toRegex())
.filter { it.isNotEmpty() }
val result = mutableListOf<String>()
var currentDelimiter = ""
for (part in initialSplit) {
if (part.matches(delimiterPattern.toRegex())) {
currentDelimiter += part
} else {
if (currentDelimiter.isNotEmpty()) {
result.add(currentDelimiter)
currentDelimiter = ""
}
result.add(part)
}
}
if (currentDelimiter.isNotEmpty()) {
result.add(currentDelimiter)
}
return result
}
fun partialAccept() {
val completion = pendingCompletion ?: return
val text = completion.text ?: return
val editor = completion.editor
val offset = completion.offset
lastChangeWasPartialAccept = true
// Split the text into words, keeping delimiters
val words = splitKeepingDelimiters(text)
println(words)
val word = words[0]
editor.document.insertString(offset, word)
editor.caretModel.moveToOffset(offset + word.length)
// Remove the completion and re-display it
hideCompletions(editor)
completion.text = text.substring(word.length)
completion.offset += word.length
renderCompletion(editor, completion.offset, completion.text!!)
}
private fun cancelCompletion(completion: PendingCompletion) {
// Send cancellation message to core
widget?.setLoading(false)

View file

@ -19,7 +19,8 @@ class ContinueCustomElementRenderer (
val text: String,
) : EditorCustomElementRenderer {
override fun calcWidthInPixels(inlay: Inlay<*>): Int {
return (inlay.editor as EditorImpl).getFontMetrics(Font.PLAIN).stringWidth(this.text)
val width = (inlay.editor as EditorImpl).getFontMetrics(Font.PLAIN).stringWidth(this.text)
return width
}
private fun font(editor: Editor): Font {

View file

@ -11,8 +11,7 @@ import com.intellij.openapi.editor.actionSystem.EditorActionHandler
class PartialAcceptAutocompleteAction: EditorAction(object : EditorActionHandler() {
override fun doExecute(editor: Editor, caret: Caret?, dataContext: DataContext?) {
ApplicationManager.getApplication().runWriteAction {
// TODO
// editor.project?.service<AutocompleteService>()?.accept()
editor.project?.service<AutocompleteService>()?.partialAccept()
}
}

View file

@ -110,10 +110,6 @@
text="Start New Continue Session"
icon="AllIcons.General.Add"
description="Start New Continue Session">
<keyboard-shortcut keymap="$default"
first-keystroke="alt ctrl N"/>
<keyboard-shortcut keymap="Mac OS X"
first-keystroke="alt meta N"/>
</action>
<action id="continue.viewHistory"
@ -149,6 +145,13 @@
text="Cancel Autocomplete Suggestion" description="Cancel Autocomplete Suggestion">
<keyboard-shortcut keymap="$default" first-keystroke="ESCAPE"/>
</action>
<action id="com.github.continuedev.continueintellijextension.autocomplete.PartialAcceptAutocompleteAction"
class="com.github.continuedev.continueintellijextension.autocomplete.PartialAcceptAutocompleteAction"
text="Partial Accept Autocomplete Suggestion"
description="Partial Accept Autocomplete Suggestion">
<keyboard-shortcut first-keystroke="control alt RIGHT" keymap="$default"/>
<keyboard-shortcut first-keystroke="alt meta RIGHT" keymap="Mac OS X"/>
</action>
</actions>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1,017 B

View file

@ -1,12 +1,12 @@
{
"name": "continue",
"version": "0.9.181",
"version": "0.9.182",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "continue",
"version": "0.9.181",
"version": "0.9.182",
"license": "Apache-2.0",
"dependencies": {
"@electron/rebuild": "^3.2.10",

View file

@ -15,15 +15,16 @@ import FileIcon from "../FileIcon";
const ContextItemDiv = styled.div`
cursor: pointer;
padding-left: 6px;
padding-right: 10px;
padding-top: 6px;
padding-bottom: 6px;
padding: 6px 10px 6px 6px;
margin-left: 4px;
display: flex;
align-items: center;
border-radius: ${defaultBorderRadius};
width: fit-content;
font-size: ${getFontSize()};
max-width: 100%;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
&:hover {
background-color: #fff1;