| 1 | import fs from "fs"; |
| 2 | |
| 3 | import { IContextProvider } from "core"; |
| 4 | import { ConfigHandler } from "core/config/ConfigHandler"; |
| 5 | import { EXTENSION_NAME, getControlPlaneEnv } from "core/control-plane/env"; |
| 6 | import { Core } from "core/core"; |
| 7 | import { FromCoreProtocol, ToCoreProtocol } from "core/protocol"; |
| 8 | import { InProcessMessenger } from "core/protocol/messenger"; |
| 9 | import { |
| 10 | getConfigJsonPath, |
| 11 | getConfigTsPath, |
| 12 | getConfigYamlPath, |
| 13 | } from "core/util/paths"; |
| 14 | import { v4 as uuidv4 } from "uuid"; |
| 15 | import * as vscode from "vscode"; |
| 16 | |
| 17 | import { ContinueCompletionProvider } from "../autocomplete/completionProvider"; |
| 18 | import { |
| 19 | monitorBatteryChanges, |
| 20 | setupStatusBar, |
| 21 | StatusBarStatus, |
| 22 | } from "../autocomplete/statusBar"; |
| 23 | import { registerAllCommands } from "../commands"; |
| 24 | import { ContinueConsoleWebviewViewProvider } from "../ContinueConsoleWebviewViewProvider"; |
| 25 | import { ContinueGUIWebviewViewProvider } from "../ContinueGUIWebviewViewProvider"; |
| 26 | import { VerticalDiffManager } from "../diff/vertical/manager"; |
| 27 | import { registerAllCodeLensProviders } from "../lang-server/codeLens"; |
| 28 | import { registerAllPromptFilesCompletionProviders } from "../lang-server/promptFileCompletions"; |
| 29 | import EditDecorationManager from "../quickEdit/EditDecorationManager"; |
| 30 | import { QuickEdit } from "../quickEdit/QuickEditQuickPick"; |
| 31 | import { setupRemoteConfigSync } from "../stubs/activation"; |
| 32 | import { UriEventHandler } from "../stubs/uriHandler"; |
| 33 | import { |
| 34 | getControlPlaneSessionInfo, |
| 35 | WorkOsAuthProvider, |
| 36 | } from "../stubs/WorkOsAuthProvider"; |
| 37 | import { Battery } from "../util/battery"; |
| 38 | import { FileSearch } from "../util/FileSearch"; |
| 39 | import { VsCodeIdeUtils } from "../util/ideUtils"; |
| 40 | import { VsCodeIde } from "../VsCodeIde"; |
| 41 | |
| 42 | import { ConfigYamlDocumentLinkProvider } from "./ConfigYamlDocumentLinkProvider"; |
| 43 | import { VsCodeMessenger } from "./VsCodeMessenger"; |
| 44 | |
| 45 | import setupNextEditWindowManager, { |
| 46 | NextEditWindowManager, |
| 47 | } from "../activation/NextEditWindowManager"; |
| 48 | import { getDefinitionsFromLsp } from "../autocomplete/lsp"; |
| 49 | import { handleTextDocumentChange } from "../util/editLoggingUtils"; |
| 50 | import type { VsCodeWebviewProtocol } from "../webviewProtocol"; |
| 51 | |
| 52 | export class VsCodeExtension { |
| 53 | // Currently some of these are public so they can be used in testing (test/test-suites) |
| 54 | |
| 55 | private configHandler: ConfigHandler; |
| 56 | private extensionContext: vscode.ExtensionContext; |
| 57 | private ide: VsCodeIde; |
| 58 | private ideUtils: VsCodeIdeUtils; |
| 59 | private consoleView: ContinueConsoleWebviewViewProvider; |
| 60 | private sidebar: ContinueGUIWebviewViewProvider; |
| 61 | private windowId: string; |
| 62 | private editDecorationManager: EditDecorationManager; |
| 63 | private verticalDiffManager: VerticalDiffManager; |
| 64 | webviewProtocolPromise: Promise<VsCodeWebviewProtocol>; |
| 65 | private core: Core; |
| 66 | private battery: Battery; |
| 67 | private workOsAuthProvider: WorkOsAuthProvider; |
| 68 | private fileSearch: FileSearch; |
| 69 | private uriHandler = new UriEventHandler(); |
| 70 | private completionProvider: ContinueCompletionProvider; |
| 71 | |
| 72 | constructor(context: vscode.ExtensionContext) { |
| 73 | // Register auth provider |
| 74 | this.workOsAuthProvider = new WorkOsAuthProvider(context, this.uriHandler); |
| 75 | void this.workOsAuthProvider.refreshSessions(); |
| 76 | context.subscriptions.push(this.workOsAuthProvider); |
| 77 | |
| 78 | this.editDecorationManager = new EditDecorationManager(context); |
| 79 | |
| 80 | let resolveWebviewProtocol: any = undefined; |
| 81 | this.webviewProtocolPromise = new Promise<VsCodeWebviewProtocol>( |
| 82 | (resolve) => { |
| 83 | resolveWebviewProtocol = resolve; |
| 84 | }, |
| 85 | ); |
| 86 | this.ide = new VsCodeIde(this.webviewProtocolPromise, context); |
| 87 | this.ideUtils = new VsCodeIdeUtils(); |
| 88 | this.extensionContext = context; |
| 89 | this.windowId = uuidv4(); |
| 90 | |
| 91 | // Dependencies of core |
| 92 | let resolveVerticalDiffManager: any = undefined; |
| 93 | const verticalDiffManagerPromise = new Promise<VerticalDiffManager>( |
| 94 | (resolve) => { |
| 95 | resolveVerticalDiffManager = resolve; |
| 96 | }, |
| 97 | ); |
| 98 | let resolveConfigHandler: any = undefined; |
| 99 | const configHandlerPromise = new Promise<ConfigHandler>((resolve) => { |
| 100 | resolveConfigHandler = resolve; |
| 101 | }); |
| 102 | this.sidebar = new ContinueGUIWebviewViewProvider( |
| 103 | this.windowId, |
| 104 | this.extensionContext, |
| 105 | ); |
| 106 | |
| 107 | // Sidebar |
| 108 | context.subscriptions.push( |
| 109 | vscode.window.registerWebviewViewProvider( |
| 110 | "continue.continueGUIView", |
| 111 | this.sidebar, |
| 112 | { |
| 113 | webviewOptions: { retainContextWhenHidden: true }, |
| 114 | }, |
| 115 | ), |
| 116 | ); |
| 117 | resolveWebviewProtocol(this.sidebar.webviewProtocol); |
| 118 | |
| 119 | const inProcessMessenger = new InProcessMessenger< |
| 120 | ToCoreProtocol, |
| 121 | FromCoreProtocol |
| 122 | >(); |
| 123 | |
| 124 | new VsCodeMessenger( |
| 125 | inProcessMessenger, |
| 126 | this.sidebar.webviewProtocol, |
| 127 | this.ide, |
| 128 | verticalDiffManagerPromise, |
| 129 | configHandlerPromise, |
| 130 | this.workOsAuthProvider, |
| 131 | this.editDecorationManager, |
| 132 | context, |
| 133 | this, |
| 134 | ); |
| 135 | |
| 136 | this.core = new Core(inProcessMessenger, this.ide); |
| 137 | this.configHandler = this.core.configHandler; |
| 138 | resolveConfigHandler?.(this.configHandler); |
| 139 | |
| 140 | void this.configHandler.loadConfig(); |
| 141 | |
| 142 | this.verticalDiffManager = new VerticalDiffManager( |
| 143 | this.sidebar.webviewProtocol, |
| 144 | this.editDecorationManager, |
| 145 | this.ide, |
| 146 | ); |
| 147 | resolveVerticalDiffManager?.(this.verticalDiffManager); |
| 148 | |
| 149 | void setupRemoteConfigSync(() => |
| 150 | this.configHandler.reloadConfig.bind(this.configHandler)( |
| 151 | "Remote config sync", |
| 152 | ), |
| 153 | ); |
| 154 | |
| 155 | void this.configHandler.loadConfig().then(({ config }) => { |
| 156 | const { verticalDiffCodeLens } = registerAllCodeLensProviders( |
| 157 | context, |
| 158 | this.verticalDiffManager.fileUriToCodeLens, |
| 159 | config, |
| 160 | ); |
| 161 | |
| 162 | this.verticalDiffManager.refreshCodeLens = |
| 163 | verticalDiffCodeLens.refresh.bind(verticalDiffCodeLens); |
| 164 | }); |
| 165 | |
| 166 | this.configHandler.onConfigUpdate( |
| 167 | async ({ config: newConfig, configLoadInterrupted }) => { |
| 168 | if (newConfig?.experimental?.optInNextEditFeature) { |
| 169 | // Set up next edit window manager only for Continue team members |
| 170 | await setupNextEditWindowManager(context); |
| 171 | |
| 172 | this.activateNextEdit(); |
| 173 | await NextEditWindowManager.freeTabAndEsc(); |
| 174 | } else { |
| 175 | NextEditWindowManager.clearInstance(); |
| 176 | this.deactivateNextEdit(); |
| 177 | await NextEditWindowManager.freeTabAndEsc(); |
| 178 | } |
| 179 | |
| 180 | if (configLoadInterrupted) { |
| 181 | // Show error in status bar |
| 182 | setupStatusBar(undefined, undefined, true); |
| 183 | } else if (newConfig) { |
| 184 | setupStatusBar(undefined, undefined, false); |
| 185 | |
| 186 | registerAllCodeLensProviders( |
| 187 | context, |
| 188 | this.verticalDiffManager.fileUriToCodeLens, |
| 189 | newConfig, |
| 190 | ); |
| 191 | } |
| 192 | }, |
| 193 | ); |
| 194 | |
| 195 | // Tab autocomplete |
| 196 | const config = vscode.workspace.getConfiguration(EXTENSION_NAME); |
| 197 | const enabled = config.get<boolean>("enableTabAutocomplete"); |
| 198 | |
| 199 | // Register inline completion provider |
| 200 | setupStatusBar( |
| 201 | enabled ? StatusBarStatus.Enabled : StatusBarStatus.Disabled, |
| 202 | ); |
| 203 | this.completionProvider = new ContinueCompletionProvider( |
| 204 | this.configHandler, |
| 205 | this.ide, |
| 206 | this.sidebar.webviewProtocol, |
| 207 | ); |
| 208 | context.subscriptions.push( |
| 209 | vscode.languages.registerInlineCompletionItemProvider( |
| 210 | [{ pattern: "**" }], |
| 211 | this.completionProvider, |
| 212 | ), |
| 213 | ); |
| 214 | |
| 215 | // Handle uri events |
| 216 | this.uriHandler.event((uri) => { |
| 217 | const queryParams = new URLSearchParams(uri.query); |
| 218 | let profileId = queryParams.get("profile_id"); |
| 219 | let orgId = queryParams.get("org_id"); |
| 220 | |
| 221 | this.core.invoke("config/refreshProfiles", { |
| 222 | selectOrgId: orgId === "null" ? undefined : (orgId ?? undefined), |
| 223 | selectProfileId: |
| 224 | profileId === "null" ? undefined : (profileId ?? undefined), |
| 225 | }); |
| 226 | }); |
| 227 | |
| 228 | // Battery |
| 229 | this.battery = new Battery(); |
| 230 | context.subscriptions.push(this.battery); |
| 231 | context.subscriptions.push(monitorBatteryChanges(this.battery)); |
| 232 | |
| 233 | // FileSearch |
| 234 | this.fileSearch = new FileSearch(this.ide); |
| 235 | registerAllPromptFilesCompletionProviders( |
| 236 | context, |
| 237 | this.fileSearch, |
| 238 | this.ide, |
| 239 | ); |
| 240 | |
| 241 | const quickEdit = new QuickEdit( |
| 242 | this.verticalDiffManager, |
| 243 | this.configHandler, |
| 244 | this.sidebar.webviewProtocol, |
| 245 | this.ide, |
| 246 | context, |
| 247 | this.fileSearch, |
| 248 | ); |
| 249 | |
| 250 | // LLM Log view |
| 251 | this.consoleView = new ContinueConsoleWebviewViewProvider( |
| 252 | this.windowId, |
| 253 | this.extensionContext, |
| 254 | this.core.llmLogger, |
| 255 | ); |
| 256 | |
| 257 | context.subscriptions.push( |
| 258 | vscode.window.registerWebviewViewProvider( |
| 259 | "continue.continueConsoleView", |
| 260 | this.consoleView, |
| 261 | ), |
| 262 | ); |
| 263 | |
| 264 | // Commands |
| 265 | registerAllCommands( |
| 266 | context, |
| 267 | this.ide, |
| 268 | context, |
| 269 | this.sidebar, |
| 270 | this.consoleView, |
| 271 | this.configHandler, |
| 272 | this.verticalDiffManager, |
| 273 | this.battery, |
| 274 | quickEdit, |
| 275 | this.core, |
| 276 | this.editDecorationManager, |
| 277 | ); |
| 278 | |
| 279 | // Disabled due to performance issues |
| 280 | // registerDebugTracker(this.sidebar.webviewProtocol, this.ide); |
| 281 | |
| 282 | // Listen for file saving - use global file watcher so that changes |
| 283 | // from outside the window are also caught |
| 284 | fs.watchFile(getConfigJsonPath(), { interval: 1000 }, async (stats) => { |
| 285 | if (stats.size === 0) { |
| 286 | return; |
| 287 | } |
| 288 | await this.configHandler.reloadConfig( |
| 289 | "Global JSON config updated - fs file watch", |
| 290 | ); |
| 291 | }); |
| 292 | |
| 293 | fs.watchFile( |
| 294 | getConfigYamlPath("vscode"), |
| 295 | { interval: 1000 }, |
| 296 | async (stats) => { |
| 297 | if (stats.size === 0) { |
| 298 | return; |
| 299 | } |
| 300 | await this.configHandler.reloadConfig( |
| 301 | "Global YAML config updated - fs file watch", |
| 302 | ); |
| 303 | }, |
| 304 | ); |
| 305 | |
| 306 | fs.watchFile(getConfigTsPath(), { interval: 1000 }, (stats) => { |
| 307 | if (stats.size === 0) { |
| 308 | return; |
| 309 | } |
| 310 | void this.configHandler.reloadConfig("config.ts updated - fs file watch"); |
| 311 | }); |
| 312 | |
| 313 | vscode.workspace.onDidChangeTextDocument(async (event) => { |
| 314 | const editInfo = await handleTextDocumentChange( |
| 315 | event, |
| 316 | this.configHandler, |
| 317 | this.ide, |
| 318 | this.completionProvider, |
| 319 | getDefinitionsFromLsp, |
| 320 | ); |
| 321 | |
| 322 | if (editInfo) this.core.invoke("files/smallEdit", editInfo); |
| 323 | }); |
| 324 | |
| 325 | vscode.workspace.onDidSaveTextDocument(async (event) => { |
| 326 | this.core.invoke("files/changed", { |
| 327 | uris: [event.uri.toString()], |
| 328 | }); |
| 329 | }); |
| 330 | |
| 331 | vscode.workspace.onDidDeleteFiles(async (event) => { |
| 332 | this.core.invoke("files/deleted", { |
| 333 | uris: event.files.map((uri) => uri.toString()), |
| 334 | }); |
| 335 | }); |
| 336 | |
| 337 | vscode.workspace.onDidCloseTextDocument(async (event) => { |
| 338 | this.core.invoke("files/closed", { |
| 339 | uris: [event.uri.toString()], |
| 340 | }); |
| 341 | }); |
| 342 | |
| 343 | vscode.workspace.onDidCreateFiles(async (event) => { |
| 344 | this.core.invoke("files/created", { |
| 345 | uris: event.files.map((uri) => uri.toString()), |
| 346 | }); |
| 347 | }); |
| 348 | |
| 349 | // When GitHub sign-in status changes, reload config |
| 350 | vscode.authentication.onDidChangeSessions(async (e) => { |
| 351 | const env = await getControlPlaneEnv(this.ide.getIdeSettings()); |
| 352 | if (e.provider.id === env.AUTH_TYPE) { |
| 353 | void vscode.commands.executeCommand( |
| 354 | "setContext", |
| 355 | "continue.isSignedInToControlPlane", |
| 356 | true, |
| 357 | ); |
| 358 | |
| 359 | const sessionInfo = await getControlPlaneSessionInfo(true, false); |
| 360 | void this.core.invoke("didChangeControlPlaneSessionInfo", { |
| 361 | sessionInfo, |
| 362 | }); |
| 363 | } else { |
| 364 | void vscode.commands.executeCommand( |
| 365 | "setContext", |
| 366 | "continue.isSignedInToControlPlane", |
| 367 | false, |
| 368 | ); |
| 369 | |
| 370 | if (e.provider.id === "github") { |
| 371 | this.configHandler.reloadConfig("Github sign-in status changed"); |
| 372 | } |
| 373 | } |
| 374 | }); |
| 375 | |
| 376 | // Refresh index when branch is changed |
| 377 | void this.ide.getWorkspaceDirs().then((dirs) => |
| 378 | dirs.forEach(async (dir) => { |
| 379 | const repo = await this.ide.getRepo(dir); |
| 380 | if (repo) { |
| 381 | repo.state.onDidChange(() => { |
| 382 | // args passed to this callback are always undefined, so keep track of previous branch |
| 383 | const currentBranch = repo?.state?.HEAD?.name; |
| 384 | if (currentBranch) { |
| 385 | if (this.PREVIOUS_BRANCH_FOR_WORKSPACE_DIR[dir]) { |
| 386 | if ( |
| 387 | currentBranch !== this.PREVIOUS_BRANCH_FOR_WORKSPACE_DIR[dir] |
| 388 | ) { |
| 389 | // Trigger refresh of index only in this directory |
| 390 | this.core.invoke("index/forceReIndex", { dirs: [dir] }); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | this.PREVIOUS_BRANCH_FOR_WORKSPACE_DIR[dir] = currentBranch; |
| 395 | } |
| 396 | }); |
| 397 | } |
| 398 | }), |
| 399 | ); |
| 400 | |
| 401 | // Register a content provider for the readonly virtual documents |
| 402 | const documentContentProvider = new (class |
| 403 | implements vscode.TextDocumentContentProvider |
| 404 | { |
| 405 | // emitter and its event |
| 406 | onDidChangeEmitter = new vscode.EventEmitter<vscode.Uri>(); |
| 407 | onDidChange = this.onDidChangeEmitter.event; |
| 408 | |
| 409 | provideTextDocumentContent(uri: vscode.Uri): string { |
| 410 | return uri.query; |
| 411 | } |
| 412 | })(); |
| 413 | context.subscriptions.push( |
| 414 | vscode.workspace.registerTextDocumentContentProvider( |
| 415 | VsCodeExtension.continueVirtualDocumentScheme, |
| 416 | documentContentProvider, |
| 417 | ), |
| 418 | ); |
| 419 | |
| 420 | const linkProvider = vscode.languages.registerDocumentLinkProvider( |
| 421 | { language: "yaml" }, |
| 422 | new ConfigYamlDocumentLinkProvider(), |
| 423 | ); |
| 424 | context.subscriptions.push(linkProvider); |
| 425 | |
| 426 | this.ide.onDidChangeActiveTextEditor((filepath) => { |
| 427 | void this.core.invoke("files/opened", { uris: [filepath] }); |
| 428 | }); |
| 429 | |
| 430 | // initializes openedFileLruCache with files that are already open when the extension is activated |
| 431 | let initialOpenedFilePaths = this.ideUtils |
| 432 | .getOpenFiles() |
| 433 | .map((uri) => uri.toString()); |
| 434 | this.core.invoke("files/opened", { uris: initialOpenedFilePaths }); |
| 435 | |
| 436 | vscode.workspace.onDidChangeConfiguration(async (event) => { |
| 437 | if (event.affectsConfiguration(EXTENSION_NAME)) { |
| 438 | const settings = await this.ide.getIdeSettings(); |
| 439 | void this.core.invoke("config/ideSettingsUpdate", settings); |
| 440 | } |
| 441 | }); |
| 442 | } |
| 443 | |
| 444 | static continueVirtualDocumentScheme = EXTENSION_NAME; |
| 445 | |
| 446 | // eslint-disable-next-line @typescript-eslint/naming-convention |
| 447 | private PREVIOUS_BRANCH_FOR_WORKSPACE_DIR: { [dir: string]: string } = {}; |
| 448 | |
| 449 | registerCustomContextProvider(contextProvider: IContextProvider) { |
| 450 | this.configHandler.registerCustomContextProvider(contextProvider); |
| 451 | } |
| 452 | |
| 453 | public activateNextEdit() { |
| 454 | this.completionProvider.activateNextEdit(); |
| 455 | } |
| 456 | |
| 457 | public deactivateNextEdit() { |
| 458 | this.completionProvider.deactivateNextEdit(); |
| 459 | } |
| 460 | } |