✨ Dropdown to select model

This commit is contained in:
Nate Sesti 2024-04-01 15:27:51 -07:00
parent 21f5affbad
commit df71ad724e
14 changed files with 162 additions and 34 deletions

View file

@ -105,6 +105,7 @@ export type Protocol = {
suffix: string;
input: string;
language: string | undefined;
modelTitle: string | undefined;
},
ProtocolGeneratorType<DiffLine>,
];

View file

@ -9,6 +9,7 @@ import {
import { streamDiff } from "../diff/streamDiff";
import { streamLines } from "../diff/util";
import { gptEditPrompt } from "../llm/templates/edit";
import { Telemetry } from "./posthog";
function constructPrompt(
prefix: string,
@ -52,6 +53,11 @@ export async function* streamDiffLines(
input: string,
language: string | undefined,
): AsyncGenerator<DiffLine> {
Telemetry.capture("inlineEdit", {
model: llm.model,
provider: llm.providerName,
});
// Strip common indentation for the LLM, then add back after generation
let oldLines =
highlighted.length > 0

View file

@ -41,7 +41,7 @@ class AcceptDiffAction : AnAction() {
private fun acceptVerticalDiff(e: AnActionEvent) {
val project = e.project ?: return
val editor = e.getData(PlatformDataKeys.EDITOR) ?: FileEditorManager.getInstance(project).selectedTextEditor ?: return
val diffStreamService = service<DiffStreamService>()
val diffStreamService = project.service<DiffStreamService>()
diffStreamService.accept(editor)
}
}
@ -60,7 +60,7 @@ class RejectDiffAction : AnAction() {
private fun rejectVerticalDiff(e: AnActionEvent) {
val project = e.project ?: return
val editor = e.getData(PlatformDataKeys.EDITOR) ?: FileEditorManager.getInstance(project).selectedTextEditor ?: return
val diffStreamService = service<DiffStreamService>()
val diffStreamService = project.service<DiffStreamService>()
diffStreamService.reject(editor)
}
}

View file

@ -36,7 +36,7 @@ class DiffStreamHandler(
) {
private val greenKey = run {
val attributes = TextAttributes().apply {
backgroundColor = JBColor(0x4000FF00.toInt(), 0x4000FF00.toInt())
backgroundColor = JBColor(0x3000FF00.toInt(), 0x3000FF00.toInt())
}
val key = TextAttributesKey.createTextAttributesKey("CONTINUE_DIFF_NEW_LINE")
key.let { editor.colorsScheme.setAttributes(it, attributes) }
@ -159,7 +159,16 @@ class DiffStreamHandler(
running = false
}
fun run(input : String, prefix : String, highlighted : String, suffix : String) {
fun toggleFocus() {
if (textArea.hasFocus()) {
textArea.transferFocus()
editor.contentComponent.requestFocus()
} else {
textArea.requestFocus()
}
}
fun run(input : String, prefix : String, highlighted : String, suffix : String, modelTitle : String) {
// Undo changes
resetState()
@ -185,6 +194,7 @@ class DiffStreamHandler(
"highlighted" to highlighted,
"suffix" to suffix,
"language" to virtualFile?.fileType?.name,
"modelTitle" to modelTitle
), null) { response ->
if (!running) {
return@request

View file

@ -3,7 +3,7 @@ package com.github.continuedev.continueintellijextension.editor
import com.intellij.openapi.components.Service
import com.intellij.openapi.editor.Editor
@Service(Service.Level.APP)
@Service(Service.Level.PROJECT)
class DiffStreamService {
private val handlers = mutableMapOf<Editor, DiffStreamHandler>()
@ -12,6 +12,7 @@ class DiffStreamService {
handlers[editor]?.reject()
}
handlers[editor] = handler
println("Registered handler for editor")
}
fun reject(editor: Editor) {
@ -23,4 +24,8 @@ class DiffStreamService {
handlers[editor]?.accept()
handlers.remove(editor)
}
fun getHandler(editor: Editor): DiffStreamHandler? {
return handlers[editor]
}
}

View file

@ -1,8 +1,11 @@
package com.github.continuedev.continueintellijextension.editor
import com.github.continuedev.continueintellijextension.`continue`.GetTheme
import com.github.continuedev.continueintellijextension.services.ContinueExtensionSettings
import com.github.continuedev.continueintellijextension.services.ContinuePluginService
import com.github.continuedev.continueintellijextension.utils.getAltKeyLabel
import com.github.continuedev.continueintellijextension.utils.getMetaKeyLabel
import com.google.gson.Gson
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
@ -12,6 +15,7 @@ import com.intellij.openapi.editor.colors.EditorColorsManager
import com.intellij.openapi.editor.colors.EditorFontType
import com.intellij.openapi.editor.impl.EditorImpl
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Ref
import com.intellij.openapi.util.TextRange
import com.intellij.ui.JBColor
@ -49,6 +53,21 @@ class InlineEditAction : AnAction(), DumbAware {
val project = e.getData(PlatformDataKeys.PROJECT) ?: return
val manager = EditorComponentInlaysManager.from(editor)
// Get list of model titles
val continuePluginService = project.service<ContinuePluginService>()
val modelTitles = mutableListOf<String>()
continuePluginService.coreMessenger?.request("config/getBrowserSerialized", null, null) { response ->
val gson = Gson()
val config = gson.fromJson(response, Map::class.java)
val models = config["models"] as List<Map<String, Any>>
modelTitles.addAll(models.map { it["title"] as String })
}
val maxWaitTime = 200
val startTime = System.currentTimeMillis()
while (modelTitles.isEmpty() && System.currentTimeMillis() - startTime < maxWaitTime) {
Thread.sleep(20)
}
// Get highlighted range
val selectionModel = editor.selectionModel
val start = selectionModel.selectionStart
@ -85,17 +104,19 @@ class InlineEditAction : AnAction(), DumbAware {
}, {
customPanelRef.get().finish()
})
val diffStreamService = service<DiffStreamService>()
val diffStreamService = project.service<DiffStreamService>()
diffStreamService.register(diffStreamHandler, editor)
diffStreamHandler.setup()
val comboBoxRef = Ref<JComboBox<String>>()
fun onEnter() {
customPanelRef.get().enter()
diffStreamHandler.run(textArea.text, prefix, highlighted, suffix)
diffStreamHandler.run(textArea.text, prefix, highlighted, suffix, comboBoxRef.get().selectedItem as String)
}
val panel = makePanel(customPanelRef, textArea, inlayRef, leftInset, {onEnter()}, {
val panel = makePanel(project, customPanelRef, textArea, inlayRef, comboBoxRef, leftInset, modelTitles, {onEnter()}, {
diffStreamService.reject(editor)
selectionModel.setSelection(start, end)
}, {
@ -115,7 +136,7 @@ class InlineEditAction : AnAction(), DumbAware {
textArea.addKeyListener(object : KeyAdapter() {
override fun keyPressed(e: KeyEvent) {
if (e.keyCode == KeyEvent.VK_ESCAPE) {
diffStreamHandler.reject()
diffStreamService.reject(editor)
// Re-highlight the selected text
selectionModel.setSelection(start, end)
@ -172,7 +193,7 @@ class InlineEditAction : AnAction(), DumbAware {
return textArea
}
fun makePanel(customPanelRef: Ref<CustomPanel>, textArea: JTextArea, inlayRef: Ref<Disposable>, leftInset: Int, onEnter: () -> Unit, onCancel: () -> Unit, onAccept: () -> Unit, onReject: () -> Unit): JPanel {
fun makePanel(project: Project, customPanelRef: Ref<CustomPanel>, textArea: JTextArea, inlayRef: Ref<Disposable>, comboBoxRef: Ref<JComboBox<String>>, leftInset: Int, modelTitles: List<String>, onEnter: () -> Unit, onCancel: () -> Unit, onAccept: () -> Unit, onReject: () -> Unit): JPanel {
val topPanel = ShadowPanel(MigLayout("wrap 1, insets 10 $leftInset 8 8, gap 0!")).apply {
val globalScheme = EditorColorsManager.getInstance().globalScheme
val defaultBackground = globalScheme.defaultBackground
@ -181,7 +202,7 @@ class InlineEditAction : AnAction(), DumbAware {
isOpaque = false
}
val panel = CustomPanel(MigLayout("wrap 1, insets 0, gap 0!, fillx"), onEnter, onCancel, onAccept, onReject).apply {
val panel = CustomPanel(MigLayout("wrap 1, insets 0, gap 0!, fillx"), project, modelTitles, comboBoxRef, onEnter, onCancel, onAccept, onReject).apply {
val globalScheme = EditorColorsManager.getInstance().globalScheme
val defaultBackground = globalScheme.defaultBackground
background = defaultBackground
@ -214,24 +235,56 @@ class InlineEditAction : AnAction(), DumbAware {
}
}
class CustomPanel(layout: MigLayout, onEnter: () -> Unit, onCancel: () -> Unit, onAccept: () -> Unit, onReject: () -> Unit): JPanel(layout) {
class CustomPanel(layout: MigLayout, project: Project, modelTitles: List<String>, comboBoxRef: Ref<JComboBox<String>>, onEnter: () -> Unit, onCancel: () -> Unit, onAccept: () -> Unit, onReject: () -> Unit): JPanel(layout) {
private val subPanelA: JPanel = JPanel(MigLayout("insets 0, fillx")).apply {
val globalScheme = EditorColorsManager.getInstance().globalScheme
val defaultBackground = globalScheme.defaultBackground
val leftButton = CustomButton("Esc to cancel", {onCancel()}).apply {
val leftButton = CustomButton("Esc to cancel") { onCancel() }.apply {
foreground = Color(128, 128, 128, 200)
background = defaultBackground
}
val rightButton = CustomButton("Submit", {onEnter()}).apply {
background = GetTheme().getHighlight()
val continueSettingsService = service<ContinueExtensionSettings>()
val dropdown = JComboBox(modelTitles.toTypedArray()).apply {
isEditable = true
background = defaultBackground
foreground = Color(128, 128, 128, 200)
font = Font("Arial", Font.PLAIN, 11)
border = EmptyBorder(2, 4, 2, 4)
isOpaque = false
isEditable = false
cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)
renderer = DefaultListCellRenderer().apply {
horizontalAlignment = SwingConstants.RIGHT
}
selectedIndex = continueSettingsService.continueState.lastSelectedInlineEditModel?.let { modelTitles.indexOf(it) } ?: 0
addActionListener {
continueSettingsService.continueState.lastSelectedInlineEditModel = selectedItem as String
}
}
comboBoxRef.set(dropdown)
val rightButton = CustomButton("Submit") { onEnter() }.apply {
// background = GetTheme().getHighlight()
background = JBColor(0xe04573e8.toInt(), 0xe04573e8.toInt())
foreground = JBColor.WHITE
}
val rightPanel = JPanel(MigLayout("insets 0, fillx")).apply {
isOpaque = false
border = EmptyBorder(0, 0, 0, 0)
add(dropdown, "align right")
add(rightButton, "align right")
}
border = EmptyBorder(4, 8, 4, 8)
add(leftButton, "align left")
add(rightButton, "align right")
add(rightPanel, "align right")
isOpaque = false
cursor = Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)
@ -242,7 +295,7 @@ class CustomPanel(layout: MigLayout, onEnter: () -> Unit, onCancel: () -> Unit,
val globalScheme = EditorColorsManager.getInstance().globalScheme
val defaultBackground = globalScheme.defaultBackground
val leftButton = CustomButton("Esc to cancel", {onCancel()}).apply {
val leftButton = CustomButton("Esc to cancel") { onCancel() }.apply {
foreground = Color(128, 128, 128, 200)
background = defaultBackground
}
@ -264,15 +317,15 @@ class CustomPanel(layout: MigLayout, onEnter: () -> Unit, onCancel: () -> Unit,
font = Font("Arial", Font.PLAIN, 11)
}
val leftButton = CustomButton("${getAltKeyLabel()}⇧N", {onReject()}).apply {
val leftButton = CustomButton("${getAltKeyLabel()}⇧N") { onReject() }.apply {
background = Color(255, 0, 0, 64)
}
val rightButton = CustomButton("${getAltKeyLabel()}⇧Y", {onAccept()}).apply {
val rightButton = CustomButton("${getAltKeyLabel()}⇧Y") { onAccept() }.apply {
background = Color(0, 255, 0, 64)
}
val rightPanel = JPanel().apply {
val rightPanel = JPanel(MigLayout("insets 0, fillx")).apply {
isOpaque = false
add(leftButton, "align right")
add(rightButton, "align right")
@ -286,11 +339,14 @@ class CustomPanel(layout: MigLayout, onEnter: () -> Unit, onCancel: () -> Unit,
}
fun setup() {
remove(subPanelB)
remove(subPanelC)
add(subPanelA, "grow, gap 0!")
}
fun enter() {
remove(subPanelA)
remove(subPanelC)
add(subPanelB, "grow, gap 0!")
revalidate()
repaint()
@ -350,7 +406,7 @@ class CustomButton(text: String, onClick: () -> Unit) : JLabel(text, CENTER) {
}
})
verticalAlignment = CENTER
// verticalAlignment = CENTER
font = Font("Arial", Font.PLAIN, 11)
border = EmptyBorder(2, 4, 2, 4)
}

View file

@ -0,0 +1,27 @@
package com.github.continuedev.continueintellijextension.editor
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.PlatformDataKeys
import com.intellij.openapi.components.service
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.DumbAware
class ToggleInlineEditAction : AnAction(), DumbAware {
override fun actionPerformed(e: AnActionEvent) {
val project = e.getData(PlatformDataKeys.PROJECT) ?: return
val editor = e.getData(PlatformDataKeys.EDITOR) ?: FileEditorManager.getInstance(project).selectedTextEditor ?: return
val diffService = project.service<DiffStreamService>()
val handler = diffService.getHandler(editor)
if (handler == null) {
val actionId = "continue.inlineEdit"
val action = ActionManager.getInstance().getAction(actionId)
if (action != null) {
e.actionManager.tryToExecute(action, e.inputEvent, null, null, true)
}
} else {
handler.toggleFocus()
}
}
}

View file

@ -62,6 +62,7 @@ class ContinueSettingsComponent: DumbAware {
open class ContinueExtensionSettings : PersistentStateComponent<ContinueExtensionSettings.ContinueState> {
class ContinueState {
var lastSelectedInlineEditModel: String? = null
var shownWelcomeDialog: Boolean = false
var remoteConfigServerUrl: String? = null
var remoteConfigSyncPeriod: Int = 60

View file

@ -21,6 +21,13 @@ class ContinuePluginToolWindowFactory : ToolWindowFactory, DumbAware {
toolWindow.contentManager.addContent(content)
val titleActions = mutableListOf<AnAction>()
createTitleActions(titleActions)
// Add MaximizeToolWindow action
val action = ActionManager.getInstance().getAction("MaximizeToolWindow")
if (action != null) {
titleActions.add(action)
}
toolWindow.setTitleActions(titleActions)
}

View file

@ -16,7 +16,7 @@
factoryClass="com.github.continuedev.continueintellijextension.toolWindow.ContinuePluginToolWindowFactory"/>
<projectService id="ContinuePluginService"
serviceImplementation="com.github.continuedev.continueintellijextension.services.ContinuePluginService"/>
<applicationService
<projectService
id="DiffStreamService"
serviceImplementation="com.github.continuedev.continueintellijextension.editor.DiffStreamService"/>
</extensions>
@ -46,6 +46,16 @@
first-keystroke="meta I"/>
</action>
<action class="com.github.continuedev.continueintellijextension.editor.ToggleInlineEditAction"
id="continue.toggleInlineEdit"
description="Toggle Inline Edit"
text="Toggle Inline Edit">
<keyboard-shortcut keymap="$default"
first-keystroke="ctrl shift I"/>
<keyboard-shortcut keymap="Mac OS X"
first-keystroke="meta shift I"/>
</action>
<action id="continue.acceptDiff"
class="com.github.continuedev.continueintellijextension.actions.AcceptDiffAction"
text="Accept Diff" description="Accept Diff">

View file

@ -39,7 +39,7 @@ select the code and toggle the Continue input box"""
# endregion
# region ————————————————————————————————— Part 2: Edit code [⌘ ⇧ J] —————————————————————————————————
# region ————————————————————————————————— Part 2: Edit code [⌘ I] —————————————————————————————————
"""Step 1: Highlight this code"""
@ -54,15 +54,15 @@ def bubble_sort(x):
return x
"""Step 2: Use the keyboard shortcut [⌘ J] to
"""Step 2: Use the keyboard shortcut [⌘ I] to
select the code"""
"""Step 3: Type "/edit <your edit request>" and press Enter"""
# e.g.) /edit optimize this function
# e.g.) /edit write comments
"""Step 3: Type instructions to edit the code and press Enter"""
# e.g.) "optimize this function"
# e.g.) "edit write comments"
"""Step 4: Use keyboard shortcuts to
accept [⌘ ⇧ ⏎] or reject [⌘ ⇧ ⌫] the edit"""
accept [⌥ ⇧ Y] or reject [⌥ ⇧ N] the edit"""
# endregion

View file

@ -1,7 +1,6 @@
import { ConfigHandler } from "core/config/handler";
import { pruneLinesFromBottom, pruneLinesFromTop } from "core/llm/countTokens";
import { getMarkdownLanguageTagForFile } from "core/util";
import { Telemetry } from "core/util/posthog";
import { streamDiffLines } from "core/util/verticalEdit";
import * as vscode from "vscode";
import { VerticalPerLineDiffHandler } from "./handler";
@ -209,10 +208,6 @@ export class VerticalPerLineDiffManager {
);
try {
Telemetry.capture("inlineEdit", {
model: llm.model,
provider: llm.providerName,
});
await diffHandler.run(
streamDiffLines(
prefix,

View file

@ -162,6 +162,16 @@ const jetbrainsShortcuts: KeyboardShortcutProps[] = [
windows: "⌃ ⇧ J",
description: "Select Code",
},
{
mac: "⌘ I",
windows: "⌃ I",
description: "Edit highlighted code",
},
{
mac: "⌘ ⇧ I",
windows: "⌃ ⇧ I",
description: "Toggle inline edit focus",
},
{
mac: "⌘ ⇧ ⏎",
windows: "⌃ ⇧ ⏎",

View file

@ -303,7 +303,7 @@ export class Core {
msg: Message<Protocol["streamDiffLines"][0]>,
) {
const data = msg.data;
const llm = await configHandler.llmFromTitle();
const llm = await configHandler.llmFromTitle(msg.data.modelTitle);
for await (const diffLine of streamDiffLines(
data.prefix,
data.highlighted,