@@ -24,7 +24,8 @@ local config = require('spectre.config')
2424local state = require (' spectre.state' )
2525local state_utils = require (' spectre.state_utils' )
2626local utils = require (' spectre.utils' )
27- local ui = require (' spectre.ui.nui_components' )
27+ -- Dynamically choose UI based on configuration
28+ local ui = nil
2829local log = require (' spectre._log' )
2930local async = require (' plenary.async' )
3031
@@ -40,6 +41,18 @@ M.setup = function(opts)
4041 end
4142 require (' spectre.highlight' ).set_hl ()
4243 M .check_replace_cmd_bins ()
44+
45+ -- Initialize UI based on configuration
46+ M .init_ui ()
47+ end
48+
49+ -- Initialize UI based on user config
50+ M .init_ui = function ()
51+ if state .user_config .use_legacy_ui then
52+ ui = require (' spectre.ui.legacy' )
53+ else
54+ ui = require (' spectre.ui.nui_components' )
55+ end
4356end
4457
4558M .check_replace_cmd_bins = function ()
@@ -80,11 +93,20 @@ M.open = function(opts)
8093 }
8194 state .regex = nil
8295
96+ -- Ensure UI is initialized
97+ if ui == nil then
98+ M .init_ui ()
99+ end
100+
83101 ui .open ()
84102end
85103
86104M .close = function ()
87105 state .is_open = false
106+ -- Ensure UI is initialized
107+ if ui == nil then
108+ M .init_ui ()
109+ end
88110 ui .close ()
89111end
90112
@@ -182,14 +204,26 @@ M.change_options = function(key)
182204 end
183205 state .regex .change_options (state_utils .get_replace_engine_config ().options_value )
184206 if state .query .search_query ~= nil then
185- ui .render_search_ui ()
207+ -- Ensure UI is initialized
208+ if ui == nil then
209+ M .init_ui ()
210+ end
211+
212+ if ui and ui .render_search_ui then
213+ ui .render_search_ui ()
214+ end
186215 M .search ()
187216 end
188217end
189218
190219M .show_options = function ()
191- if not ui then return end
192- ui .show_options ()
220+ if not ui then
221+ M .init_ui ()
222+ end
223+
224+ if ui and ui .show_options then
225+ ui .show_options ()
226+ end
193227end
194228
195229M .get_fold = function (lnum )
@@ -215,18 +249,98 @@ M.get_fold = function(lnum)
215249end
216250
217251M .tab = function ()
218- if not ui then return end
219- ui .tab ()
252+ if not ui then
253+ M .init_ui ()
254+ end
255+
256+ if ui and ui .tab then
257+ ui .tab ()
258+ end
220259end
221260
222261M .tab_shift = function ()
223- if not ui then return end
224- ui .tab_shift ()
262+ if not ui then
263+ M .init_ui ()
264+ end
265+
266+ if ui and ui .tab_shift then
267+ ui .tab_shift ()
268+ end
225269end
226270
227271M .toggle_preview = function ()
228- if not ui then return end
229- ui .toggle_preview ()
272+ if not ui then
273+ M .init_ui ()
274+ end
275+
276+ if ui and ui .toggle_preview then
277+ ui .toggle_preview ()
278+ end
279+ end
280+
281+ -- Function to toggle between different view modes
282+ M .change_view = function ()
283+ if not ui then
284+ M .init_ui ()
285+ end
286+
287+ -- Toggle view mode
288+ if state .view .mode == " both" then
289+ state .view .mode = " replace"
290+ state .view .show_search = false
291+ state .view .show_replace = true
292+ elseif state .view .mode == " replace" then
293+ state .view .mode = " search"
294+ state .view .show_search = true
295+ state .view .show_replace = false
296+ else
297+ state .view .mode = " both"
298+ state .view .show_search = true
299+ state .view .show_replace = true
300+ end
301+
302+ -- Trigger UI update if available
303+ if ui and ui .render_search_ui then
304+ ui .render_search_ui ()
305+ end
306+ end
307+
308+ -- Function to toggle between UI types
309+ M .toggle_ui = function ()
310+ state .user_config .use_legacy_ui = not state .user_config .use_legacy_ui
311+
312+ -- Re-initialize the UI
313+ M .init_ui ()
314+
315+ -- Notify the user
316+ local ui_type = state .user_config .use_legacy_ui and " legacy" or " modern"
317+ vim .notify (" Switched to " .. ui_type .. " UI. Reopen spectre panel to apply changes." , vim .log .levels .INFO )
318+
319+ -- If spectre is open, close and reopen it to apply changes
320+ if state .is_open then
321+ local query_backup = vim .deepcopy (state .query )
322+ M .close ()
323+ M .open (query_backup )
324+ end
325+ end
326+
327+ -- Function to set the UI type
328+ M .set_ui_type = function (use_legacy )
329+ state .user_config .use_legacy_ui = use_legacy
330+
331+ -- Re-initialize the UI
332+ M .init_ui ()
333+
334+ -- Notify the user
335+ local ui_type = state .user_config .use_legacy_ui and " legacy" or " modern"
336+ vim .notify (" Set to " .. ui_type .. " UI. Reopen spectre panel to apply changes." , vim .log .levels .INFO )
337+
338+ -- If spectre is open, close and reopen it to apply changes
339+ if state .is_open then
340+ local query_backup = vim .deepcopy (state .query )
341+ M .close ()
342+ M .open (query_backup )
343+ end
230344end
231345
232346return M
0 commit comments