Add fix, explain, and improve commands to the ProtoBus (#5574)
* Add fix, explain, and improve commands to Cline - Rename AddToClineRequest to CommandContext for reusability - Implement fixWithCline, explainWithCline, and improveWithCline commands - Extract common command utilities to commandUtils.ts - Move file mention logic to mentions module - Update command registration and handlers in extension.ts * Update src/core/controller/commands/addToCline.ts Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> * Update src/extension.ts Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> --------- Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
fbc517c9e5
commit
b44d3c8793
9 files changed
+207
-214
No files matched your search
@@ -1,14 +1,33 @@
|
||||
import { Controller } from "../index"
|
||||
import { AddToClineRequest, Empty } from "@/shared/proto/index.cline"
|
||||
import { CommandContext, Empty } from "@/shared/proto/index.cline"
|
||||
import { telemetryService } from "@/services/posthog/PostHogClientProvider"
|
||||
import { getFileMentionFromPath } from "@/core/mentions"
|
||||
import { singleFileDiagnosticsToProblemsString } from "@/integrations/diagnostics"
|
||||
import { WebviewProvider } from "@/core/webview"
|
||||
import { sendAddToInputEventToClient } from "../ui/subscribeToAddToInput"
|
||||
|
||||
export async function addToCline(controller: Controller, request: AddToClineRequest): Promise<Empty> {
|
||||
await controller.addSelectedCodeToChat(
|
||||
request.selectedText || "",
|
||||
request.filePath || "",
|
||||
request.language || "",
|
||||
request.diagnostics,
|
||||
)
|
||||
// 'Add to Cline' context menu in editor and code action
|
||||
// Inserts the selected code into the chat.
|
||||
export async function addToCline(controller: Controller, request: CommandContext): Promise<Empty> {
|
||||
if (!request.selectedText) {
|
||||
return {}
|
||||
}
|
||||
|
||||
const filePath = request.filePath || ""
|
||||
const fileMention = await getFileMentionFromPath(filePath)
|
||||
|
||||
let input = `${fileMention}\n\`\`\`\n${request.selectedText}\n\`\`\``
|
||||
if (request.diagnostics.length) {
|
||||
const problemsString = await singleFileDiagnosticsToProblemsString(filePath, request.diagnostics)
|
||||
input += `\nProblems:\n${problemsString}`
|
||||
}
|
||||
|
||||
const lastActiveWebview = WebviewProvider.getLastActiveInstance()
|
||||
if (lastActiveWebview) {
|
||||
await sendAddToInputEventToClient(lastActiveWebview.getClientId(), input)
|
||||
}
|
||||
|
||||
console.log("addToCline", request.selectedText, filePath, request.language)
|
||||
telemetryService.captureButtonClick("codeAction_addToChat", controller.task?.ulid)
|
||||
|
||||
return {}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Controller } from "../index"
|
||||
import { CommandContext, Empty } from "@/shared/proto/index.cline"
|
||||
import { telemetryService } from "@/services/posthog/PostHogClientProvider"
|
||||
import { HostProvider } from "@/hosts/host-provider"
|
||||
import { ShowMessageType } from "@/shared/proto/index.host"
|
||||
import { getFileMentionFromPath } from "@/core/mentions"
|
||||
|
||||
export async function explainWithCline(controller: Controller, request: CommandContext): Promise<Empty> {
|
||||
if (!request.selectedText || !request.selectedText.trim()) {
|
||||
HostProvider.window.showMessage({
|
||||
type: ShowMessageType.INFORMATION,
|
||||
message: "Please select some code to explain.",
|
||||
})
|
||||
return {}
|
||||
}
|
||||
const fileMention = await getFileMentionFromPath(request.filePath || "")
|
||||
const prompt = `Explain the following code from ${fileMention}:
|
||||
\`\`\`${request.language}\n${request.selectedText}\n\`\`\``
|
||||
await controller.initTask(prompt)
|
||||
telemetryService.captureButtonClick("codeAction_explainCode", controller.task?.ulid)
|
||||
|
||||
return {}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { Controller } from "../index"
|
||||
import { CommandContext, Empty } from "@/shared/proto/index.cline"
|
||||
import { telemetryService } from "@/services/posthog/PostHogClientProvider"
|
||||
import { getFileMentionFromPath } from "@/core/mentions"
|
||||
import { singleFileDiagnosticsToProblemsString } from "@/integrations/diagnostics"
|
||||
|
||||
export async function fixWithCline(controller: Controller, request: CommandContext): Promise<Empty> {
|
||||
const filePath = request.filePath || ""
|
||||
const fileMention = await getFileMentionFromPath(filePath)
|
||||
const problemsString = await singleFileDiagnosticsToProblemsString(filePath, request.diagnostics)
|
||||
|
||||
await controller.initTask(
|
||||
`Fix the following code in ${fileMention}
|
||||
\`\`\`\n${request.selectedText}\n\`\`\`\n\nProblems:\n${problemsString}`,
|
||||
)
|
||||
console.log("fixWithCline", request.selectedText, request.filePath, request.language, problemsString)
|
||||
|
||||
telemetryService.captureButtonClick("codeAction_fixWithCline", controller.task?.ulid)
|
||||
return {}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { Controller } from "../index"
|
||||
import { CommandContext, Empty } from "@/shared/proto/index.cline"
|
||||
import { telemetryService } from "@/services/posthog/PostHogClientProvider"
|
||||
import { HostProvider } from "@/hosts/host-provider"
|
||||
import { ShowMessageType } from "@/shared/proto/index.host"
|
||||
import { getFileMentionFromPath } from "@/core/mentions"
|
||||
|
||||
export async function improveWithCline(controller: Controller, request: CommandContext): Promise<Empty> {
|
||||
if (!request.selectedText || !request.selectedText.trim()) {
|
||||
HostProvider.window.showMessage({
|
||||
type: ShowMessageType.INFORMATION,
|
||||
message: "Please select some code to improve.",
|
||||
})
|
||||
return {}
|
||||
}
|
||||
const fileMention = await getFileMentionFromPath(request.filePath || "")
|
||||
const prompt = `Improve the following code from ${fileMention} (e.g., suggest refactorings, optimizations, or better practices):
|
||||
\`\`\`${request.language}\n${request.selectedText}\n\`\`\``
|
||||
|
||||
await controller.initTask(prompt)
|
||||
|
||||
telemetryService.captureButtonClick("codeAction_improveCode", controller.task?.ulid)
|
||||
|
||||
return {}
|
||||
}
|
||||
@@ -22,7 +22,6 @@ import { UserInfo } from "@shared/UserInfo"
|
||||
import { fileExistsAtPath } from "@utils/fs"
|
||||
import axios from "axios"
|
||||
import fs from "fs/promises"
|
||||
import { setTimeout as setTimeoutPromise } from "node:timers/promises"
|
||||
import pWaitFor from "p-wait-for"
|
||||
import * as path from "path"
|
||||
import * as vscode from "vscode"
|
||||
@@ -31,10 +30,6 @@ import { ensureMcpServersDirectoryExists, ensureSettingsDirectoryExists, GlobalF
|
||||
import { Task } from "../task"
|
||||
import { sendMcpMarketplaceCatalogEvent } from "./mcp/subscribeToMcpMarketplaceCatalog"
|
||||
import { sendStateUpdate } from "./state/subscribeToState"
|
||||
import { sendAddToInputEvent, sendAddToInputEventToClient } from "./ui/subscribeToAddToInput"
|
||||
import { WebviewProvider } from "../webview"
|
||||
import { Diagnostic, FileDiagnostics } from "@/shared/proto/index.cline"
|
||||
import { diagnosticsToProblemsString, singleFileDiagnosticsToProblemsString } from "@/integrations/diagnostics"
|
||||
|
||||
/*
|
||||
https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts
|
||||
@@ -514,87 +509,6 @@ export class Controller {
|
||||
return undefined
|
||||
}
|
||||
|
||||
// Context menus and code actions
|
||||
|
||||
async getFileMentionFromPath(filePath: string) {
|
||||
const cwd = await getCwd()
|
||||
if (!cwd) {
|
||||
return "@/" + filePath
|
||||
}
|
||||
const relativePath = path.relative(cwd, filePath)
|
||||
return "@/" + relativePath
|
||||
}
|
||||
|
||||
// 'Add to Cline' context menu in editor and code action
|
||||
async addSelectedCodeToChat(code: string, filePath: string, languageId: string, diagnostics: Diagnostic[]) {
|
||||
// Post message to webview with the selected code
|
||||
const fileMention = await this.getFileMentionFromPath(filePath)
|
||||
|
||||
let input = `${fileMention}\n\`\`\`\n${code}\n\`\`\``
|
||||
if (diagnostics.length) {
|
||||
const problemsString = singleFileDiagnosticsToProblemsString(filePath, diagnostics)
|
||||
input += `\nProblems:\n${problemsString}`
|
||||
}
|
||||
|
||||
const lastActiveWebview = WebviewProvider.getLastActiveInstance()
|
||||
if (lastActiveWebview) {
|
||||
await sendAddToInputEventToClient(lastActiveWebview.getClientId(), input)
|
||||
}
|
||||
|
||||
console.log("addSelectedCodeToChat", code, filePath, languageId)
|
||||
}
|
||||
|
||||
// 'Add to Cline' context menu in Terminal
|
||||
async addSelectedTerminalOutputToChat(output: string, terminalName: string) {
|
||||
// Ensure the sidebar view is visible
|
||||
await vscode.commands.executeCommand("claude-dev.SidebarProvider.focus")
|
||||
await setTimeoutPromise(100)
|
||||
await sendAddToInputEvent(`Terminal output:\n\`\`\`\n${output}\n\`\`\``)
|
||||
|
||||
console.log("addSelectedTerminalOutputToChat", output, terminalName)
|
||||
}
|
||||
|
||||
// 'Fix with Cline' in code actions
|
||||
async fixWithCline(code: string, filePath: string, languageId: string, diagnostics: vscode.Diagnostic[]) {
|
||||
// Ensure the sidebar view is visible
|
||||
await vscode.commands.executeCommand("claude-dev.SidebarProvider.focus")
|
||||
await setTimeoutPromise(100)
|
||||
|
||||
const fileMention = await this.getFileMentionFromPath(filePath)
|
||||
const problemsString = this.convertDiagnosticsToProblemsString(diagnostics)
|
||||
await this.initTask(`Fix the following code in ${fileMention}\n\`\`\`\n${code}\n\`\`\`\n\nProblems:\n${problemsString}`)
|
||||
|
||||
console.log("fixWithCline", code, filePath, languageId, diagnostics, problemsString)
|
||||
}
|
||||
|
||||
convertDiagnosticsToProblemsString(diagnostics: vscode.Diagnostic[]) {
|
||||
let problemsString = ""
|
||||
for (const diagnostic of diagnostics) {
|
||||
let label: string
|
||||
switch (diagnostic.severity) {
|
||||
case vscode.DiagnosticSeverity.Error:
|
||||
label = "Error"
|
||||
break
|
||||
case vscode.DiagnosticSeverity.Warning:
|
||||
label = "Warning"
|
||||
break
|
||||
case vscode.DiagnosticSeverity.Information:
|
||||
label = "Information"
|
||||
break
|
||||
case vscode.DiagnosticSeverity.Hint:
|
||||
label = "Hint"
|
||||
break
|
||||
default:
|
||||
label = "Diagnostic"
|
||||
}
|
||||
const line = diagnostic.range.start.line + 1 // VSCode lines are 0-indexed
|
||||
const source = diagnostic.source ? `${diagnostic.source} ` : ""
|
||||
problemsString += `\n- [${source}${label}] Line ${line}: ${diagnostic.message}`
|
||||
}
|
||||
problemsString = problemsString.trim()
|
||||
return problemsString
|
||||
}
|
||||
|
||||
// Task history
|
||||
|
||||
async getTaskWithId(id: string): Promise<{
|
||||
|
||||
Reference in new issue
Block a user