This repository was archived by the owner on Aug 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 477
feat(mention): allow selection of context from branches for remote files and directories #8156
Open
ichim-david
wants to merge
27
commits into
main
Choose a base branch
from
remote_directories_branch
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.
Open
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
bf241e0
feat(context): Display branch name in mention menu help text
ichim-david a71078a
feat(context): Enhance remote directory search and item retrieval
ichim-david 064ccf8
feat(context): Improve remote directory item retrieval with context s…
ichim-david 7e375b5
fix(context): Improve remote directory search tests and URL generation
ichim-david 76143f9
feat(context): Enhance remote directory search with branch support an…
ichim-david e164488
feat(context): Enhance OpenCtx with branch mentions and remote file b…
ichim-david 49af345
Fixed tests by using the exported functions and general cleanup
ichim-david 0052d3f
pnpm biome fix
ichim-david 3473b19
fixed remote file filtering once we choose the branch
ichim-david 0314735
feat(context): Improve remote directory search using getDirectoryCont…
ichim-david 4ffcf86
Removed experimental flag from remote directoy mention menu
ichim-david 0f98b3a
WIP implementing repo -> branch -> directory selection for remote dir…
ichim-david e198229
repository branch searching beyond the first 10 results
ichim-david 56730e2
fix regex for starting directories mentions on searches branches
ichim-david ca80b62
skip . folders from initial folder listing
ichim-david f32012f
simplify branch mentions after proper branch searching
ichim-david 8fe7cec
simplify check for remote directories branch or directory listing
ichim-david 1841de6
allow remote file to search for branches
ichim-david 2b43819
test fixes
ichim-david 9be17a4
fix mention menu tooltip height when branch name is long
ichim-david 57bffd7
removed `getFileBranchMentions` fallback to file search
ichim-david 29ce80a
simplify code by getting rid of need for extractBranchAndRepo
ichim-david c927aaa
simplify and bring mentions in sync between remoteFile and remoteDire…
ichim-david 4d9016f
remove needless try catch and clarify why we have empty description
ichim-david 75ef576
removed `parsedRemoteQuery` logic that ended up not being used
ichim-david 8d5a4cf
parallel fetch of directory items
ichim-david 996e08e
fix tooltips for dirname
ichim-david 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
161 changes: 161 additions & 0 deletions
161
lib/prompt-editor/src/mentions/mentionMenu/MentionMenu.branch.test.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,161 @@ | ||
| import { ContextItemSource, REMOTE_DIRECTORY_PROVIDER_URI } from '@sourcegraph/cody-shared' | ||
| import type { MentionMenuData } from '@sourcegraph/cody-shared' | ||
| import { describe, expect, test } from 'vitest' | ||
| import { URI } from 'vscode-uri' | ||
|
|
||
| // This would be imported from the MentionMenu component if it were exported | ||
| // For now, we'll create a test version of the function | ||
| function getBranchHelpText( | ||
| items: NonNullable<MentionMenuData['items']>, | ||
| mentionQuery: { text: string } | ||
| ): string { | ||
| // Check if we have branch information from the current search | ||
| const firstItem = items[0] | ||
| if (firstItem?.type === 'openctx') { | ||
| const openCtxItem = firstItem as any // Simplified for testing | ||
| if (openCtxItem.mention?.data?.branch) { | ||
| return `* Sourced from the '${openCtxItem.mention.data.branch}' branch` | ||
| } | ||
| } | ||
|
|
||
| // Check if user has specified a branch in the query | ||
| if (mentionQuery.text.includes('@')) { | ||
| const branchPart = mentionQuery.text.split('@')[1] | ||
| if (branchPart) { | ||
| // Remove anything after colon (directory path) | ||
| const branchName = branchPart.split(':')[0] | ||
| return `* Sourced from the '${branchName}' branch` | ||
| } | ||
| } | ||
|
|
||
| return '* Sourced from the remote default branch' | ||
| } | ||
|
|
||
| describe('MentionMenu branch selection', () => { | ||
| test('should show default branch text when no branch is specified', () => { | ||
| const items: MentionMenuData['items'] = [ | ||
| { | ||
| type: 'openctx', | ||
| provider: 'openctx', | ||
| title: 'src/components', | ||
| uri: URI.parse('https://example.com/repo/-/tree/src/components'), | ||
| providerUri: REMOTE_DIRECTORY_PROVIDER_URI, | ||
| source: ContextItemSource.User, | ||
| mention: { | ||
| uri: 'https://example.com/repo/-/tree/src/components', | ||
| data: { | ||
| repoName: 'test-repo', | ||
| directoryPath: 'src/components', | ||
| }, | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| const mentionQuery = { text: 'test-repo:src' } | ||
|
|
||
| const result = getBranchHelpText(items!, mentionQuery) | ||
| expect(result).toBe('* Sourced from the remote default branch') | ||
| }) | ||
|
|
||
| test('should show branch name when branch is specified in query', () => { | ||
| const items: MentionMenuData['items'] = [ | ||
| { | ||
| type: 'openctx', | ||
| provider: 'openctx', | ||
| title: 'src/components', | ||
| uri: URI.parse('https://example.com/repo/-/tree/src/components'), | ||
| providerUri: REMOTE_DIRECTORY_PROVIDER_URI, | ||
| source: ContextItemSource.User, | ||
| mention: { | ||
| uri: 'https://example.com/repo/-/tree/src/components', | ||
| data: { | ||
| repoName: 'test-repo', | ||
| directoryPath: 'src/components', | ||
| }, | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| const mentionQuery = { text: 'test-repo@feature-branch:src' } | ||
|
|
||
| const result = getBranchHelpText(items!, mentionQuery) | ||
| expect(result).toBe("* Sourced from the 'feature-branch' branch") | ||
| }) | ||
|
|
||
| test('should show branch name from mention data when available', () => { | ||
| const items: MentionMenuData['items'] = [ | ||
| { | ||
| type: 'openctx', | ||
| provider: 'openctx', | ||
| title: 'src/components', | ||
| uri: URI.parse('https://example.com/repo/-/tree/src/components'), | ||
| providerUri: REMOTE_DIRECTORY_PROVIDER_URI, | ||
| source: ContextItemSource.User, | ||
| mention: { | ||
| uri: 'https://example.com/repo/-/tree/src/components', | ||
| data: { | ||
| repoName: 'test-repo', | ||
| directoryPath: 'src/components', | ||
| branch: 'main', | ||
| }, | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| const mentionQuery = { text: 'test-repo:src' } | ||
|
|
||
| const result = getBranchHelpText(items!, mentionQuery) | ||
| expect(result).toBe("* Sourced from the 'main' branch") | ||
| }) | ||
|
|
||
| test('should prefer mention data branch over query branch', () => { | ||
| const items: MentionMenuData['items'] = [ | ||
| { | ||
| type: 'openctx', | ||
| provider: 'openctx', | ||
| title: 'src/components', | ||
| uri: URI.parse('https://example.com/repo/-/tree/src/components'), | ||
| providerUri: REMOTE_DIRECTORY_PROVIDER_URI, | ||
| source: ContextItemSource.User, | ||
| mention: { | ||
| uri: 'https://example.com/repo/-/tree/src/components', | ||
| data: { | ||
| repoName: 'test-repo', | ||
| directoryPath: 'src/components', | ||
| branch: 'actual-branch', | ||
| }, | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| const mentionQuery = { text: 'test-repo@query-branch:src' } | ||
|
|
||
| const result = getBranchHelpText(items!, mentionQuery) | ||
| expect(result).toBe("* Sourced from the 'actual-branch' branch") | ||
| }) | ||
|
|
||
| test('should handle empty items array', () => { | ||
| const items: MentionMenuData['items'] = [] | ||
| const mentionQuery = { text: 'test-repo@main:src' } | ||
|
|
||
| const result = getBranchHelpText(items!, mentionQuery) | ||
| expect(result).toBe("* Sourced from the 'main' branch") | ||
| }) | ||
|
|
||
| test('should handle non-openctx items', () => { | ||
| const items: MentionMenuData['items'] = [ | ||
| { | ||
| type: 'file', | ||
| provider: 'file', | ||
| title: 'test.ts', | ||
| uri: URI.parse('file:///test.ts'), | ||
| source: ContextItemSource.User, | ||
| }, | ||
| ] | ||
|
|
||
| const mentionQuery = { text: 'test-repo@feature:src' } | ||
|
|
||
| const result = getBranchHelpText(items!, mentionQuery) | ||
| expect(result).toBe("* Sourced from the 'feature' branch") | ||
| }) | ||
| }) |
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
Oops, something went wrong.
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.
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.
Examining the rest of the PR, it looks like you don't need
content.contentis pretty expensive, so you should use it only when you need the file contents immediately. If you think you may need the file contents, you can userawURLinstead ofcontentand then use that URL to retrieve the content.Additionally, examine whether you need the blob at all - it looks like this PR is using the directories, but not the files, so you may be able to ditch the file info (GitBlob) completely.
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.
+1 to the blob comment