custom-finders.lua 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 { cwd = get_lvim_base_dir() }
  48. opts.entry_maker = make_entry.gen_from_git_commits(opts)
  49. pickers.new(opts, {
  50. prompt_title = "~ LunarVim Changelog ~",
  51. finder = finders.new_oneshot_job(
  52. vim.tbl_flatten {
  53. "git",
  54. "log",
  55. "--pretty=oneline",
  56. "--abbrev-commit",
  57. },
  58. opts
  59. ),
  60. previewer = {
  61. previewers.git_commit_diff_as_was.new(opts),
  62. },
  63. --TODO: consider opening a diff view when pressing enter
  64. attach_mappings = function(_, map)
  65. map("i", "<enter>", copy_to_clipboard_action)
  66. map("n", "<enter>", copy_to_clipboard_action)
  67. map("i", "<esc>", actions._close)
  68. map("n", "<esc>", actions._close)
  69. map("n", "q", actions._close)
  70. return true
  71. end,
  72. sorter = sorters.generic_sorter,
  73. }):find()
  74. end
  75. -- Smartly opens either git_files or find_files, depending on whether the working directory is
  76. -- contained in a Git repo.
  77. function M.find_project_files()
  78. local ok = pcall(builtin.git_files)
  79. if not ok then
  80. builtin.find_files()
  81. end
  82. end
  83. return M