-
Notifications
You must be signed in to change notification settings - Fork 300
refactor(code-index): introduce workspace scope behind registry #1766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
edelauna
merged 2 commits into
Zoo-Code-Org:main
from
WebMad:refactor/1594-code-index-workspace-scope
Sep 28, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/services/code-index/__tests__/code-index-workspace-scope.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { makeExtensionContext, makeUri } from "../../../test-utils/vscode" | ||
| import { CodeIndexManager } from "../manager" | ||
| import { CodeIndexWorkspaceScope } from "../code-index-workspace-scope" | ||
|
|
||
| vi.mock("../manager", () => ({ | ||
| CodeIndexManager: vi.fn().mockImplementation(function () { | ||
| return { initialize: vi.fn(), dispose: vi.fn() } | ||
| }), | ||
| })) | ||
|
|
||
| describe("CodeIndexWorkspaceScope", () => { | ||
| beforeEach(() => vi.clearAllMocks()) | ||
|
|
||
| it("guards generic values and preserves defined falsy values", () => { | ||
| const scope = new CodeIndexWorkspaceScope("/workspace", makeUri("/workspace"), makeExtensionContext()) | ||
| expect(() => scope["ensureInitialized"](42)).toThrow("Code index workspace scope is not initialized") | ||
| scope.init() | ||
| expect(() => scope["ensureInitialized"](undefined)).toThrow("Code index workspace scope is not initialized") | ||
| expect(scope["ensureInitialized"](false)).toBe(false) | ||
| expect(scope["ensureInitialized"](0)).toBe(0) | ||
| expect(scope["ensureInitialized"]("")).toBe("") | ||
| }) | ||
|
|
||
| it("creates its manager only on init without starting configuration initialization", () => { | ||
| const context = makeExtensionContext() | ||
| const uri = makeUri("/workspace") | ||
| const scope = new CodeIndexWorkspaceScope(uri.fsPath, uri, context) | ||
|
|
||
| expect(CodeIndexManager).not.toHaveBeenCalled() | ||
| expect(() => scope.codeIndexManager).toThrow("Code index workspace scope is not initialized") | ||
| expect(scope.init()).toBeUndefined() | ||
| const manager = scope.codeIndexManager | ||
|
|
||
| expect(CodeIndexManager).toHaveBeenCalledExactlyOnceWith(uri.fsPath, uri, context) | ||
| expect(scope.codeIndexManager).toBe(manager) | ||
| expect(() => scope.init()).toThrow("Code index workspace scope is already initialized") | ||
| expect(scope.codeIndexManager).toBe(manager) | ||
| expect(CodeIndexManager).toHaveBeenCalledTimes(1) | ||
| expect(manager.initialize).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("synchronously disposes its manager", () => { | ||
| const scope = new CodeIndexWorkspaceScope("/workspace", makeUri("/workspace"), makeExtensionContext()) | ||
| scope.init() | ||
| const manager = scope.codeIndexManager | ||
|
|
||
| expect(scope.dispose()).toBeUndefined() | ||
| expect(() => scope.codeIndexManager).toThrow("Code index workspace scope is not initialized") | ||
| scope.dispose() | ||
| expect(manager.dispose).toHaveBeenCalledExactlyOnceWith() | ||
| }) | ||
|
|
||
| it("can dispose before dependency creation without constructing a manager", () => { | ||
| const scope = new CodeIndexWorkspaceScope("/workspace", makeUri("/workspace"), makeExtensionContext()) | ||
| scope.dispose() | ||
| expect(() => scope.codeIndexManager).toThrow("Code index workspace scope is not initialized") | ||
| expect(CodeIndexManager).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("creates a fresh manager when initialized after disposal", () => { | ||
| const scope = new CodeIndexWorkspaceScope("/workspace", makeUri("/workspace"), makeExtensionContext()) | ||
| scope.init() | ||
| const previous = scope.codeIndexManager | ||
| scope.dispose() | ||
| scope.init() | ||
| const current = scope.codeIndexManager | ||
| expect(current).not.toBe(previous) | ||
| expect(scope.codeIndexManager).toBe(current) | ||
| expect(CodeIndexManager).toHaveBeenCalledTimes(2) | ||
| expect(current.dispose).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("clears its reference even when manager disposal throws", () => { | ||
| const scope = new CodeIndexWorkspaceScope("/workspace", makeUri("/workspace"), makeExtensionContext()) | ||
| scope.init() | ||
| const manager = scope.codeIndexManager | ||
| const error = new Error("disposal failed") | ||
| vi.mocked(manager.dispose).mockImplementationOnce(() => { | ||
| throw error | ||
| }) | ||
| expect(() => scope.dispose()).toThrow(error) | ||
| expect(() => scope.codeIndexManager).toThrow("Code index workspace scope is not initialized") | ||
| scope.dispose() | ||
| expect(manager.dispose).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it("remains uninitialized when construction fails and permits retry", () => { | ||
| const scope = new CodeIndexWorkspaceScope("/workspace", makeUri("/workspace"), makeExtensionContext()) | ||
| const error = new Error("construction failed") | ||
| vi.mocked(CodeIndexManager).mockImplementationOnce(function () { | ||
| throw error | ||
| }) | ||
|
|
||
| expect(() => scope.init()).toThrow(error) | ||
| expect(() => scope.codeIndexManager).toThrow("Code index workspace scope is not initialized") | ||
| expect(scope.init()).toBeUndefined() | ||
| expect(scope.codeIndexManager).toBe(vi.mocked(CodeIndexManager).mock.results[1].value) | ||
| expect(CodeIndexManager).toHaveBeenCalledTimes(2) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import type * as vscode from "vscode" | ||
|
|
||
| import { CodeIndexManager } from "./manager" | ||
|
|
||
| /** Owns code-index services for one workspace; initialization remains with existing callers. */ | ||
| export class CodeIndexWorkspaceScope implements vscode.Disposable { | ||
| private _codeIndexManager?: CodeIndexManager | ||
| private _isInitialized = false | ||
|
|
||
| public constructor( | ||
| private readonly workspacePath: string, | ||
| private readonly folderUri: vscode.Uri, | ||
| private readonly context: vscode.ExtensionContext, | ||
| ) {} | ||
|
|
||
| public get codeIndexManager(): CodeIndexManager { | ||
| return this.ensureInitialized(this._codeIndexManager) | ||
| } | ||
|
|
||
| private ensureInitialized<T>(value: T | undefined): T { | ||
| if (!this._isInitialized || value === undefined) { | ||
| throw new Error("Code index workspace scope is not initialized") | ||
| } | ||
| return value | ||
| } | ||
|
|
||
| /** Creates the manager without loading configuration or starting indexing. */ | ||
| public init(): void { | ||
| if (this._isInitialized) { | ||
| throw new Error("Code index workspace scope is already initialized") | ||
| } | ||
| this._codeIndexManager = new CodeIndexManager(this.workspacePath, this.folderUri, this.context) | ||
| this._isInitialized = true | ||
| } | ||
|
|
||
| public dispose(): void { | ||
| const manager = this._codeIndexManager | ||
| this._codeIndexManager = undefined | ||
| this._isInitialized = false | ||
| manager?.dispose() | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.