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

change: replace vscode links with command #1919

Merged
merged 3 commits into from
Nov 28, 2023
Merged
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
16 changes: 8 additions & 8 deletions lib/shared/src/chat/prompts/display-text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ describe('replaceFileNameWithMarkdownLink', () => {

const result = replaceFileNameWithMarkdownLink(text, '@test.js', '/path/to/test.js')

expect(result).toEqual('Hello [_@test.js_](vscode://file/path/to/test.js)')
expect(result).toEqual('Hello [_@test.js_](command:cody.chat.open.file?"/path/to/test.js")')
})

it('respects spaces in file name', () => {
const text = 'Loaded @my file.js'

const result = replaceFileNameWithMarkdownLink(text, '@my file.js', '/path/to/my file.js')

expect(result).toEqual('Loaded [_@my file.js_](vscode://file/path/to/my file.js)')
expect(result).toEqual('Loaded [_@my file.js_](command:cody.chat.open.file?"/path/to/my file.js")')
})

it('returns original text if no match', () => {
Expand All @@ -32,23 +32,23 @@ describe('replaceFileNameWithMarkdownLink', () => {

const result = replaceFileNameWithMarkdownLink(text, '@test.js', '/path/with/@#special$chars.js')

expect(result).toEqual('Loaded [_@test.js_](vscode://file/path/with/@#special$chars.js)')
expect(result).toEqual('Loaded [_@test.js_](command:cody.chat.open.file?"/path/with/@#special$chars.js")')
})

it('handles line numbers', () => {
const text = 'Error in @test.js'

const result = replaceFileNameWithMarkdownLink(text, '@test.js', '/path/test.js', 10)

expect(result).toEqual('Error in [_@test.js_](vscode://file/path/test.js:10)')
expect(result).toEqual('Error in [_@test.js_](command:cody.chat.open.file?"/path/test.js:range:10")')
})

it('handles edge case where start line at 0 - exclude start line in markdown link', () => {
const text = 'Error in @test.js'

const result = replaceFileNameWithMarkdownLink(text, '@test.js', '/path/test.js', 0)

expect(result).toEqual('Error in [_@test.js_](vscode://file/path/test.js)')
expect(result).toEqual('Error in [_@test.js_](command:cody.chat.open.file?"/path/test.js")')
})

it('handles names that showed up more than once', () => {
Expand All @@ -57,7 +57,7 @@ describe('replaceFileNameWithMarkdownLink', () => {
const result = replaceFileNameWithMarkdownLink(text, '@foo.js', '/path/foo.js', 10)

expect(result).toEqual(
'Compare and explain [_@foo.js_](vscode://file/path/foo.js:10) and @bar.js. What does [_@foo.js_](vscode://file/path/foo.js:10) do?'
'Compare and explain [_@foo.js_](command:cody.chat.open.file?"/path/foo.js:range:10") and @bar.js. What does [_@foo.js_](command:cody.chat.open.file?"/path/foo.js:range:10") do?'
)
})

Expand All @@ -67,7 +67,7 @@ describe('replaceFileNameWithMarkdownLink', () => {
const result = replaceFileNameWithMarkdownLink(text, '@foo.js', '/path/foo.js', 10)

expect(result).toEqual(
'Compare and explain [_@foo.js_](vscode://file/path/foo.js:10) and @bar.js. What does @foo.js#FooBar() do?'
'Compare and explain [_@foo.js_](command:cody.chat.open.file?"/path/foo.js:range:10") and @bar.js. What does @foo.js#FooBar() do?'
)
})

Expand All @@ -83,7 +83,7 @@ describe('replaceFileNameWithMarkdownLink', () => {
)

expect(result).toEqual(
'[_@vscode/src/logged-rerank.ts:7-23#getRerankWithLog()_](vscode://file/vscode/src/logged-rerank.ts:7) what does this do'
'[_@vscode/src/logged-rerank.ts:7-23#getRerankWithLog()_](command:cody.chat.open.file?"/vscode/src/logged-rerank.ts:range:7") what does this do'
)
})
})
6 changes: 3 additions & 3 deletions lib/shared/src/chat/prompts/display-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export function replaceFileNameWithMarkdownLink(
startLine = 0
): string {
// Create markdown link to the file
const range = startLine ? `:${startLine}` : ''
const fileLink = `vscode://file${fsPath}${range}`
const markdownText = `[_${fileName.trim()}_](${fileLink})`
const range = startLine ? `:range:${startLine}` : ''
const fileLink = `${fsPath}${range}`
const markdownText = `[_${fileName.trim()}_](command:cody.chat.open.file?"${fileLink}")`

// Use regex to makes sure the file name is surrounded by spaces and not a substring of another file name
const textToBeReplaced = new RegExp(`\\s*${fileName.replaceAll(/[$()*+./?[\\\]^{|}-]/g, '\\$&')}(?!\\S)`, 'g')
Expand Down
1 change: 1 addition & 0 deletions vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Starting from `0.2.0`, Cody is using `major.EVEN_NUMBER.patch` for release versi
- Chat: The default chat model `claude-2` has been replaced with the pinned version `claude-2.0`. [pull/1860](https://github.com/sourcegraph/cody/pull/1860)
- Command: Editor title icon will only show up in non-readonly file editor views. [pull/1909](https://github.com/sourcegraph/cody/pull/1909)
- Chat: Include text in dotCom chat events. [pull/1910](https://github.com/sourcegraph/cody/pull/1910)
- Chat: Replaced vscode links with custom "cody.chat.open.file" protocol when displaying file names in chat. [pull/1919](https://github.com/sourcegraph/cody/pull/1919)

## [0.16.1]

Expand Down
17 changes: 16 additions & 1 deletion vscode/src/chat/chat-view/ChatManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export class ChatManager implements vscode.Disposable {
vscode.commands.registerCommand('cody.chat.history.clear', async () => this.clearHistory()),
vscode.commands.registerCommand('cody.chat.history.delete', async item => this.clearHistory(item)),
vscode.commands.registerCommand('cody.chat.panel.new', async () => this.createNewWebviewPanel()),
vscode.commands.registerCommand('cody.chat.panel.restore', (id, chat) => this.restorePanel(id, chat))
vscode.commands.registerCommand('cody.chat.panel.restore', (id, chat) => this.restorePanel(id, chat)),
vscode.commands.registerCommand('cody.chat.open.file', async fsPath => this.openFileFromChat(fsPath))
)

// Register config change listener
Expand Down Expand Up @@ -228,6 +229,20 @@ export class ChatManager implements vscode.Disposable {
.catch(error => console.error(error))
}

private async openFileFromChat(fsPath: string): Promise<void> {
const rangeIndex = fsPath.indexOf(':range:')
const range = rangeIndex ? fsPath.slice(Math.max(0, rangeIndex + 7)) : 0
const filteredFsPath = range ? fsPath.slice(0, rangeIndex) : fsPath
const uri = vscode.Uri.file(filteredFsPath)
// If the active editor is undefined, that means the chat panel is the active editor
// so we will open the file in the first visible editor instead
const editor = vscode.window.activeTextEditor || vscode.window.visibleTextEditors[0]
// If there is no editor or visible editor found, then we will open the file next to chat panel
const viewColumn = editor ? editor.viewColumn : vscode.ViewColumn.Beside
const doc = await vscode.workspace.openTextDocument(uri)
await vscode.window.showTextDocument(doc, { viewColumn })
}

private disposeChatPanelsManager(): void {
this.options.contextProvider.webview = this.sidebarChat.webview
this.options.authProvider.webview = this.sidebarChat.webview
Expand Down