From 9ac17c2ec8441a5bc395f5fcdb711e91e52b4b96 Mon Sep 17 00:00:00 2001 From: celestial-vault <58194240+celestial-vault@users.noreply.github.com> Date: Wed, 10 Sep 2025 18:18:44 -0700 Subject: [PATCH] cleanup unnecessary functions in StateManager to reduce clutter (#6134) --- src/core/controller/index.ts | 1 + src/core/storage/StateManager.ts | 49 ---------------------- src/core/storage/state-keys.ts | 4 +- src/core/storage/utils/state-helpers.ts | 19 ++++++--- src/core/task/index.ts | 4 +- src/core/workspace/__tests__/setup.test.ts | 27 ++++++++---- src/core/workspace/setup.ts | 25 +++++------ 7 files changed, 50 insertions(+), 79 deletions(-) diff --git a/src/core/controller/index.ts b/src/core/controller/index.ts index 330306c6b2..72931161ce 100644 --- a/src/core/controller/index.ts +++ b/src/core/controller/index.ts @@ -225,6 +225,7 @@ export class Controller { }) const cwd = this.workspaceManager?.getPrimaryRoot()?.path || (await getCwd(getDesktopDir())) + this.task = new Task( this, this.mcpHub, diff --git a/src/core/storage/StateManager.ts b/src/core/storage/StateManager.ts index 83fa81bcb8..f95c067583 100644 --- a/src/core/storage/StateManager.ts +++ b/src/core/storage/StateManager.ts @@ -1,4 +1,3 @@ -import { WorkspaceRoot } from "@core/workspace" import { ApiConfiguration, fireworksDefaultModelId } from "@shared/api" import chokidar, { FSWatcher } from "chokidar" import type { ExtensionContext } from "vscode" @@ -555,54 +554,6 @@ export class StateManager { return this.workspaceStateCache[key] } - /** - * Get workspace roots from global state - */ - getWorkspaceRoots(): WorkspaceRoot[] | undefined { - return this.getGlobalStateKey("workspaceRoots") - } - - /** - * Set workspace roots in global state - */ - setWorkspaceRoots(roots: WorkspaceRoot[]): void { - this.setGlobalState("workspaceRoots", roots) - } - - /** - * Get primary root index from global state. - * The primary root is the main workspace folder that Cline focuses on when dealing with - * multi-root workspaces. In VS Code, you can have multiple folders open in one workspace, - * and the primary root index indicates which folder (by its position in the array, 0-based) - * should be treated as the main/default working directory for operations. - */ - getPrimaryRootIndex(): number { - return this.getGlobalStateKey("primaryRootIndex") ?? 0 - } - - /** - * Set primary root index in global state - */ - setPrimaryRootIndex(index: number): void { - this.setGlobalState("primaryRootIndex", index) - } - - /** - * Check if multi-root workspace feature is enabled - */ - isMultiRootEnabled(): boolean { - // Feature flag - defaults to false - // For now, always return false to disable multi-root support by default - return this.getGlobalStateKey("multiRootEnabled") ?? false - } - - /** - * Enable or disable multi-root workspace feature - */ - setMultiRootEnabled(enabled: boolean): void { - this.setGlobalState("multiRootEnabled", enabled) - } - /** * Reinitialize the state manager by clearing all state and reloading from disk * Used for error recovery when write operations fail diff --git a/src/core/storage/state-keys.ts b/src/core/storage/state-keys.ts index 8e9162eaef..75d8f5852d 100644 --- a/src/core/storage/state-keys.ts +++ b/src/core/storage/state-keys.ts @@ -87,8 +87,8 @@ export interface GlobalState { // Multi-root workspace support workspaceRoots: WorkspaceRoot[] | undefined - primaryRootIndex: number | undefined - multiRootEnabled: boolean | undefined + primaryRootIndex: number + multiRootEnabled: boolean // Plan mode configurations planModeApiProvider: ApiProvider diff --git a/src/core/storage/utils/state-helpers.ts b/src/core/storage/utils/state-helpers.ts index 1b72446f0f..dc58e36d1a 100644 --- a/src/core/storage/utils/state-helpers.ts +++ b/src/core/storage/utils/state-helpers.ts @@ -324,9 +324,16 @@ export async function readGlobalStateFromDisk(context: ExtensionContext): Promis const taskHistory = await readTaskHistoryFromState(context) // Multi-root workspace support - const workspaceRoots = context.globalState.get("workspaceRoots") as GlobalState["workspaceRoots"] - const primaryRootIndex = context.globalState.get("primaryRootIndex") as number | undefined - const multiRootEnabled = context.globalState.get("multiRootEnabled") as boolean | undefined + const workspaceRoots = context.globalState.get("workspaceRoots") + /** + * Get primary root index from global state. + * The primary root is the main workspace folder that Cline focuses on when dealing with + * multi-root workspaces. In VS Code, you can have multiple folders open in one workspace, + * and the primary root index indicates which folder (by its position in the array, 0-based) + * should be treated as the main/default working directory for operations. + */ + const primaryRootIndex = context.globalState.get("primaryRootIndex") + const multiRootEnabled = context.globalState.get("multiRootEnabled") return { // api configuration fields @@ -463,8 +470,10 @@ export async function readGlobalStateFromDisk(context: ExtensionContext): Promis customPrompt, // Multi-root workspace support workspaceRoots, - primaryRootIndex, - multiRootEnabled, + primaryRootIndex: primaryRootIndex ?? 0, + // Feature flag - defaults to false + // For now, always return false to disable multi-root support by default + multiRootEnabled: multiRootEnabled ?? false, } } catch (error) { console.error("[StateHelpers] Failed to read global state:", error) diff --git a/src/core/task/index.ts b/src/core/task/index.ts index 140e05c7e2..6a1afa5561 100644 --- a/src/core/task/index.ts +++ b/src/core/task/index.ts @@ -282,7 +282,7 @@ export class Task { context: controller.context, workspaceManager: this.workspaceManager, globalStoragePath: controller.context.globalStorageUri.fsPath, - isMultiRootEnabled: stateManager.isMultiRootEnabled(), + isMultiRootEnabled: stateManager.getGlobalStateKey("multiRootEnabled"), updateTaskHistory: this.updateTaskHistory, say: this.say.bind(this), cancelTask: this.cancelTask, @@ -294,7 +294,7 @@ export class Task { // If multi-root, kick off non-blocking initialization if ( shouldUseMultiRoot({ - isMultiRootEnabled: stateManager.isMultiRootEnabled(), + isMultiRootEnabled: stateManager.getGlobalStateKey("multiRootEnabled"), workspaceManager: this.workspaceManager, enableCheckpoints: enableCheckpointsSetting, }) diff --git a/src/core/workspace/__tests__/setup.test.ts b/src/core/workspace/__tests__/setup.test.ts index 397ee087e9..738e662f4d 100644 --- a/src/core/workspace/__tests__/setup.test.ts +++ b/src/core/workspace/__tests__/setup.test.ts @@ -31,14 +31,27 @@ describe("setupWorkspaceManager", () => { }) => { const state: { roots?: WorkspaceRoot[]; primaryIndex?: number } = {} return { - isMultiRootEnabled: () => multiRootEnabled, - getWorkspaceRoots: () => savedRoots, - getPrimaryRootIndex: () => savedPrimaryIndex, - setWorkspaceRoots: (roots: WorkspaceRoot[]) => { - state.roots = roots + getGlobalStateKey: (key: string) => { + switch (key) { + case "multiRootEnabled": + return multiRootEnabled + case "workspaceRoots": + return savedRoots + case "primaryRootIndex": + return savedPrimaryIndex + default: + return undefined + } }, - setPrimaryRootIndex: (idx: number) => { - state.primaryIndex = idx + setGlobalState: (key: string, value: any) => { + switch (key) { + case "workspaceRoots": + state.roots = value + break + case "primaryRootIndex": + state.primaryIndex = value + break + } }, // for assertions _state: state, diff --git a/src/core/workspace/setup.ts b/src/core/workspace/setup.ts index 82f4bc44a6..4a307a0603 100644 --- a/src/core/workspace/setup.ts +++ b/src/core/workspace/setup.ts @@ -3,6 +3,7 @@ import { telemetryService } from "@/services/telemetry" import type { HistoryItem } from "@/shared/HistoryItem" import { ShowMessageType } from "@/shared/proto/host/window" import { getCwd, getDesktopDir } from "@/utils/path" +import { StateManager } from "../storage/StateManager" import type { WorkspaceRoot } from "./WorkspaceRoot" import { WorkspaceRootManager } from "./WorkspaceRootManager" @@ -18,18 +19,12 @@ export async function setupWorkspaceManager({ stateManager, detectRoots, }: { - stateManager: { - isMultiRootEnabled(): boolean - getWorkspaceRoots(): WorkspaceRoot[] | undefined - getPrimaryRootIndex(): number - setWorkspaceRoots(roots: WorkspaceRoot[]): void - setPrimaryRootIndex(idx: number): void - } + stateManager: StateManager historyItem?: HistoryItem detectRoots: DetectRoots }): Promise { const cwd = await getCwd(getDesktopDir()) - const multiRootEnabled = stateManager.isMultiRootEnabled() + const multiRootEnabled = stateManager.getGlobalStateKey("multiRootEnabled") const startTime = performance.now() try { @@ -50,8 +45,8 @@ export async function setupWorkspaceManager({ ) // Persist - stateManager.setWorkspaceRoots(manager.getRoots()) - stateManager.setPrimaryRootIndex(manager.getPrimaryIndex()) + stateManager.setGlobalState("workspaceRoots", manager.getRoots()) + stateManager.setGlobalState("primaryRootIndex", manager.getPrimaryIndex()) return manager } @@ -88,8 +83,9 @@ export async function setupWorkspaceManager({ ) console.log(`[WorkspaceManager] Single-root mode: ${cwd}`) - stateManager.setWorkspaceRoots(manager.getRoots()) - stateManager.setPrimaryRootIndex(manager.getPrimaryIndex()) + const roots = manager.getRoots() + stateManager.setGlobalState("workspaceRoots", roots) + stateManager.setGlobalState("primaryRootIndex", manager.getPrimaryIndex()) return manager } catch (error) { // Telemetry + graceful fallback to single-root from cwd @@ -98,8 +94,9 @@ export async function setupWorkspaceManager({ console.error("[WorkspaceManager] Initialization failed:", error) const manager = await WorkspaceRootManager.fromLegacyCwd(cwd) - stateManager.setWorkspaceRoots(manager.getRoots()) - stateManager.setPrimaryRootIndex(manager.getPrimaryIndex()) + const roots = manager.getRoots() + stateManager.setGlobalState("workspaceRoots", roots) + stateManager.setGlobalState("primaryRootIndex", manager.getPrimaryIndex()) HostProvider.window.showMessage({ type: ShowMessageType.WARNING,