| 1 | import { ContinueConfig, QuickActionConfig } from "core"; |
| 2 | import { Telemetry } from "core/util/posthog"; |
| 3 | import * as vscode from "vscode"; |
| 4 | |
| 5 | import { QuickEditShowParams } from "../../../quickEdit/QuickEditQuickPick"; |
| 6 | import { isTutorialFile } from "../../../util/tutorial"; |
| 7 | import { |
| 8 | CONTINUE_WORKSPACE_KEY, |
| 9 | getContinueWorkspaceConfig, |
| 10 | } from "../../../util/workspaceConfig"; |
| 11 | |
| 12 | export const ENABLE_QUICK_ACTIONS_KEY = "enableQuickActions"; |
| 13 | |
| 14 | export function getQuickActionsConfig(config: ContinueConfig) { |
| 15 | return config.experimental?.quickActions; |
| 16 | } |
| 17 | |
| 18 | export function subscribeToVSCodeQuickActionsSettings(listener: Function) { |
| 19 | vscode.workspace.onDidChangeConfiguration((e) => { |
| 20 | const configKey = `${CONTINUE_WORKSPACE_KEY}.${ENABLE_QUICK_ACTIONS_KEY}`; |
| 21 | |
| 22 | if (e.affectsConfiguration(configKey)) { |
| 23 | Telemetry.capture("VSCode Quick Actions Settings Changed", { |
| 24 | enabled: quickActionsEnabledStatus(), |
| 25 | }); |
| 26 | |
| 27 | listener(); |
| 28 | } |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | export function toggleQuickActions() { |
| 33 | const curStatus = quickActionsEnabledStatus(); |
| 34 | |
| 35 | getContinueWorkspaceConfig().update(ENABLE_QUICK_ACTIONS_KEY, curStatus); |
| 36 | } |
| 37 | |
| 38 | export function quickActionsEnabledStatus() { |
| 39 | return getContinueWorkspaceConfig().get<boolean>(ENABLE_QUICK_ACTIONS_KEY); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * A CodeLensProvider for Quick Actions. |
| 44 | * |
| 45 | * This class provides code lenses for Quick Actions, which can be either custom or default actions. |
| 46 | * It supports actions for functions and classes, and can be configured with custom quick action settings. |
| 47 | */ |
| 48 | export class QuickActionsCodeLensProvider implements vscode.CodeLensProvider { |
| 49 | /** |
| 50 | * Defines which code elements are eligible for Quick Actions. |
| 51 | * |
| 52 | * Right now, we only allow functions, methods, constructors |
| 53 | * and classes to keep things simple. |
| 54 | */ |
| 55 | quickActionSymbolKinds = [ |
| 56 | vscode.SymbolKind.Function, |
| 57 | vscode.SymbolKind.Method, |
| 58 | vscode.SymbolKind.Class, |
| 59 | vscode.SymbolKind.Constructor, |
| 60 | ]; |
| 61 | |
| 62 | constructor(private customQuickActionsConfigs?: QuickActionConfig[]) {} |
| 63 | |
| 64 | getCustomCommands( |
| 65 | range: vscode.Range, |
| 66 | quickActionConfigs: QuickActionConfig[], |
| 67 | ): vscode.Command[] { |
| 68 | return quickActionConfigs.map(({ title, prompt, sendToChat }) => { |
| 69 | return sendToChat |
| 70 | ? { |
| 71 | title, |
| 72 | command: "continue.customQuickActionSendToChat", |
| 73 | arguments: [prompt, range], |
| 74 | } |
| 75 | : { |
| 76 | title, |
| 77 | command: "continue.customQuickActionStreamInlineEdit", |
| 78 | arguments: [prompt, range], |
| 79 | }; |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | getDefaultCommand(range: vscode.Range): vscode.Command[] { |
| 84 | const quickEdit: vscode.Command = { |
| 85 | command: "continue.defaultQuickAction", |
| 86 | title: "Continue", |
| 87 | arguments: [{ range } as QuickEditShowParams], |
| 88 | }; |
| 89 | |
| 90 | return [quickEdit]; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get all top-level symbols and their immediate children. |
| 95 | * We do not recurse through all children to avoid noise. |
| 96 | */ |
| 97 | async getTopLevelAndChildrenSymbols(uri: vscode.Uri) { |
| 98 | const topLevelSymbols = await vscode.commands.executeCommand< |
| 99 | Array<vscode.DocumentSymbol> | undefined |
| 100 | >("vscode.executeDocumentSymbolProvider", uri); |
| 101 | |
| 102 | if (!topLevelSymbols) { |
| 103 | return []; |
| 104 | } |
| 105 | |
| 106 | const childrenSymbols = topLevelSymbols.flatMap( |
| 107 | (symbol) => symbol.children, |
| 108 | ); |
| 109 | |
| 110 | const symbols = [...topLevelSymbols, ...childrenSymbols]; |
| 111 | |
| 112 | const filteredSmybols = symbols?.filter( |
| 113 | (symbol) => |
| 114 | this.quickActionSymbolKinds.includes(symbol.kind) && |
| 115 | !symbol.range.isSingleLine, |
| 116 | ); |
| 117 | |
| 118 | return filteredSmybols; |
| 119 | } |
| 120 | |
| 121 | async provideCodeLenses( |
| 122 | document: vscode.TextDocument, |
| 123 | ): Promise<vscode.CodeLens[]> { |
| 124 | // The tutorial file already has a lot of Code Lenses |
| 125 | // so we don't want to add more to it. |
| 126 | if (isTutorialFile(document.uri)) { |
| 127 | return []; |
| 128 | } |
| 129 | |
| 130 | const editor = vscode.window.activeTextEditor; |
| 131 | |
| 132 | if (!editor) { |
| 133 | return []; |
| 134 | } |
| 135 | |
| 136 | const symbols = await this.getTopLevelAndChildrenSymbols(document.uri); |
| 137 | |
| 138 | return symbols.flatMap(({ range }) => { |
| 139 | const commands: vscode.Command[] = !!this.customQuickActionsConfigs |
| 140 | ? this.getCustomCommands(range, this.customQuickActionsConfigs) |
| 141 | : this.getDefaultCommand(range); |
| 142 | |
| 143 | return commands.map((command) => new vscode.CodeLens(range, command)); |
| 144 | }); |
| 145 | } |
| 146 | } |