Skip to content

Commit 5100fd2

Browse files
committed
Add internal command to record accepted code at cursor completions for commit messages
- rename file tree provider to workspace provider, add necessary types - it's been reported that contrary to `apps/editor/src/commands/apply-response-command/utils/preview-handler.ts`, 'code at cursor' doesn't use PromptsForCommitMessagesUtils.add └── apps └── editor └── src ├── commands │ ├── apply-response-command │ │ └── utils │ │ └── preview-handler.ts │ └── code-at-cursor-command │ ├── code-at-cursor-commands.ts │ └── utils │ ├── perform-code-at-cursor.ts │ └── show-ghost-text.ts ├── context │ ├── context-initialization.ts │ └── providers │ └── workspace │ └── workspace-provider.ts ├── extension.ts └── utils └── prompts-for-commit-messages-utils.ts
1 parent 61fb3e1 commit 5100fd2

4 files changed

Lines changed: 76 additions & 12 deletions

File tree

‎apps/editor/src/commands/code-at-cursor-command/code-at-cursor-commands.ts‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
11
import * as vscode from 'vscode'
22
import { PromptViewProvider } from '@/views/prompt/backend/prompt-view-provider'
3+
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
4+
import { OpenEditorsProvider } from '@/context/providers/open-editors/open-editors-provider'
5+
6+
import { PromptsForCommitMessagesUtils } from '@/utils/prompts-for-commit-messages-utils'
37
import { perform_code_at_cursor } from './utils/perform-code-at-cursor'
48

59
export const code_at_cursor_commands = (params: {
6-
file_tree_provider: any
7-
open_editors_provider: any
10+
workspace_provider: WorkspaceProvider
11+
open_editors_provider: OpenEditorsProvider
812
extension_context: vscode.ExtensionContext
913
prompt_view_provider: PromptViewProvider
1014
}) => {
1115
return [
16+
vscode.commands.registerCommand(
17+
'codeWebChat.internal.codeAtCursorAccepted',
18+
async (args: {
19+
workspace_root: string
20+
prompt: string
21+
file_path: string
22+
selected_files: string[]
23+
}) => {
24+
PromptsForCommitMessagesUtils.add({
25+
extension_context: params.extension_context,
26+
workspace_root: args.workspace_root,
27+
prompt: args.prompt,
28+
files: [args.file_path],
29+
selected_files: args.selected_files
30+
})
31+
}
32+
),
1233
vscode.commands.registerCommand('codeWebChat.codeAtCursor', async () =>
1334
perform_code_at_cursor({
14-
file_tree_provider: params.file_tree_provider,
35+
workspace_provider: params.workspace_provider,
1536
open_editors_provider: params.open_editors_provider,
1637
extension_context: params.extension_context,
1738
with_completion_instructions: false,
@@ -22,7 +43,7 @@ export const code_at_cursor_commands = (params: {
2243
'codeWebChat.codeAtCursorWithInstructions',
2344
async () =>
2445
perform_code_at_cursor({
25-
file_tree_provider: params.file_tree_provider,
46+
workspace_provider: params.workspace_provider,
2647
open_editors_provider: params.open_editors_provider,
2748
extension_context: params.extension_context,
2849
with_completion_instructions: true,
@@ -31,7 +52,7 @@ export const code_at_cursor_commands = (params: {
3152
),
3253
vscode.commands.registerCommand('codeWebChat.codeAtCursorUsing', async () =>
3354
perform_code_at_cursor({
34-
file_tree_provider: params.file_tree_provider,
55+
workspace_provider: params.workspace_provider,
3556
open_editors_provider: params.open_editors_provider,
3657
extension_context: params.extension_context,
3758
with_completion_instructions: false,
@@ -43,7 +64,7 @@ export const code_at_cursor_commands = (params: {
4364
'codeWebChat.codeAtCursorWithInstructionsUsing',
4465
async () =>
4566
perform_code_at_cursor({
46-
file_tree_provider: params.file_tree_provider,
67+
workspace_provider: params.workspace_provider,
4768
open_editors_provider: params.open_editors_provider,
4869
extension_context: params.extension_context,
4970
with_completion_instructions: true,

‎apps/editor/src/commands/code-at-cursor-command/utils/perform-code-at-cursor.ts‎

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from 'vscode'
22
import axios from 'axios'
3+
import * as path from 'path'
34
import he from 'he'
45
import { send_llm_message } from '../../../utils/send-llm-message'
56
import { code_at_cursor_instructions } from '../../../constants/instructions'
@@ -14,10 +15,12 @@ import { build_user_content } from '../../../utils/build-user-content'
1415
import { get_code_at_cursor_api_configuration } from './get-code-at-cursor-config'
1516
import { show_ghost_text } from './show-ghost-text'
1617
import { PromptBuilder } from '../../../utils/prompt-builder'
18+
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
19+
import { OpenEditorsProvider } from '@/context/providers/open-editors/open-editors-provider'
1720

1821
export const perform_code_at_cursor = async (params: {
19-
file_tree_provider: any
20-
open_editors_provider: any
22+
workspace_provider: WorkspaceProvider
23+
open_editors_provider: OpenEditorsProvider
2124
extension_context: vscode.ExtensionContext
2225
with_completion_instructions: boolean
2326
show_quick_pick?: boolean
@@ -123,7 +126,7 @@ export const perform_code_at_cursor = async (params: {
123126
)
124127

125128
const files_collector = new FilesCollector({
126-
workspace_provider: params.file_tree_provider,
129+
workspace_provider: params.workspace_provider,
127130
open_editors_provider: params.open_editors_provider
128131
})
129132

@@ -217,10 +220,48 @@ export const perform_code_at_cursor = async (params: {
217220
.replace(/\]\]>/g, '')
218221
.trim()
219222

223+
const active_file_path = editor.document.uri.fsPath
224+
const workspace_root =
225+
params.workspace_provider.get_workspace_root_for_file(
226+
active_file_path
227+
)
228+
const selected_files: string[] = []
229+
230+
if (workspace_root) {
231+
const checked_files =
232+
params.workspace_provider.get_checked_files()
233+
for (const file of checked_files) {
234+
const file_workspace_root =
235+
params.workspace_provider.get_workspace_root_for_file(file)
236+
if (file_workspace_root === workspace_root) {
237+
const relative_path = path
238+
.relative(workspace_root, file)
239+
.replace(/\\/g, '/')
240+
selected_files.push(relative_path)
241+
}
242+
}
243+
}
244+
220245
await show_ghost_text({
221246
editor,
222247
position,
223-
ghost_text: decoded_completion
248+
ghost_text: decoded_completion,
249+
command: workspace_root
250+
? {
251+
title: 'Code at Cursor Accepted',
252+
command: 'codeWebChat.internal.codeAtCursorAccepted',
253+
arguments: [
254+
{
255+
workspace_root,
256+
prompt:
257+
completion_instructions ||
258+
'Code at cursor completion',
259+
file_path: active_file_path,
260+
selected_files
261+
}
262+
]
263+
}
264+
: undefined
224265
})
225266
}
226267
break

‎apps/editor/src/commands/code-at-cursor-command/utils/show-ghost-text.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const show_ghost_text = async (params: {
44
editor: vscode.TextEditor
55
position: vscode.Position
66
ghost_text: string
7+
command?: vscode.Command
78
}) => {
89
const document = params.editor.document
910
const controller = vscode.languages.registerInlineCompletionItemProvider(
@@ -18,7 +19,8 @@ export const show_ghost_text = async (params: {
1819
return [
1920
new vscode.InlineCompletionItem(
2021
params.ghost_text,
21-
new vscode.Range(params.position, params.position)
22+
new vscode.Range(params.position, params.position),
23+
params.command
2224
)
2325
]
2426
}

‎apps/editor/src/extension.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export const activate = async (extension_context: vscode.ExtensionContext) => {
118118
prompt_view_api_calls_manager
119119
}),
120120
...code_at_cursor_commands({
121-
file_tree_provider: workspace_provider,
121+
workspace_provider,
122122
open_editors_provider,
123123
extension_context,
124124
prompt_view_provider

0 commit comments

Comments
 (0)