Skip to content

Commit c436b14

Browse files
committed
Replace has_active_selection boolean with current_selection string throughout the codebase to track actual selected text content
1 parent e505a52 commit c436b14

11 files changed

Lines changed: 53 additions & 47 deletions

File tree

‎packages/ui/src/components/editor/panel/PromptField/PromptField.stories.tsx‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const Empty = () => (
1414
on_copy={() => console.log('Copied')}
1515
is_connected={true}
1616
is_in_code_completions_mode={false}
17-
has_active_selection={false}
17+
current_selection=""
1818
has_active_editor={true}
1919
on_caret_position_change={(pos) => console.log('Caret position:', pos)}
2020
is_web_mode={false}
@@ -40,7 +40,7 @@ export const WithText = () => (
4040
on_copy={() => console.log('Copied')}
4141
is_connected={true}
4242
is_in_code_completions_mode={false}
43-
has_active_selection={false}
43+
current_selection=""
4444
has_active_editor={true}
4545
on_caret_position_change={(pos) => console.log('Caret position:', pos)}
4646
is_web_mode={false}
@@ -66,7 +66,7 @@ export const LongText = () => (
6666
on_copy={() => console.log('Copied')}
6767
is_connected={true}
6868
is_in_code_completions_mode={false}
69-
has_active_selection={false}
69+
current_selection=""
7070
has_active_editor={true}
7171
on_caret_position_change={(pos) => console.log('Caret position:', pos)}
7272
is_web_mode={false}
@@ -93,7 +93,7 @@ export const WithPlaceholderSavedContext = () => (
9393
is_connected={true}
9494
is_in_code_completions_mode={false}
9595
has_active_editor={true}
96-
has_active_selection={false}
96+
current_selection=""
9797
on_caret_position_change={(pos) => console.log('Caret position:', pos)}
9898
on_search_click={() => console.log('Search clicked')}
9999
on_at_sign_click={() => console.log('@ clicked')}
@@ -119,7 +119,7 @@ export const WithPlaceholderSelection = () => (
119119
is_connected={true}
120120
is_in_code_completions_mode={false}
121121
has_active_editor={true}
122-
has_active_selection={true}
122+
current_selection="Selected text content"
123123
on_caret_position_change={(pos) => console.log('Caret position:', pos)}
124124
on_search_click={() => console.log('Search clicked')}
125125
on_at_sign_click={() => console.log('@ clicked')}
@@ -145,7 +145,7 @@ export const WithCommit = () => (
145145
is_connected={true}
146146
is_in_code_completions_mode={false}
147147
has_active_editor={true}
148-
has_active_selection={false}
148+
current_selection=""
149149
on_caret_position_change={(pos) => console.log('Caret position:', pos)}
150150
on_search_click={() => console.log('Search clicked')}
151151
on_at_sign_click={() => console.log('@ clicked')}
@@ -171,7 +171,7 @@ export const WithCommitWithQuotes = () => (
171171
is_connected={true}
172172
is_in_code_completions_mode={false}
173173
has_active_editor={true}
174-
has_active_selection={false}
174+
current_selection=""
175175
on_caret_position_change={(pos) => console.log('Caret position:', pos)}
176176
on_search_click={() => console.log('Search clicked')}
177177
on_at_sign_click={() => console.log('@ clicked')}
@@ -196,7 +196,7 @@ export const WithContextAtCommit = () => (
196196
is_connected={true}
197197
is_in_code_completions_mode={false}
198198
has_active_editor={true}
199-
has_active_selection={false}
199+
current_selection=""
200200
on_caret_position_change={(pos) => console.log('Caret position:', pos)}
201201
on_search_click={() => console.log('Search clicked')}
202202
on_at_sign_click={() => console.log('@ clicked')}
@@ -223,7 +223,7 @@ export const WithEditFormatSelector = () => {
223223
on_copy={() => console.log('Copied')}
224224
is_connected={true}
225225
is_in_code_completions_mode={false}
226-
has_active_selection={false}
226+
current_selection=""
227227
has_active_editor={true}
228228
on_caret_position_change={(pos) => console.log('Caret position:', pos)}
229229
is_web_mode={false}
@@ -251,7 +251,7 @@ export const WithFilePaths = () => (
251251
on_copy={() => console.log('Copied')}
252252
is_connected={true}
253253
is_in_code_completions_mode={false}
254-
has_active_selection={false}
254+
current_selection=""
255255
has_active_editor={true}
256256
on_caret_position_change={(pos) => console.log('Caret position:', pos)}
257257
is_web_mode={false}

‎packages/ui/src/components/editor/panel/PromptField/PromptField.tsx‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export type PromptFieldProps = {
3232
on_copy: () => void
3333
is_connected: boolean
3434
is_in_code_completions_mode: boolean
35-
has_active_selection: boolean
35+
current_selection: string
3636
has_active_editor: boolean
3737
on_caret_position_change: (caret_position: number) => void
3838
is_web_mode: boolean
@@ -170,13 +170,13 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
170170
const highlighted_html = useMemo(() => {
171171
return get_highlighted_text({
172172
text: props.value,
173-
has_active_selection: props.has_active_selection,
173+
current_selection: props.current_selection,
174174
context_file_paths: props.context_file_paths ?? []
175175
})
176176
}, [
177177
props.value,
178178
props.is_in_code_completions_mode,
179-
props.has_active_selection,
179+
props.current_selection,
180180
props.context_file_paths
181181
])
182182

@@ -309,7 +309,7 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
309309

310310
return (
311311
<div className={styles.container}>
312-
{props.has_active_selection && props.is_in_code_completions_mode && (
312+
{!!props.current_selection && props.is_in_code_completions_mode && (
313313
<div className={styles.error}>
314314
<div className={styles.error__inner}>Remove text selection</div>
315315
</div>
@@ -325,7 +325,7 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
325325
className={cn(styles.container__inner, {
326326
[styles['container__inner--disabled']]:
327327
props.is_in_code_completions_mode &&
328-
(props.has_active_selection || !props.has_active_editor),
328+
(!!props.current_selection || !props.has_active_editor),
329329
[styles['container__inner--selecting']]: is_text_selecting
330330
})}
331331
onKeyDown={handle_container_key_down}

‎packages/ui/src/components/editor/panel/PromptField/utils/get-highlighted-text.tsx‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const process_text_part_for_files = (
4646

4747
export const get_highlighted_text = (params: {
4848
text: string
49-
has_active_selection: boolean
49+
current_selection: string
5050
context_file_paths: string[]
5151
}): string => {
5252
const saved_context_regex_part =
@@ -64,12 +64,14 @@ export const get_highlighted_text = (params: {
6464
.map((part) => {
6565
if (part == '#Selection') {
6666
const className = cn(styles['keyword'], styles['keyword--selection'], {
67-
[styles['keyword--selection-error']]: !params.has_active_selection
67+
[styles['keyword--selection-error']]: !params.current_selection
6868
})
69-
const title = !params.has_active_selection
69+
const title = !params.current_selection
7070
? 'Missing text selection'
71-
: ''
72-
return `<span class="${className}" data-type="selection-keyword" title="${title}"><span class="${styles['keyword__icon']}" data-role="keyword-icon"></span><span class="${styles['keyword__text']}" data-role="keyword-text">Selection</span></span>`
71+
: `Selection: ${params.current_selection
72+
.substring(0, 100)
73+
.replace(/\n/g, ' ')}...`
74+
return `<span class="${className}" data-type="selection-keyword" title="${escape_html(title)}"><span class="${styles['keyword__icon']}" data-role="keyword-icon"></span><span class="${styles['keyword__text']}" data-role="keyword-text">Selection</span></span>`
7375
}
7476
if (part && /^#Changes:[^\s,;:!?]+$/.test(part)) {
7577
const branch_name = part.substring('#Changes:'.length)

‎packages/vscode/src/views/panel/backend/message-handlers/handle-request-editor-selection-state.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ export const handle_request_editor_selection_state = (
55
): void => {
66
panel_provider.send_message({
77
command: 'EDITOR_SELECTION_CHANGED',
8-
has_selection: panel_provider.has_active_selection
8+
current_selection: panel_provider.current_selection
99
})
1010
}

‎packages/vscode/src/views/panel/backend/message-handlers/handle-send-prompt.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ async function resolve_presets(params: {
561561
(!params.panel_provider.websocket_server_instance.is_connected_with_browser() ||
562562
(is_in_code_completions_mode &&
563563
(!params.panel_provider.has_active_editor ||
564-
params.panel_provider.has_active_selection)) ||
564+
!!params.panel_provider.current_selection)) ||
565565
(!is_in_code_completions_mode &&
566566
!(
567567
current_instructions ||

‎packages/vscode/src/views/panel/backend/panel-provider.ts‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export class PanelProvider implements vscode.WebviewViewProvider {
112112
private _webview_view: vscode.WebviewView | undefined
113113
private _config_listener: vscode.Disposable | undefined
114114
public has_active_editor: boolean = false
115-
public has_active_selection: boolean = false
115+
public current_selection: string = ''
116116
public caret_position: number = 0
117117
public ask_about_context_instructions: string = ''
118118
public edit_context_instructions: string = ''
@@ -337,28 +337,34 @@ export class PanelProvider implements vscode.WebviewViewProvider {
337337
update_editor_state()
338338

339339
vscode.window.onDidChangeTextEditorSelection((event) => {
340-
const has_selection = !event.textEditor.selection.isEmpty
341-
if (has_selection != this.has_active_selection) {
342-
this.has_active_selection = has_selection
340+
const selection = event.textEditor.selection
341+
const current_selection = !selection.isEmpty
342+
? event.textEditor.document.getText(selection)
343+
: ''
344+
if (current_selection != this.current_selection) {
345+
this.current_selection = current_selection
343346
if (this._webview_view) {
344347
this.send_message({
345348
command: 'EDITOR_SELECTION_CHANGED',
346-
has_selection: has_selection
349+
current_selection
347350
})
348351
}
349352
}
350353
})
351354

352355
const update_selection_state = () => {
353356
const active_text_editor = vscode.window.activeTextEditor
354-
const has_selection = active_text_editor
355-
? !active_text_editor.selection.isEmpty
356-
: false
357-
this.has_active_selection = has_selection
357+
const selection = active_text_editor?.selection
358+
const current_selection =
359+
active_text_editor && selection && !selection.isEmpty
360+
? active_text_editor.document.getText(selection)
361+
: ''
362+
363+
this.current_selection = current_selection
358364
if (this._webview_view) {
359365
this.send_message({
360366
command: 'EDITOR_SELECTION_CHANGED',
361-
has_selection: has_selection
367+
current_selection
362368
})
363369
}
364370
}

‎packages/vscode/src/views/panel/frontend/Main/Main.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Props = {
4747
on_web_prompt_type_change: (mode: WebPromptType) => void
4848
on_api_prompt_type_change: (mode: ApiPromptType) => void
4949
has_active_editor: boolean
50-
has_active_selection: boolean
50+
current_selection: string
5151
chat_input_focus_and_select_key: number
5252
chat_input_focus_key: number
5353
context_size_warning_threshold: number
@@ -739,7 +739,7 @@ export const Main: React.FC<Props> = (props) => {
739739
}
740740
on_quick_action_click={handle_quick_action_click}
741741
has_active_editor={props.has_active_editor}
742-
has_active_selection={props.has_active_selection}
742+
current_selection={props.current_selection}
743743
chat_history={current_history || []}
744744
token_count={token_count}
745745
web_prompt_type={props.web_prompt_type}

‎packages/vscode/src/views/panel/frontend/Main/MainView/MainView.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type Props = {
4545
on_toggle_pinned_configuration: (id: string) => void
4646
on_manage_configurations: () => void
4747
has_active_editor: boolean
48-
has_active_selection: boolean
48+
current_selection: string
4949
chat_history: string[]
5050
token_count: number
5151
context_size_warning_threshold: number
@@ -220,7 +220,7 @@ export const MainView: React.FC<Props> = (props) => {
220220
is_web_mode={props.mode == MODE.WEB}
221221
is_connected={props.is_connected}
222222
is_in_code_completions_mode={is_in_code_completions_prompt_type}
223-
has_active_selection={props.has_active_selection}
223+
current_selection={props.current_selection}
224224
send_with_shift_enter={props.send_with_shift_enter}
225225
has_active_editor={props.has_active_editor}
226226
on_caret_position_change={props.on_caret_position_change}

‎packages/vscode/src/views/panel/frontend/Panel.tsx‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const Panel = () => {
6969
edit_context_instructions,
7070
no_context_instructions,
7171
has_active_editor,
72-
has_active_selection,
72+
current_selection,
7373
code_at_cursor_instructions,
7474
prune_context_instructions,
7575
currently_open_file_text,
@@ -133,7 +133,7 @@ export const Panel = () => {
133133
is_connected === undefined ||
134134
api_prompt_type === undefined ||
135135
has_active_editor === undefined ||
136-
has_active_selection === undefined ||
136+
current_selection === undefined ||
137137
workspace_folder_count === undefined ||
138138
context_size_warning_threshold === undefined
139139
) {
@@ -176,7 +176,7 @@ export const Panel = () => {
176176
!has_instructions &&
177177
web_prompt_type != 'code-completions') ||
178178
(web_prompt_type == 'code-completions' &&
179-
(!has_active_editor || has_active_selection))
179+
(!has_active_editor || !!current_selection))
180180

181181
const handle_apply_click = () => {
182182
post_message(vscode, {
@@ -263,7 +263,7 @@ export const Panel = () => {
263263
api_prompt_type={api_prompt_type}
264264
on_mode_change={handle_mode_change}
265265
has_active_editor={has_active_editor}
266-
has_active_selection={has_active_selection}
266+
current_selection={current_selection}
267267
on_web_prompt_type_change={handle_web_prompt_type_change}
268268
on_api_prompt_type_change={handle_api_prompt_type_change}
269269
response_history={response_history}
@@ -394,7 +394,7 @@ export const Panel = () => {
394394
!has_active_editor
395395
? 'Cannot preview in code completion mode without an active editor'
396396
: web_prompt_type == 'code-completions' &&
397-
has_active_selection
397+
!!current_selection
398398
? 'Unable to work with text selection'
399399
: !has_affixes &&
400400
!has_instructions &&

‎packages/vscode/src/views/panel/frontend/hooks/use-panel.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ export const use_panel = (vscode: any) => {
8989
const [has_active_editor, set_has_active_editor] = useState<
9090
boolean | undefined
9191
>()
92-
const [has_active_selection, set_has_active_selection] = useState<
93-
boolean | undefined
94-
>()
92+
const [current_selection, set_current_selection] = useState<string>('')
9593
const [code_at_cursor_instructions, set_code_at_cursor_instructions] =
9694
useState<string | undefined>(undefined)
9795
const [prune_context_instructions, set_prune_context_instructions] = useState<
@@ -221,7 +219,7 @@ export const use_panel = (vscode: any) => {
221219
} else if (message.command == 'EDITOR_STATE_CHANGED') {
222220
set_has_active_editor(message.has_active_editor)
223221
} else if (message.command == 'EDITOR_SELECTION_CHANGED') {
224-
set_has_active_selection(message.has_selection)
222+
set_current_selection(message.current_selection)
225223
} else if (message.command == 'CONTEXT_FILES') {
226224
set_context_file_paths(message.file_paths)
227225
} else if (message.command == 'SEND_WITH_SHIFT_ENTER') {
@@ -554,7 +552,7 @@ export const use_panel = (vscode: any) => {
554552
edit_context_instructions,
555553
no_context_instructions,
556554
has_active_editor,
557-
has_active_selection,
555+
current_selection,
558556
code_at_cursor_instructions,
559557
prune_context_instructions,
560558
currently_open_file_text,

0 commit comments

Comments
 (0)