-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: Add external folder context for monorepos and full-stack workflows #1693
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
Open
zamalali
wants to merge
15
commits into
microsoft:main
Choose a base branch
from
zamalali:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+526
−4
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
fe54902
feat(context): add 'Add System Context' command and service
zamalali cd70f75
feat(context): rename SystemContext to ExternalContext and add new se…
zamalali 5a42ad1
Merge branch 'microsoft:main' into main
zamalali e25cdd8
feat(context): finalize ExternalContext service integration and cap l…
zamalali 5679597
Merge branch 'microsoft:main' into main
zamalali cbf07b4
Merge branch 'main' of https://github.com/zamalali/vscode-copilot-chat
zamalali 85aad2c
Update package.json
zamalali 74782fb
Merge branch 'microsoft:main' into main
zamalali 70de36a
fix: enforce folder cap, remove redundant checks, and streamline exte…
zamalali 7026caa
Merge branch 'microsoft:main' into main
zamalali b4a374a
Merge branch 'microsoft:main' into main
zamalali 6f75f9e
Merge branch 'main' into main
zamalali c4e4796
Merge branch 'main' into main
zamalali d84a49f
Merge branch 'microsoft:main' into main
zamalali 9a1d9a9
Merge branch 'main' into main
zamalali 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
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,124 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import * as npath from 'path'; | ||
| import { createServiceIdentifier } from '../../../util/common/services'; | ||
| import { isEqual } from '../../../util/vs/base/common/resources'; | ||
| import { URI } from '../../../util/vs/base/common/uri'; | ||
| import { Emitter, Event } from '../../../util/vs/base/common/event'; | ||
| import { Disposable } from '../../../util/vs/base/common/lifecycle'; | ||
| import { isWindows } from '../../../util/vs/base/common/platform'; | ||
|
|
||
| const MAX_EXTERNAL_PATHS = 3; | ||
|
|
||
| export const IExternalContextService = createServiceIdentifier<IExternalContextService>('IExternalContextService'); | ||
|
|
||
| export interface IExternalContextService { | ||
| readonly _serviceBrand: undefined; | ||
| readonly onDidChangeExternalContext: Event<void>; | ||
| readonly maxExternalPaths: number; | ||
| getExternalPaths(): readonly URI[]; | ||
| addExternalPaths(paths: readonly URI[]): readonly URI[]; | ||
| replaceExternalPaths(paths: readonly URI[]): void; | ||
| removeExternalPath(path: URI): void; | ||
| clear(): void; | ||
| isExternalPath(uri: URI): boolean; | ||
| } | ||
|
|
||
| export class ExternalContextService extends Disposable implements IExternalContextService { | ||
| declare readonly _serviceBrand: undefined; | ||
|
|
||
| private readonly _onDidChangeExternalContext = this._register(new Emitter<void>()); | ||
| readonly onDidChangeExternalContext: Event<void> = this._onDidChangeExternalContext.event; | ||
|
|
||
| readonly maxExternalPaths = MAX_EXTERNAL_PATHS; | ||
|
|
||
| private readonly _paths = new Map<string, URI>(); | ||
|
|
||
| getExternalPaths(): readonly URI[] { | ||
| return [...this._paths.values()]; | ||
| } | ||
|
|
||
| addExternalPaths(paths: readonly URI[]): readonly URI[] { | ||
| const added: URI[] = []; | ||
| if (!paths.length) { | ||
| return added; | ||
| } | ||
|
|
||
| for (const path of paths) { | ||
| if (this._paths.size >= MAX_EXTERNAL_PATHS) { | ||
| break; | ||
| } | ||
| const key = path.toString(); | ||
| if (!this._paths.has(key)) { | ||
| this._paths.set(key, path); | ||
| added.push(path); | ||
| } | ||
| } | ||
| if (added.length) { | ||
| this._onDidChangeExternalContext.fire(); | ||
| } | ||
|
|
||
| return added; | ||
| } | ||
|
|
||
| replaceExternalPaths(paths: readonly URI[]): void { | ||
| this._paths.clear(); | ||
| for (const path of paths) { | ||
| if (this._paths.size >= MAX_EXTERNAL_PATHS) { | ||
| break; | ||
| } | ||
| const key = path.toString(); | ||
| if (!this._paths.has(key)) { | ||
| this._paths.set(key, path); | ||
| } | ||
| } | ||
| this._onDidChangeExternalContext.fire(); | ||
| } | ||
|
|
||
| removeExternalPath(path: URI): void { | ||
| for (const [key, storedPath] of this._paths) { | ||
| if (isEqual(storedPath, path)) { | ||
| this._paths.delete(key); | ||
| this._onDidChangeExternalContext.fire(); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| clear(): void { | ||
| if (this._paths.size === 0) { | ||
| return; | ||
| } | ||
| this._paths.clear(); | ||
| this._onDidChangeExternalContext.fire(); | ||
| } | ||
|
|
||
| isExternalPath(uri: URI): boolean { | ||
| const candidateComparable = this.toComparablePath(uri); | ||
| for (const stored of this._paths.values()) { | ||
| const storedComparable = this.toComparablePath(stored); | ||
|
|
||
| if (candidateComparable === storedComparable) { | ||
| return true; | ||
| } | ||
|
|
||
| if (this.isSubPath(candidateComparable, storedComparable) || this.isSubPath(storedComparable, candidateComparable)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private toComparablePath(uri: URI): string { | ||
| const normalized = npath.normalize(uri.fsPath); | ||
| return isWindows ? normalized.toLowerCase() : normalized; | ||
| } | ||
|
|
||
| private isSubPath(child: string, potentialParent: string): boolean { | ||
| const parentWithSep = potentialParent.endsWith(npath.sep) ? potentialParent : potentialParent + npath.sep; | ||
| return child.startsWith(parentWithSep); | ||
| } | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
src/extension/context/node/test/externalContextService.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,53 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import * as path from 'path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { URI } from '../../../../util/vs/base/common/uri'; | ||
| import { ExternalContextService } from '../externalContextService'; | ||
|
|
||
| function createUri(name: string): URI { | ||
| return URI.file(path.join(process.cwd(), 'external-context-tests', name)); | ||
| } | ||
|
|
||
| describe('ExternalContextService', () => { | ||
| it('caps at max external paths', () => { | ||
| const service = new ExternalContextService(); | ||
|
|
||
| service.addExternalPaths([ | ||
| createUri('one'), | ||
| createUri('two'), | ||
| createUri('three'), | ||
| createUri('four') | ||
| ]); | ||
|
|
||
| expect(service.getExternalPaths()).toHaveLength(service.maxExternalPaths); | ||
| }); | ||
|
|
||
| it('fires change event when paths are added', () => { | ||
| const service = new ExternalContextService(); | ||
| let fired = 0; | ||
|
|
||
| service.onDidChangeExternalContext(() => fired++); | ||
|
|
||
| service.addExternalPaths([createUri('one')]); | ||
|
|
||
| expect(fired).toBe(1); | ||
| }); | ||
|
|
||
| it('removes paths and fires event', () => { | ||
| const service = new ExternalContextService(); | ||
| const [added] = service.addExternalPaths([createUri('one')]); | ||
| let fired = 0; | ||
|
|
||
| service.onDidChangeExternalContext(() => fired++); | ||
|
|
||
| service.removeExternalPath(added); | ||
|
|
||
| expect(service.getExternalPaths()).toHaveLength(0); | ||
| expect(fired).toBe(1); | ||
| }); | ||
| }); | ||
|
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
toComparablePathmethod is called repeatedly inside the loop, converting the same stored paths on every iteration. Consider caching the comparable paths in a Map alongside the URIs to avoid redundant path normalization operations.