diff --git a/core/indexing/CodebaseIndexer.ts b/core/indexing/CodebaseIndexer.ts index b377d5a2f..bc53539f4 100644 --- a/core/indexing/CodebaseIndexer.ts +++ b/core/indexing/CodebaseIndexer.ts @@ -34,7 +34,7 @@ export class CodebaseIndexer { * - To limit memory usage for indexes that perform computations locally, e.g. FTS * - To make as few requests as possible to the embeddings providers */ - filesPerBatch = 1000; + filesPerBatch = 500; // Note that we exclude certain Sqlite errors that we do not want to clear the indexes on, // e.g. a `SQLITE_BUSY` error. diff --git a/extensions/intellij/build.gradle.kts b/extensions/intellij/build.gradle.kts index ac38ac9fc..37586546a 100644 --- a/extensions/intellij/build.gradle.kts +++ b/extensions/intellij/build.gradle.kts @@ -146,6 +146,6 @@ tasks { channels.set(listOf(environment("RELEASE_CHANNEL").getOrElse("eap"))) // We always hide the stable releases until a few days of EAP have proven them stable - hidden = environment("RELEASE_CHANNEL").map { it == "stable" }.getOrElse(false) +// hidden = environment("RELEASE_CHANNEL").map { it == "stable" }.getOrElse(false) } } 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 db7df5b54..791172388 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 @@ -2,13 +2,13 @@ package com.github.continuedev.continueintellijextension.actions import com.github.continuedev.continueintellijextension.editor.DiffStreamService import com.github.continuedev.continueintellijextension.services.ContinuePluginService -import com.google.gson.Gson import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.PlatformDataKeys import com.intellij.openapi.components.ServiceManager import com.intellij.openapi.components.service import com.intellij.openapi.fileEditor.FileEditorManager +import com.intellij.openapi.project.Project import com.intellij.openapi.ui.DialogWrapper import com.intellij.openapi.ui.Messages import com.intellij.openapi.wm.ToolWindowManager @@ -19,8 +19,10 @@ import javax.swing.event.DocumentListener import com.intellij.ui.components.JBScrollPane import java.awt.BorderLayout -fun pluginServiceFromActionEvent(e: AnActionEvent): ContinuePluginService? { - val project = e.project ?: return null +fun getPluginService(project: Project?): ContinuePluginService? { + if (project == null) { + return null + } return ServiceManager.getService( project, ContinuePluginService::class.java @@ -34,7 +36,7 @@ class AcceptDiffAction : AnAction() { } private fun acceptHorizontalDiff(e: AnActionEvent) { - val continuePluginService = pluginServiceFromActionEvent(e) ?: return + val continuePluginService = getPluginService(e.project) ?: return continuePluginService.ideProtocolClient?.diffManager?.acceptDiff(null) } @@ -53,7 +55,7 @@ class RejectDiffAction : AnAction() { } private fun rejectHorizontalDiff(e: AnActionEvent) { - val continuePluginService = pluginServiceFromActionEvent(e) ?: return + val continuePluginService = getPluginService(e.project) ?: return continuePluginService.ideProtocolClient?.diffManager?.rejectDiff(null) } @@ -131,7 +133,7 @@ class RejectDiffAction : AnAction() { class QuickTextEntryAction : AnAction() { override fun actionPerformed(e: AnActionEvent) { - val continuePluginService = pluginServiceFromActionEvent(e) ?: return + val continuePluginService = getPluginService(e.project) ?: return continuePluginService.ideProtocolClient?.sendHighlightedCode() // Create and show the dialog @@ -140,7 +142,7 @@ class QuickTextEntryAction : AnAction() { // Show the text entered by the user if (text != null) { - val service = pluginServiceFromActionEvent(e) + val service = getPluginService(e.project) service?.ideProtocolClient?.sendMainUserInput(text) val project = e.project @@ -172,27 +174,29 @@ class ViewLogsAction : AnAction() { } } +fun focusContinueInput(project: Project?) { + if (project != null) { + val toolWindowManager = ToolWindowManager.getInstance(project) + val toolWindow = toolWindowManager.getToolWindow("Continue") + if (toolWindow != null) { + if (!toolWindow.isVisible) { + toolWindow.activate(null) + } + } + } + + val continuePluginService = getPluginService(project) ?: return + continuePluginService.continuePluginWindow?.content?.components?.get(0)?.requestFocus() + continuePluginService.sendToWebview("focusContinueInputWithoutClear", null) + + continuePluginService.ideProtocolClient?.sendHighlightedCode() +} class FocusContinueInputWithoutClearAction : AnAction() { override fun actionPerformed(e: AnActionEvent) { val project = e.project - if (project != null) { - val toolWindowManager = ToolWindowManager.getInstance(project) - val toolWindow = toolWindowManager.getToolWindow("Continue") - - if (toolWindow != null) { - if (!toolWindow.isVisible) { - toolWindow.activate(null) - } - } - } - - val continuePluginService = pluginServiceFromActionEvent(e) ?: return - continuePluginService.continuePluginWindow?.content?.components?.get(0)?.requestFocus() - continuePluginService.sendToWebview("focusContinueInputWithoutClear", null) - - continuePluginService.ideProtocolClient?.sendHighlightedCode() + focusContinueInput(project) } } @@ -210,7 +214,7 @@ class FocusContinueInputAction : AnAction() { } } - val continuePluginService = pluginServiceFromActionEvent(e) ?: return + val continuePluginService = getPluginService(e.project) ?: return continuePluginService.continuePluginWindow?.content?.components?.get(0)?.requestFocus() continuePluginService.sendToWebview("focusContinueInput", null) @@ -233,7 +237,7 @@ class NewContinueSessionAction : AnAction() { } } - val continuePluginService = pluginServiceFromActionEvent(e) ?: return + val continuePluginService = getPluginService(e.project) ?: return continuePluginService.continuePluginWindow?.content?.components?.get(0)?.requestFocus() continuePluginService.sendToWebview("focusContinueInputWithNewSession", null) @@ -248,7 +252,7 @@ class ViewHistoryAction : AnAction() { val toolWindow = toolWindowManager.getToolWindow("Continue") } - val continuePluginService = pluginServiceFromActionEvent(e) ?: return + val continuePluginService = getPluginService(e.project) ?: return continuePluginService.sendToWebview("viewHistory", null) } diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/CoreMessenger.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/CoreMessenger.kt index 80ec73b70..efff51fc6 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/CoreMessenger.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/CoreMessenger.kt @@ -238,7 +238,7 @@ class CoreMessenger(private val project: Project, esbuildPath: String, continueC var err = process?.errorStream?.bufferedReader()?.readText()?.trim() if (err != null) { // There are often "⚡️Done in Xms" messages, and we want everything after the last one - val delimiter = "⚡️Done in" + val delimiter = "⚡ Done in" val doneIndex = err.lastIndexOf(delimiter) if (doneIndex != -1) { err = err.substring(doneIndex + delimiter.length) diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/IdeProtocolClient.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/IdeProtocolClient.kt index cead14fbf..e13819000 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/IdeProtocolClient.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/IdeProtocolClient.kt @@ -722,30 +722,6 @@ class IdeProtocolClient ( return getMachineUniqueID() } - fun onTextSelected( - selectedText: String, - filepath: String, - startLine: Int, - startCharacter: Int, - endLine: Int, - endCharacter: Int - ) = coroutineScope.launch { -// val jsonMessage = textSelectionStrategy.handleTextSelection( -// selectedText, -// filepath, -// startLine, -// startCharacter, -// endLine, -// endCharacter -// ) -// sendMessage("highlightedCodePush", jsonMessage) -// dispatchEventToWebview( -// "highlightedCode", -// jsonMessage, -// continuePluginService.continuePluginWindow.webView -// ) - } - fun readFile(filepath: String): String { try { val content = ApplicationManager.getApplication().runReadAction { 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 1ec77b738..42205bb0b 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 @@ -4,13 +4,13 @@ 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.google.gson.Gson import com.intellij.openapi.Disposable import com.intellij.openapi.actionSystem.ActionUpdateThread 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.editor.Editor import com.intellij.openapi.editor.colors.EditorColorsManager import com.intellij.openapi.editor.colors.EditorFontType import com.intellij.openapi.editor.impl.EditorImpl @@ -32,8 +32,6 @@ import javax.swing.* import javax.swing.border.EmptyBorder import javax.swing.event.DocumentEvent import javax.swing.event.DocumentListener -import javax.swing.plaf.ComboBoxUI -import javax.swing.plaf.basic.BasicArrowButton import javax.swing.plaf.basic.BasicComboBoxUI import kotlin.math.max @@ -41,6 +39,193 @@ import kotlin.math.max const val SHADOW_SIZE = 7 const val MAIN_FONT_SIZE = 13 +fun makeTextArea(): JTextArea { + val textArea = CustomTextArea( 2, 40).apply { + lineWrap = true + wrapStyleWord = true + isOpaque = false + background = GetTheme().getSecondaryDark() + maximumSize = Dimension(400, Short.MAX_VALUE.toInt()) + margin = JBUI.insets(8) + font = UIUtil.getFontWithFallback("Arial", Font.PLAIN, MAIN_FONT_SIZE) + } + textArea.putClientProperty(UIUtil.HIDE_EDITOR_FROM_DATA_CONTEXT_PROPERTY, true) + + return textArea +} + +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 4 $leftInset 2 2, gap 0!")).apply { + val globalScheme = EditorColorsManager.getInstance().globalScheme + val defaultBackground = globalScheme.defaultBackground +// background = defaultBackground + background = JBColor(0x20888888.toInt(), 0x20888888.toInt()) + isOpaque = false + } + + 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 + add(textArea, "grow, gap 0!, height 100%") + + putClientProperty(UIUtil.HIDE_EDITOR_FROM_DATA_CONTEXT_PROPERTY, true) + preferredSize = textArea.preferredSize + isOpaque = false + setup() + + val shadow = DropShadowBorder() + shadow.shadowColor = JBColor(0xb0b0b0, 0x505050) + shadow.isShowRightShadow = true + shadow.isShowBottomShadow = true + shadow.shadowSize = SHADOW_SIZE + border = shadow + } + customPanelRef.set(panel) + + textArea.addComponentListener(object : ComponentAdapter() { + override fun componentResized(e: ComponentEvent?) { + panel.revalidate() + panel.repaint() + } + }) + + topPanel.add(panel, "grow, gap 0!") + + return topPanel +} + +fun openInlineEdit(project: Project?, editor: Editor) { + if (project == null) return + + val manager = EditorComponentInlaysManager.from(editor) + + // Get list of model titles + val continuePluginService = project.service() + val modelTitles = mutableListOf() + continuePluginService.coreMessenger?.request("config/getSerializedProfileInfo", null, null) { response -> + val config = response as Map + val models = (config["config"] as Map)["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 startLineNumber = editor.document.getLineNumber(selectionModel.selectionStart) + val endLineNumber = editor.document.getLineNumber(selectionModel.selectionEnd) + val start = editor.document.getLineStartOffset(startLineNumber) + val end = editor.document.getLineEndOffset(endLineNumber) + val prefix = editor.document.getText(TextRange(0, start)) + val highlighted = editor.document.getText(TextRange(start, end)) + val suffix = editor.document.getText(TextRange(end, editor.document.textLength)) + + val startLineNum = editor.document.getLineNumber(start) + val endLineNum = editor.document.getLineNumber(end) + val lineNumber = max(0, startLineNum - 1) + + // Un-highlight the selected text + selectionModel.removeSelection() + + // Get indentation width in pixels + val indentationLineNum = lineNumber + 1 + val lineStart = editor.document.getLineStartOffset(indentationLineNum) + val lineEnd = editor.document.getLineEndOffset(indentationLineNum) + val text = editor.document.getText(TextRange(lineStart, lineEnd)) + val indentation = text.takeWhile { it == ' ' }.length + val charWidth = editor.contentComponent.getFontMetrics(editor.colorsScheme.getFont(EditorFontType.PLAIN)).charWidth(' ') + val leftInset = indentation * charWidth * 2 / 3 + + val inlayRef = Ref() + val customPanelRef = Ref() + + // Create text area, attach key listener + val textArea = makeTextArea() + + // Create diff stream handler + val diffStreamHandler = DiffStreamHandler(project, editor, textArea, startLineNum, endLineNum, { + inlayRef.get().dispose() + }, { + customPanelRef.get().finish() + }) + 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, comboBoxRef.get().selectedItem as String) + } + + val panel = makePanel(project, customPanelRef, textArea, inlayRef, comboBoxRef, leftInset, modelTitles, {onEnter()}, { + diffStreamService.reject(editor) + selectionModel.setSelection(start, end) + }, { + diffStreamService.accept(editor) + inlayRef.get().dispose() + }, { + diffStreamService.reject(editor) + inlayRef.get().dispose() + }) + 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)) + + // Add key listener to text area + textArea.addKeyListener(object : KeyAdapter() { + override fun keyPressed(e: KeyEvent) { + if (e.keyCode == KeyEvent.VK_ESCAPE) { + diffStreamService.reject(editor) + + // Re-highlight the selected text + selectionModel.setSelection(start, end) + } else if (e.keyCode == KeyEvent.VK_ENTER) { + if (e.modifiersEx == KeyEvent.SHIFT_DOWN_MASK) { + textArea.document.insertString(textArea.caretPosition, "\n", null) + } else if (e.modifiersEx == 0) { + onEnter() + e.consume() + } + } + } + }) + + // Listen for changes to textarea line count + textArea.document.addDocumentListener(object : DocumentListener { + private var lastNumLines: Int = 0 + private fun updateSize() { + val numLines = textArea.text.lines().size + if (numLines != lastNumLines) { + lastNumLines = numLines + viewport?.dispatchEvent(ComponentEvent(viewport, ComponentEvent.COMPONENT_RESIZED)) + } + } + override fun insertUpdate(e: DocumentEvent?) { + updateSize() + } + + override fun removeUpdate(e: DocumentEvent?) { + updateSize() + } + + override fun changedUpdate(e: DocumentEvent?) { + updateSize() + } + }) + + + textArea.requestFocus() +} + /** * Adapted from https://github.com/cursive-ide/component-inlay-example/blob/master/src/main/kotlin/inlays/InlineEditAction.kt */ @@ -55,192 +240,9 @@ class InlineEditAction : AnAction(), DumbAware { } override fun actionPerformed(e: AnActionEvent) { - if (e.project == null) return - val editor = e.getData(PlatformDataKeys.EDITOR) ?: return 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/getSerializedProfileInfo", null, null) { response -> - val config = response as Map - val models = (config["config"] as Map)["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 startLineNumber = editor.document.getLineNumber(selectionModel.selectionStart) - val endLineNumber = editor.document.getLineNumber(selectionModel.selectionEnd) - val start = editor.document.getLineStartOffset(startLineNumber) - val end = editor.document.getLineEndOffset(endLineNumber) - val prefix = editor.document.getText(TextRange(0, start)) - val highlighted = editor.document.getText(TextRange(start, end)) - val suffix = editor.document.getText(TextRange(end, editor.document.textLength)) - - val startLineNum = editor.document.getLineNumber(start) - val endLineNum = editor.document.getLineNumber(end) - val lineNumber = max(0, startLineNum - 1) - - // Un-highlight the selected text - selectionModel.removeSelection() - - // Get indentation width in pixels - val indentationLineNum = lineNumber + 1 - val lineStart = editor.document.getLineStartOffset(indentationLineNum) - val lineEnd = editor.document.getLineEndOffset(indentationLineNum) - val text = editor.document.getText(TextRange(lineStart, lineEnd)) - val indentation = text.takeWhile { it == ' ' }.length - val charWidth = editor.contentComponent.getFontMetrics(editor.colorsScheme.getFont(EditorFontType.PLAIN)).charWidth(' ') - val leftInset = indentation * charWidth * 2 / 3 - - val inlayRef = Ref() - val customPanelRef = Ref() - - // Create text area, attach key listener - val textArea = makeTextArea() - - // Create diff stream handler - val diffStreamHandler = DiffStreamHandler(project, editor, textArea, startLineNum, endLineNum, { - inlayRef.get().dispose() - }, { - customPanelRef.get().finish() - }) - 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, comboBoxRef.get().selectedItem as String) - } - - val panel = makePanel(project, customPanelRef, textArea, inlayRef, comboBoxRef, leftInset, modelTitles, {onEnter()}, { - diffStreamService.reject(editor) - selectionModel.setSelection(start, end) - }, { - diffStreamService.accept(editor) - inlayRef.get().dispose() - }, { - diffStreamService.reject(editor) - inlayRef.get().dispose() - }) - 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)) - - // Add key listener to text area - textArea.addKeyListener(object : KeyAdapter() { - override fun keyPressed(e: KeyEvent) { - if (e.keyCode == KeyEvent.VK_ESCAPE) { - diffStreamService.reject(editor) - - // Re-highlight the selected text - selectionModel.setSelection(start, end) - } else if (e.keyCode == KeyEvent.VK_ENTER) { - if (e.modifiersEx == KeyEvent.SHIFT_DOWN_MASK) { - textArea.document.insertString(textArea.caretPosition, "\n", null) - } else if (e.modifiersEx == 0) { - onEnter() - e.consume() - } - } - } - }) - - // Listen for changes to textarea line count - textArea.document.addDocumentListener(object : DocumentListener { - private var lastNumLines: Int = 0 - private fun updateSize() { - val numLines = textArea.text.lines().size - if (numLines != lastNumLines) { - lastNumLines = numLines - viewport?.dispatchEvent(ComponentEvent(viewport, ComponentEvent.COMPONENT_RESIZED)) - } - } - override fun insertUpdate(e: DocumentEvent?) { - updateSize() - } - - override fun removeUpdate(e: DocumentEvent?) { - updateSize() - } - - override fun changedUpdate(e: DocumentEvent?) { - updateSize() - } - }) - - - textArea.requestFocus() - } - - fun makeTextArea(): JTextArea { - val textArea = CustomTextArea( 2, 40).apply { - lineWrap = true - wrapStyleWord = true - isOpaque = false - background = GetTheme().getSecondaryDark() - maximumSize = Dimension(400, Short.MAX_VALUE.toInt()) - margin = JBUI.insets(8) - font = UIUtil.getFontWithFallback("Arial", Font.PLAIN, MAIN_FONT_SIZE) - } - textArea.putClientProperty(UIUtil.HIDE_EDITOR_FROM_DATA_CONTEXT_PROPERTY, true) - - return textArea - } - - 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 4 $leftInset 2 2, gap 0!")).apply { - val globalScheme = EditorColorsManager.getInstance().globalScheme - val defaultBackground = globalScheme.defaultBackground -// background = defaultBackground - background = JBColor(0x20888888.toInt(), 0x20888888.toInt()) - isOpaque = false - } - - 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 - add(textArea, "grow, gap 0!, height 100%") - - putClientProperty(UIUtil.HIDE_EDITOR_FROM_DATA_CONTEXT_PROPERTY, true) - preferredSize = textArea.preferredSize - isOpaque = false - setup() - - val shadow = DropShadowBorder() - shadow.shadowColor = JBColor(0xb0b0b0, 0x505050) - shadow.isShowRightShadow = true - shadow.isShowBottomShadow = true - shadow.shadowSize = SHADOW_SIZE - border = shadow - } - customPanelRef.set(panel) - - textArea.addComponentListener(object : ComponentAdapter() { - override fun componentResized(e: ComponentEvent?) { - panel.revalidate() - panel.repaint() - } - }) - - topPanel.add(panel, "grow, gap 0!") - - return topPanel + openInlineEdit(project, editor) } } diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/ToolTipComponent.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/ToolTipComponent.kt new file mode 100644 index 000000000..7d5ba7c08 --- /dev/null +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/ToolTipComponent.kt @@ -0,0 +1,121 @@ +import com.github.continuedev.continueintellijextension.actions.focusContinueInput +import com.github.continuedev.continueintellijextension.editor.openInlineEdit +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.editor.LogicalPosition +import com.intellij.openapi.editor.colors.EditorColorsManager +import com.intellij.ui.components.JBPanel +import java.awt.* +import java.awt.event.ActionEvent +import java.util.* +import javax.swing.JButton +import java.awt.geom.RoundRectangle2D + +class StyledButton(text: String, foregroundColor: Color) : JButton(text) { + init { + cursor = Cursor(Cursor.HAND_CURSOR) + isOpaque = false + isContentAreaFilled = false + isFocusPainted = false + border = null // Remove the border + foreground = foregroundColor + } + + override fun paintComponent(g: Graphics) { + val g2 = g.create() as Graphics2D + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON) + + val arc = 24f // Corner radius + val padding = 0f + val x = padding + val y = padding + val w = (width - 2 * padding) + val h = (height - 2 * padding) + + // Draw flat background +// g2.color = if (model.isPressed) Color(70, 70, 70) else Color(100, 100, 100) +// g2.fill(RoundRectangle2D.Float(x, y, w, h, arc, arc)) + + // Draw border +// g2.color = Color(120, 120, 120) +// g2.draw(RoundRectangle2D.Float(x, y, w - 1, h - 1, arc, arc)) + + g2.dispose() + + super.paintComponent(g) + } +} + +class ToolTipComponent(editor: Editor, x: Int, y: Int) : + JBPanel() { + private var addToChatButton: StyledButton + private var editButton: StyledButton + private var backgroundColor: Color + private var foregroundColor: Color + + init { + layout = null // Remove the FlowLayout + + val globalScheme = EditorColorsManager.getInstance().globalScheme + backgroundColor = globalScheme.defaultBackground + foregroundColor = Color( + globalScheme.defaultForeground.red, + globalScheme.defaultForeground.green, + globalScheme.defaultForeground.blue, + 200 + ) + + val cmdCtrlChar = if (System.getProperty("os.name").lowercase(Locale.getDefault()).contains("mac")) "⌘" else "Ctrl" + addToChatButton = StyledButton("${cmdCtrlChar}J Chat", foregroundColor) + editButton = StyledButton("${cmdCtrlChar}I Edit", foregroundColor) + + addToChatButton.addActionListener { e: ActionEvent? -> + focusContinueInput(editor.project) + editor.contentComponent.remove(this) + } + editButton.addActionListener { e: ActionEvent? -> + openInlineEdit(editor.project, editor) + editor.contentComponent.remove(this) + } + + // Set bounds for buttons to remove spacing + addToChatButton.setBounds(0, 0, addToChatButton.preferredSize.width, addToChatButton.preferredSize.height) + editButton.setBounds(addToChatButton.preferredSize.width, 0, editButton.preferredSize.width, editButton.preferredSize.height) + + add(addToChatButton) + add(editButton) + + val totalWidth = addToChatButton.preferredSize.width + editButton.preferredSize.width + val totalHeight = addToChatButton.preferredSize.height + setBounds(x, y, totalWidth, totalHeight) + + // Make the background transparent + isOpaque = false + background = Color(0, 0, 0, 0) + } + + override fun paintComponent(g: Graphics) { + val arc = 12f + + val g2 = g.create() as Graphics2D + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON) + + // Draw rounded rectangle background + g2.color = backgroundColor + g2.fill(RoundRectangle2D.Float(0f, 0f, width.toFloat(), height.toFloat(), arc, arc)) + + // Draw rounded rectangle border + g2.color = Color(120, 120, 120) + val strokeWidth = 1.0f + g2.stroke = BasicStroke(1.0f) + g2.draw(RoundRectangle2D.Float(0f, 0f, width.toFloat() - strokeWidth, height.toFloat() - strokeWidth, arc, arc)) + + // Draw border between buttons + g2.color = Color(120, 120, 120) + g2.stroke = BasicStroke(1.0f) + val middleX = addToChatButton.width.toFloat() + g2.drawLine(middleX.toInt(), 0, middleX.toInt(), height) + + g2.dispose() + super.paintComponent(g) + } +} diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/listeners/ContinuePluginSelectionListener.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/listeners/ContinuePluginSelectionListener.kt index e2164b090..42839b25b 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/listeners/ContinuePluginSelectionListener.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/listeners/ContinuePluginSelectionListener.kt @@ -1,8 +1,12 @@ package com.github.continuedev.continueintellijextension.listeners +import ToolTipComponent import com.github.continuedev.continueintellijextension.`continue`.IdeProtocolClient +import com.github.continuedev.continueintellijextension.services.ContinueExtensionSettings import com.github.continuedev.continueintellijextension.utils.Debouncer import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.components.service +import com.intellij.openapi.editor.LogicalPosition import com.intellij.openapi.editor.SelectionModel import com.intellij.openapi.editor.event.SelectionEvent import com.intellij.openapi.editor.event.SelectionListener @@ -10,6 +14,8 @@ import com.intellij.openapi.fileEditor.FileDocumentManager import com.intellij.openapi.project.DumbAware import com.intellij.openapi.vfs.VirtualFile import kotlinx.coroutines.CoroutineScope +import kotlin.math.max +import kotlin.math.min class ContinuePluginSelectionListener( private val ideProtocolClient: IdeProtocolClient, @@ -20,11 +26,31 @@ class ContinuePluginSelectionListener( debouncer.debounce { handleSelection(e) } } + private var toolTipComponent: ToolTipComponent? = null + private fun handleSelection(e: SelectionEvent) { ApplicationManager.getApplication().runReadAction { val editor = e.editor val model: SelectionModel = editor.selectionModel - val selectedText = model.selectedText ?: return@runReadAction + val selectedText = model.selectedText + + // If selected text is empty, remove the tooltip + if (selectedText.isNullOrEmpty()) { + ApplicationManager.getApplication().invokeLater { + toolTipComponent?.let { editor.contentComponent.remove(it) } + toolTipComponent = null + editor.contentComponent.revalidate() + editor.contentComponent.repaint() + } + return@runReadAction + } + + // Allow user to disable editor tooltip + // Note that we still check for empty selected text before this + val extensionSettingsService = service() + if (extensionSettingsService.continueState.displayEditorTooltip == false) { + return@runReadAction + } val document = editor.document val startOffset = model.selectionStart @@ -39,14 +65,34 @@ class ContinuePluginSelectionListener( FileDocumentManager.getInstance().getFile(document) val filepath = virtualFile?.path ?: "Unknown path" - ideProtocolClient.onTextSelected( - selectedText, - filepath, - startLine, - startCharacter, - endLine, - endCharacter - ) + ApplicationManager.getApplication().invokeLater { + toolTipComponent?.let { editor.contentComponent.remove(it) } + + editor.contentComponent.layout = null + + val line = startLine - 2 + if (line > 0) { + // Get the text on line number "line" +// val text = document.getText(document.getLineStartOffset(line), document.getLineEndOffset(line)) + + val pos = LogicalPosition(line, selectedText.split("\n")[0].length + 1) + val y: Int = editor.logicalPositionToXY(pos).y + editor.lineHeight + var x: Int = editor.logicalPositionToXY(pos).x + + // Check if x is out of bounds + val maxEditorWidth = editor.contentComponent.width + val maxToolTipWidth = 600 + x = max(0, min(x, maxEditorWidth - maxToolTipWidth)) + + toolTipComponent = ToolTipComponent(editor, x, y) + editor.contentComponent.add(toolTipComponent) + } else { + toolTipComponent = null + } + + editor.contentComponent.revalidate() + editor.contentComponent.repaint() + } } } } 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 6ff8277a5..81bbbb6af 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 @@ -16,6 +16,7 @@ class ContinueSettingsComponent: DumbAware { val userToken: JTextField = JTextField() val enableTabAutocomplete: JCheckBox = JCheckBox("Enable Tab Autocomplete") val enableContinueTeamsBeta: JCheckBox = JCheckBox("Enable Continue for Teams Beta (requires restart)") + val displayEditorTooltip: JCheckBox = JCheckBox("Display Editor Tooltip") init { val constraints = GridBagConstraints() @@ -42,6 +43,8 @@ class ContinueSettingsComponent: DumbAware { constraints.gridy++ panel.add(enableContinueTeamsBeta, constraints) constraints.gridy++ + panel.add(displayEditorTooltip, constraints) + constraints.gridy++ // Add a "filler" component that takes up all remaining vertical space constraints.weighty = 1.0 @@ -65,6 +68,7 @@ open class ContinueExtensionSettings : PersistentStateComponent