Skip to content

Commit ba95a7a

Browse files
committed
unify magic command
1 parent 10b5082 commit ba95a7a

1 file changed

Lines changed: 74 additions & 87 deletions

File tree

fnl/mods/tools/telescope.fnl

Lines changed: 74 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@
156156

157157
(map [:n] :fl (bindf builtin.lsp_document_symbols ivy_config)
158158
{:desc "Find symbols [LSP]"})
159-
(map [:n] :fs (bindf builtin.grep_string ivy_config) {:desc "Find string"})
160159
(map [:n] :fz (bindf builtin.grep_string fzf_opts_theme)
161160
{:desc "Fuzzy grep string"})
162161
(map [:n] :fb (bindf builtin.buffers ivy_config) {:desc "Buffer list"})
@@ -283,79 +282,83 @@
283282
(tset self.state :active :file)
284283
((. file-previewer :preview) file-previewer entry status))))})))
285284

286-
(fn new_magic_finder_job [opts]
285+
(fn new_magic_finder [opts]
287286
"Create a new finder job for the magic picker"
288287
(var fcmd [:fd :--type :f :--color :never :--follow :--max-results :5000])
289288
(set fcmd (cons fcmd [:--max-depth (. opts :fcmd_depth)]))
290289
(when (. opts :hidden)
291290
(set fcmd (cons fcmd :--hidden)))
292291
(finders.new_oneshot_job fcmd opts))
293292

293+
(fn new_magic_grepper [opts]
294+
"Create a live grep finder job for the magic picker"
295+
(var gcmd [:rg :--color=never :--no-heading :--with-filename
296+
:--line-number :--column :--smart-case
297+
:--max-depth (. opts :fcmd_depth)])
298+
(when (. opts :hidden)
299+
(set gcmd (cons gcmd :--hidden)))
300+
(finders.new_job
301+
(fn [query]
302+
(when (and query (not= query ""))
303+
(cons gcmd [query :-- (. opts :cwd)])))
304+
(make_entry.gen_from_vimgrep opts)
305+
nil
306+
(. opts :cwd)))
307+
308+
(fn update_magic_prompt_title [opts]
309+
(var mode (if (= (. opts :mode) :grep) :grep :find))
310+
(var cwd (. opts :cwd))
311+
(var relcwd (cwd:gsub (os.getenv :HOME) :$HOME))
312+
(tset opts :prompt_title (.. mode " [d:" (. opts :fcmd_depth) "] " relcwd)))
313+
294314
(fn refresh [picker opts popts]
295315
"Refresh the picker with new options"
316+
(update_magic_prompt_title opts)
317+
(when (and picker.prompt_border picker.prompt_border.change_title)
318+
(picker.prompt_border:change_title (. opts :prompt_title)))
296319
(picker.refresh_previewer picker)
297-
(picker.refresh picker (new_magic_finder_job opts) popts))
298-
299-
(fn update_magic_prompt_title [opts]
300-
(tset opts :prompt_title (.. "Magic in " (. opts :cwd))))
320+
(var finder (if (= (. opts :mode) :grep)
321+
(new_magic_grepper opts)
322+
(new_magic_finder opts)))
323+
(picker.refresh picker finder popts))
301324

302325
(var magic (fn [_opts]
303326
(var opts (deep-copy (or _opts {})))
304327
(var cwd (utils.buffer_dir))
305328
(tset opts :fcmd_depth 4)
306329
(tset opts :cwd cwd)
307-
(tset opts :entry_maker (make_entry.gen_from_file opts))
330+
(tset opts :mode (or (. opts :mode) :files))
331+
(tset opts :entry_maker (if (= (. opts :mode) :grep)
332+
(make_entry.gen_from_vimgrep opts)
333+
(make_entry.gen_from_file opts)))
308334
(update_magic_prompt_title opts)
309335
(var p
310336
(pickers.new opts
311-
{:finder (new_magic_finder_job opts)
337+
{:finder (if (= (. opts :mode) :grep)
338+
(new_magic_grepper opts)
339+
(new_magic_finder opts))
312340
:previewer (magic-previewer opts)
313341
:sorter (conf.file_sorter opts)
314342
:cache_picker false
315-
:attach_mappings (fn [_ map]
343+
:attach_mappings (fn [buf map]
316344
(map [:n] :S
317345
(fn []
318346
;; decrease search depth
319-
(when (> (. opts
320-
:fcmd_depth)
321-
1)
322-
(tset opts
323-
:fcmd_depth
324-
(- (. opts
325-
:fcmd_depth)
326-
1))
327-
(print (.. "search in "
328-
(. opts
329-
:cwd)
330-
" @ depth "
331-
(. opts
332-
:fcmd_depth)))
333-
(refresh p opts
334-
{:reset_prompt false}))))
347+
(when (> (. opts :fcmd_depth) 1)
348+
(tset opts :fcmd_depth
349+
(- (. opts :fcmd_depth) 1))
350+
(refresh p opts {:reset_prompt false}))))
335351
(map [:n] :D
336352
(fn []
337353
;; increase search depth
338-
(tset opts
339-
:fcmd_depth
340-
(+ (. opts
341-
:fcmd_depth)
342-
1))
343-
(print (.. "search in "
344-
(. opts
345-
:cwd)
346-
" @ depth "
347-
(. opts
348-
:fcmd_depth)))
349-
(refresh p opts
350-
{:reset_prompt false})))
354+
(tset opts :fcmd_depth
355+
(+ (. opts :fcmd_depth) 1))
356+
(refresh p opts {:reset_prompt false})))
351357
(map [:i :n] :<C-h>
352358
(fn []
353359
(tset opts :hidden
354360
(not (. opts
355361
:hidden)))
356-
(tset opts
357-
:entry_maker
358-
(make_entry.gen_from_file opts))
359362
(refresh p opts
360363
{:reset_prompt false})))
361364
(map [:i :n] :<C-e>
@@ -367,65 +370,49 @@
367370
nwd)
368371
(set cwd nwd)
369372
(tset opts :cwd cwd)
370-
(update_magic_prompt_title opts)
371-
(tset opts
372-
:entry_maker
373-
(make_entry.gen_from_file opts))
374-
(print (.. "search in "
375-
(. opts
376-
:cwd)
377-
" @ depth "
378-
(. opts
379-
:fcmd_depth)))
380-
(refresh p opts
381-
{:reset_prompt false}))))
373+
(refresh p opts {:reset_prompt false}))))
382374
(map [:i :n] :<tab>
383375
(fn []
384-
(var nwd
385-
(all_dirs_from_entry cwd))
386-
(when (should_cd cwd
387-
nwd)
376+
(var nwd (all_dirs_from_entry cwd))
377+
(when (should_cd cwd nwd)
388378
(set cwd nwd)
389379
(tset opts :cwd cwd)
390-
(update_magic_prompt_title opts)
391-
(tset opts
392-
:entry_maker
393-
(make_entry.gen_from_file opts))
394-
(print (.. "search in "
395-
(. opts
396-
:cwd)
397-
" @ depth "
398-
(. opts
399-
:fcmd_depth)))
400-
(refresh p opts
401-
{:reset_prompt true
402-
:prompt_title (.. "Magic in "
403-
(. opts
404-
:cwd))}))))
380+
(refresh p opts {:reset_prompt true}))))
405381
(map [:i :n] :<S-tab>
406382
(fn []
407383
(when (not (= cwd "/"))
408-
(set cwd
409-
(vim.fn.resolve (.. cwd
410-
"/..")))
384+
(set cwd (vim.fn.resolve (.. cwd "/..")))
411385
(tset opts :cwd cwd)
412-
(update_magic_prompt_title opts)
413-
(tset opts
414-
:entry_maker
415-
(make_entry.gen_from_file opts))
416-
(print (.. "search in "
417-
(. opts
418-
:cwd)
419-
" @ depth "
420-
(. opts
421-
:fcmd_depth)))
422-
(refresh p opts
423-
{:reset_prompt false}))))
386+
(refresh p opts {:reset_prompt false}))))
387+
(map [:i :n] :<C-g>
388+
(fn []
389+
(tset opts :mode
390+
(if (= (. opts :mode) :grep)
391+
:files
392+
:grep))
393+
(if (= (. opts :mode) :grep)
394+
(tset opts :entry_maker
395+
(make_entry.gen_from_vimgrep opts))
396+
(tset opts :entry_maker
397+
(make_entry.gen_from_file opts)))
398+
(refresh p opts {:reset_prompt false})))
424399
true)}))
425400
(p.find p)))
426401

427402
(local themed_magic (bindf magic ivy_config))
428403
(local small_themed_magic (bindf magic small_ivy_config))
429404

405+
(fn magic-grep-cword []
406+
(magic (merge-table ivy_config {:mode :grep
407+
:default_text (vim.fn.expand "<cword>")})))
408+
409+
(fn magic-grep-visual []
410+
(vim.cmd "noau normal! \"vy")
411+
(var text (vim.fn.getreg :v))
412+
(vim.fn.setreg :v [])
413+
(magic (merge-table ivy_config {:mode :grep :default_text text})))
414+
430415
(map [:n] :F themed_magic {:desc "Find and move around"})
431416
(map [:n] :<C-f> small_themed_magic {:desc "Find and move around"})
417+
(map [:n] :fs magic-grep-cword {:desc "Find string [magic grep]"})
418+
(map [:v] :fs magic-grep-visual {:desc "Find selection [magic grep]"})

0 commit comments

Comments
 (0)