🚧 WIP on inline edit browser embedding
This commit is contained in:
parent
2f67ded712
commit
de73f6b5c3
6 changed files with 321 additions and 6 deletions
|
|
@ -4,7 +4,7 @@ pluginGroup = com.github.continuedev.continueintellijextension
|
|||
pluginName = continue-intellij-extension
|
||||
pluginRepositoryUrl = https://github.com/continuedev/continue
|
||||
# SemVer format -> https://semver.org
|
||||
pluginVersion = 0.0.39
|
||||
pluginVersion = 0.0.40
|
||||
|
||||
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
|
||||
pluginSinceBuild = 223
|
||||
|
|
|
|||
|
|
@ -4,11 +4,6 @@ import com.github.continuedev.continueintellijextension.`continue`.uuid
|
|||
import com.github.continuedev.continueintellijextension.services.ContinueExtensionSettings
|
||||
import com.github.continuedev.continueintellijextension.services.ContinuePluginService
|
||||
import com.google.gson.Gson
|
||||
import com.intellij.codeInsight.AutoPopupController
|
||||
import com.intellij.codeInsight.lookup.LookupManager
|
||||
import com.intellij.codeInsight.lookup.impl.LookupImpl
|
||||
import com.intellij.openapi.actionSystem.ActionManager
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.openapi.application.WriteAction
|
||||
import com.intellij.openapi.application.invokeLater
|
||||
|
|
@ -95,6 +90,13 @@ class AutocompleteService(private val project: Project) {
|
|||
} else {
|
||||
editor.inlayModel.addInlineElement(offset, properties, ContinueCustomElementRenderer(editor, text))
|
||||
}
|
||||
|
||||
// val attributes = TextAttributes().apply {
|
||||
// backgroundColor = JBColor.GREEN
|
||||
// }
|
||||
// val key = TextAttributesKey.createTextAttributesKey("CONTINUE_AUTOCOMPLETE")
|
||||
// key.let { editor.colorsScheme.setAttributes(it, attributes) }
|
||||
// editor.markupModel.addLineHighlighter(key, editor.caretModel.logicalPosition.line, HighlighterLayer.LAST)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
package com.github.continuedev.continueintellijextension.editor
|
||||
|
||||
import com.intellij.openapi.editor.EditorLinePainter
|
||||
import com.intellij.openapi.editor.LineExtensionInfo
|
||||
import com.intellij.openapi.editor.colors.EditorColorsManager
|
||||
import com.intellij.openapi.editor.colors.TextAttributesKey
|
||||
import com.intellij.openapi.editor.markup.EffectType
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.ui.JBColor
|
||||
import java.awt.Color
|
||||
import java.awt.Font
|
||||
|
||||
class ContinueEditorLinePainter : EditorLinePainter() {
|
||||
override fun getLineExtensions(project: Project, file: VirtualFile, lineNumber: Int): MutableCollection<LineExtensionInfo>? {
|
||||
return null
|
||||
// return mutableListOf(LineExtensionInfo("Line $lineNumber", null, EffectType.BOLD_DOTTED_LINE, JBColor.BLUE, Font.PLAIN ))
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,158 @@
|
|||
package com.github.continuedev.continueintellijextension.editor
|
||||
|
||||
import com.intellij.openapi.Disposable
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.editor.ex.EditorEx
|
||||
import com.intellij.openapi.editor.ex.util.EditorUtil
|
||||
import com.intellij.openapi.editor.impl.EditorEmbeddedComponentManager
|
||||
import com.intellij.openapi.editor.impl.EditorImpl
|
||||
import com.intellij.openapi.editor.impl.view.FontLayoutService
|
||||
import com.intellij.openapi.util.Disposer
|
||||
import com.intellij.openapi.util.Key
|
||||
import com.intellij.ui.components.JBScrollPane
|
||||
import com.intellij.util.concurrency.annotations.RequiresEdt
|
||||
import com.intellij.util.ui.JBUI
|
||||
import java.awt.Dimension
|
||||
import java.awt.Font
|
||||
import java.awt.event.ComponentAdapter
|
||||
import java.awt.event.ComponentEvent
|
||||
import javax.swing.JComponent
|
||||
import javax.swing.ScrollPaneConstants
|
||||
import kotlin.math.ceil
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
/**
|
||||
* Copied from com.intellij.util.ui.codereview.diff.EditorComponentInlaysManager
|
||||
* via https://github.com/cursive-ide/component-inlay-example/blob/master/src/main/kotlin/inlays/EditorComponentInlaysManager.kt
|
||||
*/
|
||||
class EditorComponentInlaysManager(val editor: EditorImpl) : Disposable {
|
||||
|
||||
private val managedInlays = mutableMapOf<ComponentWrapper, Disposable>()
|
||||
private val editorWidthWatcher = EditorTextWidthWatcher()
|
||||
|
||||
init {
|
||||
editor.scrollPane.viewport.addComponentListener(editorWidthWatcher)
|
||||
Disposer.register(this, Disposable {
|
||||
editor.scrollPane.viewport.removeComponentListener(editorWidthWatcher)
|
||||
})
|
||||
|
||||
EditorUtil.disposeWithEditor(editor, this)
|
||||
}
|
||||
|
||||
@RequiresEdt
|
||||
fun insertAfter(lineIndex: Int, component: JComponent): Disposable? {
|
||||
if (Disposer.isDisposed(this)) return null
|
||||
|
||||
// Dispose all other inlays
|
||||
managedInlays.values.forEach(Disposer::dispose)
|
||||
|
||||
val wrappedComponent = ComponentWrapper(component)
|
||||
val offset = editor.document.getLineEndOffset(lineIndex)
|
||||
|
||||
return EditorEmbeddedComponentManager.getInstance()
|
||||
.addComponent(editor, wrappedComponent,
|
||||
EditorEmbeddedComponentManager.Properties(EditorEmbeddedComponentManager.ResizePolicy.none(),
|
||||
null,
|
||||
true,
|
||||
false,
|
||||
0,
|
||||
offset))?.also {
|
||||
managedInlays[wrappedComponent] = it
|
||||
Disposer.register(it, Disposable { managedInlays.remove(wrappedComponent) })
|
||||
}
|
||||
}
|
||||
|
||||
private inner class ComponentWrapper(private val component: JComponent) : JBScrollPane(component) {
|
||||
init {
|
||||
isOpaque = false
|
||||
viewport.isOpaque = false
|
||||
|
||||
border = JBUI.Borders.empty()
|
||||
viewportBorder = JBUI.Borders.empty()
|
||||
|
||||
horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
|
||||
verticalScrollBar.preferredSize = Dimension(0, 0)
|
||||
setViewportView(component)
|
||||
|
||||
component.addComponentListener(object : ComponentAdapter() {
|
||||
override fun componentResized(e: ComponentEvent) =
|
||||
dispatchEvent(ComponentEvent(component, ComponentEvent.COMPONENT_RESIZED))
|
||||
})
|
||||
}
|
||||
|
||||
override fun getPreferredSize(): Dimension {
|
||||
return Dimension(editorWidthWatcher.editorTextWidth, component.preferredSize.height)
|
||||
}
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
managedInlays.values.forEach(Disposer::dispose)
|
||||
}
|
||||
|
||||
private inner class EditorTextWidthWatcher : ComponentAdapter() {
|
||||
|
||||
var editorTextWidth: Int = 0
|
||||
|
||||
private val maximumEditorTextWidth: Int
|
||||
private val verticalScrollbarFlipped: Boolean
|
||||
|
||||
init {
|
||||
val metrics = editor.getFontMetrics(Font.PLAIN)
|
||||
val spaceWidth = FontLayoutService.getInstance().charWidth2D(metrics, ' '.toInt())
|
||||
// -4 to create some space
|
||||
maximumEditorTextWidth = ceil(spaceWidth * (editor.settings.getRightMargin(editor.project)) - 4).toInt()
|
||||
|
||||
val scrollbarFlip = editor.scrollPane.getClientProperty(JBScrollPane.Flip::class.java)
|
||||
verticalScrollbarFlipped =
|
||||
scrollbarFlip == JBScrollPane.Flip.HORIZONTAL || scrollbarFlip == JBScrollPane.Flip.BOTH
|
||||
}
|
||||
|
||||
override fun componentResized(e: ComponentEvent) = updateWidthForAllInlays()
|
||||
override fun componentHidden(e: ComponentEvent) = updateWidthForAllInlays()
|
||||
override fun componentShown(e: ComponentEvent) = updateWidthForAllInlays()
|
||||
|
||||
private fun updateWidthForAllInlays() {
|
||||
val newWidth = calcWidth()
|
||||
if (editorTextWidth == newWidth) return
|
||||
editorTextWidth = newWidth
|
||||
|
||||
managedInlays.keys.forEach {
|
||||
it.dispatchEvent(ComponentEvent(it, ComponentEvent.COMPONENT_RESIZED))
|
||||
it.invalidate()
|
||||
}
|
||||
}
|
||||
|
||||
private fun calcWidth(): Int {
|
||||
val visibleEditorTextWidth = editor.scrollPane.viewport.width - getVerticalScrollbarWidth() - getGutterTextGap()
|
||||
return min(max(visibleEditorTextWidth, 0), maximumEditorTextWidth)
|
||||
}
|
||||
|
||||
private fun getVerticalScrollbarWidth(): Int {
|
||||
val width = editor.scrollPane.verticalScrollBar.width
|
||||
return if (!verticalScrollbarFlipped) width * 2 else width
|
||||
}
|
||||
|
||||
private fun getGutterTextGap(): Int {
|
||||
return if (verticalScrollbarFlipped) {
|
||||
val gutter = (editor as EditorEx).gutterComponentEx
|
||||
gutter.width - gutter.whitespaceSeparatorOffset
|
||||
} else 0
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val INLAYS_KEY: Key<EditorComponentInlaysManager> = Key.create("EditorComponentInlaysManager")
|
||||
|
||||
fun from(editor: Editor): EditorComponentInlaysManager {
|
||||
return synchronized(editor) {
|
||||
val manager = editor.getUserData(INLAYS_KEY)
|
||||
if (manager == null) {
|
||||
val newManager = EditorComponentInlaysManager(editor as EditorImpl)
|
||||
editor.putUserData(INLAYS_KEY, newManager)
|
||||
newManager
|
||||
} else manager
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,126 @@
|
|||
package com.github.continuedev.continueintellijextension.editor
|
||||
|
||||
import com.github.continuedev.continueintellijextension.toolWindow.JS_QUERY_POOL_SIZE
|
||||
import com.intellij.icons.AllIcons
|
||||
import com.intellij.openapi.Disposable
|
||||
import com.intellij.openapi.actionSystem.*
|
||||
import com.intellij.openapi.actionSystem.impl.ActionButton
|
||||
import com.intellij.openapi.editor.EditorFactory
|
||||
import com.intellij.openapi.editor.actions.IncrementalFindAction
|
||||
import com.intellij.openapi.editor.ex.EditorEx
|
||||
import com.intellij.openapi.editor.impl.EditorImpl
|
||||
import com.intellij.openapi.fileTypes.FileTypes
|
||||
import com.intellij.openapi.project.DumbAware
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.Disposer
|
||||
import com.intellij.openapi.util.Ref
|
||||
import com.intellij.ui.EditorTextField
|
||||
import com.intellij.ui.jcef.JBCefBrowser
|
||||
import com.intellij.ui.jcef.JBCefClient
|
||||
import com.intellij.util.ui.UIUtil
|
||||
import net.miginfocom.swing.MigLayout
|
||||
import java.awt.event.ComponentAdapter
|
||||
import java.awt.event.ComponentEvent
|
||||
import java.awt.event.KeyAdapter
|
||||
import java.awt.event.KeyEvent
|
||||
import javax.swing.JButton
|
||||
import javax.swing.JPanel
|
||||
import javax.swing.KeyStroke
|
||||
|
||||
/**
|
||||
* Adapted from https://github.com/cursive-ide/component-inlay-example/blob/master/src/main/kotlin/inlays/InlineEditAction.kt
|
||||
*/
|
||||
class InlineEditAction : AnAction(), DumbAware {
|
||||
override fun update(e: AnActionEvent) {
|
||||
e.presentation.isEnabled = true
|
||||
e.presentation.isVisible = true
|
||||
}
|
||||
|
||||
private val preloadedBrowser: JBCefBrowser by lazy {
|
||||
JBCefBrowser.createBuilder().setOffScreenRendering(true).build().apply {
|
||||
jbCefClient.setProperty(
|
||||
JBCefClient.Properties.JS_QUERY_POOL_SIZE,
|
||||
JS_QUERY_POOL_SIZE
|
||||
)
|
||||
loadHTML("<html><body><input type='text'/></body></html>")
|
||||
}
|
||||
}
|
||||
|
||||
override fun actionPerformed(e: AnActionEvent) {
|
||||
val editor = e.getData(PlatformDataKeys.EDITOR) ?: return
|
||||
val project = e.getData(PlatformDataKeys.PROJECT) ?: return
|
||||
val manager = EditorComponentInlaysManager.from(editor)
|
||||
val lineNumber = editor.document.getLineNumber(editor.caretModel.offset) - 1
|
||||
val inlayRef = Ref<Disposable>()
|
||||
val panel = makePanel(makeEditor(project, inlayRef), inlayRef)
|
||||
val inlay = manager.insertAfter(lineNumber, panel)
|
||||
panel.revalidate()
|
||||
inlayRef.set(inlay)
|
||||
val viewport = (editor as? EditorImpl)?.scrollPane?.viewport
|
||||
viewport?.dispatchEvent(ComponentEvent(viewport, ComponentEvent.COMPONENT_RESIZED))
|
||||
}
|
||||
|
||||
fun makePanel(editor: EditorTextField, inlayRef: Ref<Disposable>): JPanel {
|
||||
val action = object : AnAction({ "Close" }, AllIcons.Actions.Close) {
|
||||
override fun actionPerformed(e: AnActionEvent) {
|
||||
inlayRef.get().dispose()
|
||||
}
|
||||
}
|
||||
|
||||
val browser = preloadedBrowser
|
||||
// Set height of the browser to be 100px
|
||||
browser.component.preferredSize = browser.component.preferredSize.apply {
|
||||
height = 100
|
||||
}
|
||||
|
||||
editor.addKeyListener(object : KeyAdapter() {
|
||||
override fun keyPressed(e: KeyEvent) {
|
||||
if (e.keyCode == KeyEvent.VK_ESCAPE) {
|
||||
inlayRef.get().dispose()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return JPanel(MigLayout("wrap 1, insets 0, gap 0!, fillx")).apply {
|
||||
// add(editor, "grow, gap 0!")
|
||||
add(browser.component, "grow, gap 0!")
|
||||
addComponentListener(object : ComponentAdapter() {
|
||||
override fun componentShown(e: ComponentEvent?) {
|
||||
editor.requestFocus()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fun makeEditor(project: Project, inlayRef: Ref<Disposable>): EditorTextField {
|
||||
val factory = EditorFactory.getInstance()
|
||||
val document = factory.createDocument("")
|
||||
|
||||
return object : EditorTextField(document, project, FileTypes.PLAIN_TEXT) {
|
||||
//always paint pretty border
|
||||
override fun updateBorder(editor: EditorEx) = setupBorder(editor)
|
||||
|
||||
override fun createEditor(): EditorEx {
|
||||
// otherwise border background is painted from multiple places
|
||||
return super.createEditor().apply {
|
||||
//TODO: fix in editor
|
||||
//com.intellij.openapi.editor.impl.EditorImpl.getComponent() == non-opaque JPanel
|
||||
// which uses default panel color
|
||||
component.isOpaque = false
|
||||
//com.intellij.ide.ui.laf.darcula.ui.DarculaEditorTextFieldBorder.paintBorder
|
||||
scrollPane.isOpaque = false
|
||||
}
|
||||
}
|
||||
}.apply {
|
||||
putClientProperty(UIUtil.HIDE_EDITOR_FROM_DATA_CONTEXT_PROPERTY, true)
|
||||
setOneLineMode(false)
|
||||
setPlaceholder(text)
|
||||
addSettingsProvider {
|
||||
it.putUserData(IncrementalFindAction.SEARCH_DISABLED, true)
|
||||
it.colorsScheme.lineSpacing = 1f
|
||||
it.settings.isUseSoftWraps = true
|
||||
}
|
||||
selectAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -33,6 +33,16 @@
|
|||
</extensions>
|
||||
|
||||
<actions>
|
||||
<action class="com.github.continuedev.continueintellijextension.editor.InlineEditAction"
|
||||
id="continue.inlineEdit"
|
||||
description="Inline Edit"
|
||||
text="Inline Edit">
|
||||
<keyboard-shortcut keymap="$default"
|
||||
first-keystroke="ctrl I"/>
|
||||
<keyboard-shortcut keymap="Mac OS X"
|
||||
first-keystroke="meta I"/>
|
||||
</action>
|
||||
|
||||
<action id="continue.acceptDiff"
|
||||
class="com.github.continuedev.continueintellijextension.actions.AcceptDiffAction"
|
||||
text="Accept Diff" description="Accept Diff">
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue