Skip to content
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

fix counting of context lines (don't count lines w/no chars included) #3405

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions vscode/test/e2e/command-core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ test.extend<ExpectedEvents>({
// When no selection is made, we will try to create smart selection from the cursor position
// If there is no cursor position, we will use the visible content of the editor
// NOTE: Core commands context should not start with ✨
await chatPanel.getByText('Context: 12 lines from 1 file').click()
await chatPanel.getByText('Context: 11 lines from 1 file').click()

// Check if assistant responsed
await expect(chatPanel.getByText('hello from the assistant')).toBeVisible()
Expand All @@ -64,7 +64,7 @@ test.extend<ExpectedEvents>({
await page.getByText('<title>Hello Cody</title>').click()
await expect(page.getByText('Explain Code')).toBeVisible()
await page.getByText('Explain Code').click()
await chatPanel.getByText('Context: 21 lines from 1 file').click()
await chatPanel.getByText('Context: 20 lines from 1 file').click()
await expect(chatPanel.locator('span').filter({ hasText: '@index.html:2-10' })).toBeVisible()
const disabledEditButtons = chatPanel.getByTitle('Cannot Edit Command').locator('i')
const editLastMessageButton = chatPanel.getByRole('button', { name: /^Edit Last Message / })
Expand Down
14 changes: 7 additions & 7 deletions vscode/test/e2e/command-custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ test.extend<ExpectedEvents>({

await expect(chatPanel.getByText('Add four context files from the current directory.')).toBeVisible()
// Show the current file numbers used as context
await expect(chatPanel.getByText('✨ Context: 61 lines from 5 files')).toBeVisible()
await chatPanel.getByText('✨ Context: 61 lines from 5 files').click()
await expect(chatPanel.getByText('✨ Context: 56 lines from 5 files')).toBeVisible()
await chatPanel.getByText('✨ Context: 56 lines from 5 files').click()
// Display the context files to confirm no hidden files are included
await expect(chatPanel.locator('span').filter({ hasText: '@.mydotfile:1-2' })).not.toBeVisible()
await expect(chatPanel.locator('span').filter({ hasText: '@error.ts:1-9' })).toBeVisible()
Expand All @@ -194,7 +194,7 @@ test.extend<ExpectedEvents>({
await page.keyboard.press('Enter')
await expect(chatPanel.getByText('Add lib/batches/env/var.go as context.')).toBeVisible()
// Should show 2 files with current file added as context
await expect(chatPanel.getByText('✨ Context: 14 lines from 2 files')).toBeVisible()
await expect(chatPanel.getByText('✨ Context: 12 lines from 2 files')).toBeVisible()

/* Test: context.directory with directory command */

Expand All @@ -204,8 +204,8 @@ test.extend<ExpectedEvents>({
await page.getByPlaceholder('Search command to run...').fill('directory')
await page.keyboard.press('Enter')
await expect(chatPanel.getByText('Directory has one context file.')).toBeVisible()
await expect(chatPanel.getByText('✨ Context: 14 lines from 2 file')).toBeVisible()
await chatPanel.getByText('✨ Context: 14 lines from 2 file').click()
await expect(chatPanel.getByText('✨ Context: 12 lines from 2 file')).toBeVisible()
await chatPanel.getByText('✨ Context: 12 lines from 2 file').click()
await expect(
chatPanel.locator('span').filter({ hasText: withPlatformSlashes('@lib/batches/env/var.go:1') })
).toBeVisible()
Expand All @@ -224,8 +224,8 @@ test.extend<ExpectedEvents>({
await page.keyboard.press('Enter')
await expect(chatPanel.getByText('Open tabs as context.')).toBeVisible()
// The files from the open tabs should be added as context
await expect(chatPanel.getByText('✨ Context: 14 lines from 2 files')).toBeVisible()
await chatPanel.getByText('✨ Context: 14 lines from 2 files').click()
await expect(chatPanel.getByText('✨ Context: 12 lines from 2 files')).toBeVisible()
await chatPanel.getByText('✨ Context: 12 lines from 2 files').click()
await expect(chatPanel.getByRole('button', { name: '@index.html:1-11' })).toBeVisible()
await expect(
chatPanel.getByRole('button', { name: withPlatformSlashes('@lib/batches/env/var.go:1') })
Expand Down
9 changes: 8 additions & 1 deletion vscode/webviews/chat/components/EnhancedContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ export const EnhancedContext: React.FunctionComponent<{
// It checks if file.range exists first before accessing start and end.
// If range doesn't exist, it adds 0 lines for that file.
const lineCount = contextFiles.reduce(
(total, file) => total + (file.range ? file.range?.end?.line - file.range?.start?.line + 1 : 0),
(total, file) =>
total +
(file.range
? // Don't count a line with no characters included (character == 0).
(file.range.end.character === 0 ? file.range.end.line - 1 : file.range.end.line) -
file.range.start?.line +
1
: 0),
0
)
const fileCount = new Set(contextFiles.map(file => file.uri.toString())).size
Expand Down
Loading