extension.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import * as vscode from "vscode"
  2. import { ClineProvider } from "./core/webview/ClineProvider"
  3. import { createClineAPI } from "./exports"
  4. import "./utils/path" // Necessary to have access to String.prototype.toPosix.
  5. import { CodeActionProvider } from "./core/CodeActionProvider"
  6. import { DIFF_VIEW_URI_SCHEME } from "./integrations/editor/DiffViewProvider"
  7. import { handleUri, registerCommands, registerCodeActions, registerTerminalActions } from "./activate"
  8. import { McpServerManager } from "./services/mcp/McpServerManager"
  9. /**
  10. * Built using https://github.com/microsoft/vscode-webview-ui-toolkit
  11. *
  12. * Inspired by:
  13. * - https://github.com/microsoft/vscode-webview-ui-toolkit-samples/tree/main/default/weather-webview
  14. * - https://github.com/microsoft/vscode-webview-ui-toolkit-samples/tree/main/frameworks/hello-world-react-cra
  15. */
  16. let outputChannel: vscode.OutputChannel
  17. let extensionContext: vscode.ExtensionContext
  18. // This method is called when your extension is activated.
  19. // Your extension is activated the very first time the command is executed.
  20. export function activate(context: vscode.ExtensionContext) {
  21. extensionContext = context
  22. outputChannel = vscode.window.createOutputChannel("Roo-Code")
  23. context.subscriptions.push(outputChannel)
  24. outputChannel.appendLine("Roo-Code extension activated")
  25. // Get default commands from configuration.
  26. const defaultCommands = vscode.workspace.getConfiguration("roo-cline").get<string[]>("allowedCommands") || []
  27. // Initialize global state if not already set.
  28. if (!context.globalState.get("allowedCommands")) {
  29. context.globalState.update("allowedCommands", defaultCommands)
  30. }
  31. const sidebarProvider = new ClineProvider(context, outputChannel)
  32. context.subscriptions.push(
  33. vscode.window.registerWebviewViewProvider(ClineProvider.sideBarId, sidebarProvider, {
  34. webviewOptions: { retainContextWhenHidden: true },
  35. }),
  36. )
  37. registerCommands({ context, outputChannel, provider: sidebarProvider })
  38. /**
  39. * We use the text document content provider API to show the left side for diff
  40. * view by creating a virtual document for the original content. This makes it
  41. * readonly so users know to edit the right side if they want to keep their changes.
  42. *
  43. * This API allows you to create readonly documents in VSCode from arbitrary
  44. * sources, and works by claiming an uri-scheme for which your provider then
  45. * returns text contents. The scheme must be provided when registering a
  46. * provider and cannot change afterwards.
  47. *
  48. * Note how the provider doesn't create uris for virtual documents - its role
  49. * is to provide contents given such an uri. In return, content providers are
  50. * wired into the open document logic so that providers are always considered.
  51. *
  52. * https://code.visualstudio.com/api/extension-guides/virtual-documents
  53. */
  54. const diffContentProvider = new (class implements vscode.TextDocumentContentProvider {
  55. provideTextDocumentContent(uri: vscode.Uri): string {
  56. return Buffer.from(uri.query, "base64").toString("utf-8")
  57. }
  58. })()
  59. context.subscriptions.push(
  60. vscode.workspace.registerTextDocumentContentProvider(DIFF_VIEW_URI_SCHEME, diffContentProvider),
  61. )
  62. context.subscriptions.push(vscode.window.registerUriHandler({ handleUri }))
  63. // Register code actions provider.
  64. context.subscriptions.push(
  65. vscode.languages.registerCodeActionsProvider({ pattern: "**/*" }, new CodeActionProvider(), {
  66. providedCodeActionKinds: CodeActionProvider.providedCodeActionKinds,
  67. }),
  68. )
  69. registerCodeActions(context)
  70. registerTerminalActions(context)
  71. return createClineAPI(outputChannel, sidebarProvider)
  72. }
  73. // This method is called when your extension is deactivated
  74. export async function deactivate() {
  75. outputChannel.appendLine("Roo-Code extension deactivated")
  76. // Clean up MCP server manager
  77. await McpServerManager.cleanup(extensionContext)
  78. }