@@ -82,6 +82,42 @@ function M.clear_selections()
8282 ChatContext .clear_selections ()
8383end
8484
85+ --- Captures the current visual selection and adds it to the context.
86+ --- This can be called from any buffer at any time, even when the panel is already open.
87+ --- Selections persist across file switches and accumulate across buffers.
88+ --- @param range ? OpencodeSelectionRange
89+ --- @return boolean success Whether a selection was successfully added
90+ function M .add_visual_selection (range )
91+ local buf = vim .api .nvim_get_current_buf ()
92+
93+ if not util .is_buf_a_file (buf ) then
94+ vim .notify (' Cannot add selection: not a file buffer' , vim .log .levels .WARN )
95+ return false
96+ end
97+
98+ local current_selection = BaseContext .get_current_selection (nil , range )
99+ if not current_selection then
100+ vim .notify (' No visual selection found' , vim .log .levels .WARN )
101+ return false
102+ end
103+
104+ local file = BaseContext .get_current_file_for_selection (buf )
105+ if not file then
106+ vim .notify (' Cannot determine file for selection' , vim .log .levels .WARN )
107+ return false
108+ end
109+
110+ local selection = BaseContext .new_selection (file , current_selection .text , current_selection .lines )
111+ M .add_selection (selection )
112+
113+ vim .notify (
114+ string.format (' Selection added from %s (lines %s)' , file .name , current_selection .lines ),
115+ vim .log .levels .INFO
116+ )
117+
118+ return true
119+ end
120+
85121function M .add_file (file )
86122 local is_file = vim .fn .filereadable (file ) == 1
87123 local is_dir = vim .fn .isdirectory (file ) == 1
0 commit comments