JetBrains status bar spinner for autocomplete
This commit is contained in:
parent
1bc2153f06
commit
a8a542fac3
4 changed files with 124 additions and 1 deletions
|
|
@ -14,6 +14,7 @@ import com.intellij.openapi.editor.Editor
|
|||
import com.intellij.openapi.editor.InlayProperties
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.wm.WindowManager
|
||||
|
||||
data class PendingCompletion (
|
||||
val editor: Editor,
|
||||
|
|
@ -26,6 +27,12 @@ data class PendingCompletion (
|
|||
class AutocompleteService(private val project: Project) {
|
||||
var pendingCompletion: PendingCompletion? = null;
|
||||
private val autocompleteLookupListener = project.service<AutocompleteLookupListener>()
|
||||
private var widget: AutocompleteSpinnerWidget? = null
|
||||
|
||||
init {
|
||||
val statusBar = WindowManager.getInstance().getStatusBar(project)
|
||||
widget = statusBar.getWidget("AutocompleteSpinnerWidget") as? AutocompleteSpinnerWidget
|
||||
}
|
||||
|
||||
fun triggerCompletion(editor: Editor) {
|
||||
val settings =
|
||||
|
|
@ -42,6 +49,7 @@ class AutocompleteService(private val project: Project) {
|
|||
val completionId = uuid()
|
||||
val offset = editor.caretModel.primaryCaret.offset
|
||||
pendingCompletion = PendingCompletion(editor, offset, completionId, null)
|
||||
widget?.setLoading(true)
|
||||
|
||||
// Request a completion from the core
|
||||
val virtualFile = FileDocumentManager.getInstance().getFile(editor.document)
|
||||
|
|
@ -63,6 +71,8 @@ class AutocompleteService(private val project: Project) {
|
|||
val lineLength = lineEnd - lineStart
|
||||
|
||||
project.service<ContinuePluginService>().coreMessenger?.request("autocomplete/complete", input, null, ({ response ->
|
||||
widget?.setLoading(false)
|
||||
|
||||
val completions = response as List<*>
|
||||
if (completions.isNotEmpty()) {
|
||||
val completion = completions[0].toString()
|
||||
|
|
@ -122,6 +132,7 @@ class AutocompleteService(private val project: Project) {
|
|||
|
||||
private fun cancelCompletion(completion: PendingCompletion) {
|
||||
// Send cancellation message to core
|
||||
widget?.setLoading(false)
|
||||
project.service<ContinuePluginService>().coreMessenger?.request("autocomplete/cancel", null,null, ({}))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,108 @@
|
|||
package com.github.continuedev.continueintellijextension.autocomplete
|
||||
|
||||
import com.intellij.openapi.Disposable
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.Disposer
|
||||
import com.intellij.openapi.wm.StatusBar
|
||||
import com.intellij.openapi.wm.StatusBarWidget
|
||||
import com.intellij.openapi.util.IconLoader
|
||||
import com.intellij.openapi.wm.StatusBarWidgetFactory
|
||||
import com.intellij.openapi.wm.WindowManager
|
||||
import com.intellij.openapi.wm.impl.status.EditorBasedWidget
|
||||
import com.intellij.ui.AnimatedIcon
|
||||
import com.intellij.util.Consumer
|
||||
import java.awt.event.MouseEvent
|
||||
import javax.swing.Icon
|
||||
import javax.swing.JLabel
|
||||
|
||||
class AutocompleteSpinnerWidget(project: Project): EditorBasedWidget(project), StatusBarWidget.IconPresentation, Disposable {
|
||||
private val iconLabel = JLabel()
|
||||
private var isLoading = false
|
||||
|
||||
private val animatedIcon = AnimatedIcon(
|
||||
100,
|
||||
IconLoader.getIcon("/icons/AnimationLoadingIcon/AnimationLoading1(RiderLight).svg", javaClass),
|
||||
IconLoader.getIcon("/icons/AnimationLoadingIcon/AnimationLoading2(RiderLight).svg", javaClass),
|
||||
IconLoader.getIcon("/icons/AnimationLoadingIcon/AnimationLoading3(RiderLight).svg", javaClass),
|
||||
IconLoader.getIcon("/icons/AnimationLoadingIcon/AnimationLoading4(RiderLight).svg", javaClass),
|
||||
IconLoader.getIcon("/icons/AnimationLoadingIcon/AnimationLoading5(RiderLight).svg", javaClass),
|
||||
IconLoader.getIcon("/icons/AnimationLoadingIcon/AnimationLoading6(RiderLight).svg", javaClass),
|
||||
IconLoader.getIcon("/icons/AnimationLoadingIcon/AnimationLoading7(RiderLight).svg", javaClass),
|
||||
IconLoader.getIcon("/icons/AnimationLoadingIcon/AnimationLoading8(RiderLight).svg", javaClass),
|
||||
)
|
||||
|
||||
init {
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
fun show() {
|
||||
println("Showing autocomplete spinner widget")
|
||||
}
|
||||
|
||||
override fun dispose() {}
|
||||
|
||||
override fun ID(): String {
|
||||
return "AutocompleteSpinnerWidget"
|
||||
}
|
||||
|
||||
override fun getTooltipText(): String? {
|
||||
return "Autocompleting..."
|
||||
}
|
||||
|
||||
override fun getClickConsumer(): Consumer<MouseEvent>? {
|
||||
return null
|
||||
}
|
||||
|
||||
override fun getIcon(): Icon = if (isLoading) animatedIcon else
|
||||
IconLoader.getIcon("/icons/continue.svg", javaClass)
|
||||
|
||||
fun setLoading(loading: Boolean) {
|
||||
isLoading = loading
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
private fun updateIcon() {
|
||||
iconLabel.icon = getIcon()
|
||||
|
||||
|
||||
// Update the widget
|
||||
val statusBar = WindowManager.getInstance().getStatusBar(project)
|
||||
statusBar.updateWidget(ID())
|
||||
}
|
||||
|
||||
override fun install(statusBar: StatusBar) {
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
override fun getPresentation(): StatusBarWidget.WidgetPresentation? {
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
class AutocompleteSpinnerWidgetFactory: StatusBarWidgetFactory {
|
||||
fun create(project: Project): AutocompleteSpinnerWidget {
|
||||
return AutocompleteSpinnerWidget(project)
|
||||
}
|
||||
|
||||
override fun getId(): String {
|
||||
return "AutocompleteSpinnerWidget"
|
||||
}
|
||||
|
||||
override fun getDisplayName(): String {
|
||||
return "Continue Autocomplete"
|
||||
}
|
||||
|
||||
override fun isAvailable(p0: Project): Boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
override fun createWidget(project: Project): StatusBarWidget {
|
||||
return AutocompleteSpinnerWidget(project)
|
||||
}
|
||||
|
||||
override fun disposeWidget(p0: StatusBarWidget) {
|
||||
Disposer.dispose(p0)
|
||||
}
|
||||
|
||||
override fun canBeEnabledOn(p0: StatusBar): Boolean = true
|
||||
}
|
||||
|
|
@ -58,7 +58,9 @@ class ContinueMultilineCustomElementRenderer (
|
|||
FontInfo.getFontMetrics(font, FontInfo.getFontRenderContext(editor.contentComponent))
|
||||
val fontWidth =
|
||||
font.createGlyphVector(metrics.fontRenderContext, text).visualBounds.width
|
||||
val widthBeforeCaret = (editor as EditorImpl).getFontMetrics(Font.PLAIN).stringWidth(text.substring(0, currentColumn))
|
||||
val widthBeforeCaret = (editor as EditorImpl).getFontMetrics(Font.PLAIN).stringWidth(
|
||||
text.substring(0, minOf(currentColumn, text.length))
|
||||
)
|
||||
return max(0, widthBeforeCaret - (editor as EditorImpl).scrollingModel.horizontalScrollOffset)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,6 +22,8 @@
|
|||
<projectService
|
||||
id="AutocompleteLookupListener"
|
||||
serviceImplementation="com.github.continuedev.continueintellijextension.autocomplete.AutocompleteLookupListener"/>
|
||||
<statusBarWidgetFactory implementation="com.github.continuedev.continueintellijextension.autocomplete.AutocompleteSpinnerWidgetFactory"
|
||||
id="AutocompleteSpinnerWidget"/>
|
||||
</extensions>
|
||||
|
||||
<resource-bundle>messages.MyBundle</resource-bundle>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue