Merge pull request #6702 from SoLoHiC/feature/jetbrainPlugins/terminalContent
feat: Support @Terminal Context Provider and RunInTerminal Button for Jetbrains Plugin
This commit is contained in:
commit
10861f2cd5
6 changed files with 122 additions and 26 deletions
|
|
@ -8,12 +8,13 @@ import com.github.continuedev.continueintellijextension.editor.DiffStreamService
|
|||
import com.github.continuedev.continueintellijextension.editor.EditorUtils
|
||||
import com.github.continuedev.continueintellijextension.error.ContinueErrorService
|
||||
import com.github.continuedev.continueintellijextension.protocol.*
|
||||
import com.github.continuedev.continueintellijextension.services.*
|
||||
import com.github.continuedev.continueintellijextension.utils.*
|
||||
import com.github.continuedev.continueintellijextension.services.ContinueExtensionSettings
|
||||
import com.github.continuedev.continueintellijextension.services.ContinuePluginService
|
||||
import com.github.continuedev.continueintellijextension.utils.getMachineUniqueID
|
||||
import com.github.continuedev.continueintellijextension.utils.uuid
|
||||
import com.google.gson.Gson
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.openapi.command.WriteCommandAction
|
||||
import com.intellij.openapi.components.ServiceManager
|
||||
import com.intellij.openapi.components.service
|
||||
import com.intellij.openapi.editor.SelectionModel
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager
|
||||
|
|
@ -21,7 +22,10 @@ import com.intellij.openapi.project.DumbAware
|
|||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.vfs.VirtualFileManager
|
||||
import com.intellij.openapi.wm.ToolWindowManager
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.launch
|
||||
import java.awt.Toolkit
|
||||
import java.awt.datatransfer.StringSelection
|
||||
|
||||
|
|
@ -323,7 +327,11 @@ class IdeProtocolClient(
|
|||
}
|
||||
|
||||
"runCommand" -> {
|
||||
// Running commands not yet supported in JetBrains
|
||||
val params = Gson().fromJson(
|
||||
dataElement.toString(),
|
||||
RunCommandParams::class.java
|
||||
)
|
||||
ide.runCommand(params.command, params.options)
|
||||
respond(null)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
package com.github.continuedev.continueintellijextension.`continue`
|
||||
|
||||
import com.github.continuedev.continueintellijextension.*
|
||||
|
|
@ -27,12 +26,14 @@ import com.intellij.openapi.fileEditor.FileDocumentManager
|
|||
import com.intellij.openapi.fileEditor.FileEditorManager
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.project.guessProjectDir
|
||||
import com.intellij.openapi.util.IconLoader
|
||||
import com.intellij.openapi.vfs.LocalFileSystem
|
||||
import com.intellij.openapi.vfs.VirtualFileManager
|
||||
import com.intellij.openapi.wm.ToolWindowManager
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import com.intellij.testFramework.LightVirtualFile
|
||||
import kotlinx.coroutines.*
|
||||
import org.jetbrains.plugins.terminal.ShellTerminalWidget
|
||||
import org.jetbrains.plugins.terminal.TerminalView
|
||||
import java.awt.Toolkit
|
||||
import java.awt.datatransfer.DataFlavor
|
||||
import java.io.BufferedReader
|
||||
|
|
@ -54,7 +55,7 @@ class IntelliJIDE(
|
|||
init {
|
||||
try {
|
||||
val os = getOS()
|
||||
|
||||
|
||||
if (os == OS.LINUX || os == OS.MAC) {
|
||||
val file = File(ripgrep)
|
||||
if (!file.canExecute()) {
|
||||
|
|
@ -135,7 +136,33 @@ class IntelliJIDE(
|
|||
}
|
||||
|
||||
override suspend fun getTerminalContents(): String {
|
||||
return ""
|
||||
return withContext(Dispatchers.EDT) {
|
||||
try {
|
||||
val toolWindow = ToolWindowManager.getInstance(project).getToolWindow("Terminal")
|
||||
|
||||
val terminalView = TerminalView.getInstance(project)
|
||||
// Find the first terminal widget selected, whatever its state, running command or not.
|
||||
val widget = terminalView.getWidgets().filterIsInstance<ShellTerminalWidget>().firstOrNull {
|
||||
toolWindow?.getContentManager()?.getContent(it)?.isSelected ?: false
|
||||
}
|
||||
|
||||
if (widget != null) {
|
||||
val textBuffer = widget.terminalTextBuffer
|
||||
val stringBuilder = StringBuilder()
|
||||
// Iterate through all lines in the buffer (history + screen)
|
||||
for (i in 0 until textBuffer.historyLinesCount + textBuffer.screenLinesCount) {
|
||||
stringBuilder.append(textBuffer.getLine(i).text).append('\n')
|
||||
}
|
||||
stringBuilder.toString()
|
||||
} else {
|
||||
"" // Return empty if no terminal is available
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
println("Error getting terminal contents: ${e.message}")
|
||||
e.printStackTrace()
|
||||
"" // Return empty on error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getDebugLocals(threadIndex: Int): String {
|
||||
|
|
@ -220,8 +247,65 @@ class IntelliJIDE(
|
|||
}
|
||||
}
|
||||
|
||||
override suspend fun runCommand(command: String) {
|
||||
throw NotImplementedError("runCommand not implemented in IntelliJ")
|
||||
override suspend fun runCommand(command: String, options: TerminalOptions?) {
|
||||
val terminalOptions =
|
||||
options ?: TerminalOptions(reuseTerminal = true, terminalName = null, waitForCompletion = false)
|
||||
|
||||
ApplicationManager.getApplication().invokeLater {
|
||||
try {
|
||||
val toolWindow = ToolWindowManager.getInstance(project).getToolWindow("Terminal")
|
||||
toolWindow?.activate({
|
||||
try {
|
||||
val terminalView = TerminalView.getInstance(project)
|
||||
var widget: ShellTerminalWidget? = null
|
||||
|
||||
// 1. Handle reuseTerminal option
|
||||
if (terminalOptions.reuseTerminal == true && terminalView.getWidgets().isNotEmpty()) {
|
||||
// 2. Find by terminalName if provided
|
||||
if (terminalOptions.terminalName != null) {
|
||||
widget = terminalView.getWidgets().filterIsInstance<ShellTerminalWidget>()
|
||||
.firstOrNull {
|
||||
toolWindow.contentManager.getContent(it).tabName == terminalOptions.terminalName
|
||||
&& !it.hasRunningCommands()
|
||||
}
|
||||
} else {
|
||||
// 3. Find active terminal, or fall back to the first one
|
||||
widget = terminalView.getWidgets().filterIsInstance<ShellTerminalWidget>()
|
||||
.firstOrNull { toolWindow.contentManager.getContent(it).isSelected }
|
||||
?: terminalView.getWidgets().filterIsInstance<ShellTerminalWidget>().firstOrNull {
|
||||
!it.hasRunningCommands()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Create a new terminal if needed
|
||||
if (widget == null) {
|
||||
widget = terminalView.createLocalShellWidget(
|
||||
project.basePath,
|
||||
terminalOptions.terminalName,
|
||||
true
|
||||
)
|
||||
} else {
|
||||
// Ensure the found widget is visible
|
||||
val content = toolWindow.contentManager.getContent(widget)
|
||||
if (content != null) {
|
||||
toolWindow.contentManager.setSelectedContent(content, true)
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Show and send text
|
||||
widget.ttyConnector?.write(command)
|
||||
|
||||
} catch (e: Exception) {
|
||||
println("Error during terminal widget handling: ${e.message}")
|
||||
e.printStackTrace()
|
||||
}
|
||||
}, true)
|
||||
} catch (e: Exception) {
|
||||
println("Error activating terminal tool window: ${e.message}")
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun saveFile(filepath: String) {
|
||||
|
|
@ -343,7 +427,7 @@ class IntelliJIDE(
|
|||
}
|
||||
|
||||
val command = GeneralCommandLine(commandArgs)
|
||||
|
||||
|
||||
command.setWorkDirectory(project.basePath)
|
||||
val results = ExecUtil.execAndGetOutput(command).stdout
|
||||
return results.split("\n")
|
||||
|
|
@ -357,11 +441,12 @@ class IntelliJIDE(
|
|||
throw NotImplementedError("Ripgrep not supported, this workspace is remote")
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getSearchResults(query: String, maxResults: Int?): String {
|
||||
val ideInfo = this.getIdeInfo()
|
||||
if (ideInfo.remoteName == "local") {
|
||||
try {
|
||||
val commandArgs = mutableListOf(
|
||||
val commandArgs = mutableListOf(
|
||||
ripgrep,
|
||||
"-i",
|
||||
"--ignore-file",
|
||||
|
|
@ -372,20 +457,20 @@ class IntelliJIDE(
|
|||
"2",
|
||||
"--heading"
|
||||
)
|
||||
|
||||
|
||||
// Conditionally add maxResults flag
|
||||
if (maxResults != null) {
|
||||
commandArgs.add("-m")
|
||||
commandArgs.add(maxResults.toString())
|
||||
}
|
||||
|
||||
|
||||
// Add the search query and path
|
||||
commandArgs.add("-e")
|
||||
commandArgs.add(query)
|
||||
commandArgs.add(".")
|
||||
|
||||
val command = GeneralCommandLine(commandArgs)
|
||||
|
||||
|
||||
command.setWorkDirectory(project.basePath)
|
||||
return ExecUtil.execAndGetOutput(command).stdout
|
||||
} catch (exception: Exception) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.github.continuedev.continueintellijextension.protocol
|
||||
|
||||
import com.github.continuedev.continueintellijextension.*
|
||||
import com.github.continuedev.continueintellijextension.Range
|
||||
import com.github.continuedev.continueintellijextension.TerminalOptions
|
||||
|
||||
data class GetControlPlaneSessionInfoParams(val silent: Boolean, val useOnboarding: Boolean)
|
||||
|
||||
|
|
@ -59,4 +60,6 @@ data class GetGitRootPathParams(val dir: String)
|
|||
|
||||
data class ListDirParams(val dir: String)
|
||||
|
||||
data class GetFileStatsParams(val files: List<String>)
|
||||
data class GetFileStatsParams(val files: List<String>)
|
||||
|
||||
data class RunCommandParams(val command: String, val options: TerminalOptions?)
|
||||
|
|
|
|||
|
|
@ -153,6 +153,11 @@ data class ContinueRcJson(
|
|||
val mergeBehavior: ConfigMergeType
|
||||
)
|
||||
|
||||
data class TerminalOptions(
|
||||
val reuseTerminal: Boolean?,
|
||||
val terminalName: String?,
|
||||
val waitForCompletion: Boolean?
|
||||
)
|
||||
|
||||
interface IDE {
|
||||
suspend fun getIdeInfo(): IdeInfo
|
||||
|
|
@ -196,7 +201,7 @@ interface IDE {
|
|||
|
||||
suspend fun openUrl(url: String)
|
||||
|
||||
suspend fun runCommand(command: String)
|
||||
suspend fun runCommand(command: String, options: TerminalOptions?)
|
||||
|
||||
suspend fun saveFile(filepath: String)
|
||||
|
||||
|
|
@ -317,4 +322,4 @@ data class ShowFilePayload(
|
|||
sealed class FimResult {
|
||||
data class FimEdit(val fimText: String) : FimResult()
|
||||
object NotFimEdit : FimResult()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
<![CDATA[View the latest release notes on <a href="https://github.com/continuedev/continue/releases">GitHub</a>]]></change-notes>
|
||||
|
||||
<depends>com.intellij.modules.platform</depends>
|
||||
<depends>org.jetbrains.plugins.terminal</depends>
|
||||
|
||||
<!-- See here for why this is optional: https://github.com/continuedev/continue/issues/2775#issuecomment-2535620877-->
|
||||
<depends optional="true" config-file="continueintellijextension-withJSON.xml">
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ import { CommandLineIcon } from "@heroicons/react/24/outline";
|
|||
import { useContext } from "react";
|
||||
import { lightGray, vscForeground } from "../..";
|
||||
import { IdeMessengerContext } from "../../../context/IdeMessenger";
|
||||
import { isJetBrains } from "../../../util";
|
||||
import { extractCommand } from "../utils/commandExtractor";
|
||||
|
||||
interface RunInTerminalButtonProps {
|
||||
|
|
@ -12,11 +11,6 @@ interface RunInTerminalButtonProps {
|
|||
export function RunInTerminalButton({ command }: RunInTerminalButtonProps) {
|
||||
const ideMessenger = useContext(IdeMessengerContext);
|
||||
|
||||
if (isJetBrains()) {
|
||||
// JetBrains plugin doesn't currently have a way to run the command in the terminal for the user
|
||||
return null;
|
||||
}
|
||||
|
||||
function runInTerminal() {
|
||||
// Extract just the command line
|
||||
const extractedCommand = extractCommand(command);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue