extension.ts 3.5 KB

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