custom-finders.lua 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. local M = {}
  2. local _, builtin = pcall(require, "telescope.builtin")
  3. local _, finders = pcall(require, "telescope.finders")
  4. local _, pickers = pcall(require, "telescope.pickers")
  5. local _, sorters = pcall(require, "telescope.sorters")
  6. local _, themes = pcall(require, "telescope.themes")
  7. local _, actions = pcall(require, "telescope.actions")
  8. local _, previewers = pcall(require, "telescope.previewers")
  9. local _, make_entry = pcall(require, "telescope.make_entry")
  10. function M.find_lunarvim_files(opts)
  11. opts = opts or {}
  12. local theme_opts = themes.get_ivy {
  13. sorting_strategy = "ascending",
  14. layout_strategy = "bottom_pane",
  15. prompt_prefix = ">> ",
  16. prompt_title = "~ LunarVim files ~",
  17. cwd = get_runtime_dir(),
  18. search_dirs = { get_lvim_base_dir(), lvim.lsp.templates_dir },
  19. }
  20. opts = vim.tbl_deep_extend("force", theme_opts, opts)
  21. builtin.find_files(opts)
  22. end
  23. function M.grep_lunarvim_files(opts)
  24. opts = opts or {}
  25. local theme_opts = themes.get_ivy {
  26. sorting_strategy = "ascending",
  27. layout_strategy = "bottom_pane",
  28. prompt_prefix = ">> ",
  29. prompt_title = "~ search LunarVim ~",
  30. cwd = get_runtime_dir(),
  31. search_dirs = { get_lvim_base_dir(), lvim.lsp.templates_dir },
  32. }
  33. opts = vim.tbl_deep_extend("force", theme_opts, opts)
  34. builtin.live_grep(opts)
  35. end
  36. local copy_to_clipboard_action = function(prompt_bufnr)
  37. local _, action_state = pcall(require, "telescope.actions.state")
  38. local entry = action_state.get_selected_entry()
  39. local version = entry.value
  40. vim.fn.setreg("+", version)
  41. vim.fn.setreg('"', version)
  42. vim.notify("Copied " .. version .. " to clipboard", vim.log.levels.INFO)
  43. actions.close(prompt_bufnr)
  44. end
  45. function M.view_lunarvim_changelog()
  46. local opts = themes.get_ivy {
  47. cwd = get_lvim_base_dir(),
  48. }
  49. opts.entry_maker = make_entry.gen_from_git_commits(opts)
  50. pickers
  51. .new(opts, {
  52. prompt_title = "~ LunarVim Changelog ~",
  53. finder = finders.new_oneshot_job(
  54. vim.tbl_flatten {
  55. "git",
  56. "log",
  57. "--pretty=oneline",
  58. "--abbrev-commit",
  59. },
  60. opts
  61. ),
  62. previewer = {
  63. previewers.git_commit_diff_as_was.new(opts),
  64. },
  65. --TODO: consider opening a diff view when pressing enter
  66. attach_mappings = function(_, map)
  67. map("i", "<enter>", copy_to_clipboard_action)
  68. map("n", "<enter>", copy_to_clipboard_action)
  69. map("i", "<esc>", actions.close)
  70. map("n", "<esc>", actions.close)
  71. map("n", "q", actions.close)
  72. return true
  73. end,
  74. sorter = sorters.generic_sorter,
  75. })
  76. :find()
  77. end
  78. -- Smartly opens either git_files or find_files, depending on whether the working directory is
  79. -- contained in a Git repo.
  80. function M.find_project_files(opts)
  81. opts = opts or {}
  82. local ok = pcall(builtin.git_files, opts)
  83. if not ok then
  84. builtin.find_files(opts)
  85. end
  86. end
  87. return M