Skip to content

Commit 01dd3de

Browse files
authored
Chat: improve handling of at mentions (#3114)
1 parent de72775 commit 01dd3de

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

lib/ui/src/Chat.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ export const Chat: React.FunctionComponent<ChatProps> = ({
424424
}
425425

426426
// At mention should start with @ and contains no whitespaces
427-
const isAtMention = (word: string) => /^@/.test(word) && !word.includes(' ')
427+
const isAtMention = (word: string) => /^@[^ ]*$/.test(word)
428428

429429
// Extract mention query by splitting input value into before/after caret sections.
430430
const extractMentionQuery = (input: string, caretPos: number) => {
@@ -442,6 +442,14 @@ export const Chat: React.FunctionComponent<ChatProps> = ({
442442
const mentionQuery = extractMentionQuery(inputValue, caretPosition)
443443
const query = mentionQuery.replace(/^@/, '')
444444

445+
// Cover cases where user prefer to type the file without tabbing the selection
446+
if (contextSelection?.length) {
447+
if (currentChatContextQuery === query.trimEnd()) {
448+
onChatContextSelected(contextSelection[0])
449+
return
450+
}
451+
}
452+
445453
// Filters invalid queries and sets context query state accordingly:
446454
// Sets the current chat context query state if a valid mention is detected.
447455
// Otherwise resets the context selection and query state.
@@ -454,7 +462,13 @@ export const Chat: React.FunctionComponent<ChatProps> = ({
454462
setCurrentChatContextQuery(query)
455463
postMessage({ command: 'getUserContext', query })
456464
},
457-
[postMessage, resetContextSelection]
465+
[
466+
postMessage,
467+
resetContextSelection,
468+
contextSelection,
469+
currentChatContextQuery,
470+
onChatContextSelected,
471+
]
458472
)
459473

460474
const inputHandler = useCallback(

vscode/test/e2e/chat-atFile.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { isWindows } from '@sourcegraph/cody-shared'
55
import * as mockServer from '../fixtures/mock-server'
66

77
import { sidebarSignin } from './common'
8-
import { assertEvents, test, withPlatformSlashes } from './helpers'
8+
import { assertEvents, getMetaKeyByOS, test, withPlatformSlashes } from './helpers'
99

1010
/**
1111
* Tests for @-file & @#-symbol in chat
@@ -178,6 +178,18 @@ test('@-file & @#-symbol in chat view', async ({ page, sidebar }) => {
178178
await chatInput.press('Backspace')
179179
await expect(noMatches).toBeVisible()
180180

181+
// Typing out the whole file path without pressing tab/enter should still include the
182+
// file as context
183+
const osKey = getMetaKeyByOS()
184+
await chatInput.press(`${osKey}+/`) // start a new chat
185+
await chatInput.fill('@index.htm')
186+
await chatInput.press('l')
187+
await expect(chatPanelFrame.getByRole('button', { name: 'index.html' })).toBeVisible()
188+
await chatInput.press('Space')
189+
await page.keyboard.type('explain.', { delay: 50 })
190+
await chatInput.press('Enter')
191+
await expect(chatPanelFrame.getByText(/^ Context:/)).toHaveCount(1)
192+
181193
const expectedEvents = [
182194
'CodyVSCodeExtension:at-mention:executed',
183195
'CodyVSCodeExtension:at-mention:file:executed',

vscode/test/e2e/helpers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,8 @@ export async function newChat(page: Page): Promise<FrameLocator> {
360360
export function withPlatformSlashes(input: string) {
361361
return input.replaceAll(path.posix.sep, path.sep)
362362
}
363+
364+
const isPlatform = (platform: string) => process.platform === platform
365+
export function getMetaKeyByOS(): string {
366+
return isPlatform('darwin') ? 'Meta' : 'Control'
367+
}

0 commit comments

Comments
 (0)