custom-finders.lua 2.9 KB

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