diff --git a/core/protocol.ts b/core/protocol.ts index 6e2769d45..cf98368ae 100644 --- a/core/protocol.ts +++ b/core/protocol.ts @@ -105,6 +105,7 @@ export type Protocol = { suffix: string; input: string; language: string | undefined; + modelTitle: string | undefined; }, ProtocolGeneratorType, ]; diff --git a/core/util/verticalEdit.ts b/core/util/verticalEdit.ts index 3265e04c4..341b847ee 100644 --- a/core/util/verticalEdit.ts +++ b/core/util/verticalEdit.ts @@ -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 { + 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 diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/actions/ContinuePluginActions.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/actions/ContinuePluginActions.kt index 5eaae2fc6..ee325ba89 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/actions/ContinuePluginActions.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/actions/ContinuePluginActions.kt @@ -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() + val diffStreamService = project.service() 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() + val diffStreamService = project.service() diffStreamService.reject(editor) } } diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/DiffStreamHandler.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/DiffStreamHandler.kt index ea664871d..629b5c680 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/DiffStreamHandler.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/DiffStreamHandler.kt @@ -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 diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/DiffStreamService.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/DiffStreamService.kt index 36a687fa7..48aba76b8 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/DiffStreamService.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/DiffStreamService.kt @@ -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() @@ -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] + } } \ No newline at end of file diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/InlineEditAction.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/InlineEditAction.kt index 28fec686f..6fd659237 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/InlineEditAction.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/InlineEditAction.kt @@ -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() + val modelTitles = mutableListOf() + 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> + 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() + val diffStreamService = project.service() diffStreamService.register(diffStreamHandler, editor) diffStreamHandler.setup() + val comboBoxRef = Ref>() + 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, textArea: JTextArea, inlayRef: Ref, leftInset: Int, onEnter: () -> Unit, onCancel: () -> Unit, onAccept: () -> Unit, onReject: () -> Unit): JPanel { + fun makePanel(project: Project, customPanelRef: Ref, textArea: JTextArea, inlayRef: Ref, comboBoxRef: Ref>, leftInset: Int, modelTitles: List, 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, comboBoxRef: Ref>, 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() + + 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) } diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/ToggleInlineEditAction.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/ToggleInlineEditAction.kt new file mode 100644 index 000000000..f14ce4dad --- /dev/null +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/ToggleInlineEditAction.kt @@ -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() + 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() + } + } +} \ No newline at end of file diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/services/ContinueExtensionSettingsService.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/services/ContinueExtensionSettingsService.kt index 8042cdc24..d2da9d7d8 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/services/ContinueExtensionSettingsService.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/services/ContinueExtensionSettingsService.kt @@ -62,6 +62,7 @@ class ContinueSettingsComponent: DumbAware { open class ContinueExtensionSettings : PersistentStateComponent { class ContinueState { + var lastSelectedInlineEditModel: String? = null var shownWelcomeDialog: Boolean = false var remoteConfigServerUrl: String? = null var remoteConfigSyncPeriod: Int = 60 diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/toolWindow/ContinuePluginToolWindowFactory.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/toolWindow/ContinuePluginToolWindowFactory.kt index 94a498da1..5f71aaed4 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/toolWindow/ContinuePluginToolWindowFactory.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/toolWindow/ContinuePluginToolWindowFactory.kt @@ -21,6 +21,13 @@ class ContinuePluginToolWindowFactory : ToolWindowFactory, DumbAware { toolWindow.contentManager.addContent(content) val titleActions = mutableListOf() createTitleActions(titleActions) + + // Add MaximizeToolWindow action + val action = ActionManager.getInstance().getAction("MaximizeToolWindow") + if (action != null) { + titleActions.add(action) + } + toolWindow.setTitleActions(titleActions) } diff --git a/extensions/intellij/src/main/resources/META-INF/plugin.xml b/extensions/intellij/src/main/resources/META-INF/plugin.xml index 4b3daafea..0c02a2d70 100644 --- a/extensions/intellij/src/main/resources/META-INF/plugin.xml +++ b/extensions/intellij/src/main/resources/META-INF/plugin.xml @@ -16,7 +16,7 @@ factoryClass="com.github.continuedev.continueintellijextension.toolWindow.ContinuePluginToolWindowFactory"/> - @@ -46,6 +46,16 @@ first-keystroke="meta I"/> + + + + + diff --git a/extensions/intellij/src/main/resources/continue_tutorial.py b/extensions/intellij/src/main/resources/continue_tutorial.py index 7c8d6b0c8..9a7194ad0 100644 --- a/extensions/intellij/src/main/resources/continue_tutorial.py +++ b/extensions/intellij/src/main/resources/continue_tutorial.py @@ -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 " 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 diff --git a/extensions/vscode/src/diff/verticalPerLine/manager.ts b/extensions/vscode/src/diff/verticalPerLine/manager.ts index fd41f9886..3ce6f79c0 100644 --- a/extensions/vscode/src/diff/verticalPerLine/manager.ts +++ b/extensions/vscode/src/diff/verticalPerLine/manager.ts @@ -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, diff --git a/gui/src/components/dialogs/KeyboardShortcuts.tsx b/gui/src/components/dialogs/KeyboardShortcuts.tsx index c8d3f8397..28dac7fee 100644 --- a/gui/src/components/dialogs/KeyboardShortcuts.tsx +++ b/gui/src/components/dialogs/KeyboardShortcuts.tsx @@ -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: "⌃ ⇧ ⏎", diff --git a/pkg/src/core.ts b/pkg/src/core.ts index c0b1662e7..d3afed8aa 100644 --- a/pkg/src/core.ts +++ b/pkg/src/core.ts @@ -303,7 +303,7 @@ export class Core { msg: Message, ) { 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,